-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathbot.ts
211 lines (182 loc) · 6.86 KB
/
bot.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { ATR, CCI, EMA, LWMA, SMA, UniLevel, WEMA } from '@debut/indicators';
import { SessionPluginOptions, sessionPlugin } from '@debut/plugin-session';
import { OrderExpireOptions, orderExpirePlugin } from '@debut/plugin-order-expire';
import { reinvestPlugin } from '@debut/plugin-reinvest';
import { ReportPluginAPI, IndicatorsSchema } from '@debut/plugin-report';
import { statsPlugin, StatsPluginAPI } from '@debut/plugin-stats';
import { ShutdownPluginAPI } from '@debut/plugin-genetic-shutdown';
import { Debut } from '@debut/community-core';
import { DebutOptions, BaseTransport, OrderType, Candle } from '@debut/types';
import { orders } from '@debut/plugin-utils';
import { VirtualTakesOptions, virtualTakesPlugin, VirtualTakesPluginAPI } from '@debut/plugin-virtual-takes';
export interface CCIDynamicBotOptions
extends SessionPluginOptions,
VirtualTakesOptions,
OrderExpireOptions,
DebutOptions {
atrPeriod: number;
cciPeriod: number;
stopTakeRatio: number;
levelPeriod: number;
levelRedunant: number;
levelSampleCount: number; // 1 - 3
levelSampleType: number; // 1 - 3
levelMultiplier: number;
levelOffset: number;
zeroClose: boolean;
atrMultiplier: number;
cciAtr: boolean;
reinvest?: boolean;
signalFilter?: boolean;
strictLevel?: boolean;
}
export class CCIDynamic extends Debut {
declare opts: CCIDynamicBotOptions;
declare plugins: StatsPluginAPI & ReportPluginAPI & ShutdownPluginAPI & VirtualTakesPluginAPI;
private cci: CCI;
private levels: UniLevel<typeof SMA | typeof EMA | typeof WEMA | typeof LWMA>;
private atr: ATR;
private cciValue: number;
private atrValue: number;
private upperLevel: number;
private lowerLevel: number;
private cciValues: number[] = [];
private insideNormal = false;
private barsFromLastSignal = 0;
constructor(transport: BaseTransport, opts: CCIDynamicBotOptions) {
super(transport, opts);
this.registerPlugins([
this.opts.from && this.opts.to && sessionPlugin(this.opts),
this.opts.reinvest ? reinvestPlugin() : null,
virtualTakesPlugin(this.opts),
statsPlugin(this.opts),
orderExpirePlugin(this.opts),
]);
this.atr = new ATR(this.opts.atrPeriod);
this.levels = new UniLevel(
this.opts.levelRedunant,
this.getSamplerType(this.opts.levelSampleType),
this.opts.levelSampleCount,
this.opts.levelMultiplier,
this.opts.levelOffset,
);
this.cci = new CCI(this.opts.cciPeriod);
this.levels.create(this.opts.levelPeriod);
}
public getSamplerType(levelType: number) {
switch (levelType) {
case 1:
return WEMA;
case 2:
return SMA;
case 3:
return EMA;
case 4:
return LWMA;
}
}
public getIndicators = (): IndicatorsSchema => {
return [
{
name: 'cci',
figures: [
{
name: 'cci',
getValue: () => {
return this.cciValue;
},
},
{
name: 'overbought',
getValue: () => {
return this.upperLevel;
},
},
{
name: 'oversold',
getValue: () => {
return this.lowerLevel;
},
},
],
levels: [],
},
];
};
async openMonitoring(c: number) {
const first = this.cciValues[2];
const second = this.cciValues[1];
let target: OrderType;
const overbought = (!this.opts.strictLevel || second < this.upperLevel) && first >= this.upperLevel;
const oversold = (!this.opts.signalFilter || second > this.lowerLevel) && first <= this.lowerLevel;
const currentOrder = this.orders[0];
const isZeroLevel = first * second < 0;
if (overbought) {
target = OrderType.SELL;
} else if (oversold) {
target = OrderType.BUY;
}
// DOGE loogs good with 3
if (this.opts.signalFilter && target && this.barsFromLastSignal < 5) {
return;
}
if (currentOrder && ((target && currentOrder.type !== target) || (this.opts.closeAtZero && isZeroLevel))) {
await this.closeAll();
}
if (target && !this.ordersCount) {
await this.placeOrder(c, target);
}
}
async onCandle({ h, l, c }: Candle) {
this.atrValue = this.atr.nextValue(h, l, c);
this.cciValue = this.cci.nextValue(h, l, c);
if (this.cciValue === undefined) {
return;
}
if (this.opts.cciAtr) {
this.cciValue = this.cciValue / this.atrValue;
}
this.barsFromLastSignal++;
if (this.cciValues.length === 3) {
this.cciValues.shift();
}
this.cciValues.push(this.cciValue);
const first = this.cciValues[0];
const second = this.cciValues[1];
const third = this.cciValues[2];
[this.upperLevel, this.lowerLevel] = this.levels.nextValue(this.cciValue);
if (this.ordersCount > 0 && second * third < 0) {
const profit = orders.getCurrencyProfit(this.orders[0], c);
if (profit / (this.opts.amount * this.opts.equityLevel) > 0.002) {
await this.closeAll();
}
}
this.insideNormal =
this.insideNormal ||
(first < this.lowerLevel && second > this.lowerLevel) ||
(first > this.upperLevel && second < this.upperLevel) ||
(first > 0 && second < 0) ||
(first < 0 && second > 0);
if (!this.upperLevel || !this.lowerLevel || !this.insideNormal || !this.atrValue) {
return;
}
await this.openMonitoring(c);
}
private async placeOrder(c: number, target: OrderType) {
const order = await this.createOrder(target);
let take = 0;
let stop = 0;
if (this.opts.manual) {
if (target === OrderType.BUY) {
take = c + this.atrValue * this.opts.atrMultiplier;
stop = c - this.atrValue * (this.opts.atrMultiplier / this.opts.stopTakeRatio);
} else {
take = c - this.atrValue * this.opts.atrMultiplier;
stop = c + this.atrValue * (this.opts.atrMultiplier / this.opts.stopTakeRatio);
}
this.plugins.takes.setPricesForOrder(order.cid, take, stop);
}
this.insideNormal = false;
return order;
}
}