From my research online, I understand that binance doesn't provide an option to place bracket orders directly, so, inspite of being able to use backet orders while backtesting using backtrader, it's difficult to deploy the same backtrader code into the live markets. Luckily, binance supports OCO(One cancels the other) orders which I could use to place a target and stoploss once I enter into a position. So, I am looking for some thoughts on how I could manually create bracket orders in the strategy class.
class Strategy(bt.Strategy):
def __init__(self):
self.close = self.datas[0].close
.. ..... ....
def next(self):
if not self.order and not self.position:
Print("Case 1 - Place order if strategy conditions are met")
if not self.order and self.position:
Print("Case 2 - Send the stoploss and target orders")
if self.order and not self.position:
Print("Case 3 - Check if the order is still valid based on the strategy being applied on fresh data, if not, cancel the existing entry order")
if self.order and self.position: -
Print("Case 4 - Sit back and relax!")
So, all the print statements are for reference and I want to implement the idea of the respective comments. When I have a position and no orders, that means I need to quickly send a target and stoploss order using the OCO order type(case 2) but the issue is my stoploss/target values are already decided when I place an entry order(case 1) and I just can't figure out where to store these stoploss/target values before the backtrader enters the case 2(to create an OCO order) in the next iteration after the order is filled into a position. Is there a way I could store these value somewhere?
Also, I would appreciate if there are any alternative strategies that I could to create the bracket order behaviour?
And, lastly, a fundamental question wrt live markets, let's say, I am working on 15 min candles, I believe the backtrader will only work once in 15 mins and remains quiet the rest of the time, basically whenever new data is available it triggers the next function right? If yes, I believe my approach to create the bracket order is doomed because if I wait for 15 mins to place the OCO order(stoploss and target orders) after my entry order turns into a position, I might have a naked position in the market for 15 mins in a worst case scenario and that's not an outcome I could take a risk on!
I hope someone could throw some thoughts on my issues, I want to deploy the backtrader algo on binance real-time if that info matters. Thank you!