-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit_cancel.function.ts
More file actions
191 lines (169 loc) · 4.86 KB
/
Copy pathcommit_cancel.function.ts
File metadata and controls
191 lines (169 loc) · 4.86 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
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
import { memoize, sleep } from "functools-kit";
import Binance, { Order } from "node-binance-api";
import { roundTicks } from "../utils/roundTicks";
import FETCH_BALANCE_FN from "./fetch_balance.function";
import getCoinName from "../utils/getCoinName";
import COMMIT_SELL_FN from "./commit_sell.function";
import { percentValue } from "../utils/percentValue";
const TRADE_SELL_LOWER_PERCENT = 0.999;
const getTransactionFee = memoize(
([symbol]) => `${symbol}`,
async (_: string, binance: Binance) => {
const { makerCommission, takerCommission } = await binance.account();
const maker = makerCommission / 100;
const taker = takerCommission / 100;
return { maker, taker };
}
);
const getExchangeInfo = memoize(
([symbol, filterType]) => `${symbol}-${filterType}`,
async (symbol: string, filterType = "LOT_SIZE", binance: Binance) => {
const exchangeInfo = await binance.exchangeInfo();
const lotSizes = Object.values(exchangeInfo.symbols)
.map(({ symbol, filters }) => [
symbol,
filters.find((f: any) => f.filterType === filterType),
])
.reduce<any>((acm, [k, v]) => ({ ...acm, [k]: v }), {});
const { stepSize, tickSize, minQty } = lotSizes[symbol];
return {
stepSize,
tickSize,
minQty,
};
}
);
const formatQuantity = async (
symbol: string,
quantity: number,
binance: Binance
) => {
const { stepSize } = await getExchangeInfo(symbol, "LOT_SIZE", binance);
return roundTicks(quantity, stepSize);
};
const formatPrice = async (symbol: string, price: number, binance: Binance) => {
const { tickSize } = await getExchangeInfo(symbol, "PRICE_FILTER", binance);
return roundTicks(price, tickSize);
};
const getAveragePrice = (order: Order, fallbackPrice: number) => {
const fallback = { price: fallbackPrice };
const { fills = [fallback] } = order;
const price = fills.reduce((acm, { price }) => acm + Number(price), 0);
return price / fills.length;
};
interface IParams {
symbol: string;
averagePrice: number;
}
export const COMMIT_CANCEL_FN = async (
{ symbol, averagePrice }: IParams,
binance: Binance
) => {
const coinName = getCoinName(symbol);
{
let error;
for (let i = 0; i !== 10; i++) {
let isOk = true;
const orders = await binance.openOrders(symbol);
for (const order of orders) {
try {
await sleep(1_000);
await binance.cancel(symbol, order.orderId);
error = null;
} catch (e) {
isOk = false;
error = e;
continue;
}
}
if (!orders.length) {
error = null;
break;
}
if (isOk) {
break;
}
}
if (error) {
throw error;
}
}
{
let error;
for (let i = 0; i !== 10; i++) {
try {
await sleep(1_000);
const { length: hasOrders } = await binance.openOrders(symbol);
if (hasOrders) {
error = new Error("Order not canceled");
} else {
error = null;
break;
}
} catch (e) {
error = e;
}
}
if (error) {
throw error;
}
}
const balanceMap = await FETCH_BALANCE_FN(binance);
const balance = balanceMap[coinName];
if (!balance) {
throw new Error(`Can't fetch ballance (cancelation) for ${coinName}`);
}
const { minQty } = await getExchangeInfo(symbol, "LOT_SIZE", binance);
if (!minQty) {
throw new Error(
`Can't fetch minimal quantity (cancelation) for ${coinName}`
);
}
const { maker } = await getTransactionFee(symbol, binance);
const quantity =
balance.quantity - percentValue(balance.quantity, maker) - minQty;
if (quantity <= minQty) {
return 0;
}
const quantity$ = await formatQuantity(symbol, quantity, binance);
const averagePrice$ = await formatPrice(
symbol,
averagePrice * TRADE_SELL_LOWER_PERCENT,
binance
);
const order = await binance.order(
"LIMIT",
"SELL",
symbol,
Number(quantity$),
Number(averagePrice$)
);
const { orderId, status } = order;
if (status === "FILLED") {
return getAveragePrice(order, Number(averagePrice$));
} else {
let isNotClosed = true;
let lastStatus: Order = null;
for (let i = 0; i !== 10; i++) {
await sleep(10_000);
lastStatus = await binance.orderStatus(symbol, orderId);
if (lastStatus.status === "FILLED") {
isNotClosed = false;
break;
}
}
if (isNotClosed) {
await binance.cancel(symbol, orderId);
const orderQty = await formatQuantity(
symbol,
Number(quantity) - Number(lastStatus?.executedQty || 0),
binance
);
lastStatus = await binance.marketSell(symbol, Number(orderQty));
return getAveragePrice(lastStatus, Number(averagePrice$));
} else {
return getAveragePrice(lastStatus, Number(averagePrice$));
}
}
};
export default COMMIT_CANCEL_FN;