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

ema crossing indicator working with wrong values on chart

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

    ema crossing indicator working with wrong values on chart

    Hi there,

    I am working on this indicator step by step and got stuck with the part where I compare the values of bar low with ema20 ("// check for ema20AboveINVALID condition" section in my code).
    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class EMAX2SymbolWithN6 : Indicator
    
        {
            private EMA ema20;
            private EMA ema50;
            private EMA ema200;
    
            private SimpleFont    smallFont;
    
            private bool X1ABOVE = false;
            private bool ema20AboveINVALID = false;
            private int X1ABOVEBarNumber;
            private int lastX1Bar;
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            public int EMAPeriod1 { get; set; }
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            public int EMAPeriod2 { get; set; }
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            public int EMAPeriod3 { get; set; }
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            public int TextOffset { get; set; }
    
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
            Name = "EMA X2 Symbol wN 6";
            IsOverlay = true;
            EMAPeriod1 = 20;
            EMAPeriod2 = 50;
            EMAPeriod3 = 200;
            smallFont = new Gui.Tools.SimpleFont("Arial", 10);    
            TextOffset = 30;
            }
    
    }
    
        protected override void OnBarUpdate()
        {
        Draw.Text(this, "barNumber" + CurrentBar, false, CurrentBar.ToString(), 0, High[0] + (TickSize * 20), 0, Brushes.Black, smallFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
    
        {
    
            ema20 = EMA(EMAPeriod1);
            ema50 = EMA(EMAPeriod2);
            ema200 = EMA(EMAPeriod3);
    
                if (CurrentBar < 35)
    {
        return;
    }
    
    // classical X2
                if
                ((CrossAbove(ema20, ema50, 1) && ema20[0] > ema200[0] && ema50[0] > ema200[0]) ||
                (CrossBelow(ema20, ema50, 1) && ema20[0] < ema200[0] && ema50[0] < ema200[0]))
                {    
                Draw.Text(this, "X2"+CurrentBar, "X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Black);
                }
    // DON`T CHANGE classical X2
    
    
    //ABOVE200 START **************************
    
                //X1 ABOVE condition
                if (CrossAbove(ema20, ema50, 1)&& ema20[0] < ema200[0])        
                {
                        X1ABOVE = true;
                    lastX1Bar = CurrentBar; // update lastX1Bar
                    Print("X1ABOVE flag set to true at bar " + CurrentBar);
                    Draw.Text(this, "X1-A"+CurrentBar, "X1-A", 0, Low[0] - (TickSize*TextOffset), Brushes.Orange);
                }
    
                    if (X1ABOVE)
        {
            // check for ema20AboveINVALID condition
            for (int i = lastX1Bar + 1; i < CurrentBar; i++)
            {
    
                if (Low[i] < ema20[i])
                {
                    ema20AboveINVALID = true;
                    Print("ema20AboveCHECK condition met at bar " + i + ", Low=" + Low[i] + ", Close=" + Close[i] + ", EMA20=" + ema20[i]);
                    // Print("ema20AboveCHECK condition met at bar " + i);
                    break;
                }
           }  
    
    
                // X1 ABOVE becomes false
                if ((CrossBelow(ema20, ema50, 1) && ema50[0] < ema200[0]) ||
                (CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0]))
                {
                    X1ABOVE = false;
                    ema20AboveINVALID = false;
                    Print("X1ABOVE flag set to false at bar " + CurrentBar);
                }
    }
    
    
                //X2 ABOVE200 condition
                if(CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0])
                {
                    Print("X2 ABOVE200 at bar " + CurrentBar);
                    Draw.Text(this, "AV-X2"+CurrentBar, "AV-X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Blue);
                    X1ABOVE = false;
                    Print("After X2ABOVE200 X1ABOVE flag set to false at bar " + CurrentBar);
                }
    
    
    
    //ABOVE200 END **************************            
    
    /*            //X1 BELOW condition
                if (CrossBelow(ema20, ema50, 1)&& ema20[0] > ema200[0])
                {
                    // Print("X1 BELOW " + Instrument.FullName + "  at: " + Time[0]);
                    Draw.Text(this, "X1-B"+CurrentBar, "X1-B", 0, Low[0] - (TickSize*TextOffset), Brushes.Orange);
                }
    
                //ABOVE200 condition
                if(CrossAbove(ema50, ema200, 1) && ema20[0] > ema50[0])
                {
                    // Print("ABOVE200: VALID  " + Instrument.FullName + "  at: " + Time[0]);
                    Draw.Text(this, "AV-X2"+CurrentBar, "AV-X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Blue);
                }
    
                //BELOW200 condition
                if(CrossBelow(ema50, ema200, 1) && ema20[0] < ema50[0])
                {
                    // Print("BELOW200: VALID  " + Instrument.FullName + "  at: " + Time[0]);
                    Draw.Text(this, "BV-X2"+CurrentBar, "BV-X2", 0, Low[0] - (TickSize*TextOffset), Brushes.Green);
                } */
            }    
        }
    }
    
    }​
    All the conditions and functions work as they should, apart from the fact, that the values the print shows in the Output window are from other bars than the bar No. on the print line. Here are some of the print outs:
    X1ABOVE flag set to true at bar 117
    ema20AboveCHECK condition met at bar 118, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 119, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 120, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    X1ABOVE flag set to false at bar 121
    X1ABOVE flag set to true at bar 173
    ema20AboveCHECK condition met at bar 174, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 175, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 176, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 177, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 178, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 179, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 180, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 181, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 182, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 183, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 184, Low=0,92379, Close=0,92453, EMA20=0,923950952380952
    ema20AboveCHECK condition met at bar 174, Low=0,92457, Close=0,92468, EMA20=0,924789733676327
    ema20AboveCHECK condition met at bar 174, Low=0,92453, Close=0,92471, EMA20=0,924782139992867
    ema20AboveCHECK condition met at bar 174, Low=0,92457, Close=0,9248, EMA20=0,924783840945927​
    ...

    The Low value of bar 118, 119 and 120 is the low value of bar no. 1. The bar close value of bar 118, 119 and 120 is the close value of bar 1. Also the ema20 value seems to be from bar 1. Noticeable here is, that all three emas are not drawn on the chart till bar No. 20. Then the printouts continue to show values for bar 1 for bars 174 till 184. after that the printout shows bar no. 174 again but now with the values of bar 12. Then the next bar 174 is showing the values of bar 13 and so on. So it is obvious that the script is stuck somewhere within bar 1 and bar 31. Also the other printouts show only values from bars 1 to 31.

    I am also attaching a screenshot of the chart where this is happening:
    Click image for larger version

Name:	Chart_screenshot.png
Views:	156
Size:	628.4 KB
ID:	1237135

    This is driving me crazy but I hope someone can help me solve this problem.
    Thanks!
    Best
    Peter

    #2
    Hello cyberpete76,

    The only comment that I can make about this looping logic is that you are storing CurrentBar as lastX1Bar and then you use that in your loop:

    for (int i = lastX1Bar + 1; i < CurrentBar; i++)


    CurrentBar is an index and not a BarsAgo so using the loops iterator with series like Low[i] doesn't make sense.

    You would need to find the BarsAgo to use it with a series bracket [BarsAgo]. One way is to subtract i from the CurrentBar to get an number of BarsAgo.

    If that is not the solution I would suggest to remove the loop and manually write out the conditions to see if that works as you expect, that would let you see the problem more clearly.
    JesseNinjaTrader Customer Service

    Comment


      #3
      NinjaTrader_Jesse Thanks a lot for your help! I was able to solve the problem thanks to your tip! Great customer service!
      Peter

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Segwin, 05-07-2018, 02:15 PM
      14 responses
      1,789 views
      0 likes
      Last Post aligator  
      Started by Jimmyk, 01-26-2018, 05:19 AM
      6 responses
      837 views
      0 likes
      Last Post emuns
      by emuns
       
      Started by jxs_xrj, 01-12-2020, 09:49 AM
      6 responses
      3,293 views
      1 like
      Last Post jgualdronc  
      Started by Touch-Ups, Today, 10:36 AM
      0 responses
      13 views
      0 likes
      Last Post Touch-Ups  
      Started by geddyisodin, 04-25-2024, 05:20 AM
      11 responses
      63 views
      0 likes
      Last Post halgo_boulder  
      Working...
      X