Advertisement
Guest User

Untitled

a guest
Jul 19th, 2022
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns */
  2.  
  3. let inHand = 0.5; // % of funds to keep in hand. Default 0.25
  4. let numCyclesToProject = 2; // Only buy stocks that are projected to increase for this amount of cycles. Recommended 2-5. Default 2
  5. let expectedRetentionLossToSell = -0.40; // Percent change between initial forecast and current forcast. ie if current forecast is 40% worse than initial, sell. Default -0.40
  6. let commission = 100000; // Current Buy/Sell Comission cost
  7.    
  8.  
  9. function pChange(ns, sym, oldNum, newNum){
  10.     const diff = newNum < oldNum ? -(oldNum - newNum) : newNum - oldNum;
  11.     let pdiff = diff / oldNum;
  12.     ns.print(` ${sym}:\t| ${oldNum.toFixed(5)} -> ${newNum.toFixed(5)}\t| ${(pdiff*100).toFixed(3)}%`);
  13.     return pdiff
  14. }
  15.  
  16. function format(num){
  17.     let symbols = ["","K","M","B","T","Qa","Qi","Sx","Sp","Oc"];
  18.     let i = 0;
  19.     let neg = num < 0;
  20.     if(neg) num = -num;
  21.  
  22.     for(; (num >= 1000) && (i < symbols.length); i++) num /= 1000;
  23.  
  24.     return ( (neg)?"-$":"$") + num.toFixed(3) + symbols[i];
  25. }
  26.  
  27. function getStocks(ns, stocks, myStocks){
  28.     let corpus = ns.getServerMoneyAvailable("home");
  29.     myStocks.length = 0;
  30.     for(let i = 0; i < stocks.length; i++){
  31.         let sym = stocks[i].sym;
  32.         stocks[i].price = ns.stock.getPrice(sym);
  33.         stocks[i].shares  = ns.stock.getPosition(sym)[0];
  34.         stocks[i].buyPrice = ns.stock.getPosition(sym)[1];
  35.         stocks[i].vol = ns.stock.getVolatility(sym);
  36.         stocks[i].prob = 2* (ns.stock.getForecast(sym) - 0.5);
  37.         stocks[i].expRet = stocks[i].vol * stocks[i].prob / 2;
  38.         if (stocks[i].shares > 0){
  39.             stocks[i].initExpRet ||= stocks[i].expRet;
  40.         }else{
  41.             stocks[i].initExpRet = null;
  42.         }
  43.  
  44.         corpus += stocks[i].price * stocks[i].shares;
  45.         if(stocks[i].shares > 0) myStocks.push(stocks[i]);
  46.     }
  47.     stocks.sort(function(a, b){return b.expRet - a.expRet});
  48.     return corpus;
  49. }
  50.  
  51. async function buy(ns, stock, numShares) {
  52.     const max = ns.stock.getMaxShares(stock.sym)
  53.     numShares = max < numShares ?  max : numShares;
  54.  
  55.     await ns.stock.buy(stock.sym, numShares);
  56.     ns.print(`Bought ${stock.sym} for ${format(numShares * stock.price)}`);
  57. }
  58.  
  59. async function sell(ns, stock, numShares) {
  60.     let profit = (numShares * (stock.price - stock.buyPrice)) - (2 * commission);
  61.     await ns.stock.sell(stock.sym, numShares);
  62.     ns.print(`Sold ${stock.sym} for profit of ${format(profit)}`);
  63. }
  64.  
  65.  
  66. export async function main(ns) {
  67.     //Initialise
  68.     ns.disableLog("ALL");
  69.     let stocks = [...ns.stock.getSymbols().map(_sym => {return {sym: _sym}})];
  70.     let myStocks = [];
  71.     let corpus = 0;
  72.  
  73.  
  74.     while (true) {
  75.         corpus = getStocks(ns, stocks, myStocks);
  76.        
  77.         //Symbol, Initial Return, Current Return, The % change between
  78.         // the Initial Return and the Current Return.
  79.         ns.print("Currently Owned Stocks:");
  80.         ns.print(" SYM\t| InitReturn -> CurReturn | % change");
  81.  
  82.         //Sell underperforming shares
  83.         for (let i = 0; i < myStocks.length; i++) {
  84.             if (pChange(ns, myStocks[i].sym, myStocks[i].initExpRet, myStocks[i].expRet) <= expectedRetentionLossToSell)
  85.                 await sell(ns, myStocks[i], myStocks[i].shares);
  86.  
  87.             if (myStocks[i].expRet <= 0)
  88.                 await sell(ns, myStocks[i], myStocks[i].shares);
  89.  
  90.             corpus -= commission;
  91.         }
  92.        
  93.         ns.print("----------------------------------------");
  94.  
  95.         ns.print(" SYM\t| $ invested\t| $ profit");
  96.         for (let i = 0; i < myStocks.length; i++) {
  97.             ns.print(` ${myStocks[i].sym}:\t| ${format(myStocks[i].shares * myStocks[i].buyPrice)}\t| ${format((myStocks[i].shares * (myStocks[i].price - myStocks[i].buyPrice))- (2 * commission))}`);
  98.         }
  99.  
  100.        
  101.         ns.print("________________________________________");
  102.  
  103.         //Buy shares with cash remaining in hand
  104.         for (let stock of stocks){
  105.  
  106.             if (stock.shares > 0) continue;
  107.             if (stock.expRet <= 0) continue;
  108.             let cashToSpend = ns.getServerMoneyAvailable("home") - (inHand * corpus);
  109.             let numShares = Math.floor((cashToSpend - commission) / stock.price);
  110.             if ((numShares * stock.expRet * stock.price * numCyclesToProject) > commission)
  111.                 await buy(ns, stock, numShares);
  112.                 break;
  113.         }
  114.  
  115.         await ns.sleep(5 * 1000 * numCyclesToProject + 200);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement