-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsiStrategy.mjs
134 lines (115 loc) · 2.97 KB
/
rsiStrategy.mjs
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
import { z } from "zod";
import { buy, sell, cancelSmartTrade, useRSI } from "opentrader/effects";
/**
* Buy and Sell based on RSI indicator value.
* Inspired by https://github.com/askmike/gekko/blob/develop/strategies/RSI.js
*/
export default function* rsi(ctx) {
const {
config: { settings: params },
state,
} = ctx;
if (ctx.onStart) {
console.log(params, "[RSI] Bot started with params");
return;
}
if (ctx.onStop) {
console.log("[RSI] Bot stopped");
yield cancelSmartTrade();
return;
}
if (!state.trend) {
state.trend = {
direction: "none",
duration: 0,
persisted: false,
adviced: false,
};
console.log(state, "[RSI] State initialized");
}
const rsi = yield useRSI(params.periods);
console.log(`[RSI] RSI value: ${rsi}`);
if (rsi > params.high) {
// new trend detected
if (state.trend.direction !== "high") {
state.trend = {
duration: 0,
persisted: false,
direction: "high",
adviced: false,
};
}
state.trend.duration++;
console.log(`[RSI] In high since ${state.trend.duration} candle(s)`);
if (state.trend.duration >= params.persistence) {
state.trend.persisted = true;
}
if (state.trend.persisted && !state.trend.adviced) {
state.trend.adviced = true;
console.log("[RSI] Advised to SELL");
yield sell({
quantity: params.quantity,
orderType: "Market",
});
}
} else if (rsi < params.low) {
// new trend detected
if (state.trend.direction !== "low") {
state.trend = {
duration: 0,
persisted: false,
direction: "low",
adviced: false,
};
}
state.trend.duration++;
console.log(`[RSI] In low since ${state.trend.duration} candle(s)`);
if (state.trend.duration >= params.persistence) {
state.trend.persisted = true;
}
if (state.trend.persisted && !state.trend.adviced) {
state.trend.adviced = true;
console.log("[RSI] Advised to BUY");
yield buy({
quantity: params.quantity,
orderType: "Market",
});
}
} else {
console.log("[RSI] In no trend");
}
}
rsi.displayName = "RSI Strategy";
rsi.schema = z.object({
high: z
.number()
.min(0)
.max(100)
.default(70)
.describe("Sell when RSI is above this value"),
low: z
.number()
.min(0)
.max(100)
.default(30)
.describe("Buy when RSI is below this value"),
periods: z.number().positive().default(14).describe("RSI period"),
persistence: z
.number()
.positive()
.default(1)
.describe("Number of candles to persist in trend before buying/selling"),
quantity: z
.number()
.positive()
.default(0.0001)
.describe("Quantity to buy/sell"),
});
rsi.requiredHistory = ({ settings }) => settings.periods + 1;
rsi.timeframe = ({ timeframe }) => timeframe;
rsi.runPolicy = {
onCandleClosed: true,
};
rsi.watchers = {
watchCandles: ({ symbol }) => symbol,
};