我想对交易策略进行回测。它相对简单。只需以起始价格购买股票。立即在上方的退出差价处设置卖单,在下方的进入差价处设置买入单。我希望它一直持续到最大开放批次次数。我已经设法在下面编写代码。订单是地方,但没有执行。我正在使用 backtesting.py 库 https://kernc.github.io/backtesting.py/doc/backtesting/index.html 示例:开始 = 125 订单应放置在买入 124,卖出 126..买入 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```