Lookback loop help please
Forums › ProRealTime English forum › ProBuilder support › Lookback loop help please
- This topic has 16 replies, 3 voices, and was last updated 3 years ago by robertogozzi.
-
-
01/15/2021 at 4:03 PM #157950
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.
12345678910Lookback = 10FOR i = 0 TO lookback DOIF A[i] = 7 THENDRAWARROWDOWN(Barindex[i],High[i]) COLOURED(255,0,0)IF i > 0 AND i < lookback AND A[i] =< 6 THENDRAWARROWDOWN(Barindex[i],High[i]) COLOURED(0,0,255)ENDIFENDIFNEXTThanks
Rob
01/15/2021 at 4:15 PM #157951Perhaps something like this (not tested):
123456789101112131415161718Lookback = 10flag = 0FOR i = 0 TO lookback DOIF A[i] = 7 THENDRAWARROWDOWN(Barindex-i,High[i]) COLOURED(255,0,0)flag = 1for b = i to lookbackIF A[b] =< 6 THENDRAWARROWDOWN(Barindex-i+b,High[i+b]) COLOURED(0,0,255)breakENDIFnextENDIFif flag thenbreakendifNEXT1 user thanked author for this post.
01/15/2021 at 6:38 PM #15797201/15/2021 at 8:09 PM #15798701/16/2021 at 11:50 AM #158048I 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.
123456789101112131415161718192021222324252627A = Momentum[12]Lookback = 10Flagj = 0Flagk = 0FOR j = 0 TO lookback DOIF A[j] >= 30 THENTestAj = A[j]//DRAWARROWDOWN(Barindex-j,High[j]) COLOURED(255,0,0)Flagj = 1FOR k = j TO LookbackIF (A[k]) > 2 AND (A[k] < 23) THENTestAk = A[k]Flagk = 1//DRAWARROWDOWN(Barindex-k,High[k]) COLOURED(0,0,255)BREAKENDIFNEXTENDIFIF Flagj THENBREAKENDIFNEXTRETURN A as "A", TestAj as "Test A j", Flagj as "Flag j", TestAk as "Test A k", Flagk as "Flag k"01/16/2021 at 12:31 PM #158055Try something like this:
12345678910111213141516171819202122232425A = momentum[12]Lookback = 10Flagj = 0Flagk = 0FOR j = 0 TO lookback DOIF A[j] >= 30 THEN//DRAWARROWDOWN(Barindex-j,High[j]) COLOURED(255,0,0)Flag = 1breakENDIFNEXTif flag = 1 thenFOR k = j+1 TO LookbackIF (A[k]) > 2 AND (A[k] < 23) THENDRAWARROWDOWN(Barindex-k,High[k]) COLOURED(0,0,255)BREAKENDIFNEXTendifRETURN1 user thanked author for this post.
01/16/2021 at 2:15 PM #15806501/16/2021 at 3:03 PM #158071Try this (not tested):
12345678910111213141516171819Lookback = 10Flag1 = 0Flag2 = 0FOR i = 0 TO lookback DOIF A[i] = 7 ThenFlag1 = iFor j = i DOWNTO 0IF A[j] = 6 ThenFlag2 = jBreakEndifNextBreakENDIFNEXTIf Flag1 AND Flag2 thenDRAWARROWDOWN(Barindex[Flag1],High[Flag1]) COLOURED(255,0,0)DRAWARROWDOWN(Barindex[Flag2],High[Flag2]) COLOURED(0,0,255)Endif2 users thanked author for this post.
01/16/2021 at 3:45 PM #15807901/29/2021 at 12:04 PM #159799Hi 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?
1234567891011121314151617181920212223242526272829A = momentum[12]Lookback = 10Flag1 = 0Flag2 = 0FOR i = 0 TO lookback DOIF A[i] = 7 ThenFlag1 = iFor j = i DOWNTO 0IF A[j] = 6 ThenFlag2 = jBreakIF High[j-1] > 8870 THENDRAWARROWDOWN(Barindex,High) COLOURED(0,0,0)ENDIFEndifNextBreakENDIFNEXTIf Flag1 AND Flag2 thenDRAWARROWDOWN(Barindex[Flag1],High[Flag1]) COLOURED(255,0,0)DRAWARROWDOWN(Barindex[Flag2],High[Flag2]) COLOURED(0,0,255)EndifRETURNMany thanks
Rob
01/29/2021 at 12:14 PM #159806The BREAK needs to be below the test otherwise the test will never be carried out.
1 user thanked author for this post.
01/29/2021 at 12:35 PM #159811Yes, 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
01/29/2021 at 12:42 PM #159814Make sure line 15 doesn’t have a negative value:
1IF High[max(0,j-1)] > 8870 THEN1 user thanked author for this post.
01/29/2021 at 3:39 PM #159843I 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 🙁
12345678910111213141516171819202122232425262728293031323334353637383940414243A = momentum[12]Lookback = 10Flag1 = 0Flag2 = 0// 1st loop tests A = 7 and logs closing levelFOR i = 0 TO lookback DOIF A[i] = 7 ThenFlag1 = iCloselevel = Close[i]// 2nd loop NEEDS to test that all closes between now and A equaling 6 are less than Closelevel variableFOR k = i DOWNTO jIF Close[k] < Closelevel THEN// 3rd loop to test A = 6For j = i DOWNTO 0IF A[j] = 6 ThenFlag2 = jIF High[max(0,j-1)] > Closelevel THENFlag3 = max(0,j-1)DRAWARROWDOWN(Barindex[Flag3],High[Flag3]) COLOURED(0,0,0)ENDIFBreakEndifNextENDIFNEXTBreakENDIFNEXTIf Flag1 AND Flag2 thenDRAWARROWDOWN(Barindex[Flag1],High[Flag1]) COLOURED(255,0,0)DRAWARROWDOWN(Barindex[Flag2],High[Flag2]) COLOURED(0,0,255)EndifRETURNThanks again, Rob
01/29/2021 at 4:49 PM #159851At 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.
-
AuthorPosts
Find exclusive trading pro-tools on