0

My program is trying to backtest a dataframe retrieved from public.bybit.com db with two simple SMAs indicators and the trades entries seem to be levitating above the close price/accurate entry price

here is the code:

import pandas as pd

from backtesting import Strategy, Backtest
from backtesting.lib import crossover
from backtesting.test import SMA

datasetURL = 'https://public.bybit.com/kline_for_metatrader4/AXSUSDT/2023/AXSUSDT_15_2023-01-01_2023-01-31.csv.gz'

class daStrategy(Strategy):
    def init(self) -> None:
        super().init()
        
        self.firstSMA = self.I(SMA, self.data['Close'], 9)
        self.secondSMA = self.I(SMA, self.data['Close'], 3)
        
    def next(self):
        if crossover(self.firstSMA, self.secondSMA):
            self.position.close()
            self.buy()
        elif crossover(self.secondSMA, self.firstSMA):
            self.position.close()
            self.sell()

data = pd.read_csv(
        datasetURL,
        names = [
            'datetime',
            'Open',
            'High',
            'Low',
            'Close',
            'Volume'
        ],
        compression = 'gzip',
        index_col = 'datetime',
        parse_dates= True
)

bt = Backtest(
    data,
    daStrategy,
    cash = 10000,
    margin = 1 / 1, #leverage of 1
    commission =0.025
)

stat = bt.run()
print(stat)
bt.plot()

You see how my entries are above or below the actual candles You see how my entries are above the actual candles]

1 Answers1

1

This is due to too big commission.

import pandas as pd

from backtesting import Strategy, Backtest
from backtesting.lib import crossover
from backtesting.test import SMA

datasetURL = 'https://public.bybit.com/kline_for_metatrader4/AXSUSDT/2023/AXSUSDT_15_2023-01-01_2023-01-31.csv.gz'

class daStrategy(Strategy):
    def init(self) -> None:
        super().init()
        
        self.firstSMA = self.I(SMA, self.data['Close'], 9)
        self.secondSMA = self.I(SMA, self.data['Close'], 3)
        
    def next(self):
        if crossover(self.firstSMA, self.secondSMA):
            self.position.close()
            self.buy()
        elif crossover(self.secondSMA, self.firstSMA):
            self.position.close()
            self.sell()

data = pd.read_csv(
        datasetURL,
        names = [
            'datetime',
            'Open',
            'High',
            'Low',
            'Close',
            'Volume'
        ],
        compression = 'gzip',
        index_col = 'datetime',
        parse_dates= True
)

bt = Backtest(
    data,
    daStrategy,
    cash = 10000,
    margin = 1 / 1, #leverage of 1
    commission =0.005
)

stat = bt.run()
print(stat)
bt.plot()

Output with commission =0.005: enter image description here

If your commission is too high in relation to the price (in %), then your position of trades will be shifted relative to the price chart.

dragondip
  • 54
  • 6