-
Notifications
You must be signed in to change notification settings - Fork 1
/
Oanda Data Getter.py
40 lines (32 loc) · 1.84 KB
/
Oanda Data Getter.py
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
import pandas as pd
import requests
from configs import HEADERS, PARAMS, ACCOUNT_TYPE, INSTRUMENT
def get_user_input():
response = input('Enter "m" for mid pricing or "ba" for bid/ask pricing data\n')
if (response == 'm'):
export_mid_data()
elif (response == 'ba'):
export_ba_data()
else:
print('invalid input')
get_user_input
def export_mid_data():
try:
r = requests.get("https://api-fx{}.oanda.com/v3/instruments/{}/candles?price=M".format(ACCOUNT_TYPE, INSTRUMENT), headers=HEADERS, params=PARAMS).json()
df = pd.DataFrame(([candle['time'], candle['mid']['o'], candle['mid']['h'], candle['mid']['l'], candle['mid']['c'], candle['volume']] for candle in r['candles']),
columns=['Time', 'Open', 'High', 'Low', 'Close', 'Volume'])
df.to_csv('mid_data.csv', index=False)
print('mid data exported to "mid_data.csv"')
except:
print('request failed, likely that candlestick count is out of range (max=5000)')
def export_ba_data():
try:
r = requests.get("https://api-fx{}.oanda.com/v3/instruments/{}/candles?price=BA".format(ACCOUNT_TYPE, INSTRUMENT), headers=HEADERS, params=PARAMS).json()
df = pd.DataFrame(([candle['time'], candle['bid']['o'], candle['bid']['h'], candle['bid']['l'], candle['bid']['c'],
candle['ask']['o'], candle['ask']['h'], candle['ask']['l'], candle['ask']['c'], candle['volume']] for candle in r['candles']),
columns=['Time', 'Open Bid', 'High Bid', 'Low Bid', 'Close Bid', 'Open Ask', 'High Ask', 'Low Ask', 'Close Ask', 'Volume'])
df.to_csv('bid_ask_data.csv', index=False)
print('bid/ask data exported to "bid_ask_data.csv"')
except:
print('request failed, likely that candlestick count is out of range (max=5000)')
get_user_input()