-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_data.py
52 lines (49 loc) · 1.73 KB
/
get_data.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
41
42
43
44
45
46
47
48
49
50
51
52
import csv
import datetime
from oandapyV20.contrib.factories import InstrumentsCandlesFactory
from oandapyV20 import API
class get_data():
def __init__(self):
return
def feedData(gran, ticker, start_date, end_date):
#print("date and time:", date_time)
# Oanda API setup
# from oandapyV20 import API
client = API(access_token='access token')
instrument, granularity = ticker, gran
stDt = start_date
edDt = end_date
#edDt = '2021-11-11T00:00:00Z'
params = {'from': stDt, 'to': edDt, 'granularity': granularity}
reList = []
for r in InstrumentsCandlesFactory(instrument=instrument, params=params):
client.request(r)
reList = reList + r.response.get('candles')
df = reList
candles = reList
# OHLC variables to return in array
wO = []
wH = []
wL = []
wC = []
wV = []
wD = []
for x in range(0, len(reList)):
candleData = candles[x].get("mid")
candleDate = candles[x].get("time")
v = candles[x].get("volume")
o = candleData.get("o")
h = candleData.get("h")
l = candleData.get("l")
c = candleData.get("c")
wO.append(float(o))
wH.append(float(h))
wL.append(float(l))
wC.append(float(c))
wV.append(float(v))
wD.append(candleDate)
# Write to CSV
with open("data.csv", "w") as csvfile:
wr = csv.writer(csvfile, delimiter=',')
for i in range(0, len(wO)):
wr.writerow([wD[i], wO[i],wH[i],wL[i],wC[i],wV[i]])