I can not simulate TradingView in backtrader: ROI mismatch for same strategy

111 Views Asked by At

I got data of 'TSLA' with yfinance library for same period of TradingView platform StrategyTester assumes. For example I have used RSI_SMA Strategy with 14 period parameters in both.

Here is the parameters:

Interval is 1 minute for both of them. Start period is 2023-06-12 for both of them. End period is now for both of them. The initial cash is 1M for both of them. RSI SMA period is 14 for both of them.

I have calculate ROI in stop method of backtrader strategy class.

After executing cerebro.run() and its plot, The ROI is different than Net profit of TradingView. In manner of Number of trades, TradingView shows so little than backtrader. The parameters is the same.

What is the reason of this mismach?

Here is backtrader strategy:

import backtrader as bt


class RSISMAStrategy(bt.Strategy):
    params = (
        ('period', 14),
        ('rsi_upper', 70),
        ('rsi_lower', 30),
    )

    def __init__(self):
        self.initial_value = self.broker.get_value()
        self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.period)
        self.cross_up = bt.indicators.CrossUp(self.rsi, self.p.rsi_upper)
        self.cross_down = bt.indicators.CrossDown(self.rsi, self.p.rsi_lower)

    def stop(self):
        self.final_value = self.broker.get_value()

        roi = (self.final_value - self.initial_value) / self.initial_value
        print(f"RSI-{self.p.period}-SMA-{self.p.period} ROI: {roi:.2%}")

    def next(self):
        if not self.position:  # No open position
            if self.buy_condition():
                self.buy()

            elif self.sell_condition():
                self.sell()

        else:  # Open position exists
            if self.sell_condition():
                self.close()  # Close long position and open short position

            elif self.buy_condition():
                self.close()  # Close short position and open long position

    def buy_condition(self):
        return self.cross_up

    def sell_condition(self):
        return self.cross_down```
0

There are 0 best solutions below