0

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```
mece1390
  • 161
  • 2
  • 13
  • Are you sure the input data from yfinance and trading-view are the same? (i.e. the same time increments and the same durations) – itprorh66 Jul 04 '23 at 13:54
  • I could not check all the data in two sides. But random check says it's the same. – mece1390 Jul 04 '23 at 17:16
  • If you can't be positive that the input data from both sources is the same, what makes you think there is a problem with your code. First step in diagnosing a problem of this type is verifying an error occurs when the data is the same. – itprorh66 Jul 05 '23 at 15:58

0 Answers0