Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Tick Replay and EMA

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

    Tick Replay and EMA

    I am trying to create an indicator very similar to the BuySellPressure indicator to count UpTicks and DownTicks instead of BidVolume and AskVolume. The code works fine until I try to add an EMA of the UpTicks and DownTicks series I've created. Once I add the EMA, NinjaTrader crashes when I try to run the indicator.

    Code:
    Series<double> upticks, downticks;
            private int            activeBar        = -1;
            private double lastPrice = 0;
            private int lastDir = 0, up = 0, down = 0;
            private EMA emaUp, emaDown;
    
    protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Calculate                    = Calculate.OnEachTick;
                    IsOverlay                    = false;
                    DisplayInDataBox            = true;
                    DrawOnPricePanel            = true;
                    DrawHorizontalGridLines        = true;
                    DrawVerticalGridLines        = true;
                    PaintPriceMarkers            = true;
                    ScaleJustification            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive    = true;
                    Length                    = 4;
    
                    AddPlot(new Pen(Brushes.Red, 2), PlotStyle.Bar, "Plot1");
                    AddLine(Brushes.DarkGray, 1, "Line1");                          
                }
                else if (State == State.Configure)
                {
                    upticks = new Series<double>(this);
                    downticks = new Series<double>(this);
                    
                    emaUp = EMA(upticks,Length);
                    emaDown = EMA(downticks,Length);
                }
                else if (State == State.Historical)
                {
                    activeBar    = 0;
                }
            }
    
    protected override void OnMarketData(MarketDataEventArgs e)
            {
                try
                {
                    if (CurrentBar < Math.Max(Len,activeBar)) return;
    
                    if(e.MarketDataType == MarketDataType.Last)
                    {
                        if (lastPrice>0)
                        {
                            if (e.Price>lastPrice)
                            {
                                up++;
                                lastDir=1;
                            }
                            else if (e.Price<lastPrice)
                            {
                                down++;
                                lastDir=-1;
                            }
                            else
                            {
                                if (lastDir==1)
                                    up++;
                                else if (lastDir==-1)
                                    down++;
                            }
                        }
                        
                        upticks[0]=up;
                        downticks[0]=down;
                        
                        Plot1[0] = emaUp[0]-emaDown[0];                    
                        
                        lastPrice = e.Price;
                    }
                }
                catch(Exception ex)
                {
                    Log(ex.ToString(),LogLevel.Error);
                }
            }
    
    protected override void OnBarUpdate()
            {
                if (CurrentBar < activeBar)
                    return;
    
                if(CurrentBar != activeBar)
                {
                    up = 0;
                    down = 0;
                    activeBar    = CurrentBar;
                }
            }

    #2
    I would try moving:
    emaUp = EMA(upticks,Length);
    emaDown = EMA(downticks,Length);

    Into a section State.DataLoaded...

    I think upticks & downticks are not valid yet (no data) which means they may be causing the EMA to crash.

    I do have to tell you I find debugging in NT8 more difficult as it may be the multitasking framework that seems to hide what is actually happening within the code.

    Comment


      #3
      I tried that, but I get the same "Unhandled exception" error. I also tried calling the EMA's directly from the OnMarketData() method and got the same error.

      Comment


        #4
        Originally posted by SystemTrading View Post
        I tried that, but I get the same "Unhandled exception" error. I also tried calling the EMA's directly from the OnMarketData() method and got the same error.
        What is the text of the exception?

        Comment


          #5


          My Trace Log also has the following:

          2015-06-09 12:18:08:955 *************** unhandled exception trapped ***************
          2015-06-09 12:18:08:955 Exception has been thrown by the target of an invocation.
          2015-06-09 12:18:08:958 System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
          at NinjaTrader.NinjaScript.Indicators.MyTickIndicator .get_Zero()
          --- End of inner exception stack trace ---
          at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
          at System.Reflection.RuntimeMethodInfo.UnsafeInvokeIn ternal(Object obj, Object[] parameters, Object[] arguments)
          at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
          at System.Reflection.RuntimePropertyInfo.GetValue(Obj ect obj, Object[] index)
          at NinjaTrader.NinjaScript.IndicatorBase.get_DisplayN ame()
          at NinjaTrader.NinjaScript.IndicatorBase.OnAfterSetSt ate()
          at NinjaTrader.NinjaScript.NinjaScriptBase.SetState(S tate state)
          at NinjaTrader.Gui.NinjaScript.IndicatorRenderBase.Se tState(State state)
          at NinjaTrader.NinjaScript.NinjaScriptBase.SetState(S tate state)
          at NinjaTrader.Gui.NinjaScript.IndicatorRenderBase.Se tState(State state)
          at NinjaTrader.Gui.Chart.ChartControl.<>c__DisplayCla ss28e.<RefreshIndicators>b__27c(Object o)
          at System.Threading.ExecutionContext.RunInternal(Exec utionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
          at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
          at System.Threading.QueueUserWorkItemCallback.System. Threading.IThreadPoolWorkItem.ExecuteWorkItem()
          at System.Threading.ThreadPoolWorkQueue.Dispatch()
          Attached Files

          Comment


            #6
            I see this line:
            if (CurrentBar < Math.Max(Len,activeBar)) return;


            But I don't see where "Len" is defined. Is this possible less than Length?

            Comment


              #7
              Originally posted by NJA_MC View Post
              I see this line:
              if (CurrentBar < Math.Max(Len,activeBar)) return;


              But I don't see where "Len" is defined. Is this possible less than Length?
              Len should be Length. This is just an error in my post, in my code I am using:

              if (CurrentBar < Math.Max(Length,activeBar)) return;

              Comment


                #8
                have you found a solution to this problem?

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by RubenCazorla, Today, 09:07 AM
                2 responses
                13 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by i019945nj, 12-14-2023, 06:41 AM
                7 responses
                82 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by timmbbo, 07-05-2023, 10:21 PM
                4 responses
                158 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by tkaboris, Today, 08:01 AM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by Lumbeezl, 01-11-2022, 06:50 PM
                31 responses
                820 views
                1 like
                Last Post NinjaTrader_Adrian  
                Working...
                X