Count condition help

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #67507 quote
    Lemonlover
    Participant
    New

    Hi,

    I am trying to code an indicator which does a count. The count is set to zero and rises to one when high and low of a candle is less than the high and low of the previous candle. The count would be increased by one every time the high and low of a candle are lower than the high and low of the most recent candle on which the count was increased.

    The count would continue until X (to be defined).

    The count will reset to zero when a new high is formed; this means when the new high is higher than the high of the candle which preceded the count one candle.

    Any candles not adhering to the conditions above are to be ignored.

    Managed to write a code but it only looks at the previous candle rather than at the one on which the count increased, also can’t get the reset condition in. Code is below

    Decrease = High < High[1]
    Count = 0
    WHILE Low < Low[1] DO
    IF Decrease[Count] THEN
    Count = Count + 1
    CONTINUE
    ENDIF
    BREAK 
    WEND
    RETURN Count

    Could anyone please help me build up this code?

    Thanks

    #67509 quote
    robertogozzi
    Moderator
    Master

    Try this (not tested)

    Decrease = High < High[1]
    Count = 0
    x     = 1
    WHILE Low < Low[x] DO
       IF Decrease[Count] THEN
          Count = Count + 1
          x = Count
          CONTINUE
       ENDIF
       BREAK 
    WEND
    RETURN Count
    #67520 quote
    Lemonlover
    Participant
    New

    Hi, thanks for the input, but your code is doing the same as the original code i put in.

    I need it to look at the candle which increased the count rather than the previous candle, and to reset to zero when a new high is formed.

    Any ideas?

    Thanks

    #67574 quote
    robertogozzi
    Moderator
    Master

    Can you attach a screenshot where you draw numbers or something to better explain what you mean?

    Thank you.

    #69028 quote
    Lemonlover
    Participant
    New

    Hi,

    Please find attached photo as an example. Whenever the high and low of a candle are less than or equal to the high and low of the candle with a new high(which has a 0 count), the count begins and is set to 1. The count increases to 2 when the high and low of a following candle are less than or equal to the high and low of the candle that increased the count to 1. The count increases to 3 when the high and low of a following candle are less than or equal to the high and low of the candle that increased the count to 2. The count increases to 4 when the high and low of a following candle are less than or equal to the high and low of the candle that increased the count to 3. The count continues only until it reaches 4.

    Whenever a new high is formed(whenever the high of a candle is higher than the high of the candle that had the previous count 0), the count resets to 0. Any other candles that do not meet the criteria are ignored. I hope this makes sense and someone could help with this.

    Thanks

    #69054 quote
    robertogozzi
    Moderator
    Master

    There you go. Give me some feedback. Thanks.

    ONCE MaxCount = 4                                //no more than 4 candles before restarting from 0
    ONCE Count    = 0
    ONCE MaxHigh  = 999999
    IF low < low[1] AND high < high[1] THEN          //when a candle meets requirements...
       count = count + 1                             //... increment count...
       IF Count = 1 THEN                             //... if it's the first one...
          MaxHigh = high[1]                          //... set MaxHigh to the previous HIGH (candle 0)
       ENDIF
       DRAWTEXT("#count#", barindex, low - 5 * pipsize, Dialog, Bold, 12) COLOURED(255,10,10,255) //display count
    ENDIF
    IF Count > MaxCount OR high > MaxHigh THEN       //reset to 0 after max candles OR when a higher high is met
       Count   = 0
       MaxHigh = 999999
    ENDIF
    RETURN
    immo_vienna and Gianluca thanked this post
    #69103 quote
    Lemonlover
    Participant
    New

    Hi,

    Thank you for your help again. Unfortunately is not returning what I am looking for; is the same problem as previously; rather than increasing the count when the high and low of a candle are equal to or less than the high and low of the candle that previously increased the count, is increasing the count when the high and low are equal to or less that the high and low of the preceding candle.

    Also when checking if a new high is formed, it looks at the previous candle rather than at the candle which was the previous high.

    I think the condition should be something like: “IF low < low[low of candle that increased the count] AND high < high[high of candle that increased the count] THEN “. Is something like this possible? Or can it only look at a specified candle eg. 1=previous candle?

     

    Thanks

    #69107 quote
    robertogozzi
    Moderator
    Master

    I hope this will do, I tested it on Eur/Chf, 15min and it seems to be working.

    ONCE MaxCount = 4                                //no more than 4 candles before restarting from 0
    ONCE Count    = 0
    ONCE MaxHigh  = 999999
    IF MaxHigh = 999999 THEN
       LookBack = 1
    ELSE
       LookBack = BarIndex - BarCount
    ENDIF
    IF (low <= low[LookBack]) AND (high <= high[LookBack]) THEN          //when a candle meets requirements...
       count = count + 1                             //... increment count...
       MaxHigh = high                                //... set MaxHigh to this high..
       BarCount = BarIndex
       DRAWTEXT("#count#", barindex, low - 15 * pipsize, Dialog, Bold, 12) COLOURED(255,10,10,255) //display count
    ENDIF
    IF Count >= MaxCount OR high > MaxHigh THEN      //reset to 0 after max candles OR when a higher high is met
       Count   = 0
       MaxHigh = 999999
    ENDIF
    RETURN
    #69179 quote
    Lemonlover
    Participant
    New

    The count of the candles is working now, thank you.

    The only problem left is that when checking if a new high is formed, it looks at the previous candle rather than at the candle which was the previous high.

    For example, on the chart you attached, let’s look at the first candles on the left which have the numbers 1 2 3 underneath. The candle which has the number 3 is followed by a red one and then by a blue one. The blue candle should have been number 4 (the high and low of this are less than the high and low of candle with count 3; red candle after the candle with the count of 3 should be ignored as the high of this is not higher than the high of candle 0)but it doesn’t because the red candle after the one with the count of 3 resets the count to 0 because is looking at the candle with the count 3 and a new high is formed. The high of the red candle should be looking at the high of the candle before the candle with the count 1 as this was the previous candle with the count 0.

    I hope this makes sense, as is quite complicated to explain.

    Thanks

    #69229 quote
    robertogozzi
    Moderator
    Master

    For example, on the chart you attached, let’s look at the first candles on the left which have the numbers 1 2 3 underneath. The candle which has the number 3 is followed by a red one and then by a blue one. The blue candle should have been number 4 (the high and low of this are less than the high and low of candle with count 3; red candle after the candle with the count of 3 should be ignored as the high of this is not higher than the high of candle 0)but it doesn’t because the red candle after the one with the count of 3 resets the count to 0 because is looking at the candle with the count 3 and a new high is formed. The high of the red candle should be looking at the high of the candle before the candle with the count 1 as this was the previous candle with the count 0.

    Should it reference candle 0 or the candle that incremented the count?

    #69232 quote
    robertogozzi
    Moderator
    Master

    The count will reset to zero when a new high is formed; this means when the new high is higher than the high of the candle which preceded the count one candle.

    You warned me not to reference candle 0 to increment count, but the previous candle that caused the count to be incremented, is that correct?

    When you talk about a new high, should I reference the high of candle 0 or the high of the candle that most recently caused the count to be incremented?

    #69233 quote
    robertogozzi
    Moderator
    Master

    I modified the code to reflect your last post (I hope I could understand what you meant)

    ONCE MaxCount    = 4                             //no more than 4 candles before restarting from 0
    ONCE Count       = 0
    ONCE MaxHigh     = 999999
    ONCE Candle0High = 999999
    IF MaxHigh = 999999 THEN
       LookBack = 1
    ELSE
       LookBack = BarIndex - BarCount
    ENDIF
    IF (low <= low[LookBack]) AND (high <= high[LookBack]) THEN          //when a candle meets requirements...
       count = count + 1                             //... increment count...
       MaxHigh = high                                //... set MaxHigh to this high..
       BarCount = BarIndex
       DRAWTEXT("#count#", barindex, low - 2 * pipsize, Dialog, Bold, 12) COLOURED(255,10,10,255) //display count
       IF Count = 1 THEN
          Candle0High = high[1]
       ENDIF
    ENDIF
    IF Count >= MaxCount OR high > Candle0High THEN  //reset to 0 after max candles OR when a higher high is met
       Count       = 0
       MaxHigh     = 999999
       Candle0High = 999999
    ENDIF
    RETURN
Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Count condition help


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Lemonlover @lemonlover Participant
Summary

This topic contains 11 replies,
has 2 voices, and was last updated by robertogozzi
7 years, 9 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 04/07/2018
Status: Active
Attachments: 2 files
Logo Logo
Loading...