Skip to content

重写 rsi

来源:https://uqer.io/community/share/560dfd84f9f06c4ca82fb597

import talib as ta
import numpy as np
import pandas as pd
from pandas import DataFrame,Series

start = '2014-01-01'                        # 回测起始时间
end = '2015-01-01'                         # 回测结束时间
benchmark = 'HS300'                        # 策略参考标准
universe = set_universe('HS300')                # 证券池,支持股票和基金
capital_base = 10000000                      # 起始资金
freq = 'd'                                # 策略类型,'d'表示日间策略使用日线回测
refresh_rate = 1                           # 调仓频率,表示执行handle_data的时间间隔,由于freq = 'd',时间间隔的单位为交易日
pieces=10                                #每个标的最多买1/10

def initaialize(account):
    pass


def handle_data(account):
    prices=account.get_attribute_history('closePrice',100)
    for s in universe:
        cun_price=price[s][-1]
        cun_amount[s]=account.secpos.get(s,0)
        RSI=ta.RSI(prices[s],9)
        buy_flag=RSI[-1]>RSI[-2] and RSI[-1]>30     #计算买入条件
        sell_flag = RSI[-1]<RSI[-2] and RSI[-1]<70   #计算卖出条件
        amount_max=int((0.1*capital_base)/cun_price)  #计算单支仓位上限
        amount = min(int(2500000/cun_price),amount_max-cun-amount[s]) #计算下单量
        if buy_flag and (cun_amount[s]<amount_max):
            order(s,amount)
        elif sell_flag and (cun_amount[s]>0):
            order_to(s,0)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<mercury-input-6-72d5ab71705d> in <module>()
     61         perf = quartz.perf_parse(bt, quartz_acct)
     62     elif QUARTZ_CACHE.get('start', 0) == sim_params.first_trading_day and          QUARTZ_CACHE.get('end', 0) == sim_params.last_trading_day and          QUARTZ_CACHE.get('benchmark', 0) == benchmark and          QUARTZ_CACHE.get('universe', 0) == sim_params.universe:
---> 63         strategy = quartz.sim_condition.strategy.TradingStrategy(initialize, handle_data)
     64         bt, quartz_acct = quartz.quick_backtest_generator(sim_params = QUARTZ_CACHE['sim_params'], 
     65                                                           strategy = strategy,

python2.7/site-packages/quartz/sim_condition/strategy.pyc in __init__(self, initialize, handle_data)
     19     def __init__(self, initialize=None, handle_data=None):
     20         if not hasattr(initialize, '__call__'):
---> 21             raise ValueError('initialize must be a function!')
     22         else:
     23             self._initialize = initialize

ValueError: initialize must be a function!


回到顶部