Lookback with condition

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #172502 quote
    IG_CFD_TraderIG_CFD_Trader
    Participant
    Average

    Hello All,

    This was posted in reply to Vonassi but didn’t look like it was posted. I am posting here. Apologies if it ended up being double posted.

    I have a fairly simple question
    How do I write a simple code that for example looks back at the last 100 bars and averages only positive values? For example I have an indicator that returns either a + value or – value for every bar. I want to lookback 100 bars and average only the + values (- values are skipped/ignored in average calculation)
    thank you and regards.

    #172505 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    It’s an indicator, so its correct support forum is ProBuilder.

    I moved it.

    Try this (not tested, I can’t even know if it is an accepted syntax):

    Avg = average[100,0](MyIndicator * (MyIndicator > 0))

    The idea is to average all positive values, 0 otherwise.

    #172507 quote
    IG_CFD_TraderIG_CFD_Trader
    Participant
    Average

    I didn’t work. It returns 0

    #172508 quote
    IG_CFD_TraderIG_CFD_Trader
    Participant
    Average

    Maybe a code that goes through the 100 bars and everytime that finds a positive value it stores it in a variable and once it finishes going through the 1oo bars and storing all the positive values it finds then a code to average all positive values found and stored. Perhaps an array variable that can store up to 96 values?? perhaps a much more simpler code?

    #172515 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    You must have done something wrong, as it works perfectly (applied to MACD):

    MyMACD       = Average[12,0](close) - Average[26,0](close)
    MySignalLine = Average[9,0](MyMACD)
    MyHisto      = MyMACD - MySignalLine
    Avg          = average[100,0](MyHisto * (MyHisto > 0)) //average of the last 100 periods with a Histogram > 0
    return Avg AS "Average MACD histogram when > 0"

    If, instead, you want to count how many times the Histogram was > 0:

    MyMACD       = Average[12,0](close) - Average[26,0](close)
    MySignalLine = Average[9,0](MyMACD)
    MyHisto      = MyMACD - MySignalLine
    Count        = summation[100](MyHisto > 0)
    return Count AS "How many times Hitogram was > 0"
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Lookback with condition


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
IG_CFD_Trader @wnakhoul Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by robertogozzirobertogozzi
5 years, 3 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 06/26/2021
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...