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/

    Question: How to decide buy and sell order for once?

    General Code/Help
    3
    3
    1068
    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.
    • Reo Inoue
      Reo Inoue last edited by backtrader

      Dear all
      I make the strategy to use RSI. I make easy example code to make sure the behavior follows.

      if self.RSI[0] < -80, I buy.
      if self.dataclose[0] > -20, I sell.
      And I check the command prompt, there are many sell order.
      2014-02-24, SELL CREATE, 14837.68
      2014-02-25, SELL EXECUTED, Price: 15002.51, Cost: 14803.64, Comm 0.00
      2014-03-06, SELL CREATE, 15134.75
      2014-03-07, SELL EXECUTED, Price: 15280.76, Cost: 14933.76, Comm 0.00
      ....
      

      I want to do buy and next time should be sell. How to do these?

      A 1 Reply Last reply Reply Quote 0
      • A
        ab_trader @Reo Inoue last edited by ab_trader

        @Reo-Inoue

        def next(self):
        
            if not self.position: # check if open position exists
        
                if self.RSI[0] < -80, I buy # open position
        
            if self.position:
        
                 if self.dataclose[0] > -20, I sell # close position
        
        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          Most assets have prices well above 0 and hence

          if self.data.close > -20
          

          will probably always evaluate to True (the asset in use here could have prices below 0 of course) and therefore it will sell

          Additionally, the RSI oscillates between 0 and 100, so this

          if self.rsi < -80
          

          is for sure never going to evaluate to True, i.e.: it will never buy

          If there is also no control of whether one is in the market, sell orders will be continuously issued.

          The code by ab_trader respects the original but controls the position but controls the position. The final outcome will probably be:

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