Skip to content

布林带回调系统-日内

来源: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'                                 # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
refresh_rate = 239                         # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
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))# 最大仓位1/5
            for s in buylist:
                p=account.referencePrice[s]*1.01 
                num=int(c * w / p)
                order(s, num)



回到顶部