-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRenko+EmaCross.ts
More file actions
179 lines (150 loc) · 5.38 KB
/
Renko+EmaCross.ts
File metadata and controls
179 lines (150 loc) · 5.38 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
let enabledStopLoss: boolean;
let stopLoss: number; // percentage
let enabledTakeProfit: boolean;
let takeProfit: number; // percentage
let enabledTrailingStop: boolean;
let trailingStop: number; // percentage
let trailingStart: number; // percentage
let renkoDiff: number; // price
let periodNoTrade: number; // hours
let crossLine: number;
let useEmaCross: number;
exports.init = async () => {
enabledStopLoss = false
stopLoss = -10;
enabledTakeProfit = false;
takeProfit = 50;
enabledTrailingStop = false;
trailingStart = 60;
trailingStop = 30;
renkoDiff = 5;
periodNoTrade = 1;
useEmaCross = false;
crossLine = 0;
}
Date.prototype.addMinutes = function (h) {
this.setTime(this.getTime() + (h * 60 * 1000));
return this;
}
exports.tick = async () => {
let emaFast = await context.indicators.ema(context.historicalData.close, 5);
let emaLong = await context.indicators.ema(context.historicalData.close, 68);
let emaF: number = emaFast[0][emaFast[0].length - 1];
let emaL: number = emaLong[0][emaLong[0].length - 1];
let buy = emaF > emaL;
let cross: number = Math.abs(100 * (emaF - emaL) / ((emaF + emaL) / 2));
//console.log(`cross: ${cross}`);
// trailing stop
if (enabledTrailingStop && context.position && context.state.trailingPerformance > trailingStart && context.position.performance != 0) {
let diff = context.position.performance - context.state.trailingPerformance / context.state.trailingPerformance * 100;
if (diff < -trailingStop) {
console.log(`Trailing Stop: ${context.position.performance}%`);
return closePosition();
}
}
// take profit
if (enabledTakeProfit && context.position && context.position.performance > takeProfit) {
console.log(`Take Profit: ${context.position.performance}%`);
return closePosition();
}
// stop loss
if (enabledStopLoss && context.position && context.position.performance < stopLoss) {
console.log(`Stop Loss: ${context.position.performance}%`);
return closePosition();
}
if (context.position) {
if (context.position.performance > (context.state.trailingPerformance || 0)) {
context.state.trailingPerformance = context.position.performance;
}
}
else {
context.state.trailingPerformance = 0;
}
let items = [];
for (let i = 0; i < context.historicalData.close.length; i++) {
items.push([
context.historicalData.timestamp[i],
context.historicalData.close[i]
])
}
items.push([
new Date().getTime(),
context.price
])
let result = linearDataToRenko(items, renkoDiff);
let side = ''
if (emaF > emaL) {
side = result[result.length - 1].side == 'buy' &&
(!crossLine || !!crossLine && cross > crossLine) &&
(!useEmaCross || useEmaCross && buy) ? 'buy' : 'close';
}
else {
side = result[result.length - 1].side == 'sell' &&
(!crossLine || !!crossLine && cross < -crossLine) &&
(!useEmaCross || useEmaCross && !buy) ? 'sell' : 'close';
}
if (result.length) {
if (side != context.state.side &&
(!context.state.closeDate || context.state.closeDate < new Date(context.timestamp).getTime())) {
context.state.lastOpen = context.historicalData.open[context.historicalData.open.length - 1];
context.state.side = side;
if (context.position && side) {
return closePosition();
}
else {
context.state.trailingPerformance = 0;
}
console.log(`Heikin Open ${context.state.side}`);
return context.state.side;
}
}
return "";
}
let closePosition = () => {
console.log(`Close: ${context.position.performance}%`);
context.state.side = "";
context.state.trailingPerformance = 0;
context.state.closeDate = new Date(context.timestamp).addMinutes(periodNoTrade).getTime();
return 'close';
}
exports.end = async () => {
}
function linearDataToRenko(data, change) {
var renkoData = [],
prevPrice = data[0][1],
prevTrend = 0, // 0 - no trend, 1 - uptrend, 2 - downtrend
length = data.length,
i = 1;
for (; i < length; i++) {
if (data[i][1] - data[i - 1][1] > change) {
// Up trend
if (prevTrend === 2) {
prevPrice += change;
}
renkoData.push({
x: data[i][0],
y: prevPrice,
low: prevPrice,
high: prevPrice + change,
side: 'buy'
});
prevPrice += change;
prevTrend = 1;
} else if (Math.abs(data[i][1] - data[i - 1][1]) > change) {
if (prevTrend === 1) {
prevPrice -= change;
}
// Down trend
renkoData.push({
x: data[i][0],
y: prevPrice,
low: prevPrice - change,
high: prevPrice,
side: 'sell'
});
prevPrice -= change;
prevTrend = 2;
}
}
return renkoData;
}