-
Notifications
You must be signed in to change notification settings - Fork 2
/
stock.js
131 lines (110 loc) · 3.21 KB
/
stock.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
/** @param {NS} ns **/
export async function main(ns) {
var min_buy = 0.5; // remove hysterys while switching between long-short
var bad_stock = -0.5;
var min_money = 1000 * 1000 * 100; // smallest txn at 100M
ns.print("Minimal txn amount = ", min_money);
// make sure we can access api before proceeding
while (true) {
if (ns.stock.purchase4SMarketDataTixApi()
&&
ns.stock.purchase4SMarketData()) {
break;
}
await ns.sleep(1000);
}
var symbols = ns.stock.getSymbols();
ns.print(symbols);
while (true) {
var maxshares = new Array(symbols.length);
for (var i = 0; i < symbols.length; i++)
maxshares[i] = ns.stock.getMaxShares(symbols[i]);
var fc = new Array(symbols.length);
var ibest = -1;
var position = new Array(symbols.length); // == long
var shorted = new Array(symbols.length);
var buying = true;
while (buying) {
buying = false;
for (var i = 0; i < symbols.length; i++) {
var pos = ns.stock.getPosition(symbols[i]);
position[i] = pos[0];
shorted[i] = pos[2];
fc[i] = (ns.stock.getForecast(symbols[i]) * 100.0) - 50.0;
}
// get stock forecast and get the best we dont have all shares from
for (var i = 0; i < symbols.length; i++) {
var f = fc[i];
if (f < bad_stock) {
if (shorted[i] > 0) ns.stock.sellShort(symbols[i], shorted[i]);
}
if (f > min_buy) {
if (ibest < 0 || f > fc[ibest]) {
if (position[i] < maxshares[i]) {
ibest = i;
}
}
}
}
// buy the best shares while we have money
if (ibest > 0) {
var money = ns.getPlayer().money;
if (money > min_money) { // dont buy for less then x unless there are less stock available
if (ibest >= 0) {
var max = maxshares[ibest];
var pos = position[ibest];
if (pos < max) {
var n_buy = max - pos;
var money_for_n = Math.floor(money / ns.stock.getAskPrice(symbols[ibest]));
if (money_for_n > 1) {
var n = Math.min(n_buy, money_for_n);
if(n > 0)
{
var i = 4;
while(ns.stock.buy(symbols[ibest], n) == 0 )
{
i--;
if(i == 0) break;
n = n - 1;
}
}
buying = true;
}
}
}
}
}
await (ns.sleep(100));
}
// lose all shares with a bad forecast
for (var i = 0; i < symbols.length; i++) {
if (fc[i] < 0 && position[i] > 0) {
ns.stock.sell(symbols[i], position[i]);
}
}
// reset positions
for (var i = 0; i < symbols.length; i++) {
var pos = ns.stock.getPosition(symbols[i]);
position[i] = pos[0];
shorted = pos[2];
}
// short every bad stock (might have some money now from selling or bought everything viable)
for (var i = 0; i < symbols.length; i++) {
var money = ns.getPlayer().money;
if (money < min_money) break;
var f = fc[i];
if (f < bad_stock) {
if (shorted[i] < maxshares[i]) {
var n_buy = maxshares[i] - shorted[i] - position[i];
var money_for_n = Math.floor(money / ns.stock.getAskPrice(symbols[i]));
var n = Math.min(n_buy, money_for_n) - 1;
if (n > 0) {
ns.stock.buy(symbols[i], n);
ns.stock.short(symbols[i], ns.stock.getPosition(symbols[i])[0]);
}
}
}
}
await ns.sleep(1000);
}
}