Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Developing using OnMarketData Update

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

    Developing using OnMarketData Update

    I want to use some info from the OnMarketData method such as "last" but when I did a inital test, I sometimes get a 0 value when trying to store the Last traded price to be used in OnBarUpdate. Please see below:

    Code:
            protected override void OnBarUpdate()
            {    
    
            Print(Time[0] + "   OnBarUpdate Print: " + OMDPrice);    
    
    
    
    
    
            }
    
            protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
            {
                OMDPrice = marketDataUpdate.Price; 
    
                if (marketDataUpdate.MarketDataType == MarketDataType.Last)
                    Print(marketDataUpdate.Time + "   OMD Last = " + OMDPrice + " @ " + marketDataUpdate.Volume);
    
                else if (marketDataUpdate.MarketDataType == MarketDataType.Ask)
                    Print(marketDataUpdate.Time + "   Ask = " + marketDataUpdate.Price + " @ " + marketDataUpdate.Volume);
    
                else if (marketDataUpdate.MarketDataType == MarketDataType.Bid)
                    Print(marketDataUpdate.Time + "   Bid = " + marketDataUpdate.Price + " @ " + marketDataUpdate.Volume);
    
            }
    here's the output. I also noticed the in-sequence in time where the OMD Last might be something. I don't want to say it's out of order, or out of sync as im sure there is a good reason for why it is displaying the way it does, i am just learning how to work with OnMarketDataUpdate so any insights would be helpful

    11/18/2019 11:02:19 PM Bid = 3124 @ 25
    11/18/2019 11:02:19 PM Bid = 3124 @ 24
    11/18/2019 11:03:00 PM OnBarUpdate Print: 3124
    11/18/2019 11:03:00 PM OnBarUpdate Print: 3124
    11/18/2019 11:02:19 PM OMD Last = 3124 @ 1
    11/18/2019 11:03:00 PM OnBarUpdate Print: 0
    11/18/2019 11:03:00 PM OnBarUpdate Print: 0
    11/18/2019 11:02:19 PM OMD Last = 3124 @ 1
    11/18/2019 11:02:19 PM Bid = 3124 @ 22
    11/18/2019 11:02:19 PM Bid = 3124 @ 21

    #2
    Hello Boonfly8,

    This is because the assignment is not inside of your Last price condition with your print. OnMarketData is called for many reasons besides ask/last/bid prices so the Price variable is not always set. Moving this inside your condition which is for last prices would avoid that:

    Code:
    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
    {
         OMDPrice = marketDataUpdate.Price; 
         Print(marketDataUpdate.Time + "   OMD Last = " + OMDPrice + " @ " + marketDataUpdate.Volume);
    }
    Your OnBarUpdate print will follow whatever is being set to the variable so if you are setting a 0 for one of the events which you are not specifically observing that would be the reason. You can use the following print outside of your conditions from OnMarketData to better see what data is coming through the override:

    Code:
    Print( marketDataUpdate.ToString());
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thanks for your note, totally had a blip moment and should have thought about assigning the variable inside the Last price condition.

      As for a general question. I basically would use information from OnMarketDataUpdate to be used for calculations that is happening in OnBarUpdate. Would you recommend having the method before OnBarUpdate? Or for my purposes, what best-practices would be used if I just want to grab values and assign them to variables to be used in OnBarUpdate?

      Comment


        #4
        Hello Boonfly8,

        How you have it now would be fine you just need to limit setting the variable to the Last event specifically. The order of your methods in your script otherwise does not matter, the platform will still call them in the correct order.

        Code:
        if (marketDataUpdate.MarketDataType == MarketDataType.Last)
        {
             OMDPrice = marketDataUpdate.Price; 
        }
        After surrounding the variable with the last condition that will then be set each time there is a last update, when OnBarUpdate is called it could then read the value that was set.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by schmidtbc16, Today, 03:36 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by RT001, Today, 03:25 PM
        2 responses
        15 views
        0 likes
        Last Post RT001
        by RT001
         
        Started by mbrandau, Today, 01:58 PM
        5 responses
        37 views
        0 likes
        Last Post mbrandau  
        Started by remcovdmeulen, Today, 02:15 PM
        1 response
        13 views
        0 likes
        Last Post NinjaTrader_Jim  
        Started by gbrad, Today, 09:57 AM
        7 responses
        17 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X