求助:策略执行后没有交易数据

策略分享
标签: #<Tag:0x00007fcf65f87fa0>

(colita) #1
克隆策略
In [9]:
# 1. 策略基本参数
import numpy as np
import talib as ta

instruments = ['600031.SHA']
# 起始日期
start_date = '2014-11-01'
# 结束日期
end_date = '2016-08-08'
# 初始资金
capital_base = 100000


# 2. 选择股票:为了得到更好的性能,在这里做批量计算
history_data = D.history_data(instruments, start_date, end_date, fields=['close'])

# 3. 策略主体函数
# 初始化虚拟账户状态,只在第一个交易日运行
def initialize(context):
    # 设置手续费,买入时万3,卖出是千分之1.3,不足5元以5元计
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))

# 策略交易逻辑,每个交易日运行一次
def handle_data(context,data):
    
    for instrument in instruments:
        sid = context.symbol(instrument)
        float_data = data.history(sid, 'price', 60, '1d').values # 获取最近60日的收盘价数据
 
        nhisbar = np.array(float_data) # 转化成array格式,以便talib函数调用
       
        MA10 = ta.MA(nhisbar,10)
        MA13 = ta.MA(nhisbar,13)
        MA34 = ta.MA(nhisbar,34)
        MA55 = ta.MA(nhisbar,55)
        MA60 = ta.MA(nhisbar,60)
        
        cur_position = context.portfolio.positions[sid].amount   # 目前持仓

        #调仓:买入新的股票
        stock_market_price = data.current(context.symbol(instrument), 'price')

        if MA13[-1] > MA55[-1] and MA34[-1] > MA55[-1] and MA13[-1] > MA34[-1] and cur_position == 0 and data.can_trade(sid):
            if stock_market_price is not np.nan:
                amount = int(capital_base / stock_market_price / 100) * 100
                if amount > 100:
                    context.order(context.symbol(instrument), amount)  # 由于是希望成交amount数量,因此使用order下单接口
                
        elif MA13[-1] < MA34[-1] and data.can_trade(sid):
            if stock_market_price is not np.nan:
                context.order_target_percent(context.symbol(instrument), 0)

            
# 4. 策略回测:https://bigquant.com/docs/module_trade.html
m = M.trade.v2(
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    # 买入订单以开盘价成交
    order_price_field_buy='open',
    # 卖出订单以开盘价成交
    order_price_field_sell='open',
    capital_base=capital_base+1,
    benchmark='000001.INDX'
)
[2017-10-24 09:07:08.530836] INFO: bigquant: backtest.v7 开始运行..
[2017-10-24 09:07:08.606018] INFO: bigquant: 命中缓存
  • 收益率5.64%
  • 年化收益率3.24%
  • 基准收益率24.13%
  • 阿尔法-0.02
  • 贝塔0.26
  • 夏普比率0.01
  • 收益波动率23.45%
  • 信息比率-0.3
  • 最大回撤34.17%
[2017-10-24 09:07:17.506861] INFO: bigquant: backtest.v7 运行完成[8.986518s].

(iQuant) #2

您好,策略有一些小bug,目前已经修复,请参考:

克隆策略
In [11]:
# 1. 策略基本参数
import numpy as np
import talib as ta

instruments = ['601116.SHA']
# 起始日期
start_date = '2014-11-01'
# 结束日期
end_date = '2016-08-08'
# 初始资金
capital_base = 100000


# 2. 选择股票:为了得到更好的性能,在这里做批量计算
history_data = D.history_data(instruments, start_date, end_date, fields=['close'])

# 3. 策略主体函数
# 初始化虚拟账户状态,只在第一个交易日运行
def initialize(context):
    # 设置手续费,买入时万3,卖出是千分之1.3,不足5元以5元计
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))

# 策略交易逻辑,每个交易日运行一次
def handle_data(context,data):
    
    for instrument in instruments:
        sid = context.symbol(instrument)
        float_data = data.history(sid, 'price', 60, '1d').values # 获取最近60日的收盘价数据
 
        nhisbar = np.array(float_data) # 转化成array格式,以便talib函数调用
       
        MA10 = ta.MA(nhisbar,10)
        MA13 = ta.MA(nhisbar,13)
        MA34 = ta.MA(nhisbar,34)
        MA55 = ta.MA(nhisbar,55)
        MA60 = ta.MA(nhisbar,60)
        
        cur_position = context.portfolio.positions[sid].amount   # 目前持仓

        #调仓:买入新的股票
        if MA13[-1] > MA55[-1] and MA34[-1] > MA55[-1] and MA13[-1] > MA34[-1] and cur_position == 0:
            stock_market_price = data.current(context.symbol(instrument), 'price')
            amount = int(capital_base / stock_market_price / 100) * 100
            if amount > 100:
                context.order(context.symbol(instrument), amount)  # 由于是希望成交amount数量,因此使用order下单接口
                

        elif MA13[-1] < MA34[-1]:
            context.order_target_percent(context.symbol(instrument), 0)

            
# 4. 策略回测:https://bigquant.com/docs/module_trade.html
m = M.trade.v2(
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    # 买入订单以开盘价成交
    order_price_field_buy='open',
    # 卖出订单以开盘价成交
    order_price_field_sell='open',
    capital_base=capital_base+1,
    benchmark='000300.INDX'
)
[2017-10-23 11:37:56.433051] INFO: bigquant: backtest.v7 开始运行..
[2017-10-23 11:37:59.870059] INFO: Performance: Simulated 434 trading days out of 434.
[2017-10-23 11:37:59.871268] INFO: Performance: first open: 2014-11-03 14:30:00+00:00
[2017-10-23 11:37:59.872183] INFO: Performance: last close: 2016-08-08 19:00:00+00:00
  • 收益率6.3%
  • 年化收益率3.61%
  • 基准收益率28.94%
  • 阿尔法-0.05
  • 贝塔0.46
  • 夏普比率0.02
  • 收益波动率35.89%
  • 信息比率-0.33
  • 最大回撤44.79%
[2017-10-23 11:38:01.064830] INFO: bigquant: backtest.v7 运行完成[4.631775s].