-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRSI_Candle.js
220 lines (162 loc) · 4.65 KB
/
RSI_Candle.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Let's create our own strategy
//Buy the dip determined by both the RSI and a Hammer candle stick, and sell within an exposure time in 1 min chart
var log = require('../core/log');
var config = require ('../core/util.js').getConfig();
const CandleBatcher = require('../core/candleBatcher');
const RSI = require('../strategies/indicators/RSI.js');
//Settings input
var stop_loss;
var take_profit;
var exposure_time;
var rsi_length;
var rsi_low;
var rsi_high;
var time_frame;
//Candle variables
var candle_counter = 0;
var candle_custom;
//Strat variables
var candle_batcher;
var rsi_batcher;
var adviced = false;
var buy_price;
var exposure_count = 0;
//RSI INDICATOR
var rsi_result = null;
var real_rsi;
var strat = {};
// Prepare everything our strat needs
strat.init = function() {
this.requiredHistory = config.tradingAdvisor.historySize;
// since we're relying on batching 1 minute candles into 5 minute candles
// lets throw if the settings are wrong
if (config.tradingAdvisor.candleSize !== 1) {
throw "This strategy must run with candleSize=1";
}
//Settings input initialization
stop_loss=-this.settings.stop_loss.stop_loss;
take_profit=this.settings.take_profit.take_profit;
exposure_time=this.settings.exposure_time.exposure_time;
rsi_length= this.settings.rsi.rsi_length;
rsi_low= this.settings.rsi_low.rsi_low;
rsi_high=this.settings.rsi_high.rsi_high;
time_frame = this.settings.time_frame.time_frame;
/*
console.log(stop_loss);
console.log(take_profit);
console.log(exposure_time);
console.log(rsi_length);
console.log(rsi_low);
console.log(rsi_high);
console.log(time_frame);
*/
//Strat variables initialization
candle_batcher = new CandleBatcher(time_frame);
rsi_batcher = new CandleBatcher(time_frame);
candle_batcher.on('candle', this.update_candle); //Candle Batcher callback
rsi_batcher.on('candle',this.update_rsi); //RSI Batcher callback
//RSI INDICATOR
real_rsi = new RSI({ interval: rsi_length }); //Real RSI
this.addIndicator('dummyrsi', 'RSI', { interval: rsi_length}); //Dummy RSI
}
strat.isHammer = function ()
{
if(candle_custom.close <candle_custom.open && (candle_custom.close - candle_custom.low) > (candle_custom.open - candle_custom.close) && (candle_custom.high == candle_custom.open))
return true;
else
return false;
}
strat.buy_now = function (candle)
{
if(!adviced && rsi_result <= rsi_low && this.isHammer() )
return true;
else
return false;
}
strat.sell_now = function(candle)
{
var percent_increase = (candle.close - buy_price) / buy_price * 100;
//console.log(percent_increase);
if(adviced && ((percent_increase >= take_profit || percent_increase <= stop_loss) || exposure_count >= exposure_time))
return true;
else
return false;
}
strat.update_rsi = function(candle)
{
//console.log("callback");
real_rsi.update(candle);
rsi_result = real_rsi.result;
//console.log("=========");
//console.log(rsi_result);
}
strat.update_candle = function (candle)
{
//console.log("callback");
candle_custom = candle_batcher.calculatedCandles[0];
//console.log(this.candle_custom);
}
// What happens on every new candle?
strat.update = function(candle) {
candle_batcher.write([candle]);
rsi_batcher.write([candle]);
//console.log(this.candle_batcher);
candle_batcher.flush();
rsi_batcher.flush();
//console.log("============");
//console.log(this.candle_batcher);
//console.log(rsi_result);
if(adviced)
{
exposure_count++;
}
candle_counter++;
if(candle_counter == time_frame+1)
candle_counter = 1;
/*
if(candle_counter==time_frame)
{console.log("========");
console.log(candle_custom.start.format());
console.log("rsi result " + rsi_result);
console.log("open "+candle_custom.open);
console.log("close "+candle_custom.close);
console.log("max "+candle_custom.high);
console.log("min "+candle_custom.low);
console.log(this.isHammer());
}
*/
}
// Based on the newly calculated
// information, check if we should
// update or not.
strat.check = function(candle) {
if(strat.buy_now(candle) && candle_counter == time_frame)
{
this.advice('long');
adviced = true;
buy_price = candle.close;
console.log("========");
console.log(candle_custom);
console.log("rsi result " + rsi_result);
console.log("buy price " + buy_price);
}
if(strat.sell_now(candle))
{
this.advice('short');
adviced = false;
exposure_count = 0;
}
}
// For debugging purposes.
strat.log = function() {
// your code!
}
// Optional for executing code
// after completion of a backtest.
// This block will not execute in
// live use as a live gekko is
// never ending.
strat.end = function() {
// your code!
}
module.exports = strat;