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

How do I retrieve Bid, Ask and Last?

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

    How do I retrieve Bid, Ask and Last?

    I'm trying to get the bid, ask and last. I know how to get the Open[0], High[0], Low[0] and Close[0]...how do I get the bid, ask and last?

    #2
    d.allen,

    I am happy to assist you,

    There are a couple ways to do this. So I may give you the best suggestion, could you please let me know what you are trying to do with this information?

    I.e. you can either add these as dataseries to have access to historical bid/ask/last or if you only need the most current bid/ask/last you can get them a different way that's a bit easier.

    I look forward to assisting you.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      It would really be beneficial if you could just point me to what topics and threads I need to learn/understand in order to manipulate 'real-time' data. I'm trying to get up to speed ASAP with building real time systems. I'd rather not have you show me how to do one specific thing, I'd rather just learn everything I need to know so I can get going and I want have to keep coming back asking a bunch of questions.

      Comment


        #4
        d.allen,

        Apologies, I was just trying to give you what you needed rather than bombard you with information. Some people can get overwhelmed so I was only trying to be considerate of your needs. I am more than happy to provide as much information as you would like.

        Essentially, there are two methods for getting Bid/Ask. These are :

        GetCurrentBid() and GetCurrentAsk(), they will get the most current bid/ask even on historical bars however in back-testing the current close will be substituted unless you use the other method I am about to mention. If CalculateOnBarClose = false, then Close[0] will be Last for the default instrument (the one the strategy is attached to).

        For more information on GetCurrentBid() and GetCurrentAsk(), please see the following links.

        You can also use a multi-timeframe strategy to get the Ask/Bid/Last and add them as dataseries with all historical information accessible. The drawback is you will not have current Bid/Ask/Last unless you use "CalculateOnBarClose = false". I suspect from what you mention you will be using this to have your strategy trade tick-by-tick rather than at the closing of bars, but correct me if I am wrong. Please be aware that your data provider may not offer historical bid/ask data.

        You can add these as dataseries to your strategy the following way :

        Code:
        protected override void Initialize()
        {
        Add(instrumentName, periodType, period, MarketDataType.Ask);
        Add(instrumentName, periodType, period, MarketDataType.Bid);
        Add(instrumentName, periodType, period, MarketDataType.Last);
        
        CalculateOnBarClose = false;
        
        }
        
        protected override void OnBarUpdate()
        {
            if ( BarsInProgress == 0)
            {
                  Print("This is the default instrument close : "+Close[0].ToString());
            }
            else if ( BarsInProgress == 1)
            {
                  Print("This is the instrumentName ask : "+Close[0].ToString());
            }
            else if ( BarsInProgress == 2)
            {
                 Print("This is the instrumentName bid: "+Close[0].ToString());
            }
            else if ( BarsInProgress == 3)
            {
                 Print("This is the instrumentName last: "+Close[0].ToString());
            }
        
            // other code here 
        }
        Essentially, OnBarUpdate is called each time one of the data series is updated, and you identify which is being updated by the "BarsInProgress" value.

        For more information on multi-timeframe strategies, please see the following link.

        For more information on Add(), please see the following link.

        Please keep in mind that backtesting and real-time trading have some discrepancies.

        For more information on the differences between backtesting and real-time trading, please see the following link.

        For a reference sample on getting intra-bar granularity in backtesting, please see the following link.

        Please let me know if I may assist further.
        Last edited by NinjaTrader_AdamP; 12-09-2011, 01:49 PM.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          thanks adam! no...i wasn't being arrogant or ungrateful I'd just rather go ahead and study really hard and get all of the grunt work out of the way and when I have specific questions that I can't seem to figure out on my own then I can come to you guys for assistance, that way it's a more efficient use of BOTH of our time.

          thanks for the code example, links and your willingness to help me out!

          Comment


            #6
            d.allen,

            No worries, I didn't think you were either of those things.

            Yes, please don't hesitate to contact us should you require additional assistance, or have any questions.

            Thanks for picking NinjaTrader!
            Adam P.NinjaTrader Customer Service

            Comment


              #7

              I know this post was a long while ago, but I wanted to ask if under NT 8 using this approach is faster than simply enabling on tick replay? (I know the code shown won't run in NT 8, but i updated it to do so). I have an indicator similar to the built-in “ BuySellVolume” indicator, and always want to see the historical plots (usually just a few days)--but with "enable tick replay" this seems to really bog the computer down (especially given I am doing this on about 6 charts).


              Is this method less of a resource hog?
              Last edited by Jim-Boulder; 04-24-2020, 04:07 PM.
              Jim-Boulder
              NinjaTrader Ecosystem Vendor - Elephant Tracks

              Comment


                #8
                Hi Jim-Boulder, thanks for your question.

                GetCurrentBid() and GetCurrentAsk() are used in real-time to get the very latest bid/ask update from the data feed, so this is irrelevant for historical processing. There's an expected amount of performance to be taken away during loading time with Tick Replay enabled. If your indicator and the BuySellVolume indicator have the same calculation time with tick replay enabled then that would be expected.

                We have a guide on developing for tick replay here that explains how to utilize Tick Replay in historical calculation:


                Please let me know if I can assist any further.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Chris for the reply.

                  I apologize that my question was vague.

                  My intended question was which of these 2 approaches are less of a resource hog if my intention is to plot historically volume at bid vs ask.

                  A) The 3-dataseries added with Bid, Ask, Last prices , with "Update on each Tick"
                  or
                  B) Using OnMarketUpdate event (as the BuySellVolume indicator does) with TickReplay enabled

                  (assume that I have the rest of the code to do that with either stream of data)

                  and now in writing this, I realize I have one more question, and that is whether I need to make the added dataseries' 1 Tick BarType/Pd?

                  If you'd like I can repost to a new thread if this is more involved that I am imagining it to be.

                  Thanks Chris
                  --Jim
                  Jim-Boulder
                  NinjaTrader Ecosystem Vendor - Elephant Tracks

                  Comment


                    #10
                    Hello Jim,

                    Chris is out of the office at this time.

                    Adding historical Bid/Ask/Last series will add 3 single tick data streams and can impact performance greater than say using Tick Replay which just plays through MarketDataType.Last ticks for the data series we enabled with Tick Replay. I also could not share any examples for accomplishing your goal with additional bid/ask historical streams.

                    Tick Replay is heavy on resources, so we suggest using a Multi Time Frame approach where possible. (This is the same approach our Order Flow + indicators take.)

                    I have attached some example indicators that can demonstrate using this approach. OneTickMultiSeriesTemplate describes how this approach can be taken, and BuySellPressureOneTick implements this approach to calculate BuySellPressure. You can compare BuySellPressureOneTick against OneTickMultiSeriesTemplate and the BuySellPressure indicator that comes with NinjaTrader to see how you can calculate historical plots from Bid/Ask stamped tick data instead of using Tick Replay.

                    I look forward to assisting.
                    Attached Files
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Jim! Just what I was looking for!!
                      Jim-Boulder
                      NinjaTrader Ecosystem Vendor - Elephant Tracks

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by chbruno, Today, 04:10 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post chbruno
                      by chbruno
                       
                      Started by josh18955, 03-25-2023, 11:16 AM
                      6 responses
                      436 views
                      0 likes
                      Last Post Delerium  
                      Started by FAQtrader, Today, 03:35 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post FAQtrader  
                      Started by rocketman7, Today, 09:41 AM
                      5 responses
                      19 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by frslvr, 04-11-2024, 07:26 AM
                      9 responses
                      127 views
                      1 like
                      Last Post caryc123  
                      Working...
                      X