-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.py
62 lines (54 loc) · 2.1 KB
/
strategy.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
import FinanceDataReader as fdr
import logging
from datetime import timedelta
import datetime
import numpy as np
from pandas.tseries.offsets import BDay
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
stream_hander = logging.StreamHandler()
logger.addHandler(stream_hander)
class Strategy:
def __init__(self):
self.current_date = datetime.datetime.now()
self.this_year = self.current_date.year
self.last_year = self.this_year - 1
self.last_day = (self.current_date - BDay(1)).date()
def get_stock_data(self, symbol):
try:
return fdr.DataReader(symbol=symbol, start=str(self.last_year), end=self.current_date.strftime("%Y-%m-%d"))
except Exception as e:
logger.error(e)
def get_momentum_weighted_score(self, e):
logger.debug(e)
stock_data = self.get_stock_data(e)
last_close = stock_data['Close'][self.last_day.strftime("%Y-%m-%d")]
rate = []
for i in self.MONTH_EARLY:
# get business day from timedelta
target_date = self.current_date - timedelta(i) - BDay(0)
close = stock_data['Close'][target_date.strftime("%Y-%m-%d")]
rate.append( (last_close - close)/close )
score = np.dot(np.array(self.WEIGHT), np.array(rate))
return score
def get_momentum(self, etf_list):
res = {}
for e in etf_list:
s = self.get_momentum_weighted_score(e)
res[e] = s
logger.debug(res)
return res
def get_price_per_ma(self, e, days):
logger.debug(e)
stock_data = self.get_stock_data(e)
ma = stock_data['Close'].rolling(window=days).mean() # moving average line
last_ma5 = ma[self.last_day.strftime("%Y-%m-%d")]
last_close = stock_data['Close'][self.last_day.strftime("%Y-%m-%d")]
return last_close/last_ma5
def get_ma_year(self, etf_list):
res = {}
for e in etf_list:
s = self.get_price_per_ma(e, 260)
res[e] = s
return res