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 GussJ, 03-04-2020, 03:11 PM
    11 responses
    3,229 views
    0 likes
    Last Post xiinteractive  
    Started by andrewtrades, Today, 04:57 PM
    1 response
    14 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by chbruno, Today, 04:10 PM
    0 responses
    7 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    441 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    12 views
    0 likes
    Last Post FAQtrader  
    Working...
    X