-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstrategy.js
171 lines (143 loc) · 4.11 KB
/
strategy.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
const Strategy = function Strategy() {};
const StratEnum = {
/*
Three Green Arrows strategy is a simple entry/exit strategy
used in scalping and similar fast trading systems
it enters a trade when RSI, MACD and moving average cross overs
occur, when all three are favourable.
*/
THREEGREEN: 'threeGreen',
/*
Simple cross over simple enters/exits a trade when a candle passes
over the moving average boundary.
Often used in trend following systems
*/
SIMPLECROSSOVER: 'simpleCrossOver',
/*
Simple trend following system based on two moving averages.
Go long when fast ma is above slow ma.
Go short when fast ma is below slow ma.
*/
DOUBLEMA: 'doubleMA',
DOUBLEMALONG: 'doubleMALong', // same as above but only takes longs
DOUBLEMASHORT: 'doubleMAShort', // same as above but only takes shorts
};
// CHANGE THIS VALUE TO CHANGE STRAT
const defaultstrat = StratEnum.DOUBLEMALONG;
// By default using LONG only because bitcoin is in a bubble :^)
const strats = [];
strats.threeGreen = [];
strats.threeGreen.enter = function threeGreenEnter(args) {
const {
close, sma1, macd, rsi,
} = args;
const rsiHigh = 60;
const rsiLow = 40;
if (sma1 > close && macd < 0 && rsi < rsiLow) {
return [true, 'SHORT'];
}
if (sma1 < close && macd > 0 && rsi > rsiHigh) {
return [true, 'LONG'];
}
return [false, ''];
};
strats.threeGreen.exit = function threeGreenExit(args) {
const { close, sma1, orderType } = args;
if (orderType === 'LONG') {
if (close < sma1) return true;
return false;
}
if (orderType === 'SHORT') {
if (close > sma1) return true;
return false;
}
return false;
};
strats.simpleCrossOver = [];
strats.simpleCrossOver.enter = function simpleCrossOverEnter(args) {
const { open, close, sma1 } = args;
// console.log(`open: ${open} close: ${close} sma20: ${sma20}`);
if (open > sma1 && close < sma1) return [true, 'SHORT'];
if (open < sma1 && close > sma1) return [true, 'LONG'];
return [false, ''];
};
strats.simpleCrossOver.exit = function simpleCrossOverExit(args) {
const {
open, close, sma1, orderType,
} = args;
if (orderType === 'LONG') {
if (open > sma1 && close < sma1) return true;
return false;
}
if (orderType === 'SHORT') {
if (open < sma1 && close > sma1) return true;
return false;
}
return false;
};
strats.doubleMA = [];
strats.doubleMA.enter = function doubleMAEnter(args) {
const { sma1, sma2 } = args;
if (sma1 > sma2) return [true, 'LONG'];
if (sma2 > sma1) return [true, 'SHORT'];
return [false, ''];
};
strats.doubleMA.exit = function doubleMAExit(args) {
const { sma1, sma2, orderType } = args;
if (orderType === 'LONG') {
if (sma2 > sma1) return true;
return false;
}
if (orderType === 'SHORT') {
if (sma1 > sma2) return true;
return false;
}
return false;
};
// long only
strats.doubleMALong = [];
strats.doubleMALong.enter = function doubleMALongEnter(args) {
const { sma1, sma2 } = args;
if (sma1 > sma2) return [true, 'LONG'];
// if (sma2 > sma1) return [true, 'SHORT'];
return [false, ''];
};
strats.doubleMALong.exit = function doubleMALongExit(args) {
const { sma1, sma2, orderType } = args;
if (orderType === 'LONG') {
if (sma2 > sma1) return true;
return false;
}
if (orderType === 'SHORT') {
if (sma1 > sma2) return true;
return false;
}
return false;
};
// short only
strats.doubleMAShort = [];
strats.doubleMAShort.enter = function doubleMAShortEnter(args) {
const { sma1, sma2 } = args;
// if (sma1 > sma2) return [true, 'LONG'];
if (sma2 > sma1) return [true, 'SHORT'];
return [false, ''];
};
strats.doubleMAShort.exit = function doubleMAShortExit(args) {
const { sma1, sma2, orderType } = args;
if (orderType === 'LONG') {
if (sma2 > sma1) return true;
return false;
}
if (orderType === 'SHORT') {
if (sma1 > sma2) return true;
return false;
}
return false;
};
Strategy.prototype.enter = function enter(args) {
return strats[defaultstrat].enter(args);
};
Strategy.prototype.exit = function threeGreenExit(args) {
return strats[defaultstrat].exit(args);
};
exports.Strategy = new Strategy();