Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Understanding this code snippet

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

    Understanding this code snippet

    Good afternoon,

    I'm looking to understand how this snippet of code works; it's from a NJ indicator. I know it takes readings from MACD and EMA's, but I am not sure how it fits together and produces an alert. I have uploaded the code to https://pastebin.com/GpWpZ8By as I'm not 100% sure the code I have pasted below is readable. Thanks

    -PH

    private void Update(int c)
    {
    int num = c + 1;
    int num2 = c + 2;
    if (((NinjaScriptBase)this).CurrentBar <= c + trendLength)
    {
    return;
    }
    macd[c] = MovingAverage(((NinjaScriptBase)this).Close, averageType, fastLength)[c] - MovingAverage(((NinjaScriptBase)this).Close, averageType, slowLength)[c];
    macd1[c] = ((macd[c] >= macd[num]) ? 1 : 0);
    macdup[c] = ((((NinjaScriptBase)SUM((ISeries<double>)(object)m acd1, sequentialLength))[c] == (double)sequentialLength && !(macd[c] < macd[c + trendLength])) ? 1 : 0);
    macd2[c] = ((macd[c] < macd[num]) ? 1 : 0);
    macddown[c] = ((((NinjaScriptBase)SUM((ISeries<double>)(object)m acd2, sequentialLength))[c] == (double)sequentialLength && !(macd[c] >= macd[c + trendLength])) ? 1 : 0);
    if (macddown[num] != 0.0 && macd[c] > macd[num] && K[c] < (double)overbought)
    {
    upSignal[c] = ((NinjaScriptBase)this).Low[c] - 4.0 * ((NinjaScriptBase)this).TickSize;
    }
    if (macdup[num] != 0.0 && macd[c] < macd[num] && K[c] > (double)oversold)
    {
    dnSignal[c] = ((NinjaScriptBase)this).High[c] + 4.0 * ((NinjaScriptBase)this).TickSize;
    }
    if (useAlerts && upSignal[c] != 0.0)
    {
    ((NinjaScriptBase)this).Alert("Up", (Priority)0, "Up", FileAlert1, 10, (Brush)Brushes.Transparent, (Brush)Brushes.Green);
    }
    if (useAlerts && dnSignal[c] != 0.0)
    {
    ((NinjaScriptBase)this).Alert("Dn", (Priority)0, "Dn", FileAlert1, 10, (Brush)Brushes.Transparent, (Brush)Brushes.Red);
    }
    ISeries<double> close = ((NinjaScriptBase)this).Close;
    EMA eMA = EMA(close, 5);
    EMA eMA2 = EMA(close, 9);
    EMA eMA3 = EMA(close, 21);
    buy[c] = ((((NinjaScriptBase)eMA)[c] > ((NinjaScriptBase)eMA2)[c] && ((NinjaScriptBase)eMA2)[c] > ((NinjaScriptBase)eMA3)[c] && !(((NinjaScriptBase)this).Low[c] <= ((NinjaScriptBase)eMA)[c])) ? 1 : 0);
    bool flag = ((NinjaScriptBase)eMA)[c] <= ((NinjaScriptBase)eMA2)[c];
    bool flag2 = buy[num] == 0.0 && buy[c] != 0.0;
    buysignal[c] = ((flag2 && !flag) ? 1.0 : ((buysignal[num] != 1.0 || !flag) ? buysignal[num] : 0.0));
    if (buysignal[num] == 0.0)
    {
    _ = buysignal[c];
    }
    if (buysignal[num] != 0.0)
    {
    _ = buysignal[c];
    }
    sell[c] = ((((NinjaScriptBase)eMA)[c] < ((NinjaScriptBase)eMA2)[c] && ((NinjaScriptBase)eMA2)[c] < ((NinjaScriptBase)eMA3)[c] && !(((NinjaScriptBase)this).High[c] >= ((NinjaScriptBase)eMA)[c])) ? 1 : 0);
    bool flag3 = ((NinjaScriptBase)eMA)[c] >= ((NinjaScriptBase)eMA2)[c];
    bool flag4 = sell[num] == 0.0 && sell[c] != 0.0;
    sellsignal[c] = ((flag4 && !flag3) ? 1.0 : ((sellsignal[num] != 1.0 || !flag3) ? sellsignal[num] : 0.0));
    if (sellsignal[num] == 0.0)
    {
    _ = sellsignal[c];
    }
    Colorbars[c] = ((buysignal[c] == 1.0) ? 1 : ((sellsignal[c] == 1.0) ? 2 : ((buysignal[c] == 0.0 || sellsignal[c] == 0.0) ? 3 : 0)));
    double num3 = EI.ZZ[c];
    double num4 = ((((NinjaScriptBase)this).Close[c] * percentamount / 100.0 > Math.Max((revAmount < atrreversal * ((NinjaScriptBase)ATR(atrlength))[c]) ? 1 : 0, revAmount)) ? (((NinjaScriptBase)this).Close[c] * percentamount / 100.0) : ((revAmount < atrreversal * ((NinjaScriptBase)ATR(atrlength))[c]) ? (atrreversal * ((NinjaScriptBase)ATR(atrlength))[c]) : revAmount));
    EISave[c] = ((num3 != 0.0) ? ((NinjaScriptBase)EI)[c] : EISave[num]);
    double num5 = ((EISave[c] == Price(priceH)[c]) ? Price(priceH)[c] : Price(priceL)[c]) - EISave[num];
    bool flag5 = num5 >= 0.0;
    isConf[c] = ((Math.Abs(num5) >= num4 || (num3 == 0.0 && isConf[num] != 0.0)) ? 1 : 0);
    int num6 = (flag5 ? 1 : 0);
    if (num6 <= 1 && ((NinjaScriptBase)EI)[c] != 0.0)
    {
    EnhancedLines[c] = ((NinjaScriptBase)EI)[c];
    }
    EIL[c] = ((num3 == 0.0 || flag5) ? EIL[num] : Price(priceL)[c]);
    EIH[c] = ((num3 == 0.0 || !flag5) ? EIH[num] : Price(priceH)[c]);
    dir[c] = ((EIL[c] != EIL[num] || (Price(priceL)[c] == EIL[num] && Price(priceL)[c] == EISave[c])) ? 1.0 : ((EIH[c] != EIH[num] || (Price(priceH)[c] == EIH[num] && Price(priceH)[c] == EISave[c])) ? (-1.0) : dir[num]));
    signal[c] = ((dir[c] > 0.0 && !(Price(priceL)[c] <= EIL[c])) ? ((signal[num] <= 0.0) ? 1.0 : signal[num]) : ((!(dir[c] < 0.0) || Price(priceH)[c] >= EIH[c]) ? signal[num] : ((signal[num] >= 0.0) ? (-1.0) : signal[num])));
    bool flag6 = true;
    bool flag7 = signal[c] > 0.0 && signal[num] <= 0.0;
    bool flag8 = flag6 && signal[c] < 0.0 && signal[num] >= 0.0;
    int num7 = ((NinjaScriptBase)this).CurrentBar - 10;
    if (num7 > 0 && flag7 && Colorbars[c] != 3.0)
    {
    longS[c] = ((NinjaScriptBase)this).Low[c] - 4.0 * ((NinjaScriptBase)this).TickSize;
    }
    if (num7 > 0 && flag8 && Colorbars[c] != 3.0)
    {
    shortS[c] = ((NinjaScriptBase)this).High[c] + 4.0 * ((NinjaScriptBase)this).TickSize;
    }
    if (num7 != 0 && flag8)
    {
    revLineBot[c] = 0.0;
    revLineTop[c] = ((NinjaScriptBase)this).High[num];
    }
    else if (num7 != 0 && flag7)
    {
    revLineTop[c] = 0.0;
    revLineBot[c] = ((NinjaScriptBase)this).Low[num];
    }
    else if (revLineBot[num] != 0.0 && (Colorbars[num2] == 2.0 || Colorbars[num] == 2.0))
    {
    revLineBot[c] = revLineBot[num];
    revLineTop[c] = 0.0;
    }
    else if (revLineTop[num] != 0.0 && (Colorbars[num2] == 1.0 || Colorbars[num] == 1.0))
    {
    revLineTop[c] = revLineTop[num];
    revLineBot[c] = 0.0;
    }
    else
    {
    revLineTop[c] = 0.0;
    revLineBot[c] = 0.0;
    }
    if (revLineBot[c] != 0.0)
    {
    botLine[num] = revLineBot[c];
    }
    if (revLineTop[c] != 0.0)
    {
    topLine[num] = revLineTop[c];
    }
    if (num7 != 0 && flag7 && Colorbars[c] == 3.0)
    {
    BUY[c] = ((NinjaScriptBase)this).Low[c] - 4.0 * ((NinjaScriptBase)this).TickSize;
    if (PlayAlertSound)
    {
    ((NinjaScriptBase)this).Alert(((NinjaScriptBase)th is).Name + ((NinjaScriptBase)this).CurrentBar, (Priority)0, $"BUY bar#: {((NinjaScriptBase)this).CurrentBar - c}", UpBarAlertSound, RearmSeconds, (Brush)Brushes.Black, ((Stroke)((NinjaScriptBase)this).Plots[BUY_plot]).Brush);
    }
    }
    if (num7 != 0 && flag8 && Colorbars[c] == 3.0)
    {
    SELL[c] = ((NinjaScriptBase)this).High[c] + 4.0 * ((NinjaScriptBase)this).TickSize;
    if (PlayAlertSound)
    {
    ((NinjaScriptBase)this).Alert(((NinjaScriptBase)th is).Name + ((NinjaScriptBase)this).CurrentBar, (Priority)0, $"SELL bar#: {((NinjaScriptBase)this).CurrentBar - c}", DnBarAlertSound, RearmSeconds, (Brush)Brushes.Black, ((Stroke)((NinjaScriptBase)this).Plots[SELL_plot]).Brush);
    }
    }
    }​

    #2
    Hello PostHaste,

    Thank you for your post.

    While it would not be within our support model to analyze how code from a third-party source works, you can generate alerts from within a script using Alert() which is what this code is also using.

    Alert() - https://ninjatrader.com/support/helpguides/nt8/alert.htm

    If you have more detailed questions about how the code works please reach out to the original author of the code for clarity.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    589 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    342 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    103 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    555 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    552 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X