-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_his_data.py
56 lines (46 loc) · 1.65 KB
/
collect_his_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
import pandas as pd
import datetime as dt
from instrument import Instrument
import utils
from oanda_api import OandaAPI
INCREMENTS = {
'M5' : 5,
'H1' : 60,
'H4' : 240
}
def create_file(pair, granularity, api):
candle_count = 2000
time_step = INCREMENTS[granularity] * candle_count
end_date = utils.get_utc_dt_from_string("2022-12-31 23:59:59")
date_from = utils.get_utc_dt_from_string("2020-01-01 00:00:00")
candle_dfs = []
date_to = date_from
while date_to < end_date:
date_to = date_from + dt.timedelta(minutes=time_step)
if date_to > end_date:
date_to = end_date
code, df = api.fetch_candles(pair,
granularity=granularity,
date_from=date_from,
date_to=date_to,
as_df=True)
if df is not None and df.empty == False:
candle_dfs.append(df)
elif code != 200:
print("ERROR", pair, granularity, date_from, date_to)
break
date_from = date_to
final_df = pd.concat(candle_dfs)
final_df.drop_duplicates(subset='time', inplace=True)
final_df.sort_values(by='time', inplace=True)
final_df.to_pickle(utils.get_his_data_filename(pair, granularity))
print(f"{pair} {granularity} {final_df.iloc[0].time} {final_df.iloc[-1].time}")
def run_collection():
pair_list = "GBP,EUR,USD,CAD,JPY,NZD,CHF"
api = OandaAPI()
for g in INCREMENTS.keys():
for i in Instrument.get_pairs_from_string(pair_list):
print(g, i)
create_file(i, g, api)
if __name__ == "__main__":
run_collection()