-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.ts
51 lines (40 loc) · 1.46 KB
/
index.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
import { PluginInterface } from '@debut/types';
import { orders } from '@debut/plugin-utils';
export type OrderExpireOptions = {
orderCandlesLimit: number;
closeAtZero?: boolean;
};
type LimitLookup = Record<string, number>;
export function orderExpirePlugin(opts: OrderExpireOptions): PluginInterface {
const lookup: LimitLookup = {};
const halfLimit = opts.orderCandlesLimit / 2;
let amount: number;
return {
name: 'order-expire',
async onOpen(order) {
amount = this.debut.opts.amount * (this.debut.opts.equityLevel || 1);
lookup[order.cid] = 0;
},
async onClose(order, closing) {
delete lookup[closing.cid];
},
async onCandle({ c }) {
for (const order of [...this.debut.orders]) {
if (!('orderId' in order)) {
return;
}
const counter = ++lookup[order.cid];
if (counter >= opts.orderCandlesLimit) {
await this.debut.closeOrder(order);
}
if (opts.closeAtZero && counter > halfLimit) {
const profit = orders.getCurrencyProfit(order, c);
const percentProfit = (profit / amount) * 100;
if (percentProfit >= (this.debut.opts.fee || 0)) {
await this.debut.closeOrder(order);
}
}
}
},
};
}