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

Market Data Snapshot

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

    Market Data Snapshot

    The docs for the MarketData class state

    "Snapshot data is provided to you right on subscription"



    The code example on the docs shows creating and subscribing to the Update event and having the snapshot prices available immediately:

    Code:
    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);
    However I am not seeing this behavior. The attached addon puts a connect/disconnect button on the chart trader, Subscribes to the MarketData of a random instrument, and prints out the initial snapshot of Ask, Bid, Last: and then disconnects after three updates.

    The output of consecutive clicks is as follows:

    Code:
    =============== NZDCAD Market Data Connect Snapshot: ===============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: Null
    Snapshot Bid Price is: Null
    =========== NZDCAD OnMarketDataUpdate Ask #1 ==============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: 0.9525
    Snapshot Bid Price is: Null
    =========== NZDCAD OnMarketDataUpdate Bid #2 ==============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: 0.9525
    Snapshot Bid Price is: 0.95244
    =========== NZDCAD OnMarketDataUpdate Last #3 ==============
    SnapShot Last Price is: 0.95244
    Snapshot Ask Price is: 0.9525
    Snapshot Bid Price is: 0.95244
    ==================== DISCONNECTED =====================
    =============== GBPJPY Market Data Connect Snapshot: ===============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: Null
    Snapshot Bid Price is: Null
    =========== GBPJPY OnMarketDataUpdate Ask #1 ==============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: 139.292
    Snapshot Bid Price is: Null
    =========== GBPJPY OnMarketDataUpdate Bid #2 ==============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: 139.292
    Snapshot Bid Price is: 139.281
    =========== GBPJPY OnMarketDataUpdate Last #3 ==============
    SnapShot Last Price is: 139.281
    Snapshot Ask Price is: 139.292
    Snapshot Bid Price is: 139.281
    ==================== DISCONNECTED =====================
    =============== EURCAD Market Data Connect Snapshot: ===============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: Null
    Snapshot Bid Price is: Null
    =========== EURCAD OnMarketDataUpdate Ask #1 ==============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: 1.40053
    Snapshot Bid Price is: Null
    =========== EURCAD OnMarketDataUpdate Bid #2 ==============
    SnapShot Last Price is: Null
    Snapshot Ask Price is: 1.40053
    Snapshot Bid Price is: 1.40044
    =========== EURCAD OnMarketDataUpdate Last #3 ==============
    SnapShot Last Price is: 1.40044
    Snapshot Ask Price is: 1.40053
    Snapshot Bid Price is: 1.40044
    ==================== DISCONNECTED =====================

    You'll note that after Subscription, no snapshot data is available; all the data items are NULL in the MarketData instance. they only get updated as new OnUpdate events are fired one-by-one.

    I have had to write an async MarketDataProxy using TaskCompletionSources and SemaphoreSlims to get around this and ensure that I have snapshot data available to me when I need it. However during times of low market volume there is sometimes a wait of a few seconds before I get the data.

    After re-reading the docs; I don't think this class is working as it should; and certainly not in the manner illustrated in the code example....
    Attached Files

    #2
    Hello reach4thelasers, and thank you for your question.

    First, regarding snapshots, this situation is a little difficult to explain in text, and so I have put together a video where I use your testing tool to demonstrate how snapshots are populated.



    Regarding the code sample, then, the code sample was designed to demonstrate

    • the syntax for retrieving snapshot market data from an add-on
    • how to set up a timer to capture live market data events as they come in
    • how to safely remove the timer once we are done with the strategy, so that e.g. NinjaTrader is not kept open due to a stuck thread

    I believe the code sample accomplishes these three goals.


    We appreciate your taking the time to report this, and while I hope I have helped answer your questions about NinjaTrader, if you feel the strategy guide could be made more clear, if I could ask some of your time explaining how the strategy guide could be improved, this would benefit the community greatly.


    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hey Jessica

      thanks for the clarification. I guess I thought that if Ninjatrader was connected to FXCM that it would be pulling in Price Data and it would be available immediately. So you're saying that unless I have a chart open I need to wait for new price data to filter in?

      As an alternative (let me know if this is a bad idea performance wise etc) what if I was to have make a Singleton addon that subscribes market data to an empty delegate for all FXCM's instruments (there's about 25 excluding the exotics). Would this make the data available to other instances of the MarketData class?

      As an alternative I might use FXCM's XML API https://rates.fxcm.com/RatesXML3

      Although I would rather use Ninjatrader's price data to ensure that I have the correct prices for Backtesting and Replay.

      Thank you!!

      Comment


        #4
        A single add-on that captures market data on a timer you control into singleton data series and makes this data available to other scripts you have is likely to be the best supported method. I would recommend also capturing a time series along with this, or even forgoing a Series and using a Dictionary<DateTime, double> or Dictionary<DateTime, OHLCStructure> (OHLCStructure being up to the user to define), so that you know exactly when a market data event occurred.

        This said, provided any window or script you have currently open is subscribed to a data feed, market data snapshots will populate without you writing any code.
        Jessica P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Barry Milan, Today, 10:35 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by WeyldFalcon, 12-10-2020, 06:48 PM
        14 responses
        1,428 views
        0 likes
        Last Post Handclap0241  
        Started by DJ888, Yesterday, 06:09 PM
        2 responses
        9 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        40 views
        0 likes
        Last Post jeronymite  
        Started by bill2023, Today, 08:51 AM
        2 responses
        16 views
        0 likes
        Last Post bill2023  
        Working...
        X