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/

    Consecutive Sell/Buy Orders in Multi Asset Strategy.

    Indicators/Strategies/Analyzers
    orders sizer cheat on open
    1
    1
    127
    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.
    • E
      elektor last edited by

      Hi,

      I am trying to back test a 2 (or more) asset strategy which require selling one fully and buying other asset. I am doing this with cheat on open using next_open.

       def next_open(self):
              DEBUG_EN = 0;
              # Exit positions if any
              for i, d in enumerate(self.datas):
                  dt, dn = self.datetime.date(), d._name
                  pos = self.getposition(d).size
                  if re.search(self.trade,dn):
                      pass
                  else:
                      if pos:
                          self.close(data=d)
                          print(f"DEBUG :: NEXT OPEN :: {dt} Closing Position of {dn}")
                      else:
                          pass       
              # Buy positions if any
              for i, d in enumerate(self.datas):
                  dt, dn = self.datetime.date(), d._name
                  pos = self.getposition(d).size
                  if re.search(self.trade,dn):
                      if pos:
                          # Position already exists so no need to do anything
                          pass
                      else:
                          self.buy(data=d)
                          #cash = round(self.broker.getcash(),2)
                          #no_of_shares = self.getsizing()
                          print(f"DEBUG :: NEXT OPEN :: {dt} Opening Position of {dn}")
                  else:
                      # Do nothing as trade signal is to buy {dn}
                      pass
              
                      
      

      What I am observing is that sizer is calculating the position as per leftover cash in folio without considering the cash outcome of sell transaction. I am using slight modified version of percent sizer worth 100% cash, modification to change open instead of close in next open. How can I make sizer take care of the cash generated from previous sell. Following are the debug messages I am getting from above code:

      DEBUG :: NEXT OPEN :: 2008-01-25 Closing Position of asset1
      DEBUG GETSIZING ::: Triggered asset2
      DEBUG :: NEXT OPEN :: 2008-01-25 Opening Position of asset2
      get_order_type :: Sell Order is Submitted
      get_order_type :: Buy Order is Submitted
      get_order_type :: Sell Order is Accepted
      get_order_type :: Buy Order is Accepted
      get_order_type :: Sell Order is Completed
      get_order_type :: Cash Status : 102037.1
      Notify Msg : Sell executed at price 53.9 on 2008-02-01. Total Value 99950.4
      get_order_type :: Buy Order is Completed
      get_order_type :: Cash Status : 102037.1
      Notify Msg : Buy executed at price 11.3 on 2008-02-01. Total Value 45.2
      

      Thanks and Regards
      Ashutosh Ojha

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