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

Unexpected behaviour

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

    Unexpected behaviour

    Hi,

    I am trying to build an OnMarketData indicator, which is supposed to show changes in the current bid ask depth.

    I am getting sort of unexpected bevaviour which you can see in the print statement of the indicator.

    According to my understanding the result of

    InsideAuction[e.Price].deltaBid += e.Volume - InsideAuction[e.Price].depthBid;

    should be a summation of the difference between the last and current offered depth, what I instead get is e.Volume;

    The line

    InsideAuction[e.Price].bidChange = e.Volume - InsideAuction[e.Price].depthBid;

    returns the desired result. (What should be accumulated above).

    Whats the reason for this?

    attached code

    regards and thanks

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class TestOnMarketData : Indicator
    {
    internal class insideItem
    {
    public double depthBid;
    public double deltaBid;
    public double bidChange;
    }

    private SortedList <double, insideItem> InsideAuction =
    new SortedList <double, insideItem>();

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "TestOnMarketData";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    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 = false;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Bid)
    {
    if (!InsideAuction.ContainsKey(e.Price))
    InsideAuction.Add(e.Price, new insideItem());

    InsideAuction[e.Price].bidChange = e.Volume - InsideAuction[e.Price].depthBid;
    InsideAuction[e.Price].deltaBid += e.Volume - InsideAuction[e.Price].depthBid;

    Print("Price " + e.Price + " Depth " + InsideAuction[e.Price].depthBid + " Volume " + e.Volume + " Delta " + InsideAuction[e.Price].deltaBid + " BidChange " + InsideAuction[e.Price].bidChange);

    InsideAuction[e.Price].depthBid = e.Volume;
    }
    }
    }
    }

    #2
    Hello keepsimple, and thank you for your question. I will leave this page up to give you an opportunity for someone from the community to respond. Fully debugging user code is beyond the scope of the service we may provide.

    I was able to retrieve publicly available documentation for SortedList,

    Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.


    On cursory inspection, it may help to initialize depthBid, e.g.

    Code:
    [FONT=Courier New]if (!InsideAuction.ContainsKey(e.Price))
    {
        InsideAuction.Add(e.Price, new insideItem());
        InsideAuction[e.Price].depthBid = InsideAuction[e.Price].deltaBid = InsideAuction[e.Price].bidChange = 0.0;
    }[/FONT]
    It may also help to simplify some of your other code, e.g.

    Code:
    [FONT=Courier New]InsideAuction[e.Price].bidChange = e.Volume - InsideAuction[e.Price].depthBid;
    InsideAuction[e.Price].deltaBid += InsideAuction[e.Price].bidChange;[/FONT]
    While C# typically does the former for you, and the latter should be equivalent to what you are already doing, it is my hope that these two changes will narrow down what could be occurring so that you can more easily discover what is occurring.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica,

      thanks for reply, initializing does not work, what does works is the following, but only if e.Price stays the same.

      double LastBid;

      protected override void OnMarketData(MarketDataEventArgs e)
      {
      if (e.MarketDataType == MarketDataType.Bid)
      {
      if (!InsideAuction.ContainsKey(e.Price))
      InsideAuction.Add(e.Price, new insideItem());

      InsideAuction[e.Price].bidChange = e.Volume - InsideAuction[e.Price].depthBid;

      if (e.Price != LastBid)
      InsideAuction[e.Price].deltaBid = 0;
      if (e.Price == LastBid)
      InsideAuction[e.Price].deltaBid += e.Volume - InsideAuction[e.Price].depthBid;

      Print("Price " + e.Price + " Depth " + InsideAuction[e.Price].depthBid + " Volume " + e.Volume + " Delta " + InsideAuction[e.Price].deltaBid + " BidChange " + InsideAuction[e.Price].bidChange);

      InsideAuction[e.Price].depthBid = e.Volume;
      LastBid = e.Price;
      }
      }
      }

      so I do not think the cause for the problem is a coding issue, but more a event related issue, thats why i was asking. cant really see what changes through the additional code.

      regards

      Comment


        #4
        Thank you for this additional information, keepsimple. We have not had any reports of e.Volume, e.Price, or e.MarketDataType being passed in to OnMarketData incorrectly. I will make a note to return to this thread should any reports along these lines come in.
        Jessica P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by alifarahani, Today, 09:40 AM
        3 responses
        15 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by RookieTrader, Today, 09:37 AM
        4 responses
        17 views
        0 likes
        Last Post RookieTrader  
        Started by PaulMohn, Today, 12:36 PM
        0 responses
        5 views
        0 likes
        Last Post PaulMohn  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        40 views
        0 likes
        Last Post love2code2trade  
        Started by junkone, Today, 11:37 AM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X