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()
    https://ninjatrader.com/support/help...ml?countif.htm
    Draw.ArrowUp()
    https://ninjatrader.com/support/help...aw_arrowup.htm
    Math.Max
    https://ninjatrader.com/support/foru...09056-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:
    https://ninjatrader.com/support/help...ml?addplot.htm
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by fafafdenis, Today, 04:56 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_EricB  
    Started by pjsmith, 07-18-2019, 04:21 AM
    7 responses
    245 views
    0 likes
    Last Post aligator  
    Started by ark321, Today, 05:12 PM
    0 responses
    7 views
    0 likes
    Last Post ark321
    by ark321
     
    Started by futurenow, Today, 04:49 PM
    0 responses
    9 views
    0 likes
    Last Post futurenow  
    Started by ofer1964spivak, Today, 04:27 PM
    0 responses
    5 views
    0 likes
    Last Post ofer1964spivak  
    Working...
    X