Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Rumen Nenov
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 3
    • Posts 6
    • Best 0
    • Groups 0

    Rumen Nenov

    @Rumen Nenov

    0
    Reputation
    33
    Profile views
    6
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Rumen Nenov Unfollow Follow

    Latest posts made by Rumen Nenov

    • Cancelling Bracket order following closing of the main side

      For while I've been trying to solve following problem: When I place bracket order with stop and limit, and if later the trade is closed not by stop or limit orders, but by the strategy logic, they (the children orders - Stop and Limit) remain active and one of them gets executed later. To avoid this I need to cancel it by using the order reference and call self.cancel(sop_order) or similar.

      My logic suggests that to call it I need to use something like if trade.isclosed: or maybe if self.position.size == 0: and then call the order by its id and this is where I get super confused.

      I wrote about this previously but couldn't find a solution since.

      Any ideas how to implement this part the code?

      Thanks,
      Rumen

      posted in General Code/Help
      Rumen Nenov
      Rumen Nenov
    • RE: Bracket order only with Stop

      @backtrader thank you for the guidance. I read the documentation but I struggle to implement it.

      Why it seems pointless and illogical? In fact many brokers give you a choice what to include in the bracket order (stop only, limit only, or stop/limit). What is wrong in having a stop to prevent you from excessive loss while working your own logic about the closing conditions.

      posted in General Code/Help
      Rumen Nenov
      Rumen Nenov
    • RE: Multiple Timeframes with Multiple Datafeeds

      Solved!

      def next(self):
      
              for i, (d, d1) in enumerate(zip(self.m5_data, self.d1_data)):
                  dt, (dn, d1n) = self.datetime.datetime(), (d._name, d1._name)
      
      posted in General Code/Help
      Rumen Nenov
      Rumen Nenov
    • RE: Multiple Timeframes with Multiple Datafeeds

      Great! thanks very much!

      I simply created 2 new lists inside init

      self.m5_data = self.datas[::2]
      self.d1_data = self.datas[1::2]
      
      self.inds = dict()
      for i, d in enumerate(self.m5_data):
            self.inds[d] = dict()
            self.inds[d]['atr'] = bt.indicators.ATR(d, period=self.params.atr_per)
            self.inds[d]['stoch'] = bt.indicators.Stochastic(d, period=self.params.stoch_per,
                                          period_dslow=self.params.dslow_per)
      self.daily_inds = dict()
      for i, d in enumerate(self.d1_data):
            self.daily_inds[d] = dict()
            self.daily_inds[d]['d_atr'] = bt.indicators.ATR(d, period=self.params.d_atr_per)
      

      Now however I've got trouble using it during next. For single time frame following code does the trick:

      def next(self):
      
              for i, d in enumerate(self.datas):
                  dt, dn = self.datetime.datetime(), d._name
                  pos = self.getposition(d).size
                  
                  if pos==0:
                         # And then follows the logic of the strategy
      

      but for multiple time frame i couldn't enumerate both lists simultaneously. I tried following

              for i, d in enumerate(zip(self.m5_data, self.d1_data)):
                  dt, dn = self.datetime.datetime(), d._name
                  pos = self.getposition(d).size
      # and also
              for i, d in enumerate(self.m5_data + self.d1_data):
                  dt, dn = self.datetime.datetime(), d._name
                  pos = self.getposition(d).size
      

      neither of them worked. Further ahead in the process I believe that time frames are matched by d._name and self.datetime.datetime, and they should not necessarily be enumerated simultaneously. What would be the solution here?

      posted in General Code/Help
      Rumen Nenov
      Rumen Nenov
    • Multiple Timeframes with Multiple Datafeeds

      When I use a single time frame I have no trouble resampling my data and implementing it into init exactly the way its shown in the BT documantation:

      def __init__(self):
      
              self.atr = bt.indicators.ATR(self.data, period=self.params.atr_per)
              self.stoch = bt.indicators.Stochastic(self.data, period=self.params.stoch_per,
                  period_dslow=self.params.dslow_per)
      
              self.daily_atr = bt.indicators.ATR(self.data1, period=self.params.datr_per)
      

      When I work with multiple data feed I use following code to add it to cerebro:

      for i in range(len(symbols)):
          dataframe = pandas.read_csv(symbols[i][0], index_col=0, 
                                      parse_dates=True, 
                                      dayfirst=True
                                      )
          data = PandasData(dataname=dataframe, timeframe=bt.TimeFrame.Minutes, compression=5)
          cerebro.adddata(data, name=symbols[i][1])
      

      and then following code to implement it into init:

       def __init__(self):
      
              self.inds = dict()
              for i, d in enumerate(self.datas):
                  self.inds[d] = dict()
                  self.inds[d]['atr'] = bt.indicators.ATR(d, period=self.params.atr_per)
                  self.inds[d]['stoch'] = bt.indicators.Stochastic(d, period=self.params.stoch_per,
                                          period_dslow=self.params.dslow_per)
      

      The code above works perfectly witha single timeframe.

      For a single datafeed and multiple timeframe, I have self.data for my intraday data and self.data1 for my daily data which I can pass to the indicators I want and from there on my strategy works as expected.

      For multiple datafeeds however, looks like backtrader includes both the 5 min and daily timeframes in same set of feeds (self.datas) and from here on I cannot pass the data I want to the relevant indicators. Not only this but the daily data also messes around with my intraday indicators.

      I tried to replicate the above code during init:

              self.daily_inds = dict()
              for i, d1 in enumerate(self.data1):
                  self.daily_inds[d1] = dict()
                  self.daily_inds[d1]['d_atr'] = bt.indicators.ATR(d1, period=5)
      

      but it simply wouldnt work.

      Is there a way to prevent daily data messing around with my intraday indicators and use it only for the calculation of the daily indicators?

      Daily ATR is important to me as it presents the average daily range for the past few days.

      thanks,
      Rum

      posted in General Code/Help
      Rumen Nenov
      Rumen Nenov
    • Bracket order only with Stop

      I've got following problem: Stop order is supposed to get cancelled automatically if Limit order is executed (also known as OCO) and position closed. In my strategy I do not use Limit order as my profitable positions are closed by the strategy logic. However, I cannot leave a position without a stop order. If I attach a stop or trailing stop and my trade is closed by the logic of the strategy, the stop order remains active and gets executed later. I need some help. I can't implement this part of the code.

      posted in General Code/Help
      Rumen Nenov
      Rumen Nenov