Advertisement
Guest User

stock.js

a guest
Jan 20th, 2022
2,208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const commission = 100000;
  2. const samplingLength = 30;
  3.  
  4. function predictState(samples) {
  5.   const limits = [null, null, null, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 11, 12, 12, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 19, 19, 20];
  6.   let inc = 0;
  7.   for (let i = 0; i < samples.length; ++i) {
  8.     const total = i + 1;
  9.     const idx = samples.length - total;
  10.     if (samples[idx] > 1.) {
  11.       ++inc;
  12.     }
  13.     const limit = limits[i];
  14.     if (limit === null) {
  15.       continue;
  16.     }
  17.     if (inc >= limit) {
  18.       return 1;
  19.     }
  20.     if ((total-inc) >= limit) {
  21.       return -1;
  22.     }
  23.   }
  24.   return 0;
  25. }
  26.  
  27. function format(money) {
  28.     const prefixes = ["", "k", "m", "b", "t", "q"];
  29.     for (let i = 0; i < prefixes.length; i++) {
  30.         if (Math.abs(money) < 1000) {
  31.             return `${Math.floor(money * 10) / 10}${prefixes[i]}`;
  32.         } else {
  33.             money /= 1000;
  34.         }
  35.     }
  36.     return `${Math.floor(money * 10) / 10}${prefixes[prefixes.length - 1]}`;
  37. }
  38.  
  39. function posNegDiff(samples) {
  40.   const pos = samples.reduce((acc, curr) => acc + (curr > 1. ? 1 : 0), 0);
  41.   return Math.abs(samples.length - 2*pos);
  42. }
  43.  
  44. function posNegRatio(samples) {
  45.   const pos = samples.reduce((acc, curr) => acc + (curr > 1. ? 1 : 0), 0);
  46.   return Math.round(100*(2*pos / samples.length - 1));
  47. }
  48.  
  49. export async function main(ns) {
  50.     ns.disableLog("ALL");
  51.     let symLastPrice = {};
  52.     let symChanges = {};
  53.     for (const sym of ns.stock.getSymbols()) {
  54.       symLastPrice[sym] = ns.stock.getPrice(sym);
  55.       symChanges[sym] = []
  56.     }
  57.  
  58.     while (true) {
  59.         await ns.sleep(2000);
  60.  
  61.         if (symLastPrice['FSIG'] === ns.stock.getPrice('FSIG')) {
  62.           continue;
  63.         }
  64.  
  65.         for (const sym of ns.stock.getSymbols()) {
  66.           const current = ns.stock.getPrice(sym);
  67.           symChanges[sym].push(current/symLastPrice[sym]);
  68.           symLastPrice[sym] = current;
  69.           if (symChanges[sym].length > samplingLength) {
  70.             symChanges[sym] = symChanges[sym].slice(symChanges[sym].length - samplingLength);
  71.           }
  72.         }
  73.  
  74.         const prioritizedSymbols = [...ns.stock.getSymbols()];
  75.         prioritizedSymbols.sort((a, b) => posNegDiff(symChanges[b]) - posNegDiff(symChanges[a]));
  76.        
  77.         for (const sym of prioritizedSymbols) {
  78.           const positions = ns.stock.getPosition(sym);
  79.           const longShares = positions[0];
  80.           const longPrice = positions[1];
  81.           const shortShares = positions[2];
  82.           const shortPrice = positions[3];
  83.           const state = predictState(symChanges[sym]);
  84.           const ratio = posNegRatio(symChanges[sym]);
  85.           const bidPrice = ns.stock.getBidPrice(sym);
  86.           const askPrice = ns.stock.getAskPrice(sym);
  87.           if (longShares <= 0 && shortShares <= 0 && ns.stock.getPrice(sym) < 30000) {
  88.             continue;
  89.           }
  90.  
  91.           if (longShares > 0) {
  92.             const cost = longShares * longPrice;
  93.             const profit = longShares * (bidPrice - longPrice) - 2 * commission;
  94.             if (state < 0) {
  95.               const sellPrice = ns.stock.sell(sym, longShares);
  96.               if (sellPrice > 0) {
  97.                   ns.print(`SOLD (long) ${sym}. Profit: ${format(profit)}`);
  98.               }
  99.             } else {
  100.                 ns.print(`${sym} (${ratio}): ${format(profit+cost)} / ${format(profit)} (${Math.round(profit/cost*10000)/100}%)`);
  101.             }
  102.           } else if (shortShares > 0) {
  103.             const cost = shortShares * shortPrice;
  104.             const profit = shortShares * (shortPrice - askPrice) - 2 * commission;
  105.             if (state > 0) {
  106.               const sellPrice = ns.stock.sellShort(sym, shortShares);
  107.               if (sellPrice > 0) {
  108.                   ns.print(`SOLD (short) ${sym}. Profit: ${format(profit)}`);
  109.               }
  110.             } else {
  111.                 ns.print(`${sym} (${ratio}): ${format(profit+cost)} / ${format(profit)} (${Math.round(profit/cost*10000)/100}%)`);
  112.             }
  113.           } else {
  114.             const money = ns.getServerMoneyAvailable("home");
  115.             if (state > 0) {
  116.               const sharesToBuy = Math.min(10000, ns.stock.getMaxShares(sym), Math.floor((money - commission) / askPrice));
  117.               if (ns.stock.buy(sym, sharesToBuy) > 0) {
  118.                   ns.print(`BOUGHT (long) ${sym}.`);
  119.               }
  120.             } else if (state < 0) {
  121.               const sharesToBuy = Math.min(10000, ns.stock.getMaxShares(sym), Math.floor((money - commission) / bidPrice));
  122.               if (ns.stock.short(sym, sharesToBuy) > 0) {
  123.                   ns.print(`BOUGHT (short) ${sym}.`);
  124.               }
  125.             }
  126.           }
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement