Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bid decrease statement

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

    Bid decrease statement

    Hi,

    I'm trying to make a statement for the Bid decrease, similar to High[0] < High[1] for example for bars. I need to check if the current Bid (the real time live Bid) is lower than its previous occurence (the Bid just printed milliseconds before).

    I've just learned the difference between GetCurrentBid()/Ask() and stream Bid()/Ask() from that post, but I'm not sure how to implement it.
    Hello I am trying to code an indicator signalling when a bid - ask imbalance occurs and I'm not sure I understand which of the two codes below I should use in what situation Thank you protected override void OnBarUpdate() { currentAsk = GetCurrentAsk(); currentAskVolume = GetCurrentAskVolume(); ......... }



    Code:
    protected override void OnBarUpdate()
    {
    currentBidLive = GetCurrentBid();
    }
    Code:
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    if (marketDataUpdate.MarketDataType == MarketDataType.Bid)
    {
    currentBidLast = marketDataUpdate.Price;
    }
    }
    How can I make sure the currentBidLast is the Bid just printed milliseconds ago?

    Thank you for your tips!





    #2
    Hello PaulMohn,

    Thanks for your post.

    GetCurrentBid/GetCurrentAsk looks ahead to get the most recent bid/ask value straight from the level 1 data feed.

    If you want to pick up on the last bid in relativity to the current bid, you would have to use OnMarketData and look at MarketDataType.Bid events (real-time only). If you want to look at the previous bid in historical, you would need to add a custom Series<double> variable and store MarketDataType.Bid to the series to access historical MarketDataType.Bid values from the series.

    See the help guide documentation below for more information.
    OnMarketData: https://ninjatrader.com/support/help...marketdata.htm
    MarketDataEventArgs: https://ninjatrader.com/support/help...aeventargs.htm
    GetCurrentBid: https://ninjatrader.com/support/help...currentbid.htm
    GetCurrentAsk: https://ninjatrader.com/support/help...currentask.htm
    Series<T>: https://ninjatrader.com/support/help...t8/seriest.htm

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      I'd like to test your 1st suggestion thanks.
      Do you have samples of OnMarketData looking MarketDataType.Bid events?

      How do I pick up on the last bid in relativity to the current bid with OnMarketData and MarketDataType.Bid events?

      I don't understand how to access it.

      So far here's my code:

      Code:
       protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
      {
           if (marketDataUpdate.MarketDataType == MarketDataType.Bid &&
               GetCurrentBid() < MarketDataType.Bid)
           {
                Print("Bid = " + marketDataUpdate.Price + " " + marketDataUpdate.Volume);
           }
      }
      Last edited by PaulMohn; 12-17-2021, 11:50 AM. Reason: last bid access

      Comment


        #4
        Hello PaulMohen,

        Thanks for your note.

        You could see an example of using OnMarketData in the OnMarketData help guide page linked in my previous post. This example code is also seen below.

        The help guide shows that you would check to see if marketDataUpdate.MarketDataType == MarketDataType.Bid and then you could print information about MarketDataType.Bid such as the Price and Volume.

        Code:
        protected override void OnMarketData(MarketDataEve ntArgs marketDataUpdate)
        {
            // Print some data to the Output window
            if (marketDataUpdate.MarketDataType == MarketDat aType.Last)
                  Print(string.Format("Last = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume ));
            else if (marketDataUpdate.MarketDataType == Mark etDataType.Ask)
                Print(string.Format("Ask = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume ));
        [B]    else if (marketDataUpdate.MarketDataType == Mark etDataType.Bid)
                Print(string.Format("Bid = {0} {1}", marketDataUpdate.Price, marketDataUpdate.Volume));[/B]
        }
        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Thanks for the reference. I did see it.
          What I don't understand is how to access the previous Bid (last bid) from it.
          With bars it's straightforward (for example High[0] < High[1]).
          How do I do the same with MarketDataType.Bid? MarketDataType.Bid[1]? What else?
          Thank you.

          Comment


            #6
            Hello PaulMohen,

            Thanks for your note.

            To access historical MarketDataType.Bid price information, you would need to create a Series<double> variable, then in OnMarketData() you would need to save the MarketDataType.Bid price to that Series<double> variable. Once the price is saved to the variable, you could use that variable for conditions or actions in your script similar to calling High[1].

            For example, say you have a Series<double> variable named myDoubleSeries that holds the MarketDataType.Bid price. You could use the code below to print the current and last MarketDataType.Bid price.

            // Prints the current and last bar value
            Print("The values are " + myDoubleSeries[0] + " " + myDoubleSeries[1]);


            See the Series<T> help guide page in post #2 for more information and examples.

            Let us know if we may assist further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Thank you very much for the example.

              I've tried adding a MarketDataType.Bid DataSeries but got these 2 errors:


              BidDecrease.cs The best overloaded method match for 'NinjaTrader.NinjaScript.NinjaScriptBase.AddDataSe ries(NinjaTrader.Data.BarsPeriod)' has some invalid arguments CS1502 52 5

              BidDecrease.cs Argument 1: cannot convert from 'NinjaTrader.Data.MarketDataType' to 'NinjaTrader.Data.BarsPeriod' CS1503 52 19


              Here's my code so far:


              Code:
              namespace NinjaTrader.NinjaScript.Indicators
              {
                   public class BidDecrease : Indicator
                   {
                        private Series<double> myDoubleSeries;
              
                        protected override void OnStateChange()
                        {
                             if (State == State.SetDefaults)
                             {
                                  Description = @"Enter the description for your new custom Indicator here.";
                                  Name = "BidDecrease";
                                  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 = true;
                             }
                             else if (State == State.Configure)
                             { 
                                   AddDataSeries(MarketDataType.Bid);
                             }
                        }
              
                        protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
                        {
                             if (marketDataUpdate.MarketDataType == MarketDataType.Bid &&
                                   myDoubleSeries[0] < myDoubleSeries[1])
                             { 
                                 Print("The values are " + myDoubleSeries[0] + " " + myDoubleSeries[1]);
                             }
                        }
              
                        protected override void OnBarUpdate()
                        {
                             if (GetCurrentBid() < currentBidLast)
                             {
              
                             }
                        }
              }
              }

              I've looked at the documentation but there are no MarketDataType.Bid examples (but AddDataSeries("AAPL", BarsPeriodType.Minute, 30, MarketDataType.Ask); ):

              https://ninjatrader.com/support/help...historical_bid _ask_serie.htm

              What's the correct way to add the MarketDataType.Bid?

              Also, where do I need to put the statement? In the OnBarUpdate method or in the OnMarketData method?

              Thank you.

              Comment


                #8
                Hello Len,

                Thanks for your note.

                In your script I see that you are calling AddDataSeries(MarketDataType.Bid); which is not a valid use of AddDataSeries();

                You could add a historical MarketDataType.Bid series to your script by calling something like the following in State.Configure.

                Code:
                //Add an ES 03-22 data series using the Bid series
                AddDataSeries("ES 03-22", BarsPeriodType.Minute, 30,  MarketDataType.Bid);
                When calling AddDataSeries() to add an additional Bars object to your script, a constructor overload will be available which takes a MarketDataType enumeration as an argument. This will allow you to specify the price series which will be used in that particular object. If you were to pass in MarketDataType.Ask or MarketDataType.Bid, as in the example below, that particular data series will use that price type for all of its PriceSeries collections, such as Close, Open, High, and Low.
                .
                That said, you could use AddDataSeries(); to add an additional series and specify MarketDataType.Bid. You could then check for the BarsInProgress value of that added series and call Close[0], to get the current MarketDataType.Bid close price. For example, it may look something like this if the series is the first added series in a script. Note the primary series will have a BarsInProgress value of 0, the first added series will have a BarsInProgress value of 1, and so on.

                Code:
                protected override void OnBarUpdate()
                {
                    if (BarsInProgress == 1)
                    {
                        // Print current close of first added series which uses MarketDataType.Bid price type
                        Print("Current Close: " + Close[0]);
                        // Print previous close of first added series which uses MarketDataType.Bid price type
                        Print("Previous Close: " + Close[1]);
                    }
                }
                The above will use MarketDataType.Bid as its price type for the close price since that is specified in AddDataSeries(). So we check if the added series is the series that is processing by checking the BarsInProgress value and we print the current Close price and previous Close price for that series.

                See this help guide documentation for more information on using historical Bid/Ask series: https://ninjatrader.com/support/help..._ask_serie.htm

                Let us know if we may assist further.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  Thanks for the great explanation.

                  I've updated my script below and it's printing, but I'm not sure it's the right values (screenshot attached).
                  On CL I see more than 20 ticks delta between current and previous Close. It seems it's printing the 1 minute closes, not the Bid closes.

                  Why is it not printing the Bids?

                  Code:
                  namespace NinjaTrader.NinjaScript.Indicators
                  {
                       public class BidDecrease : Indicator
                       {
                            private Series<double> myDoubleSeries;
                  
                            protected override void OnStateChange()
                            {
                                 if (State == State.SetDefaults)
                                 {
                                      Description = @"Enter the description for your new custom Indicator here.";
                                      Name = "BidDecrease";
                                      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 = true;
                                 }
                                 else if (State == State.Configure)
                                 {
                                      // Add a secondary data series to sync our Secondary Series<double>
                                      AddDataSeries("", BarsPeriodType.Minute, 30, MarketDataType.Bid);
                                 }
                            }
                  
                            protected override void OnBarUpdate()
                            {
                  
                                 if(CurrentBar<2) return;
                  
                                 if (BarsInProgress == 1 && Close[0] < Close[1])
                                 {
                                      // Print current close of first added series which uses MarketDataType.Bid price type
                                      Print("Current Close: " + Close[0]);
                                      // Print previous close of first added series which uses MarketDataType.Bid price type
                                      Print("Previous Close: " + Close[1]);
                                 }
                            }
                  
                       }
                  }

                  I've test the OnMarketData methode with below code but it's not printing:

                  Code:
                  namespace NinjaTrader.NinjaScript.Indicators
                  {
                       public class BidDecrease : Indicator
                       {
                            private Series<double> myDoubleSeries;
                  
                            protected override void OnStateChange()
                            {
                                 if (State == State.SetDefaults)
                                 {
                                      Description = @"Enter the description for your new custom Indicator here.";
                                      Name = "BidDecrease";
                                      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 = true;
                                 }
                                 else if (State == State.Configure)
                                 {
                                      // Add a secondary data series to sync our Secondary Series<double>
                                      AddDataSeries("", BarsPeriodType.Minute, 30, MarketDataType.Bid);
                                 }
                            }
                  
                            protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
                            {
                                 if (marketDataUpdate.MarketDataType == MarketDataType.Bid &&
                                           myDoubleSeries[0] < myDoubleSeries[1])
                                 {
                                      Print("The values are " + myDoubleSeries[0] + " " + myDoubleSeries[1]);
                                 }
                            }
                  
                  //       protected override void OnBarUpdate()
                  //      {
                  
                  //           if(CurrentBar<2) return;
                  
                  //           if (BarsInProgress == 1 && Close[0] < Close[1])
                  //           {
                  //                // Print current close of first added series which uses MarketDataType.Bid price type
                  //                Print("Current Close: " + Close[0]);
                  //                // Print previous close of first added series which uses MarketDataType.Bid price type
                  //                Print("Previous Close: " + Close[1]);
                  //           }
                  //      }
                  
                       }
                  }
                  Thank you.
                  Attached Files

                  Comment


                    #10
                    Hello PaulMohen,

                    Thanks for your note.

                    Since the added series is a 30-minute series using the MarketDataType.Bid price type, the prints would occur for the Close value of that 30-minute series using MarketDataType.Bid for the price type.

                    Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                    Ultimately, to understand why the script is behaving as it is, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                    In the script add prints that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

                    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                    https://ninjatrader.com/support/foru...121#post791121

                    Please let me know if I may further assist
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Thanks for the note on 30-minutes series. I've changed it to the tick series (in 1st code of post #9)

                      AddDataSeries("", BarsPeriodType.Tick, 1, MarketDataType.Bid);

                      Now it's printing correct value it seems (screenshot).

                      I'll check as you suggest with larger prints analysis.

                      Thank you.
                      Attached Files

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      580 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      335 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      102 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      554 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      552 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X