Skip to content

Is the backtesting shorting trades? #592

@brianferrell787

Description

@brianferrell787

Expected Behavior

Hello I was wondering why the backtesting code shorts a trade before I even buy it, When I look at stats._trades it should look like this:
Size EntryBar ... ExitTime Duration
0 28 21 ... 2021-06-04 12:00:00 1 days 22:00:00
1 -28 28 ... 2021-06-16 16:00:00 12 days 04:00:00
3 46 95 ... 2021-07-02 12:00:00 3 days 18:00:00
4 -46 108 ... 2021-07-08 12:00:00 6 days 00:00:00

Where every order has a following selling order.

Actual Behavior

This is what it actually does:
Size EntryBar ... ExitTime Duration
0 28 21 ... 2021-06-04 12:00:00 1 days 22:00:00
1 -28 28 ... 2021-06-16 16:00:00 12 days 04:00:00
2 -37 62 ... 2021-06-28 18:00:00 12 days 02:00:00
3 46 95 ... 2021-07-02 12:00:00 3 days 18:00:00
4 -46 108 ... 2021-07-08 12:00:00 6 days 00:00:00
5 -46 120 ... 2021-07-19 12:00:00 11 days 00:00:00
6 -67 148 ... 2021-07-22 14:00:00 3 days 02:00:00

Where it sells before it even buys. I have tried every combination of using self.sell(), self.buy(), and self.position.close and I can get it to where it looks like this:
stats._trades
Size EntryBar ... ExitTime Duration
0 28 21 ... 2021-06-04 12:00:00 1 days 22:00:00
1 36 95 ... 2021-07-02 12:00:00 3 days 18:00:00
2 40 161 ... 2021-07-30 12:00:00 7 days 22:00:00
3 28 245 ... 2021-08-24 18:00:00 4 days 04:00:00
4 27 265 ... 2021-09-07 14:00:00 11 days 00:00:00
5 27 314 ... 2021-09-20 12:00:00 4 days 20:00:00
6 26 361 ... 2021-10-08 12:00:00 6 days 22:00:00

But it does not show that it sells obviously. Code to get this looks like this:

class strat_2hr_macd(Strategy):
'''this strategy does the ema crossover on 2hr chart with MACD'''
n1 = 9
n2 = 20

def init(self):
    # Precompute the two moving averages
    self.ema1 = self.I(EMA, self.data.Close, self.n1)
    self.ema2 = self.I(EMA, self.data.Close, self.n2)
    self.macd, self.macd_signal = self.I(MACD_Cross, self.data.Close)

def next(self):
    # If ema1 crosses above ema2, close any existing
    # short trades, and buy the asset
    if crossover(self.ema1, self.ema2):
        self.position.close()
        self.buy()

    # Else, if sma1 crosses below sma2, close any existing
    # long trades, and sell the asset
    elif crossover(self.macd_signal, self.macd):
        self.position.close()

Additional info

main code:
class strat_2hr_macd(Strategy):
'''this strategy does the ema crossover on 2hr chart with MACD'''
n1 = 9
n2 = 20

def init(self):
    # Precompute the two moving averages
    self.ema1 = self.I(EMA, self.data.Close, self.n1)
    self.ema2 = self.I(EMA, self.data.Close, self.n2)
    self.macd, self.macd_signal = self.I(MACD_Cross, self.data.Close)

def next(self):
    # If ema1 crosses above ema2, close any existing
    # short trades, and buy the asset
    if crossover(self.ema1, self.ema2):
        self.position.close()
        self.buy()

    # Else, if sma1 crosses below sma2, close any existing
    # long trades, and sell the asset
    elif crossover(self.macd_signal, self.macd):
        self.sell()
        self.position.close()

from backtesting import Backtest

bt = Backtest(prices, strat_2hr_macd, cash=800, commission=0.0, trade_on_close=True,exclusive_orders=True)
stats = bt.run()
print(stats)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions