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



  • 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?



  • @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
    

  • administrators

    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

Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.