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

OnMarketUpdate not behaving as expected

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

    OnMarketUpdate not behaving as expected

    I have a multi-series strategy but only need to capture the 2nd series (a 1-min series) in it. This is what I got:

    Code:
    protected override void OnMarketData(MarketDataEventArgs e)
    {	
    
       if (BarsInProgress == 1) { // ignore all other bar series obviously !!!
           if (e.MarketDataType == MarketDataType.Ask) 
    		 currentAsk = e.Price;
    
    	else if (e.MarketDataType == MarketDataType.Bid)
    		currentBid = e.Price;
    				
    	if (!Historical && (e.MarketDataType == MarketDataType.Ask || e.MarketDataType == MarketDataType.Bid)) {
    		double currentBASpread = currentAsk - currentBid;
    		DrawTextFixed("mode", "Spread: " + currentBASpread, TextPosition.BottomLeft, Color.Red, spreadLabelFont, Color.Transparent, Color.Transparent, 1);
    	}
    }
    Basically I am capturing and plotting the current spread. Works without the BarsInProgress condition. But as soon as I add that it doesn't plot anything. I have tried all of the series - 0, 1, etc. - none work. Only plots if I leave out the condition. But then I get a mixture of all series and I don't want that.

    What am I doing wrong?

    #2
    It looks fine, but let's see what you are really getting first.

    It might be BIP=0 for whatever reason.

    Which might imply your Add isn't working.


    Code:
    protected override void OnMarketData(MarketDataEventArgs e)
    {	
    Print ( "BIP=" + BarsInProgress );
    
       if (BarsInProgress == 1) {

    Comment


      #3
      Hello molecool,

      Thank you for your post.

      This could potentially be easier using the e.ToString and StartsWith. Here is an example:
      Code:
      		
      		protected override void OnMarketData(MarketDataEventArgs e)
      		{
      			if(e.ToString().StartsWith("Instrument='ES 09-15 Globex'"));
      				Print(e.ToString());
      		}

      Comment


        #4
        Originally posted by NinjaTrader_PatrickH View Post
        Hello molecool,

        Thank you for your post.

        This could potentially be easier using the e.ToString and StartsWith. Here is an example:
        Code:
        		
        		protected override void OnMarketData(MarketDataEventArgs e)
        		{
        			if(e.ToString().StartsWith("Instrument='ES 09-15 Globex'"));
        				Print(e.ToString());
        		}
        I'm sorry but how would that be 'easier'? I would have to capture the name of the instrument every time and submit that. Not impossible but not easier by any definition.

        In any case it seems to be working with the first (larger) series - which I guess is fine since this only works in live mode anyway. Still you guys may want to look into this for NT8 to make sure it works on a second series as well.

        Comment


          #5
          Hello molecool,

          I should explain a little bit better, but there is no bar update being called to OnMarketData(). If you are trying to limit the process based on the instrument calling OnMarketData() you can use the StartsWith idea I presented. If the bar series are all the same instrument then there would be no need to limit by BarsInProgress.

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Hello molecool,

            I should explain a little bit better, but there is no bar update being called to OnMarketData(). If you are trying to limit the process based on the instrument calling OnMarketData() you can use the StartsWith idea I presented. If the bar series are all the same instrument then there would be no need to limit by BarsInProgress.
            No, that's not the issue at all, Patrick. I'm using a 1-minute second series because I need more accurate historical data to relaunch my strategies (and for backtesting - although obviously during historical I don't get OnMarketData).

            Anyway I have since settled on the first series as it's the same in live tape (again it's the same instrument). The only problem I'm still running into is that OnMarketData doesn't always get called. Today my trigger got touched in OnBarUpdate and as I was taking an entry the current bid and ask were clearly old. The only explanation is that OnMarketUpdate wasn't called when I expected it.

            Isn't it true that a 60-minute candle series would be called on every market event in live tape? Meaning if I reduce to first series (60-minute) then I 'should' be receiving the currently active bid and ask? See here:

            ###### Short Entry at: 1097.7 #########
            GC.60
            New York: 7/27/2015 6:17:00 AM Eastern Daylight Time
            New York session is inactive.
            London: 7/27/2015 11:17:00 AM GMT Daylight Time
            London session is active.
            Tokyo: 7/27/2015 7:17:00 PM Tokyo Standard Time
            Tokyo session is inactive.
            Instrument: GC.60
            Close: 1097.7
            Theoretical Entry Price: 1097.7
            *Current Bid*: 1098.5
            Current Ask: 1098.5
            B/A Spread: 0 ticks.
            Short entry order has been submitted: Order='bfa2075b68934b8e949c3b2a40fddc55/ScalpiusDemoFutures' Name='Short Position' State=PendingSubmit Instrument='GC 08-15' Action=SellShort Limit price=1098.5 Stop price=0 Quantity=2 Strategy='Scalpius' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='bfa2075b68934b8e949c3b2a40fddc55' Gtd='12/1/2099 12:00:00 AM'

            See what happened here?

            My trigger got touched at 1097.7 - but the global currentAsk and currentBid variables were a mile away (and the spread 0 at that moment which is doubtful). Those get set in my OnMarketData method (as shown below). So this is frankly a bit puzzling for me.

            Comment


              #7
              Hello molecool,

              Thank you for your response.

              Isn't it true that a 60-minute candle series would be called on every market event in live tape? Meaning if I reduce to first series (60-minute) then I 'should' be receiving the currently active bid and ask?
              This would not affect OnMarketData(), which should be updating on each market data event and does not depend on the bar type to update like OnBarUpdate().

              Close: 1097.7
              Theoretical Entry Price: 1097.7
              *Current Bid*: 1098.5
              Current Ask: 1098.5
              Please provide the full lines of code used to print these values and advise if they are updated in OnMarketData or OnBarUpdate.

              I look forward to your response.

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Hello molecool,

                Thank you for your response.


                This would not affect OnMarketData(), which should be updating on each market data event and does not depend on the bar type to update like OnBarUpdate().


                Please provide the full lines of code used to print these values and advise if they are updated in OnMarketData or OnBarUpdate.

                I look forward to your response.
                I think you misunderstand - I am fully aware that OnMarketData() has nothing to do with OnBarUpdate and that it gets called separately. AFAIK OnBarUpdate gets called when prices moves - per tick in live tape. OnMarketData gets called on every change of the bid, ask, volume, etc.. - that is what I'm relying on.

                In any case - it seems that sometimes OnMarketData is simply not being called - even on the first series. I have experienced orders not being filled because OnMarketData had previously not received an updated bid or ask. Otherwise I cannot explain the big delta between the closes[0][0] prices and the spread I shared below.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by llanqui, Today, 10:32 AM
                0 responses
                2 views
                0 likes
                Last Post llanqui
                by llanqui
                 
                Started by StockTrader88, 03-06-2021, 08:58 AM
                45 responses
                3,992 views
                3 likes
                Last Post johntraderuser2  
                Started by TAJTrades, Today, 09:46 AM
                0 responses
                8 views
                0 likes
                Last Post TAJTrades  
                Started by rhyminkevin, Yesterday, 04:58 PM
                5 responses
                62 views
                0 likes
                Last Post dp8282
                by dp8282
                 
                Started by realblubb, Today, 09:28 AM
                0 responses
                8 views
                0 likes
                Last Post realblubb  
                Working...
                X