Advertisement
Guest User

Untitled

a guest
Aug 11th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. disableLog("ALL");
  2.  
  3. //User Settings
  4. var investPercent = 0.85; //Percentage of overall networth invested at once (desired). Default 0.85
  5. var rebalanceThresh = 5000000; //Amount overbought a stock must be before selling some. Default 5mil
  6. var initialInvestmentThresh = 500000; //Minimum investment on a new stock. Default 500k
  7. //End User Settings
  8.  
  9.  
  10.  
  11. 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"];
  12. var investedStocks = 0;
  13. var shares = [];
  14. var prices = [];
  15. var totalWorth = 0;
  16. var lastCoH = 0;
  17. var stockInvestmentRatio = 1 / symbols.length;
  18. print("Getting initial positions...");
  19. for (i = 0; i < symbols.length; i = i + 1) {
  20.     position = getStockPosition(symbols[i]);
  21.     shares.push(position[0]);
  22.     prices.push(position[1]);
  23.     totalWorth += position[0] * position[1];
  24.     if (position[0] > 0) {
  25.         investedStocks++;
  26.     }
  27. }
  28.  
  29. while (true) {
  30.     totalWorth -= lastCoH;
  31.     var cashOnHand = getServerMoneyAvailable("home");
  32.     totalWorth += cashOnHand;
  33.     lastCoH = cashOnHand;
  34.     //Since we aren't investing in every stock, figure out about how much should go in to each
  35.     //Could cause an over-investment (above investPercent) or running out of COH
  36.     //if many stocks suddenly becomes attractive.
  37.  
  38.     var maxPosition = (totalWorth * investPercent) * stockInvestmentRatio;
  39.     print("-----");
  40.     print(investedStocks + " stocks currently invested in, " + nFormat(stockInvestmentRatio, "(0.00%)") + " NW per-stock ratio.");
  41.     print(nFormat(cashOnHand, "$0.000a") + " CoH, " + nFormat(totalWorth, "$0.000a") + " NW, " + nFormat(maxPosition, "$0.000a") + " is our base position");
  42.     print("-----");
  43.     symbols.forEach(function(item, index, array) {
  44.         var forecast = getStockForecast(item);
  45.         var sharesNum = shares[index];
  46.         if (forecast < 0.5) {
  47.             if (shares[index] > 0) {
  48.                 var salePrice = sellStock(item, sharesNum);
  49.                 if (salePrice != 0) {
  50.                     print("Sold " + nFormat(sharesNum, "0,0") + " shares of " + item + " for " + nFormat((sharesNum * salePrice), "$0.000a") + " Forecast: " + nFormat(forecast, "0.000"));
  51.                     investedStocks--;
  52.                     var soldPosition = getStockPosition(item);
  53.                     totalWorth -= prices[index] * shares[index];
  54.                     shares[index] = soldPosition[0];
  55.                     prices[index] = soldPosition[1];
  56.                     totalWorth += prices[index] * shares[index];
  57.                 } else {
  58.                     print("Selling " + item + " reported unsuccessful sale?");
  59.                 }
  60.             }
  61.         } else if (forecast > 0.6) {
  62.             var sharePrice = getStockPurchaseCost(item, 1, "L") - 100000;
  63.             var position = sharesNum * sharePrice;
  64.             //Weight how much to by based on forecast
  65.             var adjustedMaxPosition = maxPosition * (1 + (forecast * 0.8));
  66.             var diff = Math.floor(adjustedMaxPosition - position);
  67.             if (diff > initialInvestmentThresh && position < adjustedMaxPosition) {
  68.                 var buyPrice = buyStock(item, diff / sharePrice);
  69.                 if (buyPrice != 0) {
  70.                     print("Bought " + nFormat((diff / sharePrice), "0,0") + " shares of " + item + " for " + nFormat(((diff / sharePrice) * buyPrice), "$0.000a") + " Forecast: " + nFormat(forecast, "0.000"));
  71.                     investedStocks++;
  72.                     var boughtPosition = getStockPosition(item);
  73.                     totalWorth -= prices[index] * shares[index];
  74.                     shares[index] = boughtPosition[0];
  75.                     prices[index] = boughtPosition[1];
  76.                     totalWorth += prices[index] * shares[index];
  77.                 } else {
  78.                     print("Could not buy " + item);
  79.                 }
  80.             } else if (diff < (-1 * rebalanceThresh)) {
  81.                 var overboughtShareAmount = Math.floor(Math.abs(diff / sharePrice));
  82.                 var overboughtSalePrice = sellStock(item, overboughtShareAmount);
  83.                 if (overboughtSalePrice != 0) {
  84.                     print("Overbought: Sold " + nFormat(overboughtShareAmount, "0,0") + " shares of " + item + " for " + nFormat((overboughtShareAmount * overboughtSalePrice), "$0.000a") + " Forecast: " + nFormat(forecast, "0.000"));
  85.                     var overboughtPosition = getStockPosition(item);
  86.                     totalWorth -= prices[index] * shares[index];
  87.                     shares[index] = overboughtPosition[0];
  88.                     prices[index] = overboughtPosition[1];
  89.                     totalWorth += prices[index] * shares[index];
  90.                     if (shares[index] === 0) {
  91.                         investedStocks--;
  92.                     }
  93.                 } else {
  94.                     print("Overbought: Selling " + item + " reported unsuccessful sale?");
  95.                 }
  96.             } else {
  97.                 totalWorth -= prices[index] * shares[index];
  98.                 prices[index] = sharePrice;
  99.                 totalWorth += prices[index] * shares[index];
  100.             }
  101.         }
  102.     });
  103.     stockInvestmentRatio = 1 / (symbols.length * ((investedStocks + 2) / symbols.length));
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement