/
mktdata.py
87 lines (64 loc) · 2.58 KB
/
mktdata.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
# coding:utf-8
'''
Marketdata helpers
'''
from functools32 import lru_cache
import numpy as np
from datetime import timedelta
from marketdata import update, access
from marketdata.symbols import Symbols
MktTypes = ['open', 'high', 'low', 'close']
_AllFiels = ['date', 'adj_close'] + MktTypes
def _check_db(symbols, from_date, to_date):
''' Checks in very naive way if marketdata db is created and populated with data '''
try:
l = list(Symbols().symbols())
if len(l) < len(symbols):
return False
if len(_get_marketdata(symbols[0], from_date, from_date + timedelta(days=10))) == 0: # check if we have market data for first equity
return False
if len(_get_marketdata(symbols[-1], to_date - timedelta(days=10), to_date)) == 0: # check if we have market data for the last equity
return False
return True
except:
return False
def _init_db(symbols, from_date, to_date):
''' Initializes marketdata db '''
print('Fetching marketdata')
Symbols().clean()
Symbols().add(symbols)
update.update_marketdata(from_date, to_date)
def init_marketdata(symbols, from_date, to_date):
if not _check_db(symbols, from_date, to_date):
_init_db(symbols, from_date, to_date)
def _get_marketdata(symbol, from_date, to_date):
return access.get_marketdata(symbol, from_date, to_date)
def _to_talib_format(mdata):
''' Converts market data to talib format '''
if len(mdata) == 0:
return None
res = {}
for x in _AllFiels:
res[x] = np.array([])
for md in mdata:
for x in _AllFiels:
res[x] = np.append(res[x], md[x])
return res
@lru_cache(maxsize=32)
def get_mkt_data(symbol, from_date, to_date):
return _to_talib_format(_get_marketdata(symbol, from_date, to_date))
def approx_equal(a, b, tol):
return abs(a - b) < tol
def percent_equal(a, b, comp, tol):
return (abs(a - b) / comp) * 100 < tol
def has_split_dividents(mdata, from_date, to_date):
''' Verifies if market data interval has splits, dividends '''
from_diff = abs(mdata['close'][from_date] - mdata['adj_close'][from_date])
to_diff = abs(mdata['close'][to_date] - mdata['adj_close'][to_date])
if approx_equal(from_diff, to_diff, 0.0001):
return False
return not percent_equal(from_diff, to_diff, mdata['close'][to_date], 0.8)
def odd_data(open_position, close_position):
if approx_equal(0, open_position, 0.1) or approx_equal(0, close_position, 0.1):
return True
return abs(open_position - close_position) > min(open_position, close_position)