-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFIBO-strategy.js
More file actions
237 lines (194 loc) · 6.88 KB
/
Copy pathFIBO-strategy.js
File metadata and controls
237 lines (194 loc) · 6.88 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// This version of FIB retracement only uses the FIB MAX as the sell point.
// The buy part is handled by a roc / adx review.
//this contains a ruleset for 60second candles.
var config = require('../core/util.js').getConfig();
var settings = config['FIBO'];
//console.log(settings);
var log = require('../core/log');
var fsw = require('fs');
var grtimestamp = '';
var grreadtime = '0';
var outtxt = '0';
var headerset = '';
var headertxt = '';
var buysell = "";
var buytime = "";
var selltime ="";
var csize = config.tradingAdvisor.candleSize;
var exchange = config.watch.exchange;
var currency = config.watch.currency;
var asset = config.watch.asset;
var strat = config.tradingAdvisor.method;
var filetime = new Date().getTime();
var fname = strat+"-"+csize+"-"+exchange+"-"+currency+"-"+asset+"-"+filetime+".csv";
config.suckmyballs = "yeah";
// Let's create our own strat
var strat = {};
var config = require('../core/util.js').getConfig();
var settings = config['FIBO'];
//console.log(settings);
// Prepare everything our method needs
strat.init = function () {
this.name = 'FIBO';
this.historylength =100;
this.historyadx =[];
this.historyplusdmi =[];
this.historyminusdmi =[];
this.historyema20 =[];
this.historyema50 =[];
this.historyema100 =[];
this.historyema200 =[];
this.counter =0;
this.plusgapupcounter=0;
this.minusgapupcounter=0;
this.plusgapdowncounter=0;
this.minusgapdowncounter=0;
this.negativeroc = 0;
this.positiveroc = 0;
// this.requiredHistory = settings.histSize;
this.requiredHistory = config.tradingAdvisor.historySize;;
this.addIndicator('fibo', 'FIBO', settings);
this.addIndicator('SMA', 'SMA', 10);
this.addTalibIndicator('myadx', 'adx', settings);
this.addTalibIndicator('myroc', 'roc', settings);
this.addTalibIndicator('myplusdi', 'plus_di', settings);
this.addTalibIndicator('myplusdm', 'plus_dm', settings);
this.addTalibIndicator('myminusdi', 'minus_di', settings);
this.addTalibIndicator('myminusdm', 'minus_dm', settings);
this.addTalibIndicator('myroc', 'roc', settings);
this.addIndicator('EMA20', 'EMA', 20);
this.addIndicator('EMA50', 'EMA', 50);
this.addIndicator('EMA100', 'EMA', 100);
this.addIndicator('EMA200', 'EMA', 200);
this.trend = "";
this.direction = "";
this.stop = "";
this.num = 0;
this.candle = null;
};
// What happens on every new candle?
strat.update = function (candle) {
var adxresult = this.talibIndicators.myadx.result["outReal"];
var rocresult = this.talibIndicators.myroc.result["outReal"];
var price = candle.close;
this.candle = candle;
var historyplusgap ="";
var historyminusgap ="";
var myplusdiresult = this.talibIndicators.myplusdi.result["outReal"];
var myplusdmresult = this.talibIndicators.myplusdm.result["outReal"];
var myminusdiresult = this.talibIndicators.myminusdi.result["outReal"];
var myminusdmresult = this.talibIndicators.myminusdm.result["outReal"];
var plusgap = 0;
var minusgap = 0;
var buysell = false;
var upordown = "";
var emagap="";
var emapercentage="";
if(myplusdiresult>myminusdiresult){
plusgap=myplusdiresult- myminusdiresult;
minusgap=0;
}
if(myplusdiresult<myminusdiresult){
minusgap =myminusdiresult - myplusdiresult ;
plusgap =0;
}
var ema20 = this.indicators.EMA20.result;
var ema50 = this.indicators.EMA50.result;
var ema100 = this.indicators.EMA100.result;
var ema200 = this.indicators.EMA200.result;
this.historyadx.push(adxresult);
this.historyplusdmi.push(myplusdiresult);
this.historyminusdmi.push(myminusdiresult);
this.historyema20.push(ema20);
this.historyema50.push(ema50);
this.historyema100.push(ema100);
this.historyema200.push(ema200);
if(this.historyadx.length>this.historylength) {
this.historyadx.shift();
this.historyplusdmi.shift();
this.historyminusdmi.shift();
this.historyema20.shift();
this.historyema50.shift();
this.historyema100.shift();
this.historyema200.shift();
}
if(myplusdiresult > this.historyplusdmi[this.historyplusdmi.length-2]){
this.plusgapupcounter=this.plusgapupcounter+1;
this.plusgapdowncounter = 0;
}else{
this.plusgapdowncounter=this.plusgapdowncounter+1;
this.plusgapupcounter = 0;
}
if(myminusdiresult > this.historyminusdmi[this.historyminusdmi.length-2]){
this.minusgapupcounter=this.minusgapupcounter+1;
this.minusgapdowncounter = 0;
}else{
this.minusgapdowncounter=this.minusgapdowncounter+1;
this.minusgapupcounter = 0;
}
if(ema200>ema20){
emagap = ema200-ema20;
emapercentage = emagap/ema200;
}else if(ema200<ema20){
emagap = ema20-ema200;
emapercentage = emagap/ema20;
}
//if(price>ema20 && price>ema50 && price>ema100 && price>ema200 && ema20<ema200 && emapercentage> .025 || this.stop != "" && price<this.stop && this.trend == "long"){
//with stoploss105k usd
if(price>ema20 && price>ema50 && price>ema100 && price>ema200 && ema20<ema200 && emapercentage> .025){
if(this.stop != "" && price<this.stop){
console.log("stoplosss triggered - "+ this.stop);
}
this.direction="short";
if(this.trend == "long"){
buysell = "selling asset";
this.stop = "";
selltime = price;
this.trend = this.direction;
this.advice(this.direction);
}
}else if(price<ema20 && price<ema50 && price<ema200 && price<ema100 && emapercentage> .02 && myminusdiresult >30 && rocresult< -1.5 && adxresult>60 && this.direction!="long"){
buytime = price;
if(this.stop==""){
this.stop = buytime-(buytime*.2);
console.log("price = "+price);
console.log("stoploss = "+this.stop);
this.direction="long";
buysell = "buying asset";
this.trend = this.direction;
this.advice(this.direction);
}
}
grreadtime = candle.start.toDate();
headertxt = "date,price,roc,adx,buys (USD),sells (BTC),ema20, ema50, ema100, ema200,emapercentage,myminusdiresult,myplusdiresult,minusgapupcounter,plusgapupcounter,minusgapdowncounter,plusgapdowncounter,plusgap,minusgap\n";
outtxt = grreadtime+","+ price+","+rocresult+","+adxresult+","+buytime+","+selltime+","+ema20+","+ema50+","+ema100+","+ema200+","+ emapercentage+","+myminusdiresult+","+myplusdiresult+","+this.minusgapupcounter+","+this.plusgapupcounter+","+this.minusgapdowncounter+","+this.plusgapdowncounter+","+plusgap+","+minusgap+"\n";
if (headerset === "") {
fsw.appendFileSync(fname, headertxt, encoding = 'utf8');
headerset = "1";
}
// console.log(this.fname);
fsw.appendFileSync(fname, outtxt, encoding = 'utf8');
outtxt = "";
buysell = "";
buytime = "";
selltime = "";
if(rocresult<0 ){
this.negativeroc = this.negativeroc+1;
this.positiveroc = 0;
}else if(rocresult>0){
this.negativeroc = 0;
this.positiveroc = this.positiveroc+1;
}else if(rocresult == 0){
this.negativeroc = 0;
this.positiveroc = 0;
}
};
// For debugging purposes.
strat.log = function () {
};
// Based on the newly calculated
// information, check if we should
// update or not.
strat.check = function () {
};
module.exports = strat;