Question: How to decide buy and sell order for once?
Reo Inoue last edited by backtrader
I make the strategy to use RSI. I make easy example code to make sure the behavior follows.
if self.RSI < -80, I buy. if self.dataclose > -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?
ab_trader last edited by ab_trader
def next(self): if not self.position: # check if open position exists if self.RSI < -80, I buy # open position if self.position: if self.dataclose > -20, I sell # close position
Most assets have prices well above
if self.data.close > -20
will probably always evaluate to
True(the asset in use here could have prices below
0of course) and therefore it will
100, so this
if self.rsi < -80
is for sure never going to evaluate to
True, i.e.: it will never
If there is also no control of whether one is in the market,
sellorders will be continuously issued.
The code by
ab_traderrespects the original but controls the position but controls the position. The final outcome will probably be: