-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathrsi.ts
128 lines (107 loc) Β· 3.2 KB
/
rsi.ts
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
import { z } from "zod";
import { buy, cancelSmartTrade, IBotConfiguration, sell, TBotContext, useRSI } from "@opentrader/bot-processor";
import { logger } from "@opentrader/logger";
/**
* Inspired by https://github.com/askmike/gekko/blob/develop/strategies/RSI.js
* @param ctx - Bot context
*/
export function* rsi(ctx: TBotContext<RsiParams, RsiState>) {
const {
config: { settings: params },
state,
} = ctx;
if (ctx.onStart) {
logger.info(params, "[RSI] Bot started with params");
return;
}
if (ctx.onStop) {
logger.info("[RSI] Bot stopped");
yield cancelSmartTrade();
return;
}
if (!state.trend) {
state.trend = {
direction: "none",
duration: 0,
persisted: false,
adviced: false,
};
logger.info(state, "[RSI] State initialized");
}
const rsi: number = yield useRSI(params.periods);
logger.info(`[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++;
logger.info(`[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;
logger.info("[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++;
logger.info(`[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;
logger.info("[RSI] Advised to BUY");
yield buy({
quantity: params.quantity,
orderType: "Market",
});
}
} else {
logger.info("[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 = 15;
rsi.timeframe = ({ timeframe }: IBotConfiguration) => timeframe;
rsi.runPolicy = {
onCandleClosed: true,
};
rsi.watchers = {
watchCandles: ({ symbol }: IBotConfiguration) => symbol,
};
type RsiState = {
trend?: {
direction: "high" | "low" | "none";
duration: number;
persisted: boolean;
adviced: boolean;
};
};
export type RsiParams = IBotConfiguration<z.infer<typeof rsi.schema>>;