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

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.
    Kate W.NinjaTrader Customer Service

    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 hazylizard, Today, 08:38 AM
      0 responses
      1 view
      0 likes
      Last Post hazylizard  
      Started by Max238, Today, 01:28 AM
      5 responses
      42 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by giulyko00, Yesterday, 12:03 PM
      3 responses
      12 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by habeebft, Today, 07:27 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ChristopherS  
      Started by AveryFlynn, Today, 04:57 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_Erick  
      Working...
      X