Back to Community
Discrepancy Between Transaction Log and Results

There is a discrepancy between the trades logged in the transaction viewer and the readout provided in the results overview. One makes money and one appears to lose money? Maybe some slippage bug is causing the issue?

Clone Algorithm
0
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import talib
def RSI(prices, timeperiod=14):
    return talib.RSI(prices, timeperiod)

# Need to use context.i to avoid data error when using backtester here...
#####should remove when place in actual trading#####

def initialize(context):
    schedule_function(start_trading, 
                      date_rule=date_rules.every_day(), time_rule=time_rules.market_open(minutes=40))
    schedule_function(end_trading, 
                      date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=40))
    context.stocks = symbol('XLF')
    set_commission(commission.PerShare(cost=0.00))
    set_slippage(slippage.FixedSlippage(0.0))
    context.current_holding = 0.0
    context.trading_active = False
    context.positions_taken = 0.0
    context.start_price = 0.0
    context.total_return = 1.0

def start_trading(context, data):
    context.trading_active = True
#     print data.history(symbol('XLF'), 'price', 32, '1m')
    
def end_trading(context, data):
    order(symbol('XLF'), -context.current_holding)
    if context.current_holding > 0.0:
        print data.history(symbol('XLF'), 'price', 40, '1m').index[-1]
        context.total_return *= 1 + (data.current(symbol('XLF'), 'price') - context.start_price)/context.start_price
    context.current_holding = 0.0
    context.start_price = 0.0
    context.trading_active = False
    
    print context.total_return
    
def handle_data(context, data):
    if context.trading_active == True:
        rsi_val = RSI(data.history(symbol('XLF'), 'price', 40, '1m'), timeperiod=30)
        if rsi_val[-1] < 30 and context.current_holding == 0.0:
            order(symbol('XLF'), 1)
            context.current_holding = 1.0
            context.positions_taken += 1.0
            print data.history(symbol('XLF'), 'price', 40, '1m').index[-1]
            context.start_price =  data.current(symbol('XLF'), 'price')
        if rsi_val[-1] >= 60.0 and context.current_holding > 0.0:
            order(symbol('XLF'), -context.current_holding)
            context.current_holding = 0.0
            context.total_return *= 1 + (data.current(symbol('XLF'), 'price') - context.start_price)/context.start_price
            context.start_price = 0.0

def trade(context,data):
    pass
    
    
There was a runtime error.
1 response

I don't think there is anyway I could be booking a $2 loss on a single share trade on XLF that the transaction log records as a $6 gain

Clone Algorithm
0
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import talib
def RSI(prices, timeperiod=14):
    return talib.RSI(prices, timeperiod)

# Need to use context.i to avoid data error when using backtester here...
#####should remove when place in actual trading#####

def initialize(context):
    schedule_function(start_trading, 
                      date_rule=date_rules.every_day(), time_rule=time_rules.market_open(minutes=40))
    schedule_function(end_trading, 
                      date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=40))
    context.stocks = symbol('XLF')
    set_commission(commission.PerShare(cost=0.0035))
    set_slippage(slippage.FixedSlippage(spread=0.00))
    context.current_holding = 0.0
    context.trading_active = False
    context.positions_taken = 0.0
    context.start_price = 0.0
    context.total_return = 1.0
    context.today_gain = 0.0

def start_trading(context, data):
    context.trading_active = True
#     print data.history(symbol('XLF'), 'price', 32, '1m')
    
def end_trading(context, data):
    order(symbol('XLF'), -context.current_holding)
    if context.current_holding > 0.0:
        print data.history(symbol('XLF'), 'price', 40, '1m').index[-1]
        context.total_return *= 1 + (data.current(symbol('XLF'), 'price') - context.start_price)/context.start_price
        context.today_gain += (data.current(symbol('XLF'), 'price') - context.start_price)
    record(TODAY_GAIN=context.today_gain)
    context.today_gain = 0.0
    context.current_holding = 0.0
    context.start_price = 0.0
    context.trading_active = False
    
    print context.total_return
    
def handle_data(context, data):
    if context.trading_active == True:
        rsi_val = RSI(data.history(symbol('XLF'), 'price', 40, '1m'), timeperiod=30)
        if rsi_val[-1] < 30 and context.current_holding == 0.0:
            order(symbol('XLF'), 1)
            context.current_holding = 1.0
            context.positions_taken += 1.0
            print data.history(symbol('XLF'), 'price', 40, '1m').index[-1]
            context.start_price =  data.current(symbol('XLF'), 'price')
        if rsi_val[-1] >= 60.0 and context.current_holding > 0.0:
            order(symbol('XLF'), -context.current_holding)
            context.current_holding = 0.0
            context.total_return *= 1 + (data.current(symbol('XLF'), 'price') - context.start_price)/context.start_price
            context.today_gain += (data.current(symbol('XLF'), 'price') - context.start_price)
            context.start_price = 0.0

def trade(context,data):
    pass
    
    
There was a runtime error.