-
Notifications
You must be signed in to change notification settings - Fork 4
/
bts-market-data.js
134 lines (125 loc) · 5.72 KB
/
bts-market-data.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
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
'use strict';
const cache = require('./bts-cache');
const bitsharesjs = require('bitsharesjs');
const bsws = require('bitsharesjs-ws');
const Apis = bsws.Apis;
function getMarketPrice(base_asset_symbol, quote_asset_symbol) {
return getOpenOrders(base_asset_symbol, quote_asset_symbol)
.then(o => {
let askPrice, bidPrice, i;
for (i = 0; i < o.askOrders.length; i++) {
if (o.askOrders[i].parsed.base_amount > 0.1 && o.askOrders[i].parsed.quote_amount > 0.1) {
askPrice = o.askOrders[i].parsed.price;
break;
}
}
for (i = 0; i < o.bidOrders.length; i++) {
if (o.bidOrders[i].parsed.base_amount > 0.1 && o.bidOrders[i].parsed.quote_amount > 0.1) {
bidPrice = o.bidOrders[i].parsed.price;
break;
}
}
if (askPrice && bidPrice) {
if (askPrice >= bidPrice)
return (askPrice + bidPrice) / 2;
else {
// return 'Unknow ... ';
// 有人砸盘或者大量吃进,机器人可 。。。
return -1;
}
}
else
return -1;
})
}
function getOpenOrders(base_asset_symbol, quote_asset_symbol) {
var base_asset, quote_asset;
return cache.get_assets([base_asset_symbol, quote_asset_symbol])
.then((assets) => {
base_asset = assets[0];
quote_asset = assets[1];
return Apis.instance().db_api().exec('get_limit_orders', [base_asset.id, quote_asset.id, 25])
})
.then((orders) => {
var bidOrders = orders.filter(o => {
return o.sell_price.base.asset_id == base_asset.id;
})
var askOrders = orders.filter(o => {
return o.sell_price.base.asset_id == quote_asset.id;
})
orders.forEach((o) => {
o.parsed = {};
})
bidOrders.forEach(o => {
o.parsed.quote_amount = o.sell_price.quote.amount / Math.pow(10, quote_asset.precision);
o.parsed.base_amount = o.sell_price.base.amount / Math.pow(10, base_asset.precision);
o.parsed.current_base_amount = o.for_sale / Math.pow(10, base_asset.precision);
o.parsed.price = o.parsed.base_amount / o.parsed.quote_amount;
})
askOrders.forEach(o => {
o.parsed.quote_amount = o.sell_price.base.amount / Math.pow(10, quote_asset.precision);
o.parsed.base_amount = o.sell_price.quote.amount / Math.pow(10, base_asset.precision);
o.parsed.current_quote_amount = o.for_sale / Math.pow(10, quote_asset.precision);
o.parsed.price = o.parsed.base_amount / o.parsed.quote_amount;
})
return {
bidOrders: bidOrders,
askOrders: askOrders
}
})
}
function getFilledOrders(base_asset_symbol, quote_asset_symbol) {
var base_asset, quote_asset;
return cache.get_assets([base_asset_symbol, quote_asset_symbol])
.then((assets) => {
base_asset = assets[0];
quote_asset = assets[1];
return Apis.instance().history_api().exec('get_fill_order_history', [base_asset.id, quote_asset.id, 20])
}).then((orders) => {
orders.forEach((o) => {
o.parsed = {}
// o.parsed.quote_amout =
if (o.op.pays.asset_id == base_asset.id) { // 买单成交
o.parsed.filled_base_amount = o.op.pays.amount / Math.pow(10, base_asset.precision);
o.parsed.filled_quote_amount = o.op.receives.amount / Math.pow(10, quote_asset.precision);
o.parsed.direction = 'ask';
}
else { // 卖单成交
o.parsed.filled_base_amount = o.op.receives.amount / Math.pow(10, base_asset.precision);
o.parsed.filled_quote_amount = o.op.pays.amount / Math.pow(10, quote_asset.precision);
o.parsed.direction = 'bid';
}
if (o.op.fill_price.base.asset_id == base_asset.id) {
o.parsed.order_base_amount = o.op.fill_price.base.amount / Math.pow(10, base_asset.precision);
o.parsed.order_quote_amount = o.op.fill_price.quote.amount / Math.pow(10, quote_asset.precision);
}
else {
o.parsed.order_base_amount = o.op.fill_price.quote.amount / Math.pow(10, base_asset.precision);
o.parsed.order_quote_amount = o.op.fill_price.base.amount / Math.pow(10, quote_asset.precision);
}
o.parsed.is_maker = o.op.is_maker;
o.parsed.fill_price = o.parsed.filled_base_amount / o.parsed.filled_quote_amount;
o.parsed.order_price = o.parsed.order_base_amount / o.parsed.order_quote_amount;
});
return orders;
})
}
function getLatestFillPrice(base_asset_symbol, quote_asset_symbol) {
return getFilledOrders(base_asset_symbol, quote_asset_symbol)
.then((orders) => {
var price = -1;
for (var i = 0; i < orders.length; i++) {
if (orders[i].parsed.filled_base_amount > 0.1 && orders[i].parsed.filled_quote_amount > 0.1) {
price = orders[i].parsed.order_price;
break;
}
}
return price;
})
}
module.exports = {
getMarketPrice: getMarketPrice,
getLatestFillPrice: getLatestFillPrice,
getOpenOrders: getOpenOrders,
getFilledOrders: getFilledOrders,
}