Using a time loop to find High or Low during defined period

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #42158 quote
    nickp104
    Participant
    New

    I would like to find out how to find a high or low within a defined area.  To test, I used the example code below but probuilder tells me there’s a syntax error in line 14 – there are only 13 lines of code.

    FOR i = 20 TO 45 DO
    P1High = high
    
    
    c1 = CLOSE > P1High
    
    
    IF C1 THEN
    BUY 1 SHARES AT MARKET
    ENDIF

    It would be great if someone could let me know what I’m missing or point me towards code that would allow me to do this…

    #42161 quote
    Nicolas
    Keymaster
    Master

    A FOR/NEXT loop is always made with 3 keywords: FOR + DO + NEXT

    The correct syntax should be:

    hh = 0
    
    FOR i = 20 TO 45 DO
     hh = max(hh,high[i]) 
    NEXT
    
    c1 = CLOSE > hh
    
    IF C1 THEN
     BUY 1 SHARES AT MARKET
    ENDIF

    But you don’t have to do a loop to find the highest high of 20 bars ago, you could also use this instruction:

    hh = highest[25](high)[20]

    it will find for you, the value of the highest high 25 periods but 20 bars ago.

    #42167 quote
    juanj
    Participant
    Master

    @Nicolas I have always used the formula you referenced to but at some point realized that for some unknown reason it doesn’t give the same result as a loop. For example:

    PSH = Highest[10](high)[1] //Previous Swing High
    PSL = Lowest[10](low)[1] //Previous Swing Low

    Should yield the same result as:

    PSH = high[1] //Previous Swing High
    PSL = low[1] //Previous Swing Low
    For i = 2 to 10 Do
    If high[i] > PSH Then
    PSH = high[i]
    ElsIf low[i] < PSL Then
    PSL = low[i]
    EndIf
    Next

    And unless I am mistaken it doesn’t?

    #42182 quote
    Nicolas
    Keymaster
    Master

    @juanj

    Because you are using “elsif”, you should make 2 different conditional statement IF/ENDIF, one for highest high and one for lowest low.

    #42282 quote
    nickp104
    Participant
    New

    Thank you all for your excellent advice … problem solved.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Using a time loop to find High or Low during defined period


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
nickp104 @nickp104 Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by nickp104
8 years, 6 months ago.

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