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

Difference between GetCurrentBid and Close[0]

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

    Difference between GetCurrentBid and Close[0]

    Hello
    I am working on a DataSeries with BarsPeriodType in 1 Tick. It means i get data every 1 tick for the dataseries. I check Close[0] in OnBarUpdate and also GetCurrentBid().
    Are these values the same always in my scenario? In this strategy i will get the same value for GetCurrentBid() and Close[0]?

    #2
    Hello amiralimadadi,

    Thanks for your post.

    GetCurrentBid() and GetCurrentAsk() will return the Ask/Bid immediately as it comes in from the Level 1 data stream. It would not be the same as a "Last" tick that we typically use for data series in OnBarUpdate() that could be an Ask or Bid. It also would not always return the same thing as the using Close[0] on a data series of MarketDataType.Bid when there is a lot of activity because GetCurrentBid() skips ahead of of internal OnBarUpdate() logic and fetches that price directly from the level 1 feed.

    Please let me know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Unfortunately i did not get your point. Please describe more clear.
      Let me ask you my main question:


      Suppose that we are Trading in ZN, I want to send market order when price is 119'25'5. When i use SuperDom(static) i can see when Bid = 119'25'0 it means Ask price is a tick upper and it equals 119'25'5 so if i send market order the price would be equals to Ask and it is 119'25'5. It is totally OK when i trade manually.


      When i want to do this automatically, i GetCurrentBid() in every Tick in OnBarUpdate(). if GetCurrentBid() = 119'25'0 then EnterLong() executed. but the order filled in 119'25'0 not in 119'25'5.
      why this is happening? i check Close[0] and it always equals to GetCurrentBid() but i want to send market order with a tick upper price and it is 119'25'5.
      Did you get what i said? i can explain more if you want
      Last edited by amiralimadadi; 06-23-2018, 06:18 AM.

      Comment


        #4
        Hello amiralimadadi,

        GetCurrentBid() and GetCurrentAsk() get the bid/ask price before it gets passed through other methods that we use like OnBarUpdate() and OnMarketData(). When using a data series with MarketDataType.Last, Close[0] can be a bid or an ask. When using MarketDataType.Bid, Close[0] will be a bid price, but it may not be the "current bid" provided there is a lot of activity at that time. GetCurrentBid() would get you the most recent bid at that moment before the data gets passed to the typical NinjaScript methods.

        If you are backtesting with historical data, the simulator will use the bar's Open High Low and Close values to fill orders. Additionally, GetCurrentBid() and GetCurrentAsk() will return the Close price during State.Historical.

        GetCurrentBid() - https://ninjatrader.com/support/help...currentbid.htm

        With Realtime data we may see the following:
        Code:
        protected override void OnBarUpdate()
        {
        	if(State == State.Historical)
        		return;
        	if(GetCurrentBid() == Close[0] && Position.MarketPosition == MarketPosition.Flat)
        	{
        		Print(String.Format("Bid: {0} Ask: {1} Last: {2}", GetCurrentBid(), GetCurrentAsk(), Close[0]));
        		EnterLong();
        	}
        }
        
        protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, 
        	Cbi.MarketPosition marketPosition, string orderId, DateTime time)
        {
        	Print("Fill Price: " + price);
        }
        Output:
        Bid: 119.75 Ask: 119.765625 Last: 119.75
        Fill Price: 119.765625
        You may test the code above using realtime data or using Market Replay data in the Playback Connection.

        If you are trading live or with a demo account, slippage would be a factor to consider as well. I would recommend setting up prints as I have to see the submitted price for GetCurrentBid(), GetCurrentAsk() and the Close price as well as the execution price to further visualize these orders getting filled and the prices involved.

        Please let me know if there is anything I may do to help.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thank you for your complete explanation.

          Comment


            #6
            Originally posted by NinjaTrader_Jim View Post
            Hello amiralimadadi,

            GetCurrentBid() and GetCurrentAsk() get the bid/ask price before it gets passed through other methods that we use like OnBarUpdate() and OnMarketData(). When using a data series with MarketDataType.Last, Close[0] can be a bid or an ask. When using MarketDataType.Bid, Close[0] will be a bid price, but it may not be the "current bid" provided there is a lot of activity at that time. GetCurrentBid() would get you the most recent bid at that moment before the data gets passed to the typical NinjaScript methods.

            If you are backtesting with historical data, the simulator will use the bar's Open High Low and Close values to fill orders. Additionally, GetCurrentBid() and GetCurrentAsk() will return the Close price during State.Historical.

            GetCurrentBid() - https://ninjatrader.com/support/help...currentbid.htm

            With Realtime data we may see the following:
            Code:
            protected override void OnBarUpdate()
            {
                if(State == State.Historical)
                    return;
                if(GetCurrentBid() == Close[0] && Position.MarketPosition == MarketPosition.Flat)
                {
                    Print(String.Format("Bid: {0} Ask: {1} Last: {2}", GetCurrentBid(), GetCurrentAsk(), Close[0]));
                    EnterLong();
                }
            }
             
            protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, 
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {
                Print("Fill Price: " + price);
            }
            Output:


            You may test the code above using realtime data or using Market Replay data in the Playback Connection.

            If you are trading live or with a demo account, slippage would be a factor to consider as well. I would recommend setting up prints as I have to see the submitted price for GetCurrentBid(), GetCurrentAsk() and the Close price as well as the execution price to further visualize these orders getting filled and the prices involved.

            Please let me know if there is anything I may do to help.
            Hi Jim, I want to highlight a particular statement you said above about GetCurrentBid() being updated before OnMarketData(). If this is true, does that mean GetCurrentBid() is the fastest method to get the most recent bid in an update cycle? Is this true when you use GetCurrentBid() in OnBarUpdate() or in OnMarketData()? Or doesn't matter?

            I was always under the impression that OnMarketData() event args are the fastest as they update per event coming in.

            Comment


              #7
              Hello Boonfly8,

              Thank you for your post.
              Originally posted by Boonfly8 View Post
              Hi Jim, I want to highlight a particular statement you said above about GetCurrentBid() being updated before OnMarketData(). If this is true, does that mean GetCurrentBid() is the fastest method to get the most recent bid in an update cycle? Is this true when you use GetCurrentBid() in OnBarUpdate() or in OnMarketData()? Or doesn't matter?

              I was always under the impression that OnMarketData() event args are the fastest as they update per event coming in.
              The GetCurrentBid() and GetCurrentAsk() pull from a "snapshot" that was to the instrument thread when data was first passed from the adapter thread. This is the most current Bid and Ask value and should only be used in OnBarUpdate() as it is called before OnMarketData().

              OnMakretData() events are updates to the Ask and Bid price from the instrument thread after the OnBarUpdate() pass.

              Please see the post at the following link for further technical detail: https://ninjatrader.com/support/foru...6&postcount=22

              Please let me know if you have any questions.

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Hello Boonfly8,

                Thank you for your post.

                The GetCurrentBid() and GetCurrentAsk() pull from a "snapshot" that was to the instrument thread when data was first passed from the adapter thread. This is the most current Bid and Ask value and should only be used in OnBarUpdate() as it is called before OnMarketData().

                OnMakretData() events are updates to the Ask and Bid price from the instrument thread after the OnBarUpdate() pass.

                Please see the post at the following link for further technical detail: https://ninjatrader.com/support/foru...6&postcount=22

                Please let me know if you have any questions.
                hey Patrick, thank you for the clarification. Does this also remain true for GetCurrenBidVolume() and GetCurrentAskVolume()?

                Also, because OnBarUpdate is updated before OMD, and as far as I understand, onbarupdate is updated once every bar (or tick if use calculate tick). Then after the new tick update, then, OMD should be faster than OBU no?

                Comment


                  #9
                  Hello Boonfly8,

                  Thank you for your response.

                  This is the same case for the GetCurrentBidVolume() and the GetCurrentAskVolume() as it is with the GetCurrentBid() and GetCurrentAsk().

                  OnMarketData() updates do occur when the OnBarUpdate() is not called. Such as update to Ask and Bid that do not affect the Last update if the Bar (which updates OnBarUpdate) is set to the Last Price.

                  Please let me know if you have any questions.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by gemify, 11-11-2022, 11:52 AM
                  6 responses
                  803 views
                  2 likes
                  Last Post ultls
                  by ultls
                   
                  Started by ScottWalsh, Today, 04:52 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by ScottWalsh, Today, 04:29 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by rtwave, 04-12-2024, 09:30 AM
                  2 responses
                  22 views
                  0 likes
                  Last Post rtwave
                  by rtwave
                   
                  Started by tsantospinto, 04-12-2024, 07:04 PM
                  5 responses
                  70 views
                  0 likes
                  Last Post tsantospinto  
                  Working...
                  X