Reading Financial Data
In this example we use the Pax Financial API to read some historical hourly price data about the Bitcoin/US dollar exchange rate, and we display the hour bars we obtain.
To begin we import the Pax Development Kit and instantiate the Pax Financial API. You must replace the API key in the PaxFinancialExamples/.config
file with your API key.
import paxdk
import json
with open("../.config") as f:
data = json.load(f)
api_key = data['api_key']
pq = paxdk.PaxFinancialAPI(url_version='dev',api_key=api_key)
PaxFinancialAPI v4 loaded!
We use the get_historical_hour
call of the Pax Financial API to read some historical hourly price data about the Bitcoin/US dollar exchange rate. We use a Pandas dataframe to display the hour bars we obtain. Hour bars correspond to a one-hour period and contain the opening price, closing price, high price, and low price for that period.
import pandas as pd
import datetime
bars = pq.get_historical_hour({'api_key':api_key,
'time_in':datetime.datetime(2020,4,1,10),
'time_end':datetime.datetime(2020,4,2,10),
'Ticker':'BTCUSDT',
},remote=True)
df=pd.DataFrame(bars)
df[['DateTime','Ticker','OpenPrice','ClosePrice','HighPrice','LowPrice']]
DateTime | Ticker | OpenPrice | ClosePrice | HighPrice | LowPrice | |
---|---|---|---|---|---|---|
0 | 2020-04-01 10:00:00 | BTCUSDT | 6298.74 | 6295.03 | 6318.89 | 6288.02 |
1 | 2020-04-01 11:00:00 | BTCUSDT | 6295.03 | 6294.25 | 6301.76 | 6271.20 |
2 | 2020-04-01 12:00:00 | BTCUSDT | 6294.25 | 6217.51 | 6332.16 | 6215.66 |
3 | 2020-04-01 13:00:00 | BTCUSDT | 6217.51 | 6207.82 | 6237.52 | 6184.44 |
4 | 2020-04-01 14:00:00 | BTCUSDT | 6207.82 | 6248.66 | 6259.88 | 6200.55 |
5 | 2020-04-01 15:00:00 | BTCUSDT | 6248.66 | 6230.79 | 6257.52 | 6223.01 |
6 | 2020-04-01 16:00:00 | BTCUSDT | 6230.79 | 6186.38 | 6232.66 | 6153.80 |
7 | 2020-04-01 17:00:00 | BTCUSDT | 6186.38 | 6196.06 | 6211.07 | 6170.05 |
8 | 2020-04-01 18:00:00 | BTCUSDT | 6196.06 | 6187.99 | 6216.32 | 6183.59 |
9 | 2020-04-01 19:00:00 | BTCUSDT | 6187.99 | 6205.43 | 6211.38 | 6166.86 |
10 | 2020-04-01 20:00:00 | BTCUSDT | 6205.43 | 6336.67 | 6365.32 | 6193.98 |
11 | 2020-04-01 21:00:00 | BTCUSDT | 6336.67 | 6352.28 | 6394.89 | 6336.55 |
12 | 2020-04-01 22:00:00 | BTCUSDT | 6352.28 | 6519.82 | 6576.95 | 6352.16 |
13 | 2020-04-01 23:00:00 | BTCUSDT | 6519.82 | 6633.43 | 6665.77 | 6500.76 |
14 | 2020-04-02 00:00:00 | BTCUSDT | 6633.43 | 6611.25 | 6723.47 | 6587.76 |
15 | 2020-04-02 01:00:00 | BTCUSDT | 6611.25 | 6566.67 | 6636.58 | 6565.87 |
16 | 2020-04-02 02:00:00 | BTCUSDT | 6566.67 | 6604.55 | 6615.40 | 6565.85 |
17 | 2020-04-02 03:00:00 | BTCUSDT | 6604.55 | 6605.23 | 6613.51 | 6579.68 |
18 | 2020-04-02 04:00:00 | BTCUSDT | 6605.23 | 6596.21 | 6627.00 | 6588.75 |
19 | 2020-04-02 05:00:00 | BTCUSDT | 6596.21 | 6604.09 | 6604.89 | 6566.94 |
20 | 2020-04-02 06:00:00 | BTCUSDT | 6604.09 | 6638.63 | 6642.13 | 6590.18 |
21 | 2020-04-02 07:00:00 | BTCUSDT | 6638.63 | 6644.80 | 6673.98 | 6613.71 |
22 | 2020-04-02 08:00:00 | BTCUSDT | 6644.80 | 6648.31 | 6665.18 | 6611.15 |
23 | 2020-04-02 09:00:00 | BTCUSDT | 6648.31 | 6640.26 | 6656.08 | 6624.23 |
24 | 2020-04-02 10:00:00 | BTCUSDT | 6640.26 | 6646.44 | 6693.88 | 6622.85 |
With the price data in a dataframe, we can easily plot the data with Matplotlib. For example, we can plot the high and low prices against time:
import matplotlib.pyplot as plt
plt.plot(df['DateTime'],df['HighPrice'])
plt.plot(df['DateTime'],df['LowPrice'])
plt.show()