-
Notifications
You must be signed in to change notification settings - Fork 0
/
stk.js
132 lines (120 loc) · 4.73 KB
/
stk.js
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
// Use this script to make money in the stockmarket before you have the 4S upgrades.
// You won't get as much gains as if you did have the upgrades and ran stock.js, but it works up until then.
const commission = 100000;
const samplingLength = 30;
function predictState(samples) {
const limits = [null, null, null, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 11, 12, 12, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 19, 19, 20];
let inc = 0;
for (let i = 0; i < samples.length; ++i) {
const total = i + 1;
const idx = samples.length - total;
if (samples[idx] > 1.) {
++inc;
}
const limit = limits[i];
if (limit === null) {
continue;
}
if (inc >= limit) {
return 1;
}
if ((total-inc) >= limit) {
return -1;
}
}
return 0;
}
function format(money) {
const prefixes = ["", "k", "m", "b", "t", "q"];
for (let i = 0; i < prefixes.length; i++) {
if (Math.abs(money) < 1000) {
return `${Math.floor(money * 10) / 10}${prefixes[i]}`;
} else {
money /= 1000;
}
}
return `${Math.floor(money * 10) / 10}${prefixes[prefixes.length - 1]}`;
}
function posNegDiff(samples) {
const pos = samples.reduce((acc, curr) => acc + (curr > 1. ? 1 : 0), 0);
return Math.abs(samples.length - 2*pos);
}
function posNegRatio(samples) {
const pos = samples.reduce((acc, curr) => acc + (curr > 1. ? 1 : 0), 0);
return Math.round(100*(2*pos / samples.length - 1));
}
export async function main(ns) {
ns.disableLog("ALL");
let symLastPrice = {};
let symChanges = {};
for (const sym of ns.stock.getSymbols()) {
symLastPrice[sym] = ns.stock.getPrice(sym);
symChanges[sym] = []
}
while (true) {
await ns.sleep(2000);
if (symLastPrice['FSIG'] === ns.stock.getPrice('FSIG')) {
continue;
}
for (const sym of ns.stock.getSymbols()) {
const current = ns.stock.getPrice(sym);
symChanges[sym].push(current/symLastPrice[sym]);
symLastPrice[sym] = current;
if (symChanges[sym].length > samplingLength) {
symChanges[sym] = symChanges[sym].slice(symChanges[sym].length - samplingLength);
}
}
const prioritizedSymbols = [...ns.stock.getSymbols()];
prioritizedSymbols.sort((a, b) => posNegDiff(symChanges[b]) - posNegDiff(symChanges[a]));
for (const sym of prioritizedSymbols) {
const positions = ns.stock.getPosition(sym);
const longShares = positions[0];
const longPrice = positions[1];
const shortShares = positions[2];
const shortPrice = positions[3];
const state = predictState(symChanges[sym]);
const ratio = posNegRatio(symChanges[sym]);
const bidPrice = ns.stock.getBidPrice(sym);
const askPrice = ns.stock.getAskPrice(sym);
if (longShares <= 0 && shortShares <= 0 && ns.stock.getPrice(sym) < 30000) {
continue;
}
if (longShares > 0) {
const cost = longShares * longPrice;
const profit = longShares * (bidPrice - longPrice) - 2 * commission;
if (state < 0) {
const sellPrice = ns.stock.sell(sym, longShares);
if (sellPrice > 0) {
ns.print(`SOLD (long) ${sym}. Profit: ${format(profit)}`);
}
} else {
ns.print(`${sym} (${ratio}): ${format(profit+cost)} / ${format(profit)} (${Math.round(profit/cost*10000)/100}%)`);
}
} else if (shortShares > 0) {
const cost = shortShares * shortPrice;
const profit = shortShares * (shortPrice - askPrice) - 2 * commission;
if (state > 0) {
const sellPrice = ns.stock.sellShort(sym, shortShares);
if (sellPrice > 0) {
ns.print(`SOLD (short) ${sym}. Profit: ${format(profit)}`);
}
} else {
ns.print(`${sym} (${ratio}): ${format(profit+cost)} / ${format(profit)} (${Math.round(profit/cost*10000)/100}%)`);
}
} else {
const money = ns.getServerMoneyAvailable("home");
if (state > 0) {
const sharesToBuy = Math.min(10000, ns.stock.getMaxShares(sym), Math.floor((money - commission) / askPrice));
if (ns.stock.buy(sym, sharesToBuy) > 0) {
ns.print(`BOUGHT (long) ${sym}.`);
}
} else if (state < 0) {
const sharesToBuy = Math.min(10000, ns.stock.getMaxShares(sym), Math.floor((money - commission) / bidPrice));
if (ns.stock.short(sym, sharesToBuy) > 0) {
ns.print(`BOUGHT (short) ${sym}.`);
}
}
}
}
}
}