forked from kamukrass/Bitburner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock-trader.js
More file actions
181 lines (162 loc) · 6.45 KB
/
stock-trader.js
File metadata and controls
181 lines (162 loc) · 6.45 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// file: stock-trader.js
// requires 4s Market Data TIX API Access
// defines if stocks can be shorted (see BitNode 8)
const shortAvailable = true;
const commission = 100000;
export async function main(ns) {
ns.disableLog("ALL");
while (true) {
tendStocks(ns);
await ns.sleep(5 * 1000);
}
}
function tendStocks(ns) {
ns.print("");
var stocks = getAllStocks(ns);
stocks.sort((a, b) => b.profitPotential - a.profitPotential);
var longStocks = new Set();
var shortStocks = new Set();
var overallValue = 0;
for (const stock of stocks) {
if (stock.longShares > 0) {
if (stock.forecast > 0.5) {
longStocks.add(stock.sym);
ns.print(`INFO ${stock.summary} LONG ${ns.nFormat(stock.cost + stock.profit, "0.0a")} ${ns.nFormat(100 * stock.profit / stock.cost, "0.00")}%`);
overallValue += (stock.cost + stock.profit);
}
else {
const salePrice = ns.stock.sell(stock.sym, stock.longShares);
const saleTotal = salePrice * stock.longShares;
const saleCost = stock.longPrice * stock.longShares;
const saleProfit = saleTotal - saleCost - 2 * commission;
stock.shares = 0;
shortStocks.add(stock.sym);
ns.print(`WARN ${stock.summary} SOLD for ${ns.nFormat(saleProfit, "$0.0a")} profit`);
}
}
if (stock.shortShares > 0) {
if (stock.forecast < 0.5) {
shortStocks.add(stock.sym);
ns.print(`INFO ${stock.summary} SHORT ${ns.nFormat(stock.cost + stock.profit, "0.0a")} ${ns.nFormat(100 * stock.profit / stock.cost, "0.00")}%`);
overallValue += (stock.cost + stock.profit);
}
else {
const salePrice = ns.stock.sellShort(stock.sym, stock.shortShares);
const saleTotal = salePrice * stock.shortShares;
const saleCost = stock.shortPrice * stock.shortShares;
const saleProfit = saleTotal - saleCost - 2 * commission;
stock.shares = 0;
longStocks.add(stock.sym);
ns.print(`WARN ${stock.summary} SHORT SOLD for ${ns.nFormat(saleProfit, "$0.0a")} profit`);
}
}
}
for (const stock of stocks) {
var money = ns.getServerMoneyAvailable("home");
//ns.print(`INFO ${stock.summary}`);
if (stock.forecast > 0.55) {
longStocks.add(stock.sym);
//ns.print(`INFO ${stock.summary}`);
if (money > 500 * commission) {
const sharesToBuy = Math.min(stock.maxShares, Math.floor((money - commission) / stock.askPrice));
if (ns.stock.buy(stock.sym, sharesToBuy) > 0) {
ns.print(`WARN ${stock.summary} LONG BOUGHT ${ns.nFormat(sharesToBuy, "$0.0a")}`);
}
}
}
else if (stock.forecast < 0.45 && shortAvailable) {
shortStocks.add(stock.sym);
//ns.print(`INFO ${stock.summary}`);
if (money > 500 * commission) {
const sharesToBuy = Math.min(stock.maxShares, Math.floor((money - commission) / stock.bidPrice));
if (ns.stock.short(stock.sym, sharesToBuy) > 0) {
ns.print(`WARN ${stock.summary} SHORT BOUGHT ${ns.nFormat(sharesToBuy, "$0.0a")}`);
}
}
}
}
ns.print("Stock value: " + ns.nFormat(overallValue, "$0.0a"));
// send stock market manipulation orders to hack manager
var growStockPort = ns.getPortHandle(1); // port 1 is grow
var hackStockPort = ns.getPortHandle(2); // port 2 is hack
if (growStockPort.empty() && hackStockPort.empty()) {
// only write to ports if empty
for (const sym of longStocks) {
//ns.print("INFO grow " + sym);
growStockPort.write(getSymServer(sym));
}
for (const sym of shortStocks) {
//ns.print("INFO hack " + sym);
hackStockPort.write(getSymServer(sym));
}
}
}
export function getAllStocks(ns) {
// make a lookup table of all stocks and all their properties
const stockSymbols = ns.stock.getSymbols();
const stocks = [];
for (const sym of stockSymbols) {
const pos = ns.stock.getPosition(sym);
const stock = {
sym: sym,
longShares: pos[0],
longPrice: pos[1],
shortShares: pos[2],
shortPrice: pos[3],
forecast: ns.stock.getForecast(sym),
volatility: ns.stock.getVolatility(sym),
askPrice: ns.stock.getAskPrice(sym),
bidPrice: ns.stock.getBidPrice(sym),
maxShares: ns.stock.getMaxShares(sym),
};
var longProfit = stock.longShares * (stock.bidPrice - stock.longPrice) - 2 * commission;
var shortProfit = stock.shortShares * (stock.shortPrice - stock.askPrice) - 2 * commission;
stock.profit = longProfit + shortProfit;
stock.cost = (stock.longShares * stock.longPrice) + (stock.shortShares * stock.shortPrice)
// profit potential as chance for profit * effect of profit
var profitChance = 2 * Math.abs(stock.forecast - 0.5);
var profitPotential = profitChance * (stock.volatility);
stock.profitPotential = profitPotential;
stock.summary = `${stock.sym}: ${stock.forecast.toFixed(3)} ± ${stock.volatility.toFixed(3)}`;
stocks.push(stock);
}
return stocks;
}
function getSymServer(sym) {
const symServer = {
"WDS": "",
"ECP": "ecorp",
"MGCP": "megacorp",
"BLD": "blade",
"CLRK": "clarkinc",
"OMTK": "omnitek",
"FSIG": "4sigma",
"KGI": "kuai-gong",
"DCOMM": "defcomm",
"VITA": "vitalife",
"ICRS": "icarus",
"UNV": "univ-energy",
"AERO": "aerocorp",
"SLRS": "solaris",
"GPH": "global-pharm",
"NVMD": "nova-med",
"LXO": "lexo-corp",
"RHOC": "rho-construction",
"APHE": "alpha-ent",
"SYSC": "syscore",
"CTK": "comptek",
"NTLK": "netlink",
"OMGA": "omega-net",
"JGN": "joesguns",
"SGC": "sigma-cosmetics",
"CTYS": "catalyst",
"MDYN": "microdyne",
"TITN": "titan-labs",
"FLCM": "fulcrumtech",
"STM": "stormtech",
"HLS": "helios",
"OMN": "omnia",
"FNS": "foodnstuff"
}
return symServer[sym];
}