-
Notifications
You must be signed in to change notification settings - Fork 10
API Class Tutorial
The primary role of the API class is to request data from a Bitcoin exchange API, standardize the format of the data, and return the data as a pandas DataFrame. The class also provides additional features like ping_limit and auto_since which are examined in further detail below.
Note: You can see a complete script of this tutorial here and the complete function documentation here
##Add request jobs to API object
First you must create the API class and then add specific jobs to the class. Currently there are two types of jobs which the API can perform.
-
ticker: This job will return a single row of ticker data which gives the last price, 24 hour high and low, the current bid and ask spread, and the 24 hour volume. -
trades: This job will return the most recent trade history including the price, volume, bid/ask, and time of the trade.
In order to add jobs you must use the add_job([exchange],[symbol],[type]) method. This method also allows you to set optional settings like limit, auto_since, and ping_limit.
>> import bitquant as bq
>> a = bq.api()
>> a.add_job('bitfinex','btcusd','ticker')
>> a.add_job('bitfinex','btcusd','trades')
>> a.add_job('btcchina','btccny','trades',limit=2)
Note that you can also use
add_jobmethod to override or remove any optional settings previous set
##Run API requests jobs
To actually request data from an Bitcoin exchange API, you must use the run([exchange],[symbol],[type]) method which will return a DataFrame. You do not need to include the optional settings when using the run method.
>> tick = a.run('bitfinex','btcusd','ticker')
>> print tick
ask bid high last low timestamp \
0 231.27 231.26 231.66 231.26 226.1 1441397010.479910588
volume exchange symbol
0 11866.70689331 bitfinex btcusd
>> trd = a.run('bitfinex','btcusd','trades')
>> print trd
amount exchange price tid timestamp type symbol
0 0.275 bitfinex 231.26 11565643 1441397003 sell btcusd
1 0.422 bitfinex 231.26 11565641 1441397003 sell btcusd
2 0.422 bitfinex 231.26 11565639 1441397003 sell btcusd
3 0.422 bitfinex 231.26 11565637 1441397003 sell btcusd
4 0.422 bitfinex 231.26 11565635 1441397003 sell btcusd
5 0.421 bitfinex 231.26 11565633 1441397003 sell btcusd
6 0.422 bitfinex 231.26 11565631 1441397003 sell btcusd
7 0.271 bitfinex 231.26 11565629 1441397003 sell btcusd
8 0.151 bitfinex 231.26 11565627 1441396988 sell btcusd
>> trd = a.run('btcchina','btccny','trades')
>> print trd
amount timestamp timestamp_ms price tid type exchange symbol
0 0.037 1441397018 1441397018000 1473.66 679485368 sell okcoin btccny
1 0.012 1441397018 1441397018000 1473.62 679485370 sell okcoin btccny
##Optional Setting: limit
As demonstrated above, the limit setting will limit the number of rows returned by the API request. By default this option is turned off (limit='').
##Optional Setting: ping_limit
You may change the ping_limit for a particular job when you call the add_job method. The ping limit will enforce a provided number of seconds between API requests so your scripts to not overload the exchanges with requests. By default it is set to ping_limit=1.0 which is one request per second, but could be changed for example to ping_limit=0.5 which is two requests per second.
##Optional Setting: auto_since
The auto_since setting will automatically minimize the number of rows returned by the API request such that only new data is returned. Each exchange has slightly different support for this feature, some exchanges will limit rows based on the last trade ID and some will limit based on the last timestamp.
In the complete script for this tutorial, there is a demonstration of this feature which is displayed below.
a.add_job('okcoin','btccny','trades',auto_since='yes')
for i in range(10):
print a.run('okcoin','btccny','trades')
Output sample:
[0 rows x 0 columns]
amount timestamp timestamp_ms price tid type exchange symbol
0 0.089 1441397021 1441397021000 1473.56 679485451 buy okcoin btccny
1 0.4 1441397021 1441397021000 1473.58 679485453 buy okcoin btccny
2 0.048 1441397021 1441397021000 1473.58 679485455 buy okcoin btccny
3 0.256 1441397021 1441397021000 1473.58 679485457 buy okcoin btccny
4 0.437 1441397022 1441397022000 1473.58 679485470 sell okcoin btccny
5 0.264 1441397022 1441397022000 1473.58 679485473 sell okcoin btccny
[6 rows x 8 columns]
amount timestamp timestamp_ms price tid type exchange symbol
0 0.3 1441397024 1441397024000 1473.59 679485503 buy okcoin btccny
1 0.157 1441397024 1441397024000 1473.59 679485504 buy okcoin btccny
[2 rows x 8 columns]
amount timestamp timestamp_ms price tid type exchange symbol
0 0.042 1441397029 1441397029000 1473.58 679485560 buy okcoin btccny
1 0.258 1441397029 1441397029000 1473.58 679485562 buy okcoin btccny
2 0.06 1441397029 1441397029000 1473.58 679485563 buy okcoin btccny
3 0.295 1441397029 1441397029000 1473.58 679485565 buy okcoin btccny
[4 rows x 8 columns]
Empty DataFrame
Columns: []
Index: []