1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/** @param {NS} ns */
export async function main(ns) {
const symbol = 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 (symbol === undefined) {
ns.print("arg 0 is the stock symbol");
ns.exit();
} else if (symbol == "--help" || symbol == "help" || symbol == "-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(symbol);
//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(symbol) > buyprice) {
ns.print("Waiting for price to drop");
while (ns.stock.getAskPrice(symbol) > buyprice) {
ns.print(Floor(ns.stock.getAskPrice(symbol)));
await ns.sleep(60000);
if (ns.stock.getAskPrice(symbol) < minprice) {
minprice = ns.stock.getAskPrice(symbol);
ns.write("minprice.txt", minprice, "w");
} else if (ns.stock.getBidPrice(symbol) > maxprice) {
maxprice = ns.stock.getBidPrice(symbol);
ns.write("maxprice.txt", maxprice, "w");
}
}
}
ns.stock.buyStock(symbol, 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(symbol) < sellprice) {
ns.print("Waiting for price to increase");
while (ns.stock.getBidPrice(symbol) < sellprice) {
ns.print(Floor(ns.stock.getBidPrice(symbol)));
await ns.sleep(60000);
if (ns.stock.getBidPrice(symbol) > maxprice) {
maxprice = ns.stock.getBidPrice(symbol);
ns.write("maxprice.txt", maxprice, "w");
} else if (ns.stock.getAskPrice(symbol) < minprice) {
minprice = ns.stock.getAskPrice(symbol);
ns.write("minprice.txt", minprice, "w");
}
}
}
ns.stock.sellStock(symbol, shares);
//repeat after 10 seconds
await ns.sleep(10000);
}
} else {
let bought = 0;
if (bought == 0) {
if (debug) {log(ns, symbol);}
while (bought == 0) {
// if price is going up
if (debug) {ns.print("Forecast for ", symbol, ": ", (ns.stock.getForecast(symbol)).toFixed(2));}
if (ns.stock.getForecast(symbol) > 0.5) {
ns.stock.buyStock(symbol, shares);
pricebought = ns.stock.getAskPrice(symbol) * 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, symbol);}
if (ns.stock.getForecast(symbol) < 0.5) {
ns.stock.sellStock(symbol, shares);
pricesold = ns.stock.getBidPrice(symbol) * shares;
bought--;
await ns.sleep(1000);
ns.exit();
}
await ns.sleep(6000);
}
}
gains(ns, pricebought, pricesold);
}
}
function log(ns, symbol) {
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 ", symbol, ": ", (ns.stock.getForecast(symbol)).toFixed(2));
ns.print("Price of ", symbol, ": ", ns.stock.getPrice(symbol));
}
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 *symbol*: *number*
// remove previous messages by adding a bunch of \n
// add a price change message
|