Hi, I want to be able to have my code buy/sell on a Moving average Cross but the stock must have been under 40/over 60 RSI in the last 3 candles. Any help as too how I can do this? M
defparam preloadbars = 15
defparam CUMULATEORDERS = false
indicator2 = call "example5 : RSI"[14]
brsi = (indicator2 < 40)
indicator3 = call "example5 : RSI"[14]
srsi = (indicator3 > 60)
indicator1 = Average[9](close)
once haclose=close
once haopen=open
if barindex>1 then
haclose=(open+close+low+high)/4
haopen=(haopen[1]+haclose[1])/2
endif
//buy condition
cBuy = (haclose CROSSES OVER indicator1 and brsi)
//sell condition
cSell = (haclose CROSSES UNDER indicator1 and srsi)
If cbuy then
buy 5000 CASH at market
endif
If csell then
sell at market
endif
if csell then
sellshort 5000 CASH at market
endif
if cbuy then
exitshort at market
endif
y code so far:
To achieve this, we can make a loop in the past to see if the conditions were met in the last 3 candles. We can also make summation of the result of a condition that return 1 if its met, with the SUMMATION instruction.
Please find below your strategy modified accordingly to your needs:
defparam preloadbars = 15
defparam CUMULATEORDERS = false
// RSI COUNT CONDITIONS
myRSI = RSI[14](close)
// over 60 in the last 3 periods
countS=0
countS=Summation[3](myRSI[1]>60)
// under 40 in the last 3 periods
countB=0
countB=Summation[3](myRSI[1]<40)
indicator1 = Average[9](close)
once haclose=close
once haopen=open
if barindex>1 then
haclose=(open+close+low+high)/4
haopen=(haopen[1]+haclose[1])/2
endif
//buy condition
cBuy = (haclose CROSSES OVER indicator1 and countB=3)
//sell condition
cSell = (haclose CROSSES UNDER indicator1 and countS=3)
If cbuy then
buy 5000 CASH at market
endif
If csell then
sell at market
endif
if csell then
sellshort 5000 CASH at market
endif
if cbuy then
exitshort at market
endif
Thank you very much for your reply this was very helpful 🙂