For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
How to use cancel order?
-
Hello,
I'm tryng to write my first strategy and i have problems regard cancel order.
With first trigger there will create 3, buy market and two sell order (TP and SL)
with the second trigger (crossover close on relative_max) i would to cancel them and update them with new level."import backtrader as bt
from backtrader import CommInfoBaseif name == 'main':
class RippleBoing(bt.Strategy): params = ( ('minimum_bars', 4), ('offset_TP', 2), ('offset_SL', 1), ('Lenght_BB', 20), ) def __init__(self): self.bollinger = bt.indicators.BollingerBands(period=self.params.Lenght_BB, devfactor=2) self.fascia_inferiore = self.bollinger.bot self.counter = 0 # condizioni self.Crossunder = bt.ind.CrossDown(self.datas[0].close, self.fascia_inferiore) def next(self): if self.datas[0].close[0] < self.datas[0].open[0]: self.counter += 1 else: self.counter = 0 if self.Crossunder and self.counter >= self.params.minimum_bars: relative_max = self.datas[0].high[-self.counter] excursion = round((relative_max - self.datas[0].close), 5) stop_loss = round((self.datas[0].close - (excursion * self.params.offset_SL)), 5) take_profit = round((self.datas[0].close + (excursion * self.params.offset_TP)), 5) self.buy(exectype=bt.Order.Market) tp = ordine_tp = self.sell(exectype=bt.Order.Limit, price=take_profit) sl = ordine_sl = self.sell(exectype=bt.Order.Stop, price=stop_loss) if bt.ind.CrossUp(self.datas[0].close[0], relative_max): self.broker.cancel(tp) self.broker.cancel(sl)
returns:
UnboundLocalError: local variable 'relative_max' referenced before assignmentwhat i'm doing wrong?
Thank!!