Skip to content

Commit

Permalink
fix ask quotes truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
bitrinjani committed Feb 22, 2018
1 parent c0945da commit cd4cfd2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
10 changes: 9 additions & 1 deletion __tests__/BrokerAdapter.test.ts
@@ -1,7 +1,8 @@
import BrokerAdapter from '../lib/BrokerAdapterImpl';
import { BrokerConfigType, CashMarginType, Broker, OrderSide, OrderType, TimeInForce, OrderStatus } from '../lib/types';
import { BrokerConfigType, CashMarginType, Broker, OrderSide, OrderType, TimeInForce, OrderStatus, QuoteSide } from '../lib/types';
import * as nock from 'nock';
import { nocksetup } from './nocksetup';
import * as _ from 'lodash';

function createOrder(
broker: Broker,
Expand Down Expand Up @@ -57,6 +58,13 @@ describe('BtcboxBrokerAdapter', () => {
]);
});

test('fetchQuotes full', async () => {
const ba = new BrokerAdapter(brokerConfig);
const quotes = await ba.fetchQuotes();
const bestAsk = _(quotes).filter(q => q.side === QuoteSide.Ask).minBy(q => q.price);
expect(bestAsk.price).toBe(1107098);
});

test('send', async () => {
const ba = new BrokerAdapter(brokerConfig);
const order = createOrder('Btcbox', OrderSide.Buy, 0.001, 900000, CashMarginType.Cash, OrderType.Limit, undefined);
Expand Down
1 change: 1 addition & 0 deletions __tests__/nocksetup.ts
Expand Up @@ -5,6 +5,7 @@ import * as testData from './testData';
export function nocksetup() {
const api = nock('https://www.btcbox.co.jp');
api.get('/api/v1/depth/').reply(200, testData.depth);
api.get('/api/v1/depth/').reply(200, testData.fullDepth);
api.post('/api/v1/balance/').reply(200, testData.balance);
api.post('/api/v1/balance/').reply(200, testData.balance);
api.post('/api/v1/trade_cancel/').reply(200, testData.cancel);
Expand Down
7 changes: 5 additions & 2 deletions __tests__/testData.ts
Expand Up @@ -6,9 +6,12 @@ export const cancel = '{"result":true,"id":"12345"}';
export const error = '{"result":false,"code":"401"}';
export const getOrder =
'{"id":12345,"datetime":"2018-02-18 09:30:24","type":"buy","price":900000,"amount_original":0.001,"amount_outstanding":0.001,"status":"part"}';
export const getOrder2 =
export const getOrder2 =
'{"id":12345,"datetime":"2018-02-18 09:30:24","type":"buy","price":900000,"amount_original":0.001,"amount_outstanding":0.001,"status":"cancelled"}';
export const getOrder3 =
export const getOrder3 =
'{"id":12345,"datetime":"2018-02-18 09:30:24","type":"buy","price":900000,"amount_original":0.001,"amount_outstanding":0,"status":"all"}';
export const depth =
'{"asks":[[1200000,4.217],[1199999,0.17],[1199481,0.004]],"bids":[[1198625,0.001],[1197707,0.005],[1196306,0.4869]]}';

export const fullDepth =
'{"asks":[[19999999,2.982],[14700000,0.05],[5000000,0.1],[4600000,2],[4200000,1.5],[4180000,0.68],[4150000,2.38],[4100000,1.5],[4000000,2.05],[3988000,0.55],[3858000,0.35],[3450000,1],[3000500,0.01],[3000000,0.7],[2900000,3],[2875324,1.304],[2800000,0.01],[2700000,0.5],[2663321,1.014],[2500000,0.5],[2400000,0.79],[2350000,0.0022],[2300000,0.207],[2298200,0.176],[2200000,0.1],[2167953,0.049],[2150000,0.01],[2099900,0.01],[2089900,0.01],[2079900,0.01],[2069900,0.01],[2059900,0.01],[2049900,0.01],[2039900,0.01],[2029900,0.01],[2019900,0.01],[2009900,0.01],[2000000,11.985],[1999900,0.01],[1989900,0.01],[1988000,0.015],[1980000,0.03],[1979900,0.01],[1973330,0.1],[1971320,0.203],[1969900,0.01],[1959900,0.01],[1950000,0.152],[1949900,0.01],[1939900,0.01],[1933330,0.1],[1931000,0.025],[1929900,0.01],[1920000,0.011],[1919900,0.01],[1911502,0.072],[1909900,0.01],[1900000,1.073],[1890000,0.072],[1880000,0.03],[1873330,0.1],[1867953,0.044],[1862000,0.4],[1860000,0.059],[1850000,0.5],[1849000,0.1],[1833330,0.1],[1812000,0.3],[1800000,3.035],[1793330,0.1],[1789990,0.039],[1780000,0.03],[1767500,0.001],[1767132,0.001],[1763330,0.1],[1748000,5],[1740000,0.02],[1733330,0.1],[1730000,0.02],[1720000,0.233],[1713330,0.1],[1710000,0.001],[1700050,0.12],[1700000,0.13],[1699990,0.05],[1698848,0.05],[1689453,0.05],[1677877,0.0798],[1666666,1],[1666000,0.2],[1659990,0.05],[1610000,0.011],[1603330,0.1],[1600000,0.005],[1599990,0.015],[1580000,0.054],[1573330,0.1],[1543330,0.2],[1513330,0.1],[1511984,0.0024],[1500000,0.151],[1485000,0.24],[1483330,0.1],[1473505,0.05],[1470000,0.05],[1458550,0.39],[1453330,0.1],[1450000,0.38],[1430000,5],[1427954,0.0405],[1423330,0.1],[1412141,0.048],[1410000,5.48],[1405459,0.05],[1403584,0.05],[1400000,5.8593],[1399998,0.6645],[1398000,0.6542],[1396999,0.25],[1393486,0.0615],[1393330,0.1],[1391750,0.05],[1385673,0.05],[1380000,0.5],[1372304,0.0143],[1363330,0.1],[1356663,0.0012],[1353000,0.3],[1350000,0.973],[1333330,0.1],[1300005,0.0304],[1300000,0.557],[1298031,0.0378],[1293330,0.1],[1292020,0.001],[1290780,0.078],[1290000,0.01],[1289000,0.356],[1285000,0.1],[1280634,0.0102],[1280000,0.131],[1276822,0.01],[1276668,0.01],[1276347,0.0177],[1276342,0.1],[1276000,0.031],[1275000,1.054],[1260000,0.02],[1259981,0.3869],[1257856,0.3859],[1254728,0.3899],[1251357,0.14],[1251097,0.005],[1250893,0.004],[1250312,0.14],[1250000,0.424],[1249981,0.14],[1249185,0.01],[1249141,0.009],[1247856,0.14],[1246848,0.14],[1245721,0.004],[1245525,0.14],[1244728,0.14],[1243618,0.14],[1242468,0.14],[1241357,0.103],[1240000,0.03],[1238000,0.15],[1235997,0.01],[1230000,0.258],[1220000,0.038],[1219689,0.0014],[1219658,0.005],[1217645,0.005],[1215512,0.18],[1210000,0.01],[1208512,0.14],[1203000,0.6],[1201512,0.24],[1200000,0.905],[1199000,5],[1197800,0.004],[1197513,0.14],[1194437,0.14],[1192000,4.5737],[1190437,0.24],[1190000,0.023],[1186437,0.18],[1183237,0.2],[1179326,0.2],[1176257,0.2],[1172674,0.14],[1170832,0.002],[1169627,0.16],[1166036,0.16],[1162757,0.22],[1158437,0.16],[1151814,0.14],[1151086,0.8],[1148941,0.05],[1147681,0.16],[1146361,0.01],[1144437,0.22],[1141297,0.2],[1137931,1.6],[1137446,0.05],[1137437,0.16],[1135000,0.4332],[1131437,0.22],[1129782,0.05],[1127437,0.24],[1123602,0.22],[1123579,1.22],[1119867,0.14],[1116777,0.18],[1116776,1.6],[1114036,0.0869],[1114035,0.16],[1110164,0.1905],[1110159,0.05],[1108719,0.015],[1107511,0.005],[1107098,0.015]],"bids":[[1106719,0.015],[1105098,0.015],[1105074,0.005],[1102973,0.1905],[1100069,0.2755],[1090805,0.16],[1090000,0.02],[1080001,1.6],[1080000,0.036],[1078713,0.24],[1075685,0.14],[1073081,1.6],[1072588,0.2],[1070000,0.03],[1069237,0.16],[1065984,0.22],[1062511,0.24],[1060000,0.03],[1059707,0.8],[1055511,0.2],[1050000,0.06],[1048511,0.16],[1041652,0.0277],[1041511,0.22],[1040888,0.016],[1040000,0.07],[1035000,0.02],[1034511,0.16],[1026511,0.22],[1019511,0.22],[1012511,0.14],[1011100,0.656],[1010000,0.02],[1005511,0.18],[1000000,0.155],[998511,0.22],[990000,0.068],[980000,0.02],[961000,0.05],[923000,0.094],[902071,0.051],[902063,0.156],[900000,5.151],[884072,0.005],[857000,0.3],[850000,1.6515],[833210,0.1],[822000,0.506],[813700,0.2],[810000,0.0916],[802000,0.5],[800100,0.01],[800000,0.175],[796740,0.001],[795000,0.1],[782000,0.5],[757163,1.465],[750500,0.2075],[750000,3.9651],[748806,0.72],[748718,1],[748661,0.4405],[748347,2.0075],[747714,0.698],[747653,0.438],[747313,0.2115],[746863,0.4265],[746737,0.407],[746692,0.408],[744100,0.25],[743838,0.5],[743563,1.726],[743087,0.5415],[743001,0.25],[743000,0.495],[742628,1.2025],[742463,0.2505],[742200,0.25],[742108,1.0025],[742000,0.8308],[741909,0.8665],[741179,0.237],[741100,0.55],[741097,0.204],[740994,0.9315],[740881,0.686],[738718,1],[736975,0.5],[733000,0.65],[732200,0.25],[730100,0.58],[728718,1],[725100,0.5],[720100,0.5],[720000,0.0275],[718718,1],[718200,1.84],[715180,0.5],[713258,0.455],[712468,1.2575],[712203,5.7835],[712100,0.2],[712054,0.9975],[712000,1.025],[711925,0.721],[711760,2.271],[711531,1.116],[711499,1.205],[711152,2.0815],[711071,0.5],[710480,0.238],[710199,4.1965],[710101,0.3075],[710000,5.3316],[709611,0.225],[709497,1.2135],[706230,12.5],[705000,0.801],[701001,0.255],[701000,0.5],[700000,2.7563],[696100,0.3],[696000,0.7],[695900,0.68],[695000,0.9575],[691830,10.35],[691163,1.75],[690000,7.8002],[688000,0.2635],[687001,5.115],[687000,0.275],[685454,0.55],[682600,0.18],[682180,0.68],[681031,0.5],[681001,0.25],[681000,8.5905],[680590,0.5],[680500,0.25],[680050,0.305],[680010,0.605],[680001,0.3085],[680000,0.4],[674920,32.95],[674857,1.2225],[674775,0.716],[674689,1.227],[674450,1.325],[674373,5.3365],[672000,0.5],[662000,0.5],[650000,0.0307],[643001,0.25],[642405,0.3225],[642000,1.075],[641946,0.275],[640860,10],[639872,0.05],[639543,0.6545],[639487,7.0325],[639386,4.609],[639000,1.2469],[638964,7.213],[638876,7.0275],[638577,0.6475],[638541,0.3665],[638077,1.8055],[638001,0.64],[638000,0.4621],[637979,0.455],[637829,0.2115],[637648,1.219],[637583,0.6565],[637537,5.4095],[637500,1.0008],[637463,0.2725],[637462,0.5315],[637439,6.4565],[637301,7.209],[637239,4.427],[637075,0.702],[637022,0.2955],[636748,1.4095],[636620,0.6455],[636575,7.336],[636491,5.6965],[636358,1.4285],[636272,0.259],[636137,0.6985],[636086,5.219],[636001,0.25],[635253,1.0125],[635172,0.2695],[635064,1.2445],[635000,2.4502],[634909,0.249],[634818,2.254],[634124,4.438],[634070,0.2375],[631000,12.9922],[630000,29.8212],[628893,0.25],[625511,0.25],[625000,4.4872],[624000,0.21],[623000,0.25],[621000,10.253],[620570,0.362],[620499,0.25],[620362,0.2405],[620162,0.3365],[620001,0.7845],[620000,7.6429],[619806,1.3205],[619702,0.2025],[619357,2.1625],[619195,3.7435],[619136,1.061],[618993,2.1975],[618703,0.2235],[618694,0.421],[618400,1.0355],[618208,0.583],[617975,0.25],[617835,0.3255],[617279,2.4395],[616673,0.4945],[616281,2.941],[615888,0.25],[615499,0.454],[615347,0.258],[615230,14.435],[615118,1.3165],[615000,0.836],[614968,1.594],[614496,1.119],[614339,6.2245],[614290,0.246],[614087,0.25],[614000,0.5163],[613970,1.0515],[611000,1.445],[610000,0.055],[609000,0.305],[608000,1.1885],[607850,0.5],[607500,0.4],[606443,0.5],[606000,0.345],[605598,0.258]]}';
2 changes: 1 addition & 1 deletion lib/BrokerAdapterImpl.ts
Expand Up @@ -133,7 +133,7 @@ export default class BrokerAdapterImpl implements BrokerAdapter {

private mapToQuote(depth: DepthResponse): Quote[] {
const asks = _(depth.asks)
.take(100)
.takeRight(100)
.map(q => {
return { broker: this.broker, side: QuoteSide.Ask, price: q[0], volume: q[1] };
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@bitr/btcbox",
"version": "1.0.2",
"version": "1.0.3",
"description": "R2 broker plugin for btcbox",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit cd4cfd2

Please sign in to comment.