Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Custom indicator using CountIf and Math.Max

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Custom indicator using CountIf and Math.Max

    Hello,

    I’m struggling with a custom indicator I’m trying to code. It will be great if you can guide me how to handle this. The idea is:

    1. If “Condition 1” occurs
    2. Start counting the number of instances “Condition 2” occurs
    3. Assign the number of occurrences to “X”
    4. Draw ArrowUp when “Condition 3” occurs
    5. Keep counting until “Condition 1” occurs again (and Reset “X”)
    6. Make “X” available for other custom indicators

    Condition 1: EMA(20) is the highest value among EMA(20), DEMA(20), and TEMA(20)
    Condition 2: Open > SMA(Open, 10) AND Close >SMA(Open, 10)
    Condition 3: Value of X is 1 AND Value of X one bar ago is not 1


    The basic structure I can think of is:

    Code:
    if (Math.Max(EMA(20), DEMA(20), TEMA(20)) = EMA(20)))
    {
    Double X =
    CountIf(() =>
    Open[0] > SMA(Open, 10)
    && Close[0] > SMA(Open, 10)
    , “int period”)
    };
    
    else
    {
    X = 0;
    }
    
    
    if (X == 1 && “Value of X one bar ago” !=1 )
    {
    Draw.ArrowUp(this, "tag1", true, 0, Low[0] - (2*TickSize), Brushes.Red);
    }
    How should I structure my code? I don’t know what to put in “int period” for CountIf as the indicator should keep counting, in “Value of X one bar ago”.


    Reference:
    CountIf()

    Draw.ArrowUp()

    Math.Max





    #2
    Hello jungsoohuh,

    Thanks for your post and welcome to the NinjaTrader forums!

    I don't think Countif() is what you would want to use.

    Basically, once condition 1 is true, you want to begin the count and countif() only looks backwards.

    I would use a bool variable that is set true once condition one is true

    Use that bool to then enable counting condition 2. Use an Int type variable as a counter (pseudo code example):
    if (bool && condition 2) // if both are true
    {
    myCounter++; // increment counter
    }

    Not sure I understand condition 3, you may need a rethink.

    To make "X" available for other indicators, or strategies or market analyzer, I would suggest using a Transparent plot and assigning X to it or 0 on each bar. Plots are double type data series.
    Use transparent so it does not show on the chart.
    References:

    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Brevo, Today, 01:45 AM
    0 responses
    2 views
    0 likes
    Last Post Brevo
    by Brevo
     
    Started by aussugardefender, Today, 01:07 AM
    0 responses
    3 views
    0 likes
    Last Post aussugardefender  
    Started by pvincent, 06-23-2022, 12:53 PM
    14 responses
    238 views
    0 likes
    Last Post Nyman
    by Nyman
     
    Started by TraderG23, 12-08-2023, 07:56 AM
    9 responses
    384 views
    1 like
    Last Post Gavini
    by Gavini
     
    Started by oviejo, Today, 12:28 AM
    0 responses
    4 views
    0 likes
    Last Post oviejo
    by oviejo
     
    Working...
    X