-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_null_crypto.py
More file actions
executable file
·68 lines (51 loc) · 1.96 KB
/
generate_null_crypto.py
File metadata and controls
executable file
·68 lines (51 loc) · 1.96 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 20 17:43:08 2021
@author: Guido Meijer
"""
import random
import time
import datetime
import numpy as np
from os import mkdir
from os.path import join, dirname, exists, realpath
from Historic_Crypto import HistoricalData
# Settings
TICKER = 'BTC'
ITERATIONS = 1000
BIN_SIZE = 60 # seconds
DURATION = 5 # hours
# Set directory
data_dir = join(dirname(realpath(__file__)), 'data')
if not exists(data_dir):
mkdir(data_dir)
def str_time_prop(start, end, format, prop):
"""Get a time at a proportion of a range of two formatted times.
start and end should be strings specifying times formated in the
given format (strftime-style), giving an interval [start, end].
prop specifies how a proportion of the interval to be taken after
start. The returned time will be in the specified format.
"""
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def random_date(start, end, prop):
return str_time_prop(start, end, '%Y-%m-%d-%H-%M', prop)
# Get a null distribution of crypto vectors
null_crypto = []
while len(null_crypto) < ITERATIONS:
start_time = random_date('2019-01-01-12-00', '2020-01-01-12-00', random.random())
end_time = datetime.datetime.strptime(start_time, '%Y-%m-%d-%H-%M') + datetime.timedelta(hours=5)
try:
crypto_data = HistoricalData(f'{TICKER}-USD', BIN_SIZE, start_time,
end_time.strftime("%Y-%m-%d-%H-%M")).retrieve_data()
if crypto_data['open'].values.shape[0] == (DURATION * 60 * 60) / BIN_SIZE:
null_crypto.append(crypto_data['open'].values)
except:
print('Error while fetching crypto data')
# Convert to ndarray
null_crypto = np.array(null_crypto)
# Save to disk
np.save(join(data_dir, f'{TICKER}_null_vectors.npy'), null_crypto)