-
Notifications
You must be signed in to change notification settings - Fork 24
/
grab_data.py
132 lines (102 loc) · 3.8 KB
/
grab_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from __future__ import print_function
import csv
import sys
import yaml
from pyoanda import Client, TRADE
from pyoanda.exceptions import BadRequest
from datetime import datetime, timedelta
# Get my credentials
#
# YAML format, please see:
# http://ess.khhq.net/wiki/YAML_Tutorial
#
# Config file for connecting to Oanda
#
# ACCOUNT_NUM: Integer number
# ACCOUNT_KEY: String of your account Key
with open("Config.yaml") as f:
config = yaml.load(f.read())
client = Client(
TRADE,
account_id=config['ACCOUNT_NUM'],
access_token=config['ACCOUNT_KEY']
)
DAYS = 50
GRAN = 'M15'
INST = 'EUR_JPY'
start = datetime.now() - timedelta(days=DAYS)
with open('data/data-set-{}-days.csv'.format(DAYS), 'w') as f:
# We will map fields of the returned data to a more human readable format.
mapFields = {}
# Remove ""Mids", remove "Complete"
mapFields['time'] = 'time'
mapFields['lowMid'] = 'low'
mapFields['complete'] = None
mapFields['highMid'] = 'high'
mapFields['openMid'] = 'open'
mapFields['volume'] = 'volume'
mapFields['closeMid'] = 'close'
# Create the writer which will output to file
writer = csv.DictWriter(f, fieldnames=mapFields.values())
writer.writeheader()
print('Fetching data from server...')
print('-'*100)
# Save the day to display progression output
lastPct = 0.
# Loop through every day and save the data found
for day in range(DAYS):
# Flush previously written output to file (Force write)
f.flush()
for _ in range(2): # 24/12 hours
# Find the start time from which to begin retrieving data
end = start + timedelta(hours=12)
# Create the dictionary which will be sent to PyOanda
kwargs = dict(
count=None,
instrument=INST,
granularity=GRAN,
candle_format="midpoint",
end=end.strftime("%Y-%m-%dT%H:%M:%S.%f%z"),
start=start.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
)
# Try to retrieve the data from Oanda
try:
# Fetch the data from Oanda
candles = client.get_instrument_history(**kwargs)["candles"]
# Remove 'Mid' from the key names
for candle in candles:
for key, newKey in mapFields.items():
# Does the key exist in the candle?
if key in candle:
# Does the new key exist? (Delete it if not)
if newKey is None:
del candle[key]
# Change the key name
else:
candle[newKey] = candle.pop(key)
# Try to fetch and write the data to the file
writer.writerows(candles)
# Print progress to the terminal
pct = int(float(day) / DAYS * 100.0)
# print pct
if pct != lastPct:
strToWrite = '#'
if pct - lastPct > 1:
strToWrite *= int(pct-lastPct)
sys.stdout.write(strToWrite)
sys.stdout.flush()
lastPct = pct
# Catch any bad requests to Oanda and print the error
except BadRequest as br:
# print 'PyOanda suffered a bad request exception:', br
pass
# Catch generic exceptions
except Exception as e:
print('GrabData suffered an exception:', e)
print('GrabData will now terminate!')
print('-'*100)
exit()
# Reset the end time to the last candle retrieved
start = end
print()
print('-'*100)