forked from Haehnchen/crypto-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpivot_reversal_strategy.js
139 lines (113 loc) · 3.96 KB
/
pivot_reversal_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
'use strict';
let SignalResult = require('../dict/signal_result')
module.exports = class PivotReversalStrategy {
getName() {
return 'pivot_reversal_strategy'
}
buildIndicator(indicatorBuilder, options) {
indicatorBuilder.add('candles_1m', 'candles', '1m')
indicatorBuilder.add('pivot_points', 'pivot_points_high_low', '15m', {
'left': options['left'] || 4,
'right': options['right'] || 2,
})
indicatorBuilder.add('sma200', 'sma', '1h', {
'length': 200,
})
indicatorBuilder.add('sma50', 'sma', '1h', {
'length': 50,
})
}
async period(indicatorPeriod) {
let debug
let currentValues = debug = indicatorPeriod.getLatestIndicators()
if (!currentValues['sma200'] || currentValues['sma200'].length < 10) {
return
}
let candles1m = indicatorPeriod.getIndicator('candles_1m')
if (candles1m){
debug.candles = candles1m.slice(-3).map(c => c.close).join(', ')
}
// close; use watchdog!
let lastSignal = indicatorPeriod.getLastSignal();
if (lastSignal) {
return SignalResult.createEmptySignal(debug)
if (lastSignal === 'long' && !this.getPivotSignal(false, indicatorPeriod)) {
return SignalResult.createSignal('close', debug)
}
if (lastSignal === 'short' && !this.getPivotSignal(true, indicatorPeriod)) {
return SignalResult.createSignal('close', debug)
}
return SignalResult.createEmptySignal(debug)
}
let long = indicatorPeriod.getPrice() > currentValues['sma200']
let signal = this.getPivotSignal(long, indicatorPeriod)
if (signal) {
return SignalResult.createSignal(signal, debug)
}
return SignalResult.createEmptySignal(debug)
}
getPivotSignal(long, indicatorPeriod) {
for (let value of indicatorPeriod.visitLatestIndicators(3)) {
if (!long && value.pivot_points && value.pivot_points.low && value.pivot_points.low.low) {
let candles1m = indicatorPeriod.getIndicator('candles_1m')
let mins = candles1m.slice(-7)
let closes = mins.map(c => c.close).reduce((acc, val) => { return acc + val; }, 0) / mins.length
if (closes < value.pivot_points.low.low) {
return 'short'
}
break;
}
if (long && value.pivot_points && value.pivot_points.high && value.pivot_points.high.high) {
let candles1m = indicatorPeriod.getIndicator('candles_1m')
let mins = candles1m.slice(-7)
let closes = mins.map(c => c.close).reduce((acc, val) => { return acc + val; }, 0) / mins.length
if (closes > value.pivot_points.high.high) {
return 'long'
}
break;
}
}
return
}
getBacktestColumns() {
return [
{
'label': 'SMA 50/200',
'value': 'sma50',
'type': 'cross',
'cross': 'sma200',
},
{
'label': 'sma200',
'value': 'sma200',
},
{
'label': 'sma50',
'value': 'sma50',
},
{
'label': 'Pivot Points',
'value': 'pivot_points',
},
{
'label': 'candles_1m',
'value': 'candles_1m.close',
},
{
'label': 'candles',
'value': 'candles',
},
{
'label': 'debug',
'value': 'debug',
},
]
}
getOptions() {
return {
'period': '15m',
'left': 4,
'right': 2,
}
}
}