掘金社区

策略回测问题Pinned highlighted

q***t@gmail.com 发表在掘金2 2016-03-25 14:59:49

掘金2
1732
7
0

为啥是亏损24800呢。。

评论: 7
  • "id":"f248854b-f295-11e5-b56c-00aca8a69adb"

    2016-03-25 15:00:23
  • 没看明白问题,需要给出更完整的数据。

    2016-03-29 03:24:38
  • 就是3.10模拟成交见上图,但是汇总的业绩给出的是24800,我已经给出了策略id,不知道你们能否通过id看到上面的结果?

    上面是卖开 2963,买平2933

    但你们策略显示是亏钱。

    2016-03-29 05:37:42
  • 不行,数据都在用户机器上。我们看不到数据的。回测能重现吗?如果一小段策略程序能重现这个问题,会方便我们跟进。

    2016-03-30 01:43:25
  • 就是你们git上面的例子,这里没法贴附件?

    2016-03-30 09:29:35
  • encoding: utf-8

    from gmsdk.api import StrategyBase
    from gmsdk import md
    from gmsdk.enums import *
    import arrow
    import time

    #每次开仓量
    OPEN_VOL = 1

    #最大开仓次数
    MAX_TRADING_TIMES = 1

    debug = False

    class Hans123(StrategyBase) :
    def init(self, *args, **kwargs):
    super(Hans123, self).init(*args, **kwargs)

        #是否已获取当天时间标识
        self.time_flag = False
    
        #是否已获取当天上、下轨数据
        self.data_flag = False 
        
        #持仓量
        self.long_hoding = 0;
        self.short_hoding = 0;
    
        #当天交易次数
        self.trading_times = 0;
    
        self.__get_param()
    
        self.tradedate = ''
        self.dayclose_flag = False
    
    
    def __get_param( self ):
        '''
        获取配置参数
        '''
        #交易证券代码
        self.trade_symbol = self.config.get('para', 'trade_symbol')
        pos = self.trade_symbol.find('.')
        self.exchange = self.trade_symbol[:pos]
        self.sec_id = self.trade_symbol[pos+1:]
        
        #开盘时间
        self.open_time = self.config.get( 'para', 'open_time')
        
        #hans时间
        self.hans_time = self.config.get('para', 'hans_time') 
        
        #强制平仓时间
        self.ex_time = self.config.get('para', 'ex_time')
    
    def __get_time( self, cur_utc ):
        '''
        获取当天的重要时间参数
        '''
        utc = arrow.get( cur_utc ).replace(tzinfo='local')
        cur_date = utc.format('YYYY-MM-DD')
        FMT = '%s %s'
        self.today_open_time = FMT%(cur_date, self.open_time)
        if debug:
            print('today open time: %s'%self.today_open_time)
        
        self.today_hans_time = FMT%(cur_date, self.hans_time)
        if debug:
            print('today hans time: %s'%self.today_hans_time)
    
        today_ex_time = FMT%(cur_date, self.ex_time )
        if debug:
            print('today exit time:%s'%today_ex_time)
    
        self.ex_time_utc = arrow.get(today_ex_time).replace(tzinfo='local').timestamp
        self.hans_time_utc = arrow.get(self.today_hans_time).replace(tzinfo='local').timestamp
        #qxy 2016
        #是否已获取当天上、下轨数据
        self.data_flag = False
    
        #持仓量
        self.long_hoding = 0;
        self.short_hoding = 0;
    
        #当天交易次数
        self.trading_times = 0;
        self.dayclose_flag = False
    
    def __init_band_data( self, bar_type ):
         '''
         获取上、下轨数据
         '''
         bars = self.get_bars( self.trade_symbol, bar_type, self.today_open_time, self.today_hans_time )
         close_list =[bar.close for bar in bars ]
    
         #上轨
         self.upr_band = max(close_list)
         if debug:
            print('upper band:%s'%self.upr_band)
    
         #下轨
         self.dwn_band = min(close_list)
         if debug:
            print('down band: %s'%self.dwn_band)
    
    def on_tick(self, tick):
         '''
         tick行情事件
         '''
         #获取最新价
         self.last_price = tick.last_price
    
    def on_bar(self, bar):
        '''
        bar周期数据事件
        ''' 
        #获取当天的时间参数
        if self.tradedate == '':
            self.tradedate = bar.strtime[:10]
    
        if self.tradedate != bar.strtime[:10]:
            self.tradedate = bar.strtime[:10]
            self.time_flag = False
            self.dayclose_flag = False
    
        if  self.time_flag is False :
            if self.dayclose_flag is False:
                self.__get_time( bar.utc_time )
                self.time_flag = True
    
        #计算上、下轨
        if bar.utc_time < self.ex_time_utc and bar.utc_time > self.hans_time_utc :
            if self.time_flag is True and self.data_flag is False:
                self.__init_band_data( bar.bar_type )
                self.data_flag = True
    
        #休市前强平当天仓位
        if bar.utc_time > self.ex_time_utc :
            if self.time_flag is True :
                self.time_flag = False
                self.dayclose_flag = True
    
            if self.long_hoding > 0 :
                self.close_long( self.exchange, self.sec_id, 0, self.long_hoding )
                if debug:
                    print('exit time close long: %s, vol: %s'%( self.trade_symbol, self.long_hoding ))
                self.long_hoding = 0
                
            elif self.short_hoding > 0:
                self.close_short( self.exchange, self.sec_id, 0, self.short_hoding )
                if debug:
                    print('exit time close long: %s, vol: %s'%( self.trade_symbol, self.short_hoding ))
                self.short_hoding = 0
            return 
    
        if self.trading_times >= MAX_TRADING_TIMES :
            if debug:
                print('trading times more than max trading times, stop trading')
            return
    
        #交易时间段
        if bar.utc_time > self.hans_time_utc and bar.utc_time < self.ex_time_utc :
            if bar.close > self.upr_band :
                if self.short_hoding > 0 :
                    #有空仓,先平空仓
                    self.close_short( self.exchange, self.sec_id, 0, self.short_hoding )
                    if debug:
                        print('close short: %s, vol:%s'%(self.trade_symbol, self.short_hoding))
                    self.short_hoding = 0
                
                #开多仓
                self.open_long( self.exchange, self.sec_id, 0, OPEN_VOL )  
                if debug:
                    print('open long: %s, vol:%s'%(self.trade_symbol, OPEN_VOL))
                self.long_hoding += OPEN_VOL
    
                #开仓次数+1
                self.trading_times += 1
            elif bar.close < self.dwn_band :
                if self.long_hoding > 0 :
                    #有多仓,先平多仓
                    self.close_long( self.exchange, self.sec_id, 0, self.long_hoding )
                    if debug:
                        print('close long: %s, vol:%s'%(self.trade_symbol, self.long_hoding))
                    self.long_hoding = 0
    
                #开空仓
                self.open_short( self.exchange, self.sec_id, 0,  OPEN_VOL )
                if debug:
                    print('open short: %s, vol:%s'%(self.trade_symbol, OPEN_VOL))
                self.short_hoding += OPEN_VOL
    
               #开仓次数+1
                self.trading_times += 1
    

    if name == 'main':
    hans123 = Hans123(config_file='Hans123.ini')
    ret = hans123.run()
    print(hans123.get_strerror(ret))

    2016-03-30 09:30:14
  • [strategy]
    td_addr=localhost:8001

    username=qdigest@gmail.com

    password=milanhongshu

    strategy_id=37c86aa5-f1ba-11e5-b56c-00aca8a69adb

    mode=4

    ;CFFEX.IF1604
    ;CFFEX.IF1604
    ;CFFEX.IF1604

    subscribe_symbols=CFFEX.IF1604.tick, CFFEX.IF1604.bar.60

    [backtest]
    start_time=2016-03-01 09:30:00

    end_time=2016-03-25 15:00:00

    initial_cash=2000000

    transaction_ratio=1

    commission_ratio=0.0002

    slippage_ratio=0.001

    [para]
    trade_symbol=CFFEX.IF1604
    open_time=09:30:00
    hans_time=10:30:00
    ex_time=14:55:00
    limit_times=3

    2016-03-30 09:30:34

Looks like your connection to 掘金量化社区 - 量化交易者的策略交流学习社区 was lost, please wait while we try to reconnect.