Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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?

    General Code/Help
    1
    1
    179
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 19Patrick99
      19Patrick99 last edited by

      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 CommInfoBase

      if 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 assignment

      what i'm doing wrong?

      Thank!!

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors