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

Accessing Incoming Tick Data - continued

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

    Accessing Incoming Tick Data - continued

    Hi. I finally got around to coding an indicator to track the incoming ticks.

    in the OnBarUpdate method, I have the following code:

    if (MarketData!=null)

    {

    if (MarketData.Bid!=null)

    {

    Print(MarketData.Bid.Time.ToString()+" "+MarketData.Bid.Price.ToString()+" "+MarketData.Ask.Price.ToString()+" "+MarketData.Bid.Volume.ToString()+"x"+MarketData. Ask.Volume.ToString());

    Log(MarketData.Bid.Time.ToString("MMddyyyy"),Marke tData.Bid.Time.ToString("yyyy/MM/dd")+","+MarketData.Bid.Time.ToString("HH:mm:ss")+ ","+MarketData.Bid.Price.ToString()+","+MarketData .Ask.Price.ToString()+","+MarketData.Bid.Volume.To String()+","+MarketData.Ask.Volume.ToString());

    }

    if (MarketData.Last!=null)

    {

    Print(MarketData.Last.Time.ToString()+" "+MarketData.Last.Price.ToString()+" "+MarketData.Last.Volume.ToString());

    Log(MarketData.Last.Time.ToString("MMddyyyy"),Mark etData.Last.Time.ToString("yyyy/MM/dd")+","+MarketData.Last.Time.ToString("HH:mm:ss") +","+MarketData.Last.Price.ToString()+","+MarketDa ta.Last.Volume.ToString());

    }

    }



    The Log method will just write to a file.

    My problem is this:

    I've noticed that the bid/ask does not always agree with my esignal "Time and Sales" window which issupposed to show the same thing. I also noticed that in esignal, all changed bid/sales (without any previous price traded) will be displayed, but I think the OnBarUpdate() filters these changes and only returns the final bid/ask. Is this correct?

    The other problem I'm seeing is that the Last traded object does not add up with esignal. Also when I am inspecting the Bid object, it seems like the Last object may have been from a previous trade and not a part of this "tick" data (i.e., the bid/ask data). Is that correct? And if so, how can I insure that the Last object that I'm inspecting is really a new trade that occurred and not from a previous trade?

    Thanks.



    #2
    imported post

    You are coding this against the OnBarUpdate method which only fires -

    - On change in the last trade (not bid/ask)
    - There is optimization under the hood that may prevent the OnBarUpdate() method to fire on every change in last trade. For example, the last trade and price change you get the event, if only the last trade size changes and its value is the same as the prior, the event will not fire. This is because most chart indicators's values and plots will not change so therefore there is no reason to have NT re-calcualte values.

    Because of the above, you will only log the bid/ask at the current time that the OnBarUpdate() event is fired which is less frequent than NT's internal market data events. I will check to see if there is a way to code against market data events.

    Ray
    RayNinjaTrader Customer Service

    Comment


      #3
      imported post

      You can attach an event handler for all incoming market data events.

      MarketData.MarketDataItem += new MarketDataItemEventHandler(MarketData_MarketDataIt em);

      I am not sure if you can add that to the Initalize() method or if you will have to do it in the OnBarUpdate(). MarketData may still be null in the Initalize() method.

      Then create a method:

      private void MarketData_MarketDataItem(object sender, MarketDataEventArgs e)
      {
      if (e.MarketDataType == Data.MarketDataType.Ask)
      // Do something with e.Price, e.Volume or e.Time
      }

      Don't forget, the indictor display will only update when the OnBarUpdate() method fires therefore any values calculated in the market data handler need to be stored.


      RayNinjaTrader Customer Service

      Comment


        #4
        imported post

        Thanks Ray for your reply.

        Please check to see if there is a way for me to read the raw tick data that's coming in. I need to be able totrack the bid/ask (even if there is no trade in between) and each trade that occurs (without any filtering).

        Thanks.




        Comment


          #5
          imported post

          Richard,

          The replies I gave you provide a way to read the incoming market data events unfiltered in real-time. If you want to update the chart display for each incoming market data event...this is not possible since the chart refresh methods are outside of the indicator class.


          Ray
          RayNinjaTrader Customer Service

          Comment


            #6
            imported post

            Ray,

            Sorry, I didn't see your 3:34 post.

            I tried the following code:

            privatevoid MarketData_MarketDataItem(object sender, MarketDataEventArgs e)

            {

            if (e.MarketDataType == Data.MarketDataType.Ask)

            {

            // Do something with e.Price, e.Volume or e.Time

            Print("ASK: "+e.Time.ToString()+" "+e.Price.ToString()+" "+e.Volume.ToString());

            }

            elseif (e.MarketDataType == Data.MarketDataType.Bid)

            {

            // Do something with e.Price, e.Volume or e.Time

            Print("BID: "+e.Time.ToString()+" "+e.Price.ToString()+" "+e.Volume.ToString());

            }

            elseif (e.MarketDataType == Data.MarketDataType.Last)

            {

            // Do something with e.Price, e.Volume or e.Time

            Print("LAST: "+e.Time.ToString()+" "+e.Price.ToString()+" "+e.Volume.ToString());

            }

            Print("1");



            }





            ///<summary>

            /// Called on each bar update event (incoming tick)

            ///</summary>

            protectedoverridevoid OnBarUpdate()

            {

            // Use this method for calculating your indicator values. Assign a value to each

            // plot below by replacing 'Close[0]' with your own formula.

            Plot0.Set(Close[0]);

            Print("barupdate:");

            MarketData.MarketDataItem +=
            new MarketDataItemEventHandler(MarketData_MarketDataIt em);

            }

            When I put

            MarketData.MarketDataItem += new MarketDataItemEventHandler(MarketData_MarketDataIt em);

            in Initialize(), I get no error messages, but when I run my indicator, OnBarUpdate() is not firing.

            So I put it in OnBarUpdate() and now the indicator just hangs. Sorry, I am not a C# programmer. I am a Visual Studio 6.0 programmer. Maybe, I'm just doing something stupid here? But it looks like you are creating a new event handler everytime in OnBarUpdate()?

            Thanks for your help.

            Comment


              #7
              imported post

              richard wrote:
              But it looks like you are creating a new event handler everytime in OnBarUpdate()?
              Could be the case. Add some code so that it is only created once.

              private bool handlerCreated

              OnBarUpdate()
              {
              if (!handerCreated)
              MarketData.MarketDataItem += new MarketDataItemEventHandler(MarketData_MarketDataIt em);
              }


              Something like that.

              RayNinjaTrader Customer Service

              Comment


                #8
                imported post

                Ray,

                I added the check and now only create the event handler once in OnBarUpdate(). It looks like I am now getting the correct bid/ask tick data. But if you look at my Print from the Output window, it looks like the Bid, ask and Last will always fire exactly twice everytime. Why is that? Can I prevent that?

                Also, if you look, there are some lines with just a "1" printed. This means the event handler fired and the object passed was not the Ask, Bid or Last object. Is there any documentation on what else is in the MarkletData object?

                Thanks again.



                Dump of some output window lines:

                barupdate:

                BID: 4/27/2006 10:20:56 PM 1312 9

                1

                BID: 4/27/2006 10:20:56 PM 1312 9

                1

                ASK: 4/27/2006 10:20:56 PM 1312.25 39

                1

                ASK: 4/27/2006 10:20:56 PM 1312.25 39

                1

                BID: 4/27/2006 10:21:12 PM 1312 10

                1

                BID: 4/27/2006 10:21:12 PM 1312 10

                1

                ASK: 4/27/2006 10:21:12 PM 1312.25 39

                1

                ASK: 4/27/2006 10:21:12 PM 1312.25 39

                1

                LAST: 4/27/2006 10:21:27 PM 1312.25 1

                1

                LAST: 4/27/2006 10:21:27 PM 1312.25 1

                1

                1

                1

                barupdate:

                barupdate:

                BID: 4/27/2006 10:21:27 PM 1312 9

                1

                BID: 4/27/2006 10:21:27 PM 1312 9

                1

                ASK: 4/27/2006 10:21:27 PM 1312.25 38

                1

                ASK: 4/27/2006 10:21:27 PM 1312.25 38

                1

                BID: 4/27/2006 10:21:29 PM 1312 11

                1

                BID: 4/27/2006 10:21:29 PM 1312 11

                1

                ASK: 4/27/2006 10:21:29 PM 1312.25 38

                1

                ASK: 4/27/2006 10:21:29 PM 1312.25 38

                1


                Comment


                  #9
                  imported post

                  It would mean that there are likely two event handlers registered. This stuff we are getting into is really purposely undocumented sincewe arenot prepared to support this at this time. There could be two event handlers due to your code or because of something on our side. There are numerous events that would fire like daily high, low,allof which areundocumneted at this time.

                  Ray
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    imported post

                    Ray,

                    Weird. The problem went away this morning. Not sure, if its because I rebooted or the previous code (without checking if the event handler was created or not) was causing this.

                    But either way, its working for now. And I do truly appreciate your help in getting me setup on this. As a programmer, I know supporting about undocumented features.

                    Barring any further problems, what I have now should work for me. A BIG Thank You again!


                    Comment


                      #11
                      imported post

                      Great Richard and you are welcome!
                      RayNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by helpwanted, Today, 03:06 AM
                      1 response
                      14 views
                      0 likes
                      Last Post sarafuenonly123  
                      Started by Brevo, Today, 01:45 AM
                      0 responses
                      11 views
                      0 likes
                      Last Post Brevo
                      by Brevo
                       
                      Started by aussugardefender, Today, 01:07 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post aussugardefender  
                      Started by pvincent, 06-23-2022, 12:53 PM
                      14 responses
                      242 views
                      0 likes
                      Last Post Nyman
                      by Nyman
                       
                      Started by TraderG23, 12-08-2023, 07:56 AM
                      9 responses
                      387 views
                      1 like
                      Last Post Gavini
                      by Gavini
                       
                      Working...
                      X