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

OnMarketData and OnBarUpdate

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

    OnMarketData and OnBarUpdate

    Greetings

    I've created an indicator where the calculations are being done in OnMarketData and the inserted into the indicator array in OnBarUpdate. However it seems data coming into OnMarketData at the begining of the bar is not being updated in OnBarUpdate.

    Here is the code snippet:

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Ask) m_LastAskPrice = e.Price;
    else if (e.MarketDataType == MarketDataType.Bid) m_LastBidPrice = e.Price;

    else if (e.MarketDataType == MarketDataType.Last)
    {
    if (m_LastAskPrice <= e.Price) AskVol = e.Volume;
    else if (m_LastBidPrice >= e.Price) BidVol = e.Volume;
    }
    }

    protected override void OnBarUpdate()
    {
    //keep updating currentbar
    if (m_ActiveBar == CurrentBar)
    {
    Ask.Set(AskVol );
    Bid.Set(BidVol );
    }
    //new bar: reset
    else
    {

    Ask.Set(0);
    Bid.Set(0);

    m_ActiveBar = CurrentBar;
    }

    }

    The output from OnMarketData matches T&S perfectly. The only problem is that some values are not being inserted into the data array.


    Is the basic approach correct? Is there a better way to synch OnBarUpdate and OnMarketData?

    Please advise. Thanks in advance.

    #2
    Hello,


    OnBarUpdate and OnMarketData are not sync-able. These are different threads and can execute at completely different times. This may be the source of your issue.

    Also, your m_ActiveBar variable seems like it may be involved as it looks like you will not get information from the first tick of the first bar because m_ActiveBar = CurrentBar does not happen until AFTER the first tick of the new bar. (Assuming you have CalculateOnBarClose set to false.) I suggest using if(FirstTickOfBar). You will need to change your logic a bit but I think you will be better off.
    DenNinjaTrader Customer Service

    Comment


      #3
      Hello Ben

      Thanks for reply. FirstTickOfBar solved one of the problems, thanks. However one remains which is: when bids and asks are updated in OnMarketData but there is no corresponding tick for OnBarUpdate, naturally the indicators do not get updated. I've only been testing this indicator during the after market hours for symbol ES where ticks can be scarce at times. I will have to see if this is a problem during the RTH.

      Is there a way around this problem?

      Thanks in advance.

      Comment


        #4
        Hello,


        I am not sure what you are try to accomplish exactly but you may want to put your plot.Set(...) in the OnMarketData() block of code.
        DenNinjaTrader Customer Service

        Comment


          #5
          Hi Ben

          Thanks for the reply.

          The indicator is just a cummulation of ask and bid sizes for a given bar. In other words, it is an attempt to take all the ask and all the bid sizes in T&S from start of a bar to its end and then sum them up respectively.

          I've tried placing the plot.set(...) method in OnMarketData but the results were the same. I will make another try.

          Regrds.

          Comment


            #6
            Drive all your logic from OnMarketData() and just reset your aggregate variables in OnBarUpdate(). Run with CalculateOnBarClose = true so you know a bar has closed when you get an OnBarUpdate() event.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thanks, Josh. I will try that.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by gentlebenthebear, Today, 01:30 AM
              2 responses
              13 views
              0 likes
              Last Post gentlebenthebear  
              Started by Kaledus, Today, 01:29 PM
              2 responses
              7 views
              0 likes
              Last Post Kaledus
              by Kaledus
               
              Started by frankthearm, Yesterday, 09:08 AM
              13 responses
              45 views
              0 likes
              Last Post frankthearm  
              Started by PaulMohn, Today, 12:36 PM
              2 responses
              16 views
              0 likes
              Last Post PaulMohn  
              Started by Conceptzx, 10-11-2022, 06:38 AM
              2 responses
              56 views
              0 likes
              Last Post PhillT
              by PhillT
               
              Working...
              X