-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpaper.module.ts
More file actions
79 lines (76 loc) · 2.27 KB
/
Copy pathpaper.module.ts
File metadata and controls
79 lines (76 loc) · 2.27 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
import { addExchangeSchema, addFrameSchema, roundTicks, setConfig } from "backtest-kit";
import { singleshot } from "functools-kit";
import ccxt from "ccxt";
setConfig({
CC_MAX_STOPLOSS_DISTANCE_PERCENT: 100,
});
const getExchange = singleshot(async () => {
const exchange = new ccxt.binance({
options: {
defaultType: "spot",
adjustForTimeDifference: true,
recvWindow: 60000,
},
enableRateLimit: true,
});
await exchange.loadMarkets();
return exchange;
});
addExchangeSchema({
exchangeName: "ccxt-exchange",
getCandles: async (symbol, interval, since, limit) => {
const exchange = await getExchange();
const candles = await exchange.fetchOHLCV(
symbol,
interval,
since.getTime(),
limit,
);
return candles.map(([timestamp, open, high, low, close, volume]) => ({
timestamp,
open,
high,
low,
close,
volume,
}));
},
getOrderBook: async (symbol, depth, _from, _to, backtest) => {
if (backtest) {
throw new Error(
"Order book fetching is not supported in backtest mode for the default exchange schema. Please implement it according to your needs.",
);
}
const exchange = await getExchange();
const bookData = await exchange.fetchOrderBook(symbol, depth);
return {
symbol,
asks: bookData.asks.map(([price, quantity]) => ({
price: String(price),
quantity: String(quantity),
})),
bids: bookData.bids.map(([price, quantity]) => ({
price: String(price),
quantity: String(quantity),
})),
};
},
formatPrice: async (symbol, price) => {
const exchange = await getExchange();
const market = exchange.market(symbol);
const tickSize = market.limits?.price?.min || market.precision?.price;
if (tickSize !== undefined) {
return roundTicks(price, tickSize);
}
return exchange.priceToPrecision(symbol, price);
},
formatQuantity: async (symbol, quantity) => {
const exchange = await getExchange();
const market = exchange.market(symbol);
const stepSize = market.limits?.amount?.min || market.precision?.amount;
if (stepSize !== undefined) {
return roundTicks(quantity, stepSize);
}
return exchange.amountToPrecision(symbol, quantity);
},
});