Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnMarketDepth metrics and bid/ask spread

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

    OnMarketDepth metrics and bid/ask spread

    Is possible to make an indicator from OnMarketDepth metrics and visualize the Spread bid/ask ?

    Ninja compile this code,but nothing other than 1 is visualized.
    Can someone give me some suggestions?
    I allegate the core fragment.

    Thanks

    Paolo

    --------

    namespace NinjaTrader.Indicator
    {

    [Description("Plots the Total of Buy and Sell Orders shown in the rows of the order book of an instrument.")]
    //[Gui.Design.DisplayName("Spread Level")]

    public class BidAskLevel: Indicator
    {
    #region Variables
    // Wizard generated variables
    int maxrows=10;
    bool init =false;
    double totAVol;
    double totAVol1,totAVol2,totAVol3;
    int idx1;
    int idx2;
    int AskDataType=0,BidDataType=0,InsertDataType=0,Updat eDataType=0,RemoveDataType;
    double totBVol;
    private DateTime lastRefresh;
    // User defined variables (add any user defined variables below)
    private long activeBar = -1;
    private double buy = 0;
    private string MM;
    private double sell = 0;
    private double delta = 0;

    private bool showBidAskVolume = true;
    private List<LadderRow> askRows = new List<LadderRow>();
    private List<LadderRow> bidRows = new List<LadderRow>();
    private double askTotY;
    private double bidTotY;
    private bool firstPaint = true;
    private System.Collections.ArrayList alBuy = new System.Collections.ArrayList();
    private System.Collections.ArrayList alSell = new System.Collections.ArrayList();
    private System.Collections.ArrayList alDelta = new System.Collections.ArrayList();


    #endregion

    /// <summary>
    ///
    /// </summary>
    protected override void Initialize()
    {

    Add(new Plot(Color.Red, "DepthIns"));
    Add(new Plot(Color.RoyalBlue, "DepthBid"));
    Add(new Plot(Color.Yellow, "DepthUpd"));
    Add(new Plot(Color.Cyan, "Spread"));


    Add(new Line(Color.DimGray, 0, "zeroLine"));
    Lines[0].Pen.DashStyle = DashStyle.Dash;
    Lines[0].Pen.Width=2;


    CalculateOnBarClose = false;
    Overlay = false;
    PriceTypeSupported = false;
    PaintPriceMarkers = true;
    AutoScale = true;
    }

    protected override void OnBarUpdate()

    {

    if(CurrentBar<3)return;
    }


    protected override void OnMarketDepth(MarketDepthEventArgs e)
    {
    // try, catch will handle any runtime errors, preventing the program from crashing
    try{ // this { is the start of the area that is being monitored for errors
    // there may be runtime errors when this starts up in NT7
    {

    List<LadderRow> rows = null;

    // Checks to see if the event involves ask data
    if (e.MarketDataType == MarketDataType.Ask)
    {
    rows = askRows;
    AskDataType=+1;
    }

    // Checks to see if the event involves bid data
    else if (e.MarketDataType == MarketDataType.Bid)
    {
    rows = bidRows;
    BidDataType=+1;
    }

    if (rows == null) // event involves neither bid nor ask data
    return;

    #if NT7 // This switch will prevent the bracketed code from being compiled in NT6.5.
    // In NT7, MUST explcitly create list rows before inserting into them, to prevent runtime error
    { // Could also have used the Add method but this is more fun.
    if(rows.Count<4) //Create as many rows as you will need for your Level II data. I don't know if you can get more than 20.
    { idx1 =0;
    for (idx1 = 0; idx1 < maxrows; idx1++)
    { //insert placholder objects of type LadderRowrows into the list
    askRows.Insert(idx1, new LadderRow((int)0,(int) 0, ""));
    bidRows.Insert(idx1, new LadderRow((int)0,(int) 0, ""));
    }
    }
    }
    #endif
    // In NT7, insertion will fail if there is not already an empty row to insert into


    // Checks to see if the action was an insertion into the ladder
    if (e.Operation == Operation.Insert)
    {
    rows.Insert(e.Position, new LadderRow(0,(int) e.Volume, ""));
    InsertDataType=+1;
    }
    /* Checks to see if data were removed from the ladder

    -> check if e.Position is within valid range */
    else if (e.Operation == Operation.Remove && e.Position < rows.Count)
    {
    rows.RemoveAt(e.Position);
    RemoveDataType=+1;
    }
    /* Checks to see if the action was an update of data within the ladder
    -> check if e.Position is within valid range */
    else if (e.Operation == Operation.Update && e.Position < rows.Count)
    {
    rows[e.Position].Volume =(int) e.Volume; //VOLUME IS A DOUBLE IN NT7
    UpdateDataType=+1;
    }



    try{
    if(CurrentBar<3)return;


    totAVol = 0;
    totBVol = 0;
    for (idx2 = 0; idx2 < maxrows; idx2++)
    {
    if(idx2<askRows.Count)totAVol += askRows[(int)idx2].Volume;
    if(idx2<bidRows.Count)totBVol -= bidRows[(int)idx2].Volume;

    Values[0].Set(InsertDataType);
    Values[1].Set(BidDataType);
    Values[2].Set(UpdateDataType);
    Values[3].Set(askRows[(int)0].Price-bidRows[(int)0].Price);

    }
    } catch (Exception ex){

    Print("Trapped exception in XXX OnMarketDepth" + ex.Message);
    return;
    }

    }
    } //This is the end of the region that is being monitored for runtime errors
    catch (Exception ex){
    // Following is what we do if there is an error, sure beats crashing the indicator
    Print("Trapped exception in XXXX.OnMarketDepth: " + ex.Message);
    return; //prevents the indicator from crashing
    }
    }

    ----

    #2
    Unfortunately we could not debug the code posted for you - but why are you not using OnMarketData() (Level 1 info) or just straight GetCurrentBid / GetCurrentAsk() in the OnBarUpdate()? That would be much simpler for accessing the current spread.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    606 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    353 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    105 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    560 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    561 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X