Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Want to Plot Bid - ask difference as a line

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

    Want to Plot Bid - ask difference as a line

    Hello friends,

    How can I plot Bid-ask aka Spread as a Continues line. Current and also on Past data?

    GetCurrentBid(0) - GetCurrentAsk(0) will give me diffence but how to plot them ?

    and also How to plot historical spread as a line?

    Thank you

    #2
    Hello svadukia,

    Thank you for your note.

    For real time data, you could use something like this:

    Code:
    Values[0][0] = Math.Abs((GetCurrentBid() - GetCurrentAsk()) * 10000);
    To get the historical bid and ask information, however, you'll need to first turn on Tick Replay.

    If you enable Tick Replay, you will be able to use your indicator with historical data and some special coding. To turn the global Tick Replay option on, navigate to Control Center > Tools > Options and under the Market Data category, check "Show Tick Replay". Once that is complete, right click on your chart and select "Data Series" from the menu. Select the dataseries you are using and check the Tick Replay box.

    Here are some links regarding using Tick Replay:

    https://ninjatrader.com/support/helpGuides/nt8/en-us/power_volume_indicators.htm

    Once you've set up Tick Replay, you'll need to set up your indicator to get that data. In this case, it's going to come through as an OnMarketData event so we'll need to watch for it there:

    Code:
            private double currentSpread;
    
            protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
            {
                 // TickReplay events only occur on the "Last" market data type
                 if (marketDataUpdate.MarketDataType == MarketDataType.Last)
                 {
                      currentSpread = Math.Abs((marketDataUpdate.Bid - marketDataUpdate.Ask) * 10000);
    
                 }
            }
    So since we only need tick replay for our historical data, and our real time data will use what we've got set up in OnBarUpdate() with GetCurrentBid() and GetCurrentAsk(); we can now go back and change what we've got there a bit. Here's my finished example:

    Code:
    public class ForexSpread : Indicator
        {    
            private double currentSpread;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                            = @"Shows a Bid and Ask Line";
                    Name                                = "ForexSpread";
                    Calculate                            = Calculate.OnPriceChange;
                    IsAutoScale                            = false;
                    IsOverlay                            = false;
                    DisplayInDataBox                    = false;
                    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;
    
                    AddPlot(new Stroke(Brushes.Red,    1),PlotStyle.Line,"Spread");
    
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {    
                if(State == State.Historical)
                    Values[0][0] = currentSpread;
                else if (State == State.Realtime)
                    Values[0][0] = Math.Abs((GetCurrentBid() - GetCurrentAsk()) * 10000);
    
            }
    
            protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
            {
              // TickReplay events only occur on the "Last" market data type
              if (marketDataUpdate.MarketDataType == MarketDataType.Last)
              {
                 currentSpread = Math.Abs((marketDataUpdate.Bid - marketDataUpdate.Ask) * 10000);
    
              }
            }
        }
    I'd try that out and you can then modify it as you wish.

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Originally posted by NinjaTrader_Kate View Post
      Hello svadukia,

      Thank you for your note.

      For real time data, you could use something like this:

      Code:
      Values[0][0] = Math.Abs((GetCurrentBid() - GetCurrentAsk()) * 10000);
      To get the historical bid and ask information, however, you'll need to first turn on Tick Replay.

      If you enable Tick Replay, you will be able to use your indicator with historical data and some special coding. To turn the global Tick Replay option on, navigate to Control Center > Tools > Options and under the Market Data category, check "Show Tick Replay". Once that is complete, right click on your chart and select "Data Series" from the menu. Select the dataseries you are using and check the Tick Replay box.

      Here are some links regarding using Tick Replay:

      https://ninjatrader.com/support/helpGuides/nt8/en-us/power_volume_indicators.htm

      Once you've set up Tick Replay, you'll need to set up your indicator to get that data. In this case, it's going to come through as an OnMarketData event so we'll need to watch for it there:

      Code:
       private double currentSpread;
      
      protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
      {
      // TickReplay events only occur on the "Last" market data type
      if (marketDataUpdate.MarketDataType == MarketDataType.Last)
      {
      currentSpread = Math.Abs((marketDataUpdate.Bid - marketDataUpdate.Ask) * 10000);
      
      }
      }
      So since we only need tick replay for our historical data, and our real time data will use what we've got set up in OnBarUpdate() with GetCurrentBid() and GetCurrentAsk(); we can now go back and change what we've got there a bit. Here's my finished example:

      Code:
      public class ForexSpread : Indicator
      {
      private double currentSpread;
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Shows a Bid and Ask Line";
      Name = "ForexSpread";
      Calculate = Calculate.OnPriceChange;
      IsAutoScale = false;
      IsOverlay = false;
      DisplayInDataBox = false;
      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;
      
      AddPlot(new Stroke(Brushes.Red, 1),PlotStyle.Line,"Spread");
      
      }
      else if (State == State.Configure)
      {
      }
      }
      
      protected override void OnBarUpdate()
      {
      if(State == State.Historical)
      Values[0][0] = currentSpread;
      else if (State == State.Realtime)
      Values[0][0] = Math.Abs((GetCurrentBid() - GetCurrentAsk()) * 10000);
      
      }
      
      protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
      {
      // TickReplay events only occur on the "Last" market data type
      if (marketDataUpdate.MarketDataType == MarketDataType.Last)
      {
      currentSpread = Math.Abs((marketDataUpdate.Bid - marketDataUpdate.Ask) * 10000);
      
      }
      }
      }
      I'd try that out and you can then modify it as you wish.

      Please let us know if we may be of further assistance to you.
      Totally understood.

      Thank you very much for your assistance.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      599 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      344 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      103 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      558 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      557 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X