-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mom.py
68 lines (60 loc) · 2.66 KB
/
Mom.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
import Public
import pymongo as pm
import cx_Oracle as co
import datetime as dt
import numpy as np
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
def Mom(priceColName, momPrefix, momDaysList, endDate=None):
if endDate is None:
dtNow = dt.datetime.now() - dt.timedelta(days=1)
endDate = dt.datetime(dtNow.year, dtNow.month, dtNow.day)
else:
endDate = dt.datetime(endDate.year, endDate.month, endDate.day)
if endDate.date() >= dt.datetime.now().date():
endDate = dt.datetime(dt.datetime.now().year, dt.datetime.now().month, dt.datetime.now().day) - \
dt.timedelta(days=1)
mongoConn = Public.GetPara('mongoConn')
mc = pm.MongoClient(mongoConn)
db = mc['factor']
for days in momDaysList:
facName = momPrefix + '_' + str(days)
lastUpdateDate = Public.GetLastUpdateDate(facName, mc)
tradingDateSet = Public.GetCalendar(lastUpdateDate, endDate)
currentDate = lastUpdateDate - dt.timedelta(days=days)
savedDate = lastUpdateDate
priceDict = {}
while currentDate + dt.timedelta(days=1) <= endDate:
currentDate += dt.timedelta(days=1)
# if currentDate not in tradingDateSet:
# continue
# get data
record = db[priceColName].find_one({'_id': currentDate})
if record is None:
continue
for symbol, priceList in priceDict.items():
priceList.append(np.nan)
for symbol, price in record.items():
if symbol[0] == '_':
continue
if symbol not in priceDict.keys():
priceDict[symbol] = [np.nan]
priceDict[symbol][-1] = price
if currentDate <= lastUpdateDate:
continue
# evaluate momentum and save to db
mongoDoc = {'_id': currentDate, '_updateTime': dt.datetime.now(), '_isTrade': (currentDate in tradingDateSet)}
for symbol, priceList in priceDict.items():
arr = np.array(priceList[max(0, len(priceList) - days) : ])
if np.sum(np.isfinite(arr)) < days / 3: # data is not sufficient
continue
mom = arr[-1] / np.nanmean(arr[0 : len(arr) - 1])
mongoDoc[symbol] = mom
db[facName].save(mongoDoc)
savedDate = currentDate
print(facName + ' ' + str(currentDate))
db.cfgUpdate.save({'_id': facName, 'lastUpdateDate': savedDate})
priceColName = 'DAY_CLOSE'
momPrefix = 'DAY_MOM'
momDaysList = [30, 60, 90, 180, 360]
Mom(priceColName, momPrefix, momDaysList, endDate=None)