Skip to content

Just defining ema indicator changes return without any reason #674

Closed
@therexroder

Description

@therexroder

I have the weird behavior that if I add an exponential moving average in init() the return changes, although the strategy in next() is still the same.

import pandas_ta as ta

class SmaCross(Strategy):
    macd_max, macd_min = get_min_max_macd(df.Close)
    macd_long_entry = 0.9  # At what % from mac_min should entry long
    macd_short_entry = 0.1  # At what % from mac_max should entry short

    def init(self):
        close = self.data.df["Close"]

        self.macd = self.I(macd, close, )
        self.ema = self.I(ta.ema, close, 200)  # Adding or removing this line changes the return

    def next(self):
        current_price = self.data.Close[-1]
        macd_line = self.macd[0]
        signal_line = self.macd[2]
        if crossover(macd_line, signal_line):
            self.buy(sl=0.95 * current_price)
        elif crossover(signal_line, macd_line):
            self.sell(sl=1.05 * current_price)

        if macd_line[-1] >= 0:
            if self.position.is_long:
                self.position.close()
        elif macd_line[-1] <= 0:
            if self.position.is_short:
                self.position.close()

bt = Backtest(df, SmaCross,
              cash=1000000000, commission=.002,
              exclusive_orders=False, trade_on_close=True)

stats = bt.run()

print(stats)

Stats if ema is defined in next():

Start                     2022-06-24 14:00:00
End                       2022-06-27 16:45:00
Duration                      3 days 02:45:00
Exposure Time [%]                   10.666667
Equity Final [$]                1016926767.83
Equity Peak [$]                 1019500544.81
Return [%]                           1.692677
Buy & Hold Return [%]               -2.658726
Return (Ann.) [%]                   362.57613
Volatility (Ann.) [%]               54.306491
Sharpe Ratio                         6.676479
Sortino Ratio                             inf
Calmar Ratio                       980.045829
Max. Drawdown [%]                   -0.369958
Avg. Drawdown [%]                   -0.188506
Max. Drawdown Duration        0 days 01:45:00
Avg. Drawdown Duration        0 days 00:54:00
# Trades                                    4
Win Rate [%]                            100.0
Best Trade [%]                        0.87461
Worst Trade [%]                      0.136878
Avg. Trade [%]                       0.483997
Max. Trade Duration           0 days 02:45:00
Avg. Trade Duration           0 days 01:45:00
Profit Factor                             NaN
Expectancy [%]                       0.484381
SQN                                  3.001196
_strategy                            SmaCross
_equity_curve                             ...
_trades                       Size  EntryB...
dtype: object

Stats if ema is removed in next():

Start                     2022-06-24 14:00:00
End                       2022-06-27 16:45:00
Duration                      3 days 02:45:00
Exposure Time [%]                   44.666667
Equity Final [$]               1042026904.548
Equity Peak [$]                1044664202.388
Return [%]                            4.20269
Buy & Hold Return [%]               -2.658726
Return (Ann.) [%]                 3705.710087
Volatility (Ann.) [%]              433.024325
Sharpe Ratio                         8.557741
Sortino Ratio                             inf
Calmar Ratio                      4219.069447
Max. Drawdown [%]                   -0.878324
Avg. Drawdown [%]                   -0.376627
Max. Drawdown Duration        0 days 16:45:00
Avg. Drawdown Duration        0 days 02:52:00
# Trades                                   11
Win Rate [%]                        90.909091
Best Trade [%]                       1.142592
Worst Trade [%]                     -0.294099
Avg. Trade [%]                       0.398027
Max. Trade Duration           0 days 10:15:00
Avg. Trade Duration           0 days 02:48:00
Profit Factor                       15.915333
Expectancy [%]                        0.39878
SQN                                  3.238633
_strategy                            SmaCross
_equity_curve                             ...
_trades                        Size  Entry...
dtype: object

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