-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarketData.js
42 lines (35 loc) · 1.32 KB
/
marketData.js
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
const Exchange = require('./exchange');
const BinanceExchange = require('./binanceExchange');
class MarketData {
constructor(apiKey, apiSecret) {
this.exchange = new Exchange(apiKey, apiSecret);
this.binanceExchange = new BinanceExchange();
}
async getOrderBook(pair) {
const marketDataFull = await this.exchange.getMarketData(pair);
if (marketDataFull && marketDataFull.data) {
let marketData = marketDataFull.data;
if (marketData.buy && marketData.buy.length > 0 && marketData.sell && marketData.sell.length > 0) {
const bestBid = parseFloat(marketData.buy[0][0]);
const bestAsk = parseFloat(marketData.sell[0][0]);
return { bestBid, bestAsk };
} else {
throw new Error('Order book data is incomplete or empty');
}
} else {
throw new Error('Unable to fetch market data from LCX');
}
}
async getBinanceOrderBook(pair) {
const symbol = pair.replace('/', ''); // Remove '/' from pair
const marketData = await this.binanceExchange.getMarketData(symbol);
if (marketData) {
const bestBid = parseFloat(marketData.bidPrice);
const bestAsk = parseFloat(marketData.askPrice);
return { bestBid, bestAsk };
} else {
throw new Error('Unable to fetch market data from Binance');
}
}
}
module.exports = MarketData;