Navigation

    Backtrader Community

    • Register
    • 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/

    Issue with order_target on live trading

    General Code/Help
    live trading interactive brokers
    3
    7
    1200
    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.
    • J
      J. Javier Gálvez last edited by

      Hi, Backtraders!

      I have been using the function self.order_target_percent() in backtesting mode and it works perfectly. Now that I'm testing a strategy in paper trading with Interactive Brokers I'm facing an issue. The system always says that my order is "Rejected". I made a simple test to see if it was something related to my strategy, I used self.order_target_percent(target=0.1) but the problem persists. So, I dug in to know how the function makes the estimation and it seems that all is ok with the logic. The only thing I found was something wrong in line 1292 in the strategy.py that use the following value = self.broker.getvalue(datas=[data]). Thie first time that line is executed, value should be 0 as happened when you are in backtesting mode. But that doesn't happen when I switch to live mode with IB. It seems something related of how to retrieve that info from IB.

      Could it be a bug? Or I'm making something wrong? Any clue of how to fix it?

      Thanks again and happy trading!

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @J. Javier Gálvez last edited by

        The reported account value is the value of the entire account. The target orders didn't exist when IB came into existence, hence the incompatibility. It will be looked into.

        1 Reply Last reply Reply Quote 0
        • J
          J. Javier Gálvez last edited by

          The get_value() method it supposes to return the portfolio value, not the value of the entire account. But that method doesn't work properly when is connected with IB. I came up with a solution when I'm working with a single instrument. I override the order_target_value in my strategy as follow:

              def order_target_value(self, data=None, target=0.0, price=None, **kwargs):
          
                  # if isinstance(data, string_types):
                  #     data = self.getdatabyname(data)
                  # elif data is None:
                  #    data = self.data
          
                  data = self.data  # My moddification
          
                  possize = self.getposition(data, self.broker).size
          
                  if not target and possize:  # closing a position
                      return self.close(data=data, size=possize, price=price, **kwargs)
          
                  else:
                      # value = self.broker.getvalue(datas=[data])
                      value = possize * self.datas[0].close  # My modification
                      comminfo = self.broker.getcommissioninfo(data)
          
                      # Make sure a price is there
                      price = price if price is not None else data.close[0]
          
                      if target > value:
                          size = comminfo.getsize(price, target - value)
                          return self.buy(data=data, size=size, price=price, **kwargs)
          
                      elif target < value:
                          size = comminfo.getsize(price, value - target)
                          return self.sell(data=data, size=size, price=price, **kwargs)
          
                  return None  # no execution size == possize
          

          I tested it without IB and got the right result. So, I could expect the same with IB. What do you think about this solution? Is it correct?

          B 1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators @J. Javier Gálvez last edited by backtrader

            @j-javier-gálvez said in Issue with order_target on live trading:

            The get_value() method it supposes to return the portfolio value, not the value of the entire account

            The convention in backtrader is to return the sum of the value of the assets and the cash, hence the account value

            1 Reply Last reply Reply Quote 0
            • J
              J. Javier Gálvez last edited by

              But in the documentation doesn't say that and in practice in backtrader doesn' do that either. self.broker.get_value(data) it shouldn't take into account the cash, because it is the portfolio value, not the account value. Otherwise, it would not work in backtesting with the default broker.

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators last edited by

                We are talking about the specific case of the interaction of the order_target_xxx and the broker for Interactive Brokers which was developed before those family of calls existed.

                1 Reply Last reply Reply Quote 0
                • T
                  Trade Prophet last edited by

                  I have the same problem as reported in the initial post:
                  in my strategy, I call self.order_target_value(...), when debugging into that function I get to the framework call value = self.broker.getvalue(datas=[data]) , this call returns an invalid value(a very large value, close to the value of the entire portfolio), in my case, it should return 0 since the ticker does not exist in IB portfolio
                  Can you try and replicate this behavior and fix in case there is a bug?
                  Thank you

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