Hi
I am trying to write an indicator which looks back 10 bars (for example) for the first condition.
e.g. variable A = 7
I then want it to look for a secondary condition from that bar onwards but within the original lookback period of 10 bars.
So, let’s say eight bars back A = 7 and five bars back A = 6, I want to be able to highlight both conditions with an arrow of a different colour, for testing purposes.
However, I’m struggling with the code and would be grateful if someone could point me in the right direction. I think I need a second FOR loop maybe?
This is what I have so far.
Lookback = 10
FOR i = 0 TO lookback DO
IF A[i] = 7 THEN
DRAWARROWDOWN(Barindex[i],High[i]) COLOURED(255,0,0)
IF i > 0 AND i < lookback AND A[i] =< 6 THEN
DRAWARROWDOWN(Barindex[i],High[i]) COLOURED(0,0,255)
ENDIF
ENDIF
NEXT
Thanks
Rob
Perhaps something like this (not tested):
Lookback = 10
flag = 0
FOR i = 0 TO lookback DO
IF A[i] = 7 THEN
DRAWARROWDOWN(Barindex-i,High[i]) COLOURED(255,0,0)
flag = 1
for b = i to lookback
IF A[b] =< 6 THEN
DRAWARROWDOWN(Barindex-i+b,High[i+b]) COLOURED(0,0,255)
break
ENDIF
next
ENDIF
if flag then
break
endif
NEXT
Thanks Vonasi, I will play around with this and see if I can get there!
I edited line 10 in my post as it should have been High[i+b].
I still can’t quite get there, but using flags to debug has helped.
It seems to be the second FOR loop I’m struggling with, it seems to kick in at the same time as the first FOR loop.
I have updated the code so this could be used with any indicator, I think. In this case I have used Momentum as an example only.
I don’t just want to see if Mom[5] < Mom[10] if you see what I mean, as I need to use variable lookback periods and possibly test more complicated conditions.
Here is where I am at the moment, and should allow for testing.
It seems Flagk activates as soon as Flagj does and I only want it to activate after the first condition has been met but in the same lookback period.
Thanks again, Rob.
A = Momentum[12]
Lookback = 10
Flagj = 0
Flagk = 0
FOR j = 0 TO lookback DO
IF A[j] >= 30 THEN
TestAj = A[j]
//DRAWARROWDOWN(Barindex-j,High[j]) COLOURED(255,0,0)
Flagj = 1
FOR k = j TO Lookback
IF (A[k]) > 2 AND (A[k] < 23) THEN
TestAk = A[k]
Flagk = 1
//DRAWARROWDOWN(Barindex-k,High[k]) COLOURED(0,0,255)
BREAK
ENDIF
NEXT
ENDIF
IF Flagj THEN
BREAK
ENDIF
NEXT
RETURN A as "A", TestAj as "Test A j", Flagj as "Flag j", TestAk as "Test A k", Flagk as "Flag k"
Try something like this:
A = momentum[12]
Lookback = 10
Flagj = 0
Flagk = 0
FOR j = 0 TO lookback DO
IF A[j] >= 30 THEN
//DRAWARROWDOWN(Barindex-j,High[j]) COLOURED(255,0,0)
Flag = 1
break
ENDIF
NEXT
if flag = 1 then
FOR k = j+1 TO Lookback
IF (A[k]) > 2 AND (A[k] < 23) THEN
DRAWARROWDOWN(Barindex-k,High[k]) COLOURED(0,0,255)
BREAK
ENDIF
NEXT
endif
RETURN
Thanks Vonasi
Although it works in this situation, it’s not quite what I need yet but I will work on it.
Many thanks
Rob
Try this (not tested):
Lookback = 10
Flag1 = 0
Flag2 = 0
FOR i = 0 TO lookback DO
IF A[i] = 7 Then
Flag1 = i
For j = i DOWNTO 0
IF A[j] = 6 Then
Flag2 = j
Break
Endif
Next
Break
ENDIF
NEXT
If Flag1 AND Flag2 then
DRAWARROWDOWN(Barindex[Flag1],High[Flag1]) COLOURED(255,0,0)
DRAWARROWDOWN(Barindex[Flag2],High[Flag2]) COLOURED(0,0,255)
Endif
Hi Roberto
I’m trying to test the High of the candle after ‘
IF A
[j
] = 6 Then’ but I can’t seem to reference the candle correctly.
‘j – 1’ marks the candle but I can’t test it that way. What am I doing wrong please?
A = momentum[12]
Lookback = 10
Flag1 = 0
Flag2 = 0
FOR i = 0 TO lookback DO
IF A[i] = 7 Then
Flag1 = i
For j = i DOWNTO 0
IF A[j] = 6 Then
Flag2 = j
Break
IF High[j-1] > 8870 THEN
DRAWARROWDOWN(Barindex,High) COLOURED(0,0,0)
ENDIF
Endif
Next
Break
ENDIF
NEXT
If Flag1 AND Flag2 then
DRAWARROWDOWN(Barindex[Flag1],High[Flag1]) COLOURED(255,0,0)
DRAWARROWDOWN(Barindex[Flag2],High[Flag2]) COLOURED(0,0,255)
Endif
RETURN
Many thanks
Rob
The BREAK needs to be below the test otherwise the test will never be carried out.
Yes, I tried that but it doesn’t like the test of
‘ High[j–1]’
It fails with “A positive integer field is expected with”, I think because it doesn’t like the minus – sign in the High test.
That’s why I think I need to reference the candle after the ‘IF A[j] = 6 Then’ test another way but can’t work out how to get the candle after it.
Thanks.
Rob
Make sure line 15 doesn’t have a negative value:
IF High[max(0,j-1)] > 8870 THEN
I need another test to make sure that between condition 1 “A
[i
] = 7″ and condition 2 “A
[j
] = 6″ that there hasn’t been close above (in this case) a close since condition 1 was met.
I think I need another FOR loop rather than and combined IF statement, correct?
I’ve tried this but it doesn’t work 🙁
A = momentum[12]
Lookback = 10
Flag1 = 0
Flag2 = 0
// 1st loop tests A = 7 and logs closing level
FOR i = 0 TO lookback DO
IF A[i] = 7 Then
Flag1 = i
Closelevel = Close[i]
// 2nd loop NEEDS to test that all closes between now and A equaling 6 are less than Closelevel variable
FOR k = i DOWNTO j
IF Close[k] < Closelevel THEN
// 3rd loop to test A = 6
For j = i DOWNTO 0
IF A[j] = 6 Then
Flag2 = j
IF High[max(0,j-1)] > Closelevel THEN
Flag3 = max(0,j-1)
DRAWARROWDOWN(Barindex[Flag3],High[Flag3]) COLOURED(0,0,0)
ENDIF
Break
Endif
Next
ENDIF
NEXT
Break
ENDIF
NEXT
If Flag1 AND Flag2 then
DRAWARROWDOWN(Barindex[Flag1],High[Flag1]) COLOURED(255,0,0)
DRAWARROWDOWN(Barindex[Flag2],High[Flag2]) COLOURED(0,0,255)
Endif
RETURN
Thanks again, Rob
At line 16 J has no value.
My first post was about a hypothetical use, without indicators.
You have added the MOMENTUM indicator, can you explain what exactly you want to detect.