-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit_trade.function.ts
More file actions
250 lines (215 loc) · 6.53 KB
/
Copy pathcommit_trade.function.ts
File metadata and controls
250 lines (215 loc) · 6.53 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { memoize, sleep, str } from "functools-kit";
import Binance, { OCOOrder } from "node-binance-api";
import { roundTicks } from "../utils/roundTicks";
import COMMIT_BUY_FN from "./commit_buy.function";
import FETCH_BALANCE_FN from "./fetch_balance.function";
import getCoinName from "../utils/getCoinName";
import { percentValue } from "../utils/percentValue";
const STOP_LOSS_LOWER_PERCENT = 0.999;
const TAKE_PROFIT_UPPER_PERCENT = 1.001;
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 getBalance = async (binance: Binance) => {
const account = await binance.account();
const usdtBalance = account.balances.find(
(balance) => balance.asset === "USDT"
);
if (!usdtBalance) {
return 0;
}
return parseFloat(usdtBalance.free);
};
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 orderOco = async (
dto: {
symbol: string;
quantity: number;
takeProfitPrice: number;
stopLossPrice: number;
},
binance: Binance
) => {
const params = {
// below order (stop loss)
belowType: "STOP_LOSS_LIMIT",
belowPrice: Number(
await formatPrice(
dto.symbol,
dto.stopLossPrice * STOP_LOSS_LOWER_PERCENT,
binance
)
),
belowStopPrice: Number(
await formatPrice(dto.symbol, dto.stopLossPrice, binance)
),
belowTimeInForce: "GTC",
// above order (take profit)
aboveType: "TAKE_PROFIT_LIMIT",
aboveStopPrice: Number(
await formatPrice(dto.symbol, dto.takeProfitPrice, binance)
),
abovePrice: Number(
await formatPrice(
dto.symbol,
dto.takeProfitPrice * TAKE_PROFIT_UPPER_PERCENT,
binance
)
),
aboveTimeInForce: "GTC",
};
const qty = Number(await formatQuantity(dto.symbol, dto.quantity, binance));
console.log("Sending OCO order...", { symbol: dto.symbol, params, qty });
return await binance.ocoOrder("SELL", dto.symbol, qty, params);
};
interface IParams {
amountUSDT: number;
averagePrice: number;
takeProfitPrice: number;
stopLossPrice: number;
symbol: string;
}
export const COMMIT_TRADE_FN = async (
{ amountUSDT, averagePrice, takeProfitPrice, stopLossPrice, symbol }: IParams,
binance: Binance
) => {
const { length: hasOrders } = await binance.openOrders(symbol);
if (hasOrders) {
return 0;
}
const balance = await getBalance(binance);
if (balance < amountUSDT) {
return 0;
}
const coinName = getCoinName(symbol);
const { minQty } = await getExchangeInfo(symbol, "LOT_SIZE", binance);
if (!minQty) {
throw new Error(
`Can't fetch minimal quantity (cancelation) for ${coinName}`
);
}
const ballanceBeforeMap = await FETCH_BALANCE_FN(binance);
const balanceBefore = ballanceBeforeMap[coinName];
if (!balanceBefore) {
throw new Error(`Can't fetch ballance (Before) for ${coinName}`);
}
const currentPrice = await COMMIT_BUY_FN(
{
amountUSDT,
averagePrice,
symbol,
},
binance
);
const balanceAfterMap = await FETCH_BALANCE_FN(binance);
const balanceAfter = balanceAfterMap[coinName];
if (!balanceAfter) {
throw new Error(`Can't fetch ballance (After) for ${coinName}`);
}
const { maker } = await getTransactionFee(symbol, binance);
let orderQuantity: number;
{
orderQuantity = balanceAfter.quantity - balanceBefore.quantity;
orderQuantity = orderQuantity - percentValue(orderQuantity, maker);
orderQuantity = orderQuantity - minQty;
}
let orderStatus: OCOOrder = null;
if (
!Number.isFinite(orderQuantity) ||
Number.isNaN(orderQuantity) ||
orderQuantity <= 0
) {
throw new Error(
`Invalid balance after buy orderQuantity=${orderQuantity} symbol=${symbol}`
);
}
// Создание OCO ордера через binance-api-node
await sleep(1_000);
console.log("Creating OCO order with params:", {
symbol,
quantity: orderQuantity,
takeProfitPrice,
stopLossPrice,
});
orderStatus = await orderOco(
{
symbol,
quantity: orderQuantity,
takeProfitPrice,
stopLossPrice,
},
binance
);
{
let error;
for (let i = 0; i !== 10; i++) {
try {
await sleep(1_000);
const orders = await binance.openOrders(symbol);
const { length: hasOrders } = orders;
if (!hasOrders) {
error = new Error(
"Order not created\n" + JSON.stringify(orderStatus, null, 2)
);
} else {
error = null;
break;
}
} catch (e) {
error = e;
}
}
if (error) {
throw error;
}
}
const date = new Date().toLocaleDateString();
const time = new Date().toLocaleTimeString();
const report = str.newline(
`Бот купил ${symbol} (в размере ${await formatQuantity(symbol, orderQuantity, binance)}) по цене ${await formatPrice(symbol, currentPrice, binance)} (${(orderQuantity * currentPrice).toFixed(2)}$)`,
`и выставил OCO ордер (take profit + stop loss):`,
`- Take Profit: ${await formatPrice(symbol, takeProfitPrice, binance)} (${(Number(orderQuantity) * Number(takeProfitPrice)).toFixed(2)}$)`,
`- Stop Loss: ${await formatPrice(symbol, stopLossPrice, binance)} (${(Number(orderQuantity) * Number(stopLossPrice)).toFixed(2)}$)`,
`Дата/время: ${date} ${time}`
);
return {
status: "ok",
content: report,
};
};
export default COMMIT_TRADE_FN;