diff options
Diffstat (limited to 'stocks')
-rw-r--r-- | stocks/liquidate.js | 10 | ||||
-rw-r--r-- | stocks/maxprice.txt | 1 | ||||
-rw-r--r-- | stocks/minprice.txt | 1 | ||||
-rw-r--r-- | stocks/stocker.js | 144 | ||||
-rw-r--r-- | stocks/stockgains.txt | 0 | ||||
-rw-r--r-- | stocks/stocks.js | 25 |
6 files changed, 181 insertions, 0 deletions
diff --git a/stocks/liquidate.js b/stocks/liquidate.js new file mode 100644 index 0000000..79f902e --- /dev/null +++ b/stocks/liquidate.js @@ -0,0 +1,10 @@ +/** @param {NS} ns */ +export async function main(ns) { + const syms = ns.stock.getSymbols(); + ns.scriptKill("stocks.js", "home"); + ns.scriptKill("stocker.js", "home"); + for (let i = 0; i < syms.length; i++) { + let shares = ns.stock.getMaxShares(syms[i]); + ns.stock.sellStock(syms[i], shares); + } +}
\ No newline at end of file diff --git a/stocks/maxprice.txt b/stocks/maxprice.txt new file mode 100644 index 0000000..58e8fae --- /dev/null +++ b/stocks/maxprice.txt @@ -0,0 +1 @@ +921.2475880659246
\ No newline at end of file diff --git a/stocks/minprice.txt b/stocks/minprice.txt new file mode 100644 index 0000000..123c27c --- /dev/null +++ b/stocks/minprice.txt @@ -0,0 +1 @@ +526.5046927941131
\ No newline at end of file diff --git a/stocks/stocker.js b/stocks/stocker.js new file mode 100644 index 0000000..9f5a6cb --- /dev/null +++ b/stocks/stocker.js @@ -0,0 +1,144 @@ +/** @param {NS} ns */ +export async function main(ns) { + const sym = ns.args[0]; + const target = ns.args[1]; + let manip = ns.args[2]; + let debug = ns.args[3]; + let pricebought; + let pricesold; + debug = true; + + const maxmoney = 0; + if (sym === undefined) { + ns.print("arg 0 is the stock symbol"); + ns.exit(); + } else if (sym == "--help" || sym == "help" || sym == "-h") { + ns.tprint("\n script that buys stocks and sells high.\n 1st arg is the stock symbol.\n 2nd arg is the stock's server.\n 3rd arg is whether to hack and grow servers to manipulate stock prices."); + ns.exit(); + } + if (target === undefined) { + ns.print("arg 1 is the target server"); + } else { + maxmoney = ns.getServerMaxMoney(target); + } + if (manip === undefined) { + ns.print("arg 2 is boolean, proceeding with false"); + manip = false; + } + if (debug === undefined) { + ns.print("arg 3 is debug; turning off"); + debug = false; + } + let minprice = Number(ns.read("minprice.txt")); + let maxprice = Number(ns.read("maxprice.txt")); + const buyprice = minprice; + const sellprice = maxprice; + let shares = ns.stock.getMaxShares(sym); + //const minprice = ns.stock. + ns.disableLog("sleep"); + + if (manip == true) { + while (true) { + // Hack foodnstuff to bring price down + if (ns.getServerMoneyAvailable(target) != 0) { + ns.print("bringing price down."); + while (ns.getServerMoneyAvailable(target) != 0) { + ns.exec("misc/run.js", "home", 1, target, "start/temphack.js"); + await ns.sleep(5000); + } + } + // after price is down, buy max stock + // homemade place order command + if (ns.stock.getAskPrice(sym) > buyprice) { + ns.print("Waiting for price to drop"); + while (ns.stock.getAskPrice(sym) > buyprice) { + ns.print(Floor(ns.stock.getAskPrice(sym))); + await ns.sleep(60000); + if (ns.stock.getAskPrice(sym) < minprice) { + minprice = ns.stock.getAskPrice(sym); + ns.write("minprice.txt", minprice, "w"); + } else if (ns.stock.getBidPrice(sym) > maxprice) { + maxprice = ns.stock.getBidPrice(sym); + ns.write("maxprice.txt", maxprice, "w"); + } + } + } + ns.stock.buyStock(sym, shares); + // after bought, raise price with grow + if (ns.getServerMoneyAvailable(target) < maxmoney) { + ns.print("bringing price up."); + while (ns.getServerMoneyAvailable(target) < maxmoney) { + ns.exec("misc/run.js", "home", 1, target, "start/tempgrow.js"); + await ns.sleep(5000); + } + } + // after raising price, sell all stock + if (ns.stock.getBidPrice(sym) < sellprice) { + ns.print("Waiting for price to increase"); + while (ns.stock.getBidPrice(sym) < sellprice) { + ns.print(Floor(ns.stock.getBidPrice(sym))); + await ns.sleep(60000); + if (ns.stock.getBidPrice(sym) > maxprice) { + maxprice = ns.stock.getBidPrice(sym); + ns.write("maxprice.txt", maxprice, "w"); + } else if (ns.stock.getAskPrice(sym) < minprice) { + minprice = ns.stock.getAskPrice(sym); + ns.write("minprice.txt", minprice, "w"); + } + } + } + ns.stock.sellStock(sym, shares); + //repeat after 10 seconds + await ns.sleep(10000); + } + } else { + let bought = 0; + if (bought == 0) { + + if (debug) {log(ns, sym);} + while (bought == 0) { + // if price is going up + if (debug) {ns.print("Forecast for ", sym, ": ", (ns.stock.getForecast(sym)).toFixed(2));} + if (ns.stock.getForecast(sym) > 0.5) { + ns.stock.buyStock(sym, shares); + pricebought = ns.stock.getAskPrice(sym) * shares; + bought++; + break; + } + await ns.sleep(6000); + } + } if (bought == 1) { + if (debug) {ns.print("Waiting for forecast to show going down.");} + while (bought == 1) { + // log window messages + if (debug) {log(ns, sym);} + if (ns.stock.getForecast(sym) < 0.5) { + ns.stock.sellStock(sym, shares); + pricesold = ns.stock.getBidPrice(sym) * shares; + bought--; + await ns.sleep(1000); + ns.exit(); + } + await ns.sleep(6000); + } + } + gains(ns, pricebought, pricesold); + } +} + +function log(ns, sym) { + ns.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); + ns.print("Forecast for ", sym, ": ", (ns.stock.getForecast(sym)).toFixed(2)); + ns.print("Price of ", sym, ": ", ns.stock.getPrice(sym)); +} + +function gains(ns, pricebought, pricesold) { + let net = pricesold - pricebought; + let prev = ns.read("stockgains.js"); + let total = prev + net; + ns.write("stockgains.js", total, "w"); +} +// round forecast to 1 decimal +// shorten forecast message to *sym*: *number* +// remove previous messages by adding a bunch of \n +// add a price change message diff --git a/stocks/stockgains.txt b/stocks/stockgains.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/stocks/stockgains.txt diff --git a/stocks/stocks.js b/stocks/stocks.js new file mode 100644 index 0000000..c456534 --- /dev/null +++ b/stocks/stocks.js @@ -0,0 +1,25 @@ +/** @param {NS} ns */ +export async function main(ns) { + let manip = ns.args[0]; + if (manip == "-h" || manip == "help" || manip == "--help") { + ns.tprint("arg 0 is the boolean, to manipulate prices or not. \n True uses more scripts"); + manip = false; + } + //ns.disableLog("sleep"); + const syms = ns.stock.getSymbols(); + while (true) { + for (let i = 0; i < syms.length; i++) { + let sym = syms[i]; + if (ns.stock.getForecast(sym) > 0.5 && !(ns.getRunningScript("stocker.js", "home", sym))) { + if ((ns.stock.getAskPrice(sym) * ns.stock.getMaxShares(sym)) < ns.getServerMoneyAvailable("home")) { + ns.exec("stocker.js", "home", 1, sym); + } + } + await ns.sleep(1000); + } + await ns.sleep(1000); + } +} + +// look through all the stocks to see what is going up, and run stocker on whatever is +//
\ No newline at end of file |