Summation with for to next loop

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #78095 quote
    Vagus_Lux
    Participant
    Average
    DEFPARAM DRAWONLASTBARONLY=TRUE
    
    LFC=summation[5](close)

     

    I’d like to achieve the same result with the following simple ‘for next loop’instead using ‘summation’ but there is no output using the ‘for next loop’

    for i=1 to 5 do
    LFC=close[i]+LFC
    next
    
    return LFC
    MeinIndikator.itf
    #78097 quote
    robertogozzi
    Moderator
    Master

    The last 5 bars are 0-4, not 1-5 (these are the previous 5 bars).

    The problem could be the value returned by LFC, too big to be displayed.

    On DAX that would be about 62000 (5 * 12400), but your are not resetting it to 0 at the beginning, so within a few bars that could become a huge number!

    #78098 quote
    Vagus_Lux
    Participant
    Average

    Thank you so much for your super fast response!
    You are right! I added one reset ahead of the for next loop and it worked.

    LFC = 0 //Reset LFC
    
    for i=1 to 5 do
    LFC=close[i]+LFC
    next
    #78101 quote
    Vonasi
    Moderator
    Master

    Vagus_Lux – please use the ‘Insert PRT Code’ button when putting code in your posts to make it more readable for others. I have tidied up your posts for you. 🙂

    I am guessing that Robert is posting from his phone otherwise he would have done the same!

    #78103 quote
    robertogozzi
    Moderator
    Master

    AhAhAh…. I am indeed posting from my phone!

    #84509 quote
    simeong
    Participant
    Average

    I have a quick question there.

    In the example code below, I do have the following query

    for i=8 to 20 do
    LFC=close[i]+close[28]
    next
     
    return LFC

    is close[28] inside the for statement 28 candles from close[0] or 28 candles from “i” or “close[i]” ?

    Kind Regards,

    #84510 quote
    robertogozzi
    Moderator
    Master

    From CLOSE[0], it is constant through the loop.

    To be from i you’ll have to write 28+i within brackets.

    simeong thanked this post
    #121845 quote
    VN
    Participant
    Average

    Hi Roberto, I am trying to count the total number of times an event took place in a given timeframe of my chart.

    For example I am counting the number of times RSI crossed above 70 and I want that calculation to be applied to any timeframe so from the beginning of time to end of time loaded.

    I don’t know how to set the beginning and end of time in a FOR NEXT loop.

    Any eloquent way that this can be achieved?

    #121864 quote
    robertogozzi
    Moderator
    Master

    The beginning is always BARINDEX=0.

    BARINDEX is the progressive numeber of bars, so you need to do is, from current bar to zero:

    Count = 0
    FOR i = BarIndex DOWNTO 0
       IF Rsi[14](close)[i] CROSSES OVER 70 THEN
          Count = Count + 1
       ENDIF
    NEXT
    RETURN Count AS "Count"

    But… all the above is not necessary, since any indicator ALWAYS starts from the beginning, so all you want to do is to sum any occurrence of the event:

    ONCE Count = 0
    IF Rsi[14](close) CROSSES OVER 70 THEN
       Count = Count + 1
    ENDIF
    RETURN Count AS "Count"
    VN thanked this post
    #132165 quote
    VN
    Participant
    Average

    hi @ robertogozzi, i want to modify the code such that it counts how many times the RSI crossed over 70 in the last 30 bars. So the count will be a rolling count between period zero until 30 periods ago. Can this be done?

    #132166 quote
    robertogozzi
    Moderator
    Master

    There you go:

    x = summation[31](Rsi[14](close) crosses over 70)

    X will retain the value you are looking for.

    from current bar [0] up to 30 bars ago [30], it’s 31 bars.

    There’s not need to use loops here.

    #132173 quote
    VN
    Participant
    Average

    thanks Roberto! I did not think of using summation in this context, but that works nicely!

    I used this code to also count the number of times the current close was less than prior close, but that doesn’t return the right answer.

    x = summation[31](close < close[1])

    Have i done something wrong?

    #132177 quote
    Vonasi
    Moderator
    Master

    What you have coded will compare the current close to the previous close and then the 2nd to last  close to the one before that and then the third to last close to the one before that…. and so on and so on.

    If you want to compare the latest close to the 30 previous candles then you will need a loop.

    x = 0
    for a = 1 to 30
    if close < close[a] then
    x = x + 1
    endif
    next
    
    #132183 quote
    VN
    Participant
    Average

    thanks vonasi..

    the above is part of a wider problem i am trying to code, but with not much success.

    I am trying to count the number of times the price changed direction in a specified period e.g. last 10 candles, in other words the number of times the candle colour changed in the last 10 candles.

    so for example, if the first 3 candles were green, but the forth was red, then the counter = 1 as that is the 1st time the close went below prior close.

    then candle 5 and 6 were both green then the counter = 2 since the close at candle 5 reversed  from red (4th candle) to green (5th candle). I don’t want to count the candles when the colour is same, only when they reverse.

    #132184 quote
    robertogozzi
    Moderator
    Master
    Bullish = close > open
    Bearish = close < open
    Change  = (Bullish AND Bearish[1]) OR (Bullish[1] AND Bearish)
    x       = summation[10](Change)

    There you go.

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

Summation with for to next loop


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Vagus_Lux @vagus_lux Participant
Summary

This topic contains 16 replies,
has 5 voices, and was last updated by VN
5 years, 9 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 08/13/2018
Status: Active
Attachments: 1 files
Logo Logo
Loading...