simple query about a for/next loop

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #55208 quote
    IG_CFD_Trader
    Participant
    Average

    Hi Nicolas, I have a simple query. for example: For i=0 to 10 do how do I write a code that adds up bars with close>open only (i.e. adds up green bars only)?    regards, Walid

    #55221 quote
    robertogozzi
    Moderator
    Master

    If you want to use FOR…NEXT you may write:

    BullCount = 0
    FOR i = 0 TO 10 DO
       IF close[i] > open[i] THEN
          BullCount = BullCount + 1
       ENDIF
    NEXT

    but there is a more elegant, fast and efficient way:

    BullCount = summation[11](close > open)

    Roberto

    #55238 quote
    IG_CFD_Trader
    Participant
    Average

    Hi Roberto,

    Thank you for your reply. Perhaps I was not clear enough in my query.

    BullCount = summation [11](close > open) will return the number of bars that meet the condition close > open in the last 11 bars. Let’s say for example it returned BullCount = 5 (i.e. there were 5 bars with close > open)

    What I am after is how I would then add the closes of these 5 bars together?

    Walid

    #55243 quote
    robertogozzi
    Moderator
    Master
    BullSum = summation[11](close   (close > open)) //sum of CLOSing price of last BULLish bars
    BullSum = summation[11](range * (close > open)) //sum of RANGE of last BULLish bars

    This example multiplies CLOSE by the logical/boolean value 0 (when the condition is  false) or 1 (when true), so if there are 5 bullish bars their CLOSing price will be summed up.

    To adapt this to BEARish bars you just need to change the condition to “close < open”.

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

simple query about a for/next loop


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
IG_CFD_Trader @wnakhoul Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by robertogozzi
8 years, 2 months ago.

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