-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMACD.ts
More file actions
113 lines (91 loc) · 3.31 KB
/
MACD.ts
File metadata and controls
113 lines (91 loc) · 3.31 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
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 fastPeriod: number;
let slowPeriod: number;
let signalPeriod: number;
let periodNoTrade: number; // hours
exports.init = async () => {
fastPeriod = 34;
slowPeriod = 74;
signalPeriod = 40;
enabledStopLoss = true
stopLoss = -80;
enabledTakeProfit = false;
takeProfit = 100;
enabledTrailingStop = true;
trailingStart = 20;
trailingStop = 50;
periodNoTrade = 60;
}
Date.prototype.addMinutes = function (h) {
this.setTime(this.getTime() + (h * 60 * 1000));
return this;
}
exports.tick = async () => {
// 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 side = ''
let macdData = await context.indicators.macd(context.historicalData.close,
fastPeriod,
slowPeriod,
signalPeriod);
let macd: number = macdData[0][macdData[0].length - 1];
let signal: number = macdData[1][macdData[1].length - 1];
let history: number = macdData[2][macdData[2].length - 1];
let buy = macd >= signal;
let sell = macd < signal;
if (buy) {
side = "buy";
} else if (sell) {
side = "sell";
}
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) {
context.state.trailingPerformance = 0;
}
console.log(`MACD 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 () => {
};