Repository navigation
Expand file tree
/
Copy pathSTOA.py
More file actions
59 lines (52 loc) · 2.2 KB
/
Copy pathSTOA.py
File metadata and controls
59 lines (52 loc) · 2.2 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
#coding:gbk
import numpy as np
import pandas
import math
import time
import datetime
def init(ContextInfo):
ContextInfo.stock = ContextInfo.stockcode + '.' + ContextInfo.market
ContextInfo.set_universe([ContextInfo.stock])
ContextInfo.month = '00'
ContextInfo.STOA = 0.0
def handlebar(ContextInfo):
try:
d = ContextInfo.barpos
lastdate = timetag_to_datetime(ContextInfo.get_bar_timetag(d - 1), '%Y%m%d')
date = timetag_to_datetime(ContextInfo.get_bar_timetag(d), '%Y%m%d')
time_region = str(get_days_before_lastdate(date, 35*12))
vt_list = ContextInfo.get_market_data(fields=['volume'],stock_code=[ContextInfo.stock],start_time=time_region,end_time=date)
vt_list_index_reverse = vt_list.index.tolist()
vt_list_index_reverse.reverse()
vt_list_array = [vt_list['volume'][vt_list_index_reverse[k]] for k in range(252)]
circulating_capital_frame = ContextInfo.get_financial_data(['CAPITALSTRUCTURE.circulating_capital'], [ContextInfo.stock],time_region,date)
cc_list_index_reverse = circulating_capital_frame.index.tolist()
cc_list_index_reverse.reverse()
cc_list_array = [circulating_capital_frame['circulating_capital'][cc_list_index_reverse[k]] for k in range(252)]
total_stom = 0.0
for i in range(12):
d_index = d-i*21
per_date = timetag_to_datetime(ContextInfo.get_bar_timetag(d_index), '%Y%m%d')
per_stom = get_STOM(ContextInfo, vt_list_array[i*21:(i+1)*21], cc_list_array[i*21:(i+1)*21])
total_stom += math.exp(per_stom)
print("total_stom_i", i, total_stom)
ContextInfo.STOA = math.log(float(total_stom/12))
except:
pass
ContextInfo.paint('STOA', ContextInfo.STOA, -1, 0)
def get_STOM(ContextInfo, vt_list_one=[], cc_list_one=[]):
stom_total = 0.0
for i in range(21):
if cc_list_one[i] :
stom_total += float(vt_list_one[i] / cc_list_one[i])
return stom_total
def get_days_before_lastdate(lastdate, n=0):
'''
date format = "YYYY-MM-DD HH:MM:SS"
'''
last_date = datetime.datetime.strptime(lastdate, '%Y%m%d')
if(n<0):
return datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
else:
n_days_before = last_date - datetime.timedelta(days=n)
return datetime.datetime(n_days_before.year, n_days_before.month, n_days_before.day).strftime('%Y%m%d')