pandas backtesting.py 错误的交易条目

t8e9dugd  于 2023-05-05  发布在  其他
关注(0)|答案(1)|浏览(88)

我的程序试图用两个简单的SMA指标回测从public.bybit.com db检索的 Dataframe ,交易条目似乎悬浮在收盘价/准确的进入价格之上
代码如下:

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()

你看我的条目是如何高于或低于实际蜡烛x1c 0d1x]

e5nqia27

e5nqia271#

这是因为佣金太大。

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()

含佣金的输出=0.005:enter image description here
如果您的佣金相对于价格太高(以%计),那么您的交易头寸将相对于价格图表移动。

相关问题