forked from yura505/robinbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
196 lines (168 loc) · 6.67 KB
/
actions.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var n = require('numbro');
var orders = require('./orders.js');
var positions = require('./positions.js');
var account = require('./account.js');
var quotes = require('./quotes.js');
var rsa = require('./rsa.js');
var conf = require('./conf.js');
var markets = require('./markets.js');
var actions = module.exports = {
create: function(symbol, signal, ta, count, price) {
return {
symbol: symbol,
signal: signal,
ta: ta,
count: count,
price: price
}
},
align: function() {
_actions.forEach(function(action) {
// reset action.signal if needed
if (((action.signal == "BUY") && (orders.isSoldToday(action.symbol) || markets.breadth !== "BUY")) ||
((action.signal == "SELL") && (positions.exists(action.symbol) === undefined)))
action.signal = null;
});
},
add: function(action) {
_actions.push(action);
},
get buy() {
return _actions.filter(action => action.signal == 'BUY');
},
get sell() {
return _actions.filter(action => action.signal == 'SELL');
},
get hold() {
return _actions.filter(action => action.signal == 'HOLD');
},
get stop() {
return _actions.filter(action => action.signal == 'STOP');
},
get keep() {
return _actions.filter(action => action.signal == 'KEEP');
},
sum: function(actions) {
return (actions.length > 0) ? actions.reduce(function(total, action) {
return total + ((action.count !== undefined && action.price !== undefined) ?
action.count * action.price : 0);
}, 0) : 0;
},
distribute_cash: function() {
let cash = account.cash;
console.log("CASH="+cash);
let portfolio_value = this.asset_value + cash;
console.log("PORTFOLIO VALUE="+portfolio_value);
this.buy.forEach(function(action) {
let price = quotes.get(action.symbol)[0].close;
if (portfolio_value * conf.POSITION_SIZING > price) {
action.price = price * 1.005;
action.count = 0;
} else {
action.signal = null;
}
});
this.sell.forEach(function(action) {
let price = quotes.get(action.symbol)[0].close;
let count = positions.quantity(action.symbol);
action.price = price * 0.995;
action.count = count;
cash += count * price;
});
this.hold.forEach(function(action) {
let count = positions.quantity(action.symbol);
action.count = count;
});
if (this.buy.length > Math.trunc( 1 / conf.POSITION_SIZING)) {
this.buy.sort(function(a, b) {
return rsa.get(b.symbol) - rsa.get(a.symbol);
}).slice(Math.trunc( 1 / conf.POSITION_SIZING)).forEach(function(action) {
action.signal = null;
});
}
do {
var min_action = (function(){
var ret_action;
var min_allocated;
actions.buy.forEach(function(action) {
let allocated = (positions.quantity(action.symbol) + action.count + 1) * action.price;
if (((!min_allocated) || (allocated < min_allocated)) && (allocated < portfolio_value * conf.POSITION_SIZING) && (!action.full)) {
min_allocated = allocated;
ret_action = action;
}
});
return ret_action;
})();
if (min_action) {
if (min_action.price < cash) {
min_action.count++;
cash -= min_action.price;
} else {
min_action.full = true;
}
}
} while (min_action);
console.log("SUM FROM SALE="+this.sum(this.sell));
console.log("SUM TO BUY="+this.sum(this.buy));
},
get asset_value() {
return positions.sum() - this.sum(this.sell) + this.sum(this.buy);
},
allocate_stops: function() {
var stop_list = { };
[...this.buy, ...this.hold].forEach(function(action) {
if (action.symbol in stop_list) {
stop_list[action.symbol].count += action.count;
} else {
stop_list[action.symbol] = { count: action.count, ta: action.ta };
}
});
for (let symbol in stop_list) {
this.stopLoss(symbol, stop_list[symbol].count, stop_list[symbol].ta);
}
},
stopLoss: function(symbol, count, ta) {
let stop_loss;
if (conf.trailing_stop == "max") {
let position_ratio = (quotes.get(symbol)[0].close * count) / this.asset_value;
let max_possible_loss = this.asset_value * conf.MAX_LOSS_THRESHOLD * position_ratio / conf.POSITION_SIZING;
let max_loss = quotes.get(symbol)[0].close - max_possible_loss / count;
stop_loss = max_loss;
} else if (conf.trailing_stop == "atr") {
let atr_loss = quotes.get(symbol)[0].close - 3 * ta.atr; // TODO: make 3 configurable
stop_loss = atr_loss;
} else if (conf.trailing_stop == "sar") {
let sar_loss = (ta.sar !== undefined && ta.sar.bull) ? ta.sar.psar : null;
stop_loss = sar_loss;
}
if (stop_loss) {
let stop_order = orders.getStopOrder(symbol);
if (stop_order) {
let stop_price = n(stop_order.stop_price).value();
let quantity = n(stop_order.quantity).value();
if (stop_price > stop_loss) stop_loss = stop_price;
if ((quantity == count) && (stop_loss == stop_price)) {
actions.add(actions.create(symbol, "KEEP", ta, count, stop_price));
orders.keepStopOrder(stop_order);
return;
}
}
if (stop_loss < quotes.get(symbol)[0].close) {
var action = this.create(symbol, "STOP", ta, count, stop_loss);
this.add(action);
}
}
},
get stop_risk() {
let today_sum =
[...this.keep, ...this.stop].reduce(function(total, action) {
return total + quotes.get(action.symbol)[0].close * action.count;
}, 0);
let stop_sum = this.sum(this.stop) + this.sum(this.keep);
return today_sum - stop_sum;
},
/* backtest methods */
get all() { return _actions },
clear: function() { _actions = [ ] },
}
var _actions = [ ];