Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @param {NS} ns */
- let inHand = 0.5; // % of funds to keep in hand. Default 0.25
- let numCyclesToProject = 2; // Only buy stocks that are projected to increase for this amount of cycles. Recommended 2-5. Default 2
- 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
- let commission = 100000; // Current Buy/Sell Comission cost
- function pChange(ns, sym, oldNum, newNum){
- const diff = newNum < oldNum ? -(oldNum - newNum) : newNum - oldNum;
- let pdiff = diff / oldNum;
- ns.print(` ${sym}:\t| ${oldNum.toFixed(5)} -> ${newNum.toFixed(5)}\t| ${(pdiff*100).toFixed(3)}%`);
- return pdiff
- }
- function format(num){
- let symbols = ["","K","M","B","T","Qa","Qi","Sx","Sp","Oc"];
- let i = 0;
- let neg = num < 0;
- if(neg) num = -num;
- for(; (num >= 1000) && (i < symbols.length); i++) num /= 1000;
- return ( (neg)?"-$":"$") + num.toFixed(3) + symbols[i];
- }
- function getStocks(ns, stocks, myStocks){
- let corpus = ns.getServerMoneyAvailable("home");
- myStocks.length = 0;
- for(let i = 0; i < stocks.length; i++){
- let sym = stocks[i].sym;
- stocks[i].price = ns.stock.getPrice(sym);
- stocks[i].shares = ns.stock.getPosition(sym)[0];
- stocks[i].buyPrice = ns.stock.getPosition(sym)[1];
- stocks[i].vol = ns.stock.getVolatility(sym);
- stocks[i].prob = 2* (ns.stock.getForecast(sym) - 0.5);
- stocks[i].expRet = stocks[i].vol * stocks[i].prob / 2;
- if (stocks[i].shares > 0){
- stocks[i].initExpRet ||= stocks[i].expRet;
- }else{
- stocks[i].initExpRet = null;
- }
- corpus += stocks[i].price * stocks[i].shares;
- if(stocks[i].shares > 0) myStocks.push(stocks[i]);
- }
- stocks.sort(function(a, b){return b.expRet - a.expRet});
- return corpus;
- }
- async function buy(ns, stock, numShares) {
- const max = ns.stock.getMaxShares(stock.sym)
- numShares = max < numShares ? max : numShares;
- await ns.stock.buy(stock.sym, numShares);
- ns.print(`Bought ${stock.sym} for ${format(numShares * stock.price)}`);
- }
- async function sell(ns, stock, numShares) {
- let profit = (numShares * (stock.price - stock.buyPrice)) - (2 * commission);
- await ns.stock.sell(stock.sym, numShares);
- ns.print(`Sold ${stock.sym} for profit of ${format(profit)}`);
- }
- export async function main(ns) {
- //Initialise
- ns.disableLog("ALL");
- let stocks = [...ns.stock.getSymbols().map(_sym => {return {sym: _sym}})];
- let myStocks = [];
- let corpus = 0;
- while (true) {
- corpus = getStocks(ns, stocks, myStocks);
- //Symbol, Initial Return, Current Return, The % change between
- // the Initial Return and the Current Return.
- ns.print("Currently Owned Stocks:");
- ns.print(" SYM\t| InitReturn -> CurReturn | % change");
- //Sell underperforming shares
- for (let i = 0; i < myStocks.length; i++) {
- if (pChange(ns, myStocks[i].sym, myStocks[i].initExpRet, myStocks[i].expRet) <= expectedRetentionLossToSell)
- await sell(ns, myStocks[i], myStocks[i].shares);
- if (myStocks[i].expRet <= 0)
- await sell(ns, myStocks[i], myStocks[i].shares);
- corpus -= commission;
- }
- ns.print("----------------------------------------");
- ns.print(" SYM\t| $ invested\t| $ profit");
- for (let i = 0; i < myStocks.length; i++) {
- 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))}`);
- }
- ns.print("________________________________________");
- //Buy shares with cash remaining in hand
- for (let stock of stocks){
- if (stock.shares > 0) continue;
- if (stock.expRet <= 0) continue;
- let cashToSpend = ns.getServerMoneyAvailable("home") - (inHand * corpus);
- let numShares = Math.floor((cashToSpend - commission) / stock.price);
- if ((numShares * stock.expRet * stock.price * numCyclesToProject) > commission)
- await buy(ns, stock, numShares);
- break;
- }
- await ns.sleep(5 * 1000 * numCyclesToProject + 200);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement