Lookback loop help please

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #157950 quote
    robdav
    Participant
    Veteran

    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

    #157951 quote
    Vonasi
    Moderator
    Master

    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
    robdav thanked this post
    #157972 quote
    robdav
    Participant
    Veteran

    Thanks Vonasi, I will play around with this and see if I can get there!

    #157987 quote
    Vonasi
    Moderator
    Master

    I edited line 10 in my post as it should have been High[i+b].

    robdav thanked this post
    #158048 quote
    robdav
    Participant
    Veteran

    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"
    #158055 quote
    Vonasi
    Moderator
    Master

    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
    robdav thanked this post
    #158065 quote
    robdav
    Participant
    Veteran

    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

    #158071 quote
    robertogozzi
    Moderator
    Master

    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
    robdav and ZeroCafeine thanked this post
    #158079 quote
    robdav
    Participant
    Veteran

    Thanks Roberto

    That looks great!

    Cheers

    Rob

    robertogozzi thanked this post
    #159799 quote
    robdav
    Participant
    Veteran
    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
    #159806 quote
    Vonasi
    Moderator
    Master
    The BREAK needs to be below the test otherwise the test will never be carried out.
    robertogozzi thanked this post
    #159811 quote
    robdav
    Participant
    Veteran
    Yes, I tried that but it doesn’t like the test of ‘ High[j1]’ 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
    #159814 quote
    robertogozzi
    Moderator
    Master
    Make sure line 15 doesn’t have a negative value:
    IF High[max(0,j-1)] > 8870 THEN
    robdav thanked this post
    #159843 quote
    robdav
    Participant
    Veteran
    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
    #159851 quote
    robertogozzi
    Moderator
    Master
    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.
Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.

Lookback loop help please


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
robdav @robdav Participant
Summary

This topic contains 16 replies,
has 3 voices, and was last updated by robertogozzi
5 years ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 01/15/2021
Status: Active
Attachments: No files
Logo Logo
Loading...