布林带回调系统-日内
来源:https://uqer.io/community/share/566929a4f9f06c6c8a91b6e6
import numpy as np
import pandas as pd
from pandas import DataFrame
start = '2014-01-01'
end = '2015-01-01'
benchmark = 'HS300'
universe = set_universe('HS300')
capital_base = 100000
freq = 'm'
refresh_rate = 239
period = 10
multiple=1.5
threshold=-0.1
boll=pd.DataFrame(index=universe,columns = ['mean_cp','high_channel','low_channel'])
def initialize(account):
pass
def handle_data(account):
if(account.current_minute=='09:30'):
close_prices = account.get_daily_attribute_history('closePrice', period)
for s in account.universe:
mean_cp = close_prices[s].mean()
bias = multiple*np.std(close_prices[s])
high_channel = mean_cp + bias
low_channel = mean_cp - bias
boll.at[s,'high_channel']=high_channel
boll.at[s,'low_channel']=low_channel
boll.at[s,'mean_cp']=mean_cp
elif(account.current_minute=='14:50'):
print account.current_date,",",account.valid_secpos
else:
for s in account.valid_secpos:
if account.referencePrice[s]>=boll.at[s,'mean_cp'] :
order_to(s, 0)
buylist=[]
c = account.referencePortfolioValue
for s in account.universe:
if ((account.referencePrice[s]-boll.at[s,'low_channel'])/boll.at[s,'low_channel'])<=threshold:
buylist.append(s)
if (len(buylist)==0):
return
else:
w=min(0.2,1.0/len(buylist))
for s in buylist:
p=account.referencePrice[s]*1.01
num=int(c * w / p)
order(s, num)