-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit_buy.function.ts
More file actions
130 lines (114 loc) · 3.34 KB
/
Copy pathcommit_buy.function.ts
File metadata and controls
130 lines (114 loc) · 3.34 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
import { memoize, sleep } from "functools-kit";
import Binance, { Order } from "node-binance-api";
import { roundTicks } from "../utils/roundTicks";
import { usdToCoins } from "../utils/usdToCoins";
const TRADE_BUY_LOWER_PERCENT = 1.001;
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 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 {
amountUSDT: number;
averagePrice: number;
symbol: string;
}
export const COMMIT_BUY_FN = async (
{ amountUSDT, averagePrice, 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 orderPrice = await formatPrice(
symbol,
averagePrice * TRADE_BUY_LOWER_PERCENT,
binance
);
const quantity = await formatQuantity(
symbol,
usdToCoins(amountUSDT, averagePrice),
binance
);
const order = await binance.order(
"LIMIT",
"BUY",
symbol,
Number(quantity),
Number(orderPrice)
);
const { orderId, status } = order;
if (status === "FILLED") {
return getAveragePrice(order, Number(orderPrice));
} 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.marketBuy(symbol, Number(orderQty));
return getAveragePrice(lastStatus, Number(orderPrice));
} else {
return getAveragePrice(lastStatus, Number(orderPrice));
}
}
};
export default COMMIT_BUY_FN;