Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- disableLog("ALL");
- //User Settings
- var investPercent = 0.85; //Percentage of overall networth invested at once (desired). Default 0.85
- var rebalanceThresh = 5000000; //Amount overbought a stock must be before selling some. Default 5mil
- var initialInvestmentThresh = 500000; //Minimum investment on a new stock. Default 500k
- //End User Settings
- var symbols = ["ECP", "MGCP", "BLD", "CLRK", "OMTK", "FSIG", "KGI", "FLCM", "STM", "DCOMM", "HLS", "VITA", "ICRS", "UNV", "AERO", "OMN", "SLRS", "GPH", "NVMD", "WDS", "LXO", "RHOC", "APHE", "SYSC", "CTK", "NTLK", "OMGA", "FNS", "SGC", "JGN", "CTYS", "MDYN", "TITN"];
- var investedStocks = 0;
- var shares = [];
- var prices = [];
- var totalWorth = 0;
- var lastCoH = 0;
- var stockInvestmentRatio = 1 / symbols.length;
- print("Getting initial positions...");
- for (i = 0; i < symbols.length; i = i + 1) {
- position = getStockPosition(symbols[i]);
- shares.push(position[0]);
- prices.push(position[1]);
- totalWorth += position[0] * position[1];
- if (position[0] > 0) {
- investedStocks++;
- }
- }
- while (true) {
- totalWorth -= lastCoH;
- var cashOnHand = getServerMoneyAvailable("home");
- totalWorth += cashOnHand;
- lastCoH = cashOnHand;
- //Since we aren't investing in every stock, figure out about how much should go in to each
- //Could cause an over-investment (above investPercent) or running out of COH
- //if many stocks suddenly becomes attractive.
- var maxPosition = (totalWorth * investPercent) * stockInvestmentRatio;
- print("-----");
- print(investedStocks + " stocks currently invested in, " + nFormat(stockInvestmentRatio, "(0.00%)") + " NW per-stock ratio.");
- print(nFormat(cashOnHand, "$0.000a") + " CoH, " + nFormat(totalWorth, "$0.000a") + " NW, " + nFormat(maxPosition, "$0.000a") + " is our base position");
- print("-----");
- symbols.forEach(function(item, index, array) {
- var forecast = getStockForecast(item);
- var sharesNum = shares[index];
- if (forecast < 0.5) {
- if (shares[index] > 0) {
- var salePrice = sellStock(item, sharesNum);
- if (salePrice != 0) {
- print("Sold " + nFormat(sharesNum, "0,0") + " shares of " + item + " for " + nFormat((sharesNum * salePrice), "$0.000a") + " Forecast: " + nFormat(forecast, "0.000"));
- investedStocks--;
- var soldPosition = getStockPosition(item);
- totalWorth -= prices[index] * shares[index];
- shares[index] = soldPosition[0];
- prices[index] = soldPosition[1];
- totalWorth += prices[index] * shares[index];
- } else {
- print("Selling " + item + " reported unsuccessful sale?");
- }
- }
- } else if (forecast > 0.6) {
- var sharePrice = getStockPurchaseCost(item, 1, "L") - 100000;
- var position = sharesNum * sharePrice;
- //Weight how much to by based on forecast
- var adjustedMaxPosition = maxPosition * (1 + (forecast * 0.8));
- var diff = Math.floor(adjustedMaxPosition - position);
- if (diff > initialInvestmentThresh && position < adjustedMaxPosition) {
- var buyPrice = buyStock(item, diff / sharePrice);
- if (buyPrice != 0) {
- print("Bought " + nFormat((diff / sharePrice), "0,0") + " shares of " + item + " for " + nFormat(((diff / sharePrice) * buyPrice), "$0.000a") + " Forecast: " + nFormat(forecast, "0.000"));
- investedStocks++;
- var boughtPosition = getStockPosition(item);
- totalWorth -= prices[index] * shares[index];
- shares[index] = boughtPosition[0];
- prices[index] = boughtPosition[1];
- totalWorth += prices[index] * shares[index];
- } else {
- print("Could not buy " + item);
- }
- } else if (diff < (-1 * rebalanceThresh)) {
- var overboughtShareAmount = Math.floor(Math.abs(diff / sharePrice));
- var overboughtSalePrice = sellStock(item, overboughtShareAmount);
- if (overboughtSalePrice != 0) {
- print("Overbought: Sold " + nFormat(overboughtShareAmount, "0,0") + " shares of " + item + " for " + nFormat((overboughtShareAmount * overboughtSalePrice), "$0.000a") + " Forecast: " + nFormat(forecast, "0.000"));
- var overboughtPosition = getStockPosition(item);
- totalWorth -= prices[index] * shares[index];
- shares[index] = overboughtPosition[0];
- prices[index] = overboughtPosition[1];
- totalWorth += prices[index] * shares[index];
- if (shares[index] === 0) {
- investedStocks--;
- }
- } else {
- print("Overbought: Selling " + item + " reported unsuccessful sale?");
- }
- } else {
- totalWorth -= prices[index] * shares[index];
- prices[index] = sharePrice;
- totalWorth += prices[index] * shares[index];
- }
- }
- });
- stockInvestmentRatio = 1 / (symbols.length * ((investedStocks + 2) / symbols.length));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement