嘉庆离散指标测试
来源:https://uqer.io/community/share/55595229f9f06c6c7104f8b3
import numpy as np
start = datetime(2011, 1, 1)
end = datetime(2015, 4, 27)
benchmark = 'HS300'
universe = set_universe('HS300')
capital_base = 100000
short_history = 30
longest_history = 60
pos_pieces = 10
enter_window = 20
exit_window = 10
N = 4
def initialize(account):
account.postion_size_hold = {}
for stk in universe:
account.postion_size_hold[stk] = 0
def handle_data(account):
for stock in account.universe:
cnt_price = account.referencePrice[stock]
a1 = account.get_attribute_history('closePrice', longest_history)[stock]-account.get_attribute_history('lowPrice', longest_history)[stock]
b1 = account.get_attribute_history('closePrice', longest_history)[stock]-account.get_attribute_history('highPrice',longest_history)[stock]
c1 = account.get_attribute_history('highPrice',longest_history)[stock]-account.get_attribute_history('lowPrice',longest_history)[stock]
d1 = account.get_attribute_history('turnoverVol', longest_history)[stock]
adl = ((((a1)-(b1))/(c1)))*d1
a2 = account.get_attribute_history('closePrice', short_history)[stock]-account.get_attribute_history('lowPrice', short_history)[stock]
b2 = account.get_attribute_history('closePrice', short_history)[stock]-account.get_attribute_history('highPrice',short_history)[stock]
c2 = account.get_attribute_history('highPrice',short_history)[stock]-account.get_attribute_history('lowPrice',short_history)[stock]
d2 = account.get_attribute_history('turnoverVol', short_history)[stock]
ads = ((((a2)-(b2))/(c2)))*d2
mean_cp1 = adl.mean()
mean_cp2 = ads.mean()
flag = mean_cp1 - mean_cp2
if flag > 0 and account.postion_size_hold[stock]<N:
order_to(stock, capital_base/pos_pieces/cnt_price/N)
account.postion_size_hold[stock] += 1
elif flag < 0 :
order_to(stock, 0)
account.postion_size_hold[stock] = 0