Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsRequest and MarketData subscription

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

    BarsRequest and MarketData subscription

    Hello,

    I have some issues with subscription to MarketData events (Level1 or Time&Sales) on BarsRequest objects.

    I can subscribe well to OnBarUpdate (Update event) from BarsRequest objects and works fine.

    But I need to subscribe also to MarketData event from BarsRequest objects.
    If I subscribe to MarketDataUpdate event from BarsRequest.Bars.Instrument.MarketDataUpdate looks like all works fine: first an OnBarUpdate event is received and then inmediately an OnMarketData event is received. The volumens match exactly and the time secuency is correct.

    The next image reports the following sequence:
    First: OnBarUpdate event, with Close price and Volume of the bar.
    Next: MarketDataUpdate event, with Last Price and volume of the print.




    The bug happens when I try unsubscribe to MarketDataUpdate when the indicator is Terminated. Then I get an exception, and the connection continues live receiving OnMarketData events.




    Other alternative is subscribe to BarsRequest.Bars.Instrument.MarketData.Update like the Help advises. In this case the desubscription doesn't raise any exception, but the live events (OnBarUpdate vs OnMarketData) are very unsynchronized.



    But the main issue is that some times the OnMarketData events are duplicated as you can see in the image.



    I attach the code. It is an indicator but should works equal like add-on (the bugs are the same).
    For example, I load the indicator on ES 06-17 Range-4, 3-days.

    In short, there are two bugs:
    1.- Subscribing to MarketDataUpdate: exception when I make the disposing (bug).
    2.- Subscribing to MarketData.Update: desynchronization (can be expected) and duplicated events (bug).

    Sorry for the length but my english level is not high and wanted explain correctly the context.

    Thanks.
    Attached Files

    #2
    Hello cls71, and thank you for your question. I noticed this was an Indicator. May I ask why we are attempting to use Market Data subscriptions, rather than simply using the built-in hook to OnMarketData ? From an indicator, the following code will, by itself, allow you to subscribe to market data events :

    Code:
    // http://ninjatrader.com/support/helpGuides/nt8/en-us/onmarketdata.htm
    // http://ninjatrader.com/support/helpGuides/nt8/en-us/marketdataeventargs.htm
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
        // Print some data to the Output window
        if (marketDataUpdate.MarketDataType == MarketDataType.Last)
              Print(string.Format("Last = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume));
        else if (marketDataUpdate.MarketDataType == MarketDataType.Ask)
            Print(string.Format("Ask = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume));
        else if (marketDataUpdate.MarketDataType == MarketDataType.Bid)
            Print(string.Format("Bid = {0} {1}", marketDataUpdate.Price, marketDataUpdate.Volume));
    }
    This said if there is a reason such as code reuse for wanting to subscribe to market data the same way you would from an add-on, you will need to use Bars.Instrument.MarketData.Update and not Instrument.MarketDataUpdate. This example from the help guide may clear up how it is intended these sorts of market data subscriptions are designed to work.

    Code:
    // http://ninjatrader.com/support/helpGuides/nt8/en-us/marketdata.htm
    /* Example of subscribing/unsubscribing to market data from an Add On. The concept can be carried over
     to any NinjaScript object you may be working on. */
     public class MyAddOnTab : NTTabPage
     {
          private MarketData marketData;
      
          public MyAddOnTab()
          {
               // Subscribe to market data. Snapshot data is provided right on subscription
               // Note: "instrument" is a placeholder in this example, you will need to replace 
               // with a valid Instrument object through various methods or properties available depending
               // on the NinjaScript type you are working with (e.g., Bars.Instrument or Instrument.GetInstrument()
               marketData = new MarketData(instrument);
               marketData.Update += OnMarketData;
      
               // Printing snapshot market data for the last price and time
               NinjaTrader.Code.Output.Process(marketData.Last.Price.ToString() + " " + marketData.Last.Time.ToString(),
                    PrintTo.OutputTab1);
          }
      
          // This method is fired on market data events
          private void OnMarketData(object sender, MarketDataEventArgs e)
          {
               // Do something with market data events
          }
      
          // Called by TabControl when tab is being removed or window is closed
          public override void Cleanup()
          {
               // Make sure to unsubscribe to the market data subscription
               if (marketData != null)
                    marketData.Update -= OnMarketData;
          }
      
          // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code.
     }
    You are correct that these events are asynchronous. This publicly available MSDN C# code will show you how to use a lock statement to synchronize multiple asynchronous threads.

    https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

    As you are using Ninja in a way other than intended we can not consider these behaviors bugs. However, if you can describe how you would like Ninja to work I will be happy to present a case for a new feature being added.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hello JessicaP,

      How I already said in my previous post, when I subscribe via Instrument.MarketData.Update I receive duplicated events.

      I attach an add-on that would be the expected way, instead an indicator. In order to reproduce the bug, please follow these steps:
      1.- Load the add-on. A new menu item is added which name is "Bars Request Bug".
      2.- Open the NinjaScript Output window.
      3.- Connect to one realtime datafeed with ES 06-17 marketdata.
      4.- Click on menu "Bars Request Bug".

      From now you will receive events of type MarketData. You will see them in NinjaScript Output.

      5.- If you observe closely you will see the marketdata events are duplicated. Select an infrequent volume and you will detect better the bug. In the attached image I have selected an volumen equal 21. As you can see, above and below each volume, events are repeated.



      So it seems like this is not about locks. The attached code is simple and I think has not any error.

      Regards
      Attached Files

      Comment


        #4
        Thank you for your report.

        This behavior was confirmed on our end. We will be investigating this further. Please keep an eye on the NinjaTrader 8 Release Notes page for updates and bugfixes.

        http://ninjatrader.com/support/helpG...ease_notes.htm

        Tracking ID: NTEIGHT-11394
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hello cls71,

          I have some feedback regarding this behavior. This behavior is to be expected. If you compare the output of the two attached scripts, an add-on and an indicator, you will see that they output the same data. The duplicate ticks are coming in from your data feed provider. If you would like a convenient way to filter out these duplicate ticks please examine the dictionary inside the Test11394.cs add-on.

          Steps to reproduce
          1) Place Test11394Addon.cs in bin\Custom\AddOns folder
          2) Place Test11394Indicator.cs in bin\Custom\Indicators folder
          3) In NT open a NS Editor and compile
          4) Restart NT8
          5) New -> NinjaScript Output
          6) New -> Chart -> ES 06-17 1 minute
          7) Connect to data
          8) Apply Test11394Indicator.cs to chart
          9) Wait a little bit then copy everything from output tab 1 into one side of a diff checker and everything from output tab 2 into the other side
          10) Line up the results by time (Because we didnt start both subscriptions at the same time -> Delete some of the beginning of the addon output)
          11) Do the diff

          You will see that the results are exactly the same.
          Attached Files
          Jessica P.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by michaelsh, Today, 04:28 PM
          0 responses
          4 views
          0 likes
          Last Post michaelsh  
          Started by Bmatarese, Today, 03:13 PM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_ChrisL  
          Started by sdauteuil, Today, 12:32 PM
          4 responses
          17 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by Gaterz, 09-27-2020, 05:52 PM
          10 responses
          51 views
          0 likes
          Last Post tcea9
          by tcea9
           
          Started by derivtrader, Today, 12:04 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Spencer  
          Working...
          X