Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving average flatlines when connected to feed

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

    Moving average flatlines when connected to feed

    I wrote an MA indicator to display the higher timeframe moving average on my lower time frame chart. I trade the 3min chart and have a 20 bar MA. The indicator uses the 3-min data series regardless of what the timeframe is, and plots that.

    I am using it on a 1-min chart and a 30-sec chart. However when connected, it flatlines. If I reload the ninjascript, then it display correctly.

    What am I doing wrong?

    PHP Code:
        public class EMA3Min Indicator
        
    {
            private 
    int period 1

            protected 
    override void Initialize()
            {
                
    Add(PeriodType.Minute3);            
                
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line"EMA 3-min"));
                
    Overlay                true;
            }

            protected 
    override void OnBarUpdate()
            {
                if (
    CurrentBars[0] <= BarsRequired 
                    
    || CurrentBars[1] <= BarsRequired)
                    return;
                if (
    CurrentBars.Length && CurrentBars[1] > 0)
                    
    Value.Set(EMA(BarsArray[1], period)[0]);
                else
                    
    Value.Set(Input[0]);
            }

            [
    Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            
    [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            
    public DataSeries Plot0
            
    {
                
    get { return Values[0]; }
            }

            [
    Description("")]
            [
    GridCategory("Parameters")]
            public 
    int Period
            
    {
                
    get { return period; }
                
    set period Math.Max(1value); }
            }
        } 

    #2
    adamus, looks like you're not filtering for any BarsInProgress call here? You would want to not process anything else besides the primary series, so you can return out of OnBarUpdate() here. Then please recheck.

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      I added

      if (BarsInProgress != 0) return;

      and reloaded everything. My 30-sec chart is still displaying the problem. Curiously the 1-min chart does not - although I'm not sure if it ever did or if it is now OK due to the new code.

      Comment


        #4
        Thanks adamus, any change for the 30 sec chart with this snippet -

        if (CurrentBars.Length > 1 && CurrentBars[1] > 0)
        Value.Set(EMA(BarsArray[1], period)[0]);
        else
        Value.Set(Closes[1][0]);
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I'll try it out and let you know how it behaves next time I start up and connect.

          Thanks

          Comment


            #6
            Still problematic......

            PHP Code:
                public class EMA3Min Indicator
                
            {
                    private 
            int period 1

                    protected 
            override void Initialize()
                    {
                        
            Add(PeriodType.Minute3);            
                        
            Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line"EMA 3-min"));
                        
            Overlay                true;
                    }

                    protected 
            override void OnBarUpdate()
                    {
                        if (
            BarsInProgress != 0) return;
                        if (
            CurrentBars[0] <= BarsRequired 
                            
            || CurrentBars[1] <= BarsRequired)
                            return;
                        if (
            CurrentBars.Length && CurrentBars[1] > 0)
                            
            Value.Set(EMA(BarsArray[1], period)[0]);
                        else
                            
            Value.Set(Closes[1][0]);
                    }

                    [
            Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                    
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                    
            public DataSeries Plot0
                    
            {
                        
            get { return Values[0]; }
                    }

                    [
            Description("")]
                    [
            GridCategory("Parameters")]
                    public 
            int Period
                    
            {
                        
            get { return period; }
                        
            set period Math.Max(1value); }
                    }
                } 
            What I'm seeing now is the 1min chart is fine, 30sec chart flatlines, and the 1sec chart shows nothing.

            Could this have something to do with the IB datafeed which only provides 1-min bars historically?

            Comment


              #7
              Hi Adamus, yes could be the case of missing historical tick data then - I've just tried the setup on a feed with full tick backfill and don't see any issues with your code provided. Do you see the same if you reload the NS code after the day when the full day's tick history is present on your charts?
              BertrandNinjaTrader Customer Service

              Comment


                #8
                I rarely try to do that, so I'm not sure. I can try later this evening.

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  Hi Adamus, yes could be the case of missing historical tick data then - I've just tried the setup on a feed with full tick backfill and don't see any issues with your code provided. Do you see the same if you reload the NS code after the day when the full day's tick history is present on your charts?
                  Hi Bertrand,

                  when I disconnect and restart with only a short gap of a couple of minutes in the data stream, none of the charts show a flat line for the MA.

                  As long as I can reload the NS code and get it working quickly it's not an issue, but I'll know for sure tomorrow when I start up with no overnight history in the db. The 1-second chart might cause problems, if I remember what happened yesterday correctly.

                  Regards

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by PaulMohn, Today, 03:49 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post PaulMohn  
                  Started by inanazsocial, Today, 01:15 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Jason  
                  Started by rocketman7, Today, 02:12 AM
                  0 responses
                  10 views
                  0 likes
                  Last Post rocketman7  
                  Started by dustydbayer, Today, 01:59 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post dustydbayer  
                  Started by trilliantrader, 04-18-2024, 08:16 AM
                  5 responses
                  23 views
                  0 likes
                  Last Post trilliantrader  
                  Working...
                  X