Ejemplo n.º 1
0
def test():
    xdf = tstool.get_cont_data('rb1701', datetime.date(2016,1,1), datetime.date(2016,8,19), freq = '30m', nearby = 0, rollrule = '-10b')
    xdf['WPR'] = dh.WPR(xdf, 9)
    xdf["SAR"] = dh.SAR(xdf, incr = 0.01, maxaf = 0.1)
    xdf['RSI'] = dh.RSI(xdf, 14)
    xdf['MA10'] = dh.MA(xdf, 10)
    xdf['MA120'] = dh.MA(xdf, 120)
    ind_fields = [['SAR'], \
                  ['WPR', 'RSI'], \
                  ['close', 'MA10', 'MA120']]
    ind_levels = [[],\
                  [70, 30],\
                  []]
    plot_indicators(xdf, ind_fields, ind_levels)
Ejemplo n.º 2
0
def asctrend_sim(mdf, config):
    offset = config['offset']
    tcost = config['trans_cost']
    bar_func = config.get('bar_conv_func', 'dh.bar_conv_func2')
    param = config['param']
    freq = config['freq']
    close_daily = config['close_daily']
    xdf = dh.conv_ohlc_freq(mdf,
                            freq,
                            extra_cols=['contract'],
                            bar_func=eval(bar_func))
    wpr_period = param[0]
    wpr_level = param[1]
    rsi_sig = config['rsi_sig']
    wpr_sig = config.get('wpr_sig', True)
    wpr = dh.WPR(xdf, wpr_period)
    wpr_buy = 50 + wpr_level
    wpr_sell = 50 - wpr_level
    wpr_signal = pd.Series(0, index=wpr.index)
    if wpr_sig:
        wpr_signal[dh.CROSSOVER(wpr, wpr_buy, 1)] = 1
        wpr_signal[dh.CROSSOVER(wpr, wpr_sell, -1)] = -1
    else:
        wpr_signal[(wpr >= wpr_buy)] = 1
        wpr_signal[(wpr <= wpr_sell)] = -1
    xdf['wpr_signal'] = wpr_signal.shift(1).fillna(0)
    rsi_period = param[2]
    rsi_offset = param[3]
    rsi_buy = 50 + rsi_offset
    rsi_sell = 50 - rsi_offset
    rsi = dh.RSI(xdf, n=rsi_period)
    rsi_signal = pd.Series(0, index=rsi.index)
    if rsi_sig:
        rsi_signal[dh.CROSSOVER(rsi, rsi_buy, 1)] = 1
        rsi_signal[dh.CROSSOVER(rsi, rsi_sell, -1)] = -1
    else:
        rsi_signal[(rsi >= rsi_buy)] = 1
        rsi_signal[(rsi <= rsi_sell)] = -1
    xdf['rsi_signal'] = rsi_signal.shift(1).fillna(0)
    if len(param) > 4:
        sar_step = param[4]
        sar_max = param[5]
    else:
        sar_step = 0.005
        sar_max = 0.02
    sar = dh.SAR(xdf, incr=sar_step, maxaf=sar_max)
    sar_signal = pd.Series(0, index=sar.index)
    sar_signal[(sar < xdf['close'])] = 1
    sar_signal[(sar > xdf['close'])] = -1
    xdf['sar_signal'] = sar_signal.shift(1)
    xdf['sar_stop'] = sar.shift(1)
    xdf['close_ind'] = np.isnan(xdf['close'].shift(-1))
    if close_daily:
        daily_end = (xdf['date'] != xdf['date'].shift(-1))
        xdf['close_ind'] = xdf['close_ind'] | daily_end
    long_signal = pd.Series(np.nan, index=xdf.index)
    short_signal = pd.Series(np.nan, index=xdf.index)
    long_signal[(xdf['wpr_signal'] > 0) & (xdf['rsi_signal'] > 0) &
                (xdf['sar_signal'] > 0)] = 1
    long_signal[(xdf['wpr_signal'] < 0) | (xdf['rsi_signal'] < 0)] = 0
    long_signal[xdf['close_ind']] = 0
    long_signal = long_signal.fillna(method='ffill').fillna(0)
    short_signal[(xdf['wpr_signal'] < 0) & (xdf['rsi_signal'] < 0) &
                 (xdf['sar_signal'] < 0)] = -1
    short_signal[(xdf['wpr_signal'] > 0) | (xdf['rsi_signal'] > 0)] = 0
    short_signal[xdf['close_ind']] = 0
    short_signal = short_signal.fillna(method='ffill').fillna(0)
    if len(xdf[(long_signal > 0) & (short_signal < 0)]) > 0:
        print xdf[(long_signal > 0) & (short_signal < 0)]
        print "something wrong with the position as long signal and short signal happen the same time"
    xdf['pos'] = long_signal + short_signal
    xdf['cost'] = abs(xdf['pos'] - xdf['pos'].shift(1)) * (offset +
                                                           xdf['open'] * tcost)
    xdf['cost'] = xdf['cost'].fillna(0.0)
    xdf['traded_price'] = xdf.open + (xdf['pos'] -
                                      xdf['pos'].shift(1)) * offset
    closed_trades = backtest.simdf_to_trades1(xdf, slippage=offset)
    return (xdf, closed_trades)
Ejemplo n.º 3
0
def asctrend_sim(mdf, config):
    offset = config['offset']
    tcost = config['trans_cost']
    unit = config['unit']
    stoploss = config['stoploss']
    bar_func = config.get('bar_conv_func', 'dh.bar_conv_func2')
    param = config['param']
    freq = config['freq']
    pos_update = config.get('pos_update', False)
    pos_class = config['pos_class']
    pos_args = config['pos_args']
    close_daily = config['close_daily']
    xdf = dh.conv_ohlc_freq(mdf,
                            freq,
                            extra_cols=['contract'],
                            bar_func=eval(bar_func))
    asc_period = param[0]
    asc_risk = param[1]
    rsi_sig = config['rsi_sig']
    asctrend = dh.ASCTREND(xdf, asc_period, risk=asc_risk)
    xdf['asc_signal'] = asctrend["ASCSIG_%s" % str(asc_period)].shift(1)
    xdf['asc_stop'] = asctrend["ASCSTOP_%s" % str(asc_period)].shift(1)
    rsi_period = param[2]
    rsi_offset = param[3]
    rsi_buy = 50 + rsi_offset
    rsi_sell = 50 - rsi_offset
    rsi = dh.RSI(xdf, n=rsi_period)
    rsi_signal = pd.Series(0, index=rsi.index)
    if rsi_sig:
        rsi_signal[(rsi >= rsi_buy) & (rsi.shift(1) < rsi_buy)] = 1
        rsi_signal[(rsi <= rsi_sell) & (rsi.shift(1) > rsi_sell)] = -1
    else:
        rsi_signal[(rsi >= rsi_buy)] = 1
        rsi_signal[(rsi <= rsi_sell)] = -1
    xdf['rsi_signal'] = rsi_signal.shift(1)
    if len(param) > 4:
        sar_step = param[4]
        sar_max = param[5]
    else:
        sar_step = 0.005
        sar_max = 0.02
    sar = dh.SAR(xdf, incr=sar_step, maxaf=sar_max)
    sar_signal = pd.Series(0, index=sar.index)
    sar_signal[(sar < xdf['close'])] = 1
    sar_signal[(sar > xdf['close'])] = -1
    xdf['sar_signal'] = sar_signal.shift(1)
    xdf['sar_stop'] = sar.shift(1)
    xdf['close_ind'] = np.isnan(xdf['close'].shift(-1))
    if close_daily:
        daily_end = (xdf['date'] != xdf['date'].shift(-1))
        xdf['close_ind'] = xdf['close_ind'] | daily_end
    xdf['pos'] = 0
    xdf['cost'] = 0
    xdf['traded_price'] = xdf.open
    curr_pos = []
    closed_trades = []
    tradeid = 0
    for idx, dd in enumerate(xdf.index):
        mslice = xdf.loc[dd]
        if len(curr_pos) == 0:
            pos = 0
        else:
            pos = curr_pos[0].pos
        xdf.set_value(dd, 'pos', pos)
        if np.isnan(mslice.sar_stop) or np.isnan(mslice.asc_stop):
            continue
        buy_trig = (mslice.asc_signal > 0) and (mslice.rsi_signal >
                                                0) and (mslice.sar_signal > 0)
        sell_trig = (mslice.asc_signal < 0) and (mslice.rsi_signal <
                                                 0) and (mslice.sar_signal < 0)
        buy_close = (mslice.asc_signal < 0) or (mslice.rsi_signal < 0)
        sell_close = (mslice.asc_signal > 0) or (mslice.rsi_signal > 0)
        if mslice.close_ind:
            if (pos != 0):
                curr_pos[0].close(mslice.open - misc.sign(pos) * offset, dd)
                tradeid += 1
                curr_pos[0].exit_tradeid = tradeid
                closed_trades.append(curr_pos[0])
                curr_pos = []
                xdf.set_value(
                    dd, 'cost',
                    xdf.at[dd, 'cost'] - abs(pos) * (mslice.open * tcost))
                xdf.set_value(dd, 'traded_price',
                              mslice.open - misc.sign(pos) * offset)
                pos = 0
        else:
            if pos_update and pos != 0:
                curr_pos[0].update_price(mslice.asc_stop)
                pos_close = curr_pos[0].check_exit(mslice.open, 0)
            else:
                pos_close = False
            if (buy_close and (pos > 0)) or (sell_close and
                                             (pos < 0)) or pos_close:
                curr_pos[0].close(mslice.open - misc.sign(pos) * offset, dd)
                tradeid += 1
                curr_pos[0].exit_tradeid = tradeid
                closed_trades.append(curr_pos[0])
                curr_pos = []
                xdf.set_value(
                    dd, 'cost',
                    xdf.at[dd, 'cost'] - abs(pos) * (mslice.open * tcost))
                xdf.set_value(dd, 'traded_price',
                              mslice.open - misc.sign(pos) * offset)
                pos = 0
            if (buy_trig or sell_trig) and (pos == 0):
                target_pos = buy_trig * unit - sell_trig * unit
                new_pos = pos_class([mslice.contract], [1], target_pos,
                                    mslice.open, mslice.asc_stop, **pos_args)
                tradeid += 1
                new_pos.entry_tradeid = tradeid
                new_pos.open(mslice.open + misc.sign(target_pos) * offset, dd)
                curr_pos.append(new_pos)
                pos = target_pos
                xdf.set_value(
                    dd, 'cost', xdf.at[dd, 'cost'] - abs(target_pos) *
                    (mslice.open * tcost))
                xdf.set_value(dd, 'traded_price',
                              mslice.open + misc.sign(target_pos) * offset)
        xdf.set_value(dd, 'pos', pos)
    return (xdf, closed_trades)