Advertisement
lanolinoil

stocks.ns

Dec 19th, 2021
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns **/
  2. export async function main(ns) {
  3.     //*********PARAMS FOR SCRIPT ************//
  4.     var maxSharePer = 0.45                  // maximum percent of a symbol's total stock to buy
  5.     var stockBuyPer = 0.6                  //  percent probablity to buy symbol
  6.     var stockVolPer = .03                 //   percent stock will move to buy
  7.     var moneyKeep = 5000000000           //    min money to keep on hand
  8.     var minSharePer = 5                 //     min shares to buy
  9.     var orderMax = 500000000           //      max money to spend on a single order
  10.     var profPer = 0.05                //       min profit percentage to sell
  11.     var panicPer = 0.15              //        percentage loss to panic sell at
  12.     //******************************//
  13.     while (true) {
  14.         ns.disableLog('disableLog');
  15.         ns.disableLog('sleep');
  16.         ns.disableLog('getServerMoneyAvailable')
  17.         var stocks = ns.stock.getSymbols();
  18.         for (const stock of stocks) {
  19.             var position = ns.stock.getPosition(stock)
  20.             if (position[0]) {
  21.                 ns.print('Position: ' + stock + ", " + position[0] + " Profit: " + ns.nFormat(Math.round(ns.stock.getSaleGain(stock, position[0], "Long") - (position[0] * position[1])), '0,0', "Long") + ' % ' + ns.nFormat(ns.stock.getSaleGain(stock, position[0], "Long") / (position[0] * position[1]), "0%"));
  22.                 sellPositions(stock);
  23.             }
  24.             buyPositions(stock);
  25.  
  26.         }
  27.         await ns.sleep(6000);
  28.     }
  29.     function sellPositions(stock) {
  30.         //check profit
  31.         if (ns.stock.getSaleGain(stock, position[0], "Long") / (position[0] * position[1]) >= 1 + profPer && ns.stock.getSaleGain(stock, position[0], "Long") > 100000) {
  32.             //sell stock
  33.             ns.toast("SOLD STOCK " + stock + " for " + ns.nFormat(Math.round(ns.stock.getSaleGain(stock, position[0], "Long") - (position[0] * position[1])), '0,0') + " profit!");
  34.             ns.stock.sell(stock, position[0]);
  35.         } else if (ns.stock.getSaleGain(stock, position[0], "Long") / (position[0] * position[1]) < (1 - panicPer)) {
  36.             ns.toast("SOLD STOCK " + stock + " for " + ns.nFormat(Math.round(ns.stock.getSaleGain(stock, position[0], "Long") - (position[0] * position[1])), '0,0') + " loss!", "warning");
  37.             ns.stock.sell(stock, position[0]);
  38.         }
  39.     }
  40.  
  41.     function buyPositions(stock) {
  42.         var maxShares = (ns.stock.getMaxShares(stock) * maxSharePer) - position[0];
  43.         var askPrice = ns.stock.getAskPrice(stock);
  44.         var forecast = ns.stock.getForecast(stock);
  45.         var volPer = ns.stock.getVolatility(stock);
  46.         var playerMoney = ns.getServerMoneyAvailable('home');
  47.         //if the stock will move positive by stockbuyper or more and it will grow stockvolper or more
  48.         if (forecast >= stockBuyPer && volPer <= stockVolPer) {
  49.             //check money for one share
  50.             if (playerMoney - moneyKeep > ns.stock.getPurchaseCost(stock, minSharePer, "Long")) {
  51.                 var shares = Math.min((playerMoney - moneyKeep - 100000) / askPrice, orderMax / askPrice);
  52.                 ns.stock.buy(stock, Math.min(shares, maxShares));
  53.             }
  54.         }
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement