wrong price when cheat-on-open and resampledata at the same time
-
I need allin strategy, so i use cheat-on-open, and resample minutes/1 to minutes/60 data, when creating orders, it seems order_target_value / order_target_percent got the right open price of next minutes/60, but _execute in bbroker got open price of minutes/1, which leads to cash not enough error.
-
I really doubt that people here have mind reading skills, so posting your code and outputs which can help to identify the issue would be really helpful. In case if you really want to get some answers.
-
@ab_trader sorry for inconvenient, I add many logger to backtrader and It's not easy to provide all modify code which I dig into, but you are right, I will construct a data set and strategy to reproduce the issue later
-
@ab_trader here is code
import backtrader as bt import pandas as pd class DemoStrategy(bt.Strategy): def __init__(self): self.cheating = self.cerebro.p.cheat_on_open self.count = 0 def notify_order(self, order): if order.status == order.Margin: raise ValueError('This should not happend when cheat on open') def proccess(self): self.count += 1 if self.count % 2 == 1: self.order_target_percent(target=1) else: self.close() def next_open(self): if self.cheating: self.proccess() def next(self): if not self.cheating: self.proccess() if __name__ == '__main__': cerebro = bt.Cerebro(cheat_on_open=True) # Add a strategy cerebro.addstrategy(DemoStrategy) # Create a Data Feed df = pd.read_csv('./data.csv', parse_dates=['time']) data = bt.feeds.PandasData(dataname=df, timeframe=bt.TimeFrame.Minutes, datetime='time', open='open', high='high', low='low', close='close', volume='volume', openinterest=-1) # cerebro.adddata(data) cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2) # Set our desired cash start cerebro.broker.setcash(100000.0) # Run over everything results = cerebro.run()
here is csv data
time,open,high,low,close,volume 2019-09-01 00:00:00,21.3288,21.3438,21.3214,21.3437,659.98 2019-09-01 00:01:00,21.3355,21.3582,21.3076,21.3131,334.44 2019-09-01 00:02:00,21.3132,21.3133,21.2553,21.2748,965.16 2019-09-01 00:03:00,21.2699,21.2842,21.2676,21.2733,559.16 2019-09-01 00:04:00,21.2693,21.2693,21.2569,21.2629,327.58 2019-09-01 00:05:00,21.2629,21.2817,21.2605,21.27,320.12 2019-09-01 00:06:00,21.2635,21.29,21.2605,21.2796,765.25 2019-09-01 00:07:00,21.2829,21.2919,21.2605,21.2769,165.63
after more research, I found the key is order_target_percent default use close value instead of open value under cheat-on-open condition.
-
Problem 1
All-In
is not the new black.
I am still trying to find out where is the book (or the cryptocurrency guru) that is spreading the word on
All-In
@Jingsi said in wrong price when cheat-on-open and resampledata at the same time:
after more research, I found the key is order_target_percent default use close value instead of open value under cheat-on-open condition.
Problem 2
cheat-on-open
gives you the chance to manually specify things. It's not a magic tool.From: Docs - Strategy - order_target_percent - https://www.backtrader.com/docu/strategy/
It uses order_target_value to execute the order.
From the same documentation page
order_target_value(data=None, target=0.0, price=None, **kwargs)
which means you can specify the
price
at which the execution (and hence the calculations have to take place).After which things can still fail ... for obvious things like floating point precision.
-
thanks @backtrader I just use all-in to verify some new idea, It's not been tuned yet.
and seems I misunderstood the cheat-on-open option, which is not fully supported over all order related function,
thanks for the answer anyway, I will turn to order_target_value
-
@Jingsi said in wrong price when cheat-on-open and resampledata at the same time:
just use all-in to verify some new idea, It's not been tuned yet.
More to the point. All-in is not the new black and it is also for sure not the stake to test some new idea.
@Jingsi said in wrong price when cheat-on-open and resampledata at the same time:
and seems I misunderstood the cheat-on-open option, which is not fully supported over all order related function,
You still misunderstand it. It is NOT supported in any "order related function".
It gives you the chance to do things by having a peek at the prices before things (like indicators for example) are calculated and before the broker executes anything.
But you have to do the things.
@Jingsi said in wrong price when cheat-on-open and resampledata at the same time:
I will turn to order_target_value
You can use
order_target_percent
and pass aprice
argument. That's the point of indicating the chain of command in the documentation. -
@backtrader Thank you, I've learn a lot today.