Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnMarketData called multiple times

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

    OnMarketData called multiple times

    Hello,

    I have some trouble understanding the methods OnMarketData and OnBarUpdate.
    I want to get the current ask/bid price (and not a historical value) every time a new tick comes in and save it to a variable. When the second tick comes in, I can compare the value from tick 2 with tick 1. I'm using "OnMarketData(..)" for that, but somehow it gets called more frequently than a new tick comes in.

    I have created a simple script just to log the incoming ticks:

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Ask)
    {
    Print (GetCurrentAsk());
    }
    }

    This method is called sometimes 5 times a second, although just one tick came in per second. Why?

    Also, I don't use OnBarUpdate(), because whenever I start the script, it returns all previous ticks. I don't want any historical data when I start my script.

    Any advice?

    Thank you!

    Robert

    #2
    Hello Robert,

    Thank you for your note.

    GetCurrentAsk() and GetCurrentBid() are usually accessed within the OnBarUpdate() method and e.Price is used for the OnMarketData() method. For example:
    Code:
    		protected override void OnMarketData(MarketDataEventArgs e)
    		{
    			if(e.MarketDataType == MarketDataType.Ask)
    			{
    				Print("Ask: " + e.Price);
    				Print("Time of Ask: " + Time[0]);
    			}
    		}
    For information on the MarketDataEventArgs (such as e.Price) please visit the following link: http://www.ninjatrader.com/support/h...aeventargs.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Hello Patrick,

      thanks for your reply.
      When running this script, I get the following output:
      Code:
      Time of Ask: 22.10.2013 18:36:32
      Ask: 8936
      Time of Ask: 22.10.2013 18:36:32
      Ask: 8936
      Time of Ask: 22.10.2013 18:36:32
      Ask: 8936
      Time of Ask: 22.10.2013 18:36:32
      Ask: 8936
      Time of Ask: 22.10.2013 18:36:32
      Ask: 8936
      Time of Ask: 22.10.2013 18:36:32
      Ask: 8936
      Time of Ask: 22.10.2013 18:36:32
      As you can see, I get the same price at the same time several times.
      I'm connected to ZenFire.
      Maybe it is not a problem with the script, but with ZenFire, which sends the same data several times?

      Edit: It must be related to ZenFire, because when I run the simulated data feed, everything is fine.
      Last edited by roberttk; 10-22-2013, 10:49 AM.

      Comment


        #4
        Hello Robert,

        Thank you for your response.

        This is reporting exactly as it should. There can be multiple trades placed to the Ask price and in turn would call OnMarketData().
        Last edited by NinjaTrader_PatrickH; 11-17-2013, 10:36 PM.

        Comment


          #5
          Originally posted by NinjaTrader_PatrickH View Post
          Hello Robert,

          Thank you for your response.

          This is reporting exactly as it should. There can be multiple trades placed to the Ask price and in turn would call OnMarketDate().
          Hi

          The above relates to similar questions I had on the usage of OnMarketData.

          But first of all, for Robert's query, Isn't it better to check e.MarketDataType==MarketDataType.Last and then compare e.Price to a local variable, for example private double prevTickPrice?

          if (e.MarketDataType == MarketDataType.Last)
          {
          // code to process NEW tick

          // reset previous tick price
          prevTickPrice=e.Price;
          }


          Which leads to my 2 questions:

          1. How often will OnMarketData() be called for each new trade?

          - I was under the impression that it would be called only once
          - but, OnMarketData() is a Level 1 event, right, so it will also be called when there is a change in Bid/Ask Price & Size?
          - so, if you check e.MarketDataType==MarketDataType.Ask and e.Price (as mentioned in your response), aren't you just looking at the last Ask Price, which is not necessarily the Last trade price?
          - ie, if you first of all check for e.MarketDataType==MarketDataType.Ask or e.MarketDataType==MarketDataType.Bid, you need to reference e.MarketData.Last.Time, e.MarketData.Last.Price, e.MarketData.Last.Volume to get the Last trade's details

          2. Finally (if the previous bullet points of mine are correct), when I want to access ALL the Level 1 data for the Last trade, am I right in thinking that I need to first of all check e.MarketDataType ==MarketDataType.Last then reference:
          - e.Price, e.Volume, e.MarketData.Bid.Price, e.MarketData.Bid.Volume, e.MarketData.Ask.Price, e.MarketData.Ask.Volume


          Please can you confirm.
          Last edited by pbmi66; 11-16-2013, 09:21 PM. Reason: Typo mistake

          Comment


            #6
            Hello pbmi66,

            Thank you for your post and welcome to the NinjaTrader Support Forum!
            Originally posted by pbmi66 View Post
            if (e.MarketDataType == MarketDataType.Last)
            {
            // code to process NEW tick

            // reset previous tick price
            prevTickPrice=e.Price;
            }
            This will work if you wish to ensure the last traded price has updated before checking further values. OnMarketData() will only be called once per update to the market data types, which means a trade must occur for the update to occur. And you are correct in that you need to check Last to check for updates to the last traded price.

            Please let me know if I may be of further assistance.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Max238, Today, 01:28 AM
            1 response
            22 views
            0 likes
            Last Post CactusMan  
            Started by giulyko00, Yesterday, 12:03 PM
            2 responses
            10 views
            0 likes
            Last Post giulyko00  
            Started by r68cervera, Today, 05:29 AM
            0 responses
            4 views
            0 likes
            Last Post r68cervera  
            Started by geddyisodin, Today, 05:20 AM
            0 responses
            6 views
            0 likes
            Last Post geddyisodin  
            Started by JonesJoker, 04-22-2024, 12:23 PM
            6 responses
            38 views
            0 likes
            Last Post JonesJoker  
            Working...
            X