Closed doubravap closed 2 years ago
I think this is a known issue. Might be related to the report at #4184. Sometimes the stock market can fail to load properly. You might want to try the old "turn it off and on again" trick: save your game, quit, then reload. Hope that helps.
RUNTIME ERROR /stock/stock3.js@home (PID - 2693)
getAskPrice: Invalid stock symbol: 'ECP'
Stack: /stock/stock3.js:L28@refresh /stock/stock3.js:L142@Module.main
Code: `/* @param {NS} ns / //https://www.reddit.com/r/Bitburner/comments/9o1xle/comment/hq6s1gw/
//Requires access to the TIX API and the 4S Mkt Data API
let fracL = 0.1; //Fraction of assets to keep as cash in hand let fracH = 0.15; // let commission = 100000; //Buy or sell commission let numCycles = 2; //Each cycle is 5 seconds let totalProfit = 0.0; let totalLosses = 0.0
/**
@param {[]} myStocks Owned stocks */ function refresh(ns, stocks, myStocks) { let cashToSpend = ns.getServerMoneyAvailable("home"); //corpus stores equity (cash + value of all stocks) let corpus = cashToSpend; myStocks.length = 0;
//refresh stock data for (let i = 0; i < stocks.length; i++) { let sym = stocks[i].sym; stocks[i].price = ns.stock.getAskPrice(sym); stocks[i].maxShares = ns.stock.getMaxShares(sym); stocks[i].shares = ns.stock.getPosition(sym)[0]; stocks[i].buyPrice = ns.stock.getPosition(sym)[1]; stocks[i].vol = ns.stock.getVolatility(sym); //calculate probability of stock price gain/loss stocks[i].prob = 2 (ns.stock.getForecast(sym) - 0.5); //calculate expected return stocks[i].expRet = stocks[i].vol stocks[i].prob / 2; //add stock equity to corpus corpus += stocks[i].price * stocks[i].shares;
}
//prunes low-cost stocks that are inefficient stocks.forEach(function (_, i) { //calculate max purchase cost let cost = stocks[i].price * (stocks[i].maxShares - stocks[i].shares);
});
//order stocks by profitability % stocks.sort(function (a, b) { return b.expRet - a.expRet });
return corpus; }
/**
/**
@param {number} numShares Number of shares to sell / function sell(ns, stock, numShares) { let profit = numShares (stock.price - stock.buyPrice) - 2 * commission;
if (ns.stock.sell(stock.sym, numShares) > 0) { let message =
Sold ${format(numShares, false)} shares of ${stock.sym} for profit of ${format(profit, true)}
; ns.print(message);} else { ns.print(
Failed to sell ${format(numShares, false)} shares of ${stock.sym} for profit of ${format(profit, true)}
); }}
/**
num
represents a monetary numerical value or not */ function format(num, isMonetary) { //define suffixes let symbols = ["", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc"]; let i = 0; //determine correct suffix for (; (Math.abs(num) >= 1000) && (i < symbols.length); i++) num /= 1000; //return formatted number return ((Math.sign(num) < 0) ? "-" : "") + (isMonetary ? "$" : "") + Math.abs(num).toFixed(3) + symbols[i]; }/**
*/ export async function main(ns) { //Initialise ns.disableLog("ALL"); let stocks = []; let myStocks = []; let corpus = 0; let symbols = ns.stock.getSymbols();
//populate stock list and add stock symbols for (let i = 0; i < symbols.length; i++) { let sym = symbols[i]; stocks.push({ sym: sym }); }
while (true) { //refresh equity at beginning of loop corpus = refresh(ns, stocks, myStocks);
} }`