【问题标题】:Backtesting trading strategy using backtesting.py使用 backtesting.py 回测交易策略
【发布时间】:2020-08-18 05:05:10
【问题描述】:

我想对交易策略进行回测。它相对简单。只是 以起始价格购买股票。立即在退出时设置卖单 上面的差价和下面的入场差价的买单。我想要 它会持续到最大开放批次次数。我已经设法 在下面写代码。订单是地方,但没有执行。我在用 backtesting.py 库 https://kernc.github.io/backtesting.py/doc/backtesting/index.html 示例:开始 = 125 订单应放置在买入 124,卖出 126..buy 125,卖127..买126,卖128等等。下一个函数运行 数据的每一行以及我无法设置的数据 我目前的买卖价格。请帮助任何人

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

class Scalp_buy(Strategy):

    start = 125
    lot_step = 5
    buy_criteria = 1
    sell_criteria = 1
    max_open = 10
    lot_size = 6000
    max_loss = 1000
    equity_list = []
    current_buy_order = []
    current_sell_order = []
    current_buy = start - buy_criteria
    current_sell = start + sell_criteria

    def init(self):
        super().init()
        self.current_buy = self.start - self.buy_criteria
        self.current_sell = self.start + self.sell_criteria
        self.buy(price = self.start, tp = self.current_sell)

    def next(self):
        super().next()
        for x in range(0,self.max_open): 
            self.orders.set_entry(price = self.current_buy)
            self.orders.set_tp(price = self.current_sell)
            self.current_buy  += self.buy_criteria
            self.current_sell  += self.sell_criteria

       # print(self.position.open_time,self.position.open_price,self.position.pl, self.position.pl_pct , self.position.size)


bt = Backtest(df, Scalp_buy, cash=10000, commission=.0014)

output = bt.run()
output```

【问题讨论】:

    标签: python trading back-testing


    【解决方案1】:

    根据文档 [在此处输入链接描述][1]

    如果 trade_on_close 为 True,市价单将根据当前柱的收盘价而不是下一根柱的开盘价执行。

    您应该使用带有参数 trade_on_close=True 的回测

    bt = Backtest(df, Scalp_buy, cash=10000, commission=.0014, trade_on_close=True)
    

    【讨论】:

    • 请通过添加缺少的链接来编辑您的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多