Trying to get the algorithm to wait for a crossover between the two moving averages to occur, and then checks the RSI value in the next 5 candles. If the RSI is below 30 in one of those candles, the algorithm generates a buy signal. This way, the algorithm waits for confirmation of the oversold condition before entering a trade. This code do not work it trades on the crossover but the crossover should only indicate the starting point from where it should look at the RSI
// Define the lengths of the moving averages and RSI indicator
maLength1 = 20
maLength2 = 30
rsiLength = 14
// Calculate the moving averages and RSI value
ma1 = Average[maLength1](close)
ma2 = Average[maLength2](close)
nrsi = rsi[rsiLength](close)
// Identify the cross over using the moving averages
crossover = 0
if ma1 > ma2 and ma1[1] < ma2[1] Then
crossover = 1 // moving average crossover occurred
Endif
// Use the cross over signal and RSI value to determine the entry price
direction = 0
if crossover = 1 Then
for i = 0 to 4
if nrsi[i] < 30 Then
direction = 1 // buy
Endif
NEXT
Endif
If direction = 1 Then
Buy 1 contract at market
Endif
// Stops and targets
SET STOP pLOSS 100
SET TARGET pPROFIT 100
JSParticipant
Senior
Hi @johann,
Try replacing the For…Next with:
Direction=0
If CrossOver=1 and nrsi[0]<30 or nrsi[1]<30 or nrsi[2]<30 or nrsi[3]<30 or nrsi[4]<30 then
Direction=1
EndIf
Hi JS, nope unfortunately it does not work. The idea is that the algo should look at the next 5 bars whether the RSI is in oversold territory just after the cross over up of the 20SMA and 30SMA. The trade has to take place within 5 bars after the cross over. If the RSI does not go into oversold territory within the next 5 bars the trade aborts and wait for the next cross over.
Here is a new attempt, store the barindex of the event, then check bars elasped since. Not tested.
// Define the lengths of the moving averages and RSI indicator
maLength1 = 20
maLength2 = 30
rsiLength = 14
// Calculate the moving averages and RSI value
ma1 = Average[maLength1](close)
ma2 = Average[maLength2](close)
nrsi = rsi[rsiLength](close)
// Identify the cross over using the moving averages
if ma1 > ma2 and ma1[1] < ma2[1] Then
crossover = barindex // moving average crossover occurred = store the barindex
Endif
// Use the cross over signal and RSI value to determine the entry price
if barindex-crossover <= 5 and crossover>0 Then //crossover occured less than 5 bars ago
if nrsi < 30 Then
Buy 1 contract at market
crossover = 0 //reset and prevent next order with same crossover
Endif
endif
// Stops and targets
SET STOP pLOSS 100
SET TARGET pPROFIT 100
Thank you Nicolas will try your suggestion…