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

Bid/Ask During Backtest

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

    Bid/Ask During Backtest

    My strategy is blocking waiting for Bid/Ask data during a backtest. I've established that I needed to enable TickReplay for this. But its still blocking.

    I notice the docs for Tick replay at:


    says this...

    "NinjaTrader stores the best bid price and best ask price as the last trade occurs during the MarketDataType.Last event and provides it per the table below:"

    Why is bid and ask data being provided during the MarketDataType.Last event? This is not how live Bid/Ask updates happen. I've programmed my strategy to look for Bid updates during the MarketDataType.Bid event and Ask data during the MarketDataType.Ask event - seems logical, right?

    However, despite working perfectly in live and in playback, backtesting causes my strategy to block waiting for bid ask data that never comes. Due to waiting for the wrong event.

    Help me understand this madness, and what I can do to ensure that I get Bid/Ask updates during the appropriate event in Live/Playback - but also get the Bid/Ask in backtesting when they arrive in the wrong event.

    #2
    Hello reach4thelasers,

    From the help guide:
    "3. Tick Replay ONLY replays the Last market data event, and only stores the best inside bid/ask price at the time of the last trade event. You can think of this as the equivalent of the bid/ask price at the time a trade was reported. As such, historical bid/ask market data events (i..e, bid/ask volume) DO NOT work with Tick Replay. To obtain those values, you need to use a historical bid/ask series separately from TickReplay through OnBarUpdate()"

    TickReplay is recorded as a single stream of last ticks and is not recorded as 3 separate streams.

    Because of this, Ask and Bid will not trigger in OnMarketData. Instead, add a bid and or ask series to your script and get these values in OnBarUpdate.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      This seems a bit silly, because I need to program my strategy differently for Replay and Live. When actually it should be agnostic of how its being run.

      I get that its difficult to get actual bid and ask data in a non-live environement, But why doesn't Ninjatrader internally just provide the Last price as the Bid, and add on a pip or two to simulate the spread and provide that as the Ask Price? I don't really need the bid and ask during replay, but as it stands my strategy is waiting forever for a price that will never come.

      Bid/Ask are not important during Replay, but they are important during live. Not so much for the basic MovingAverageCrossover EnterLong EnterShort type strategy. But when dealing with large trade volumes Limit orders are essential and the Bid/Ask needs to be carefully factored in.

      I can add an edge case for now. How can I detemine if market data is coming via a Backtest? FYI my market data code is agnostic of strategy, so I don't have a strategy reference.

      Comment


        #4
        Hello reach4thelasers,

        When you say "Replay" you are referring to doing a backtest with historical data with TickReplay enabled, and you are not referring to Playback with Market Replay data, is this correct?

        Historical data is fundamentally different than real-time data. TickReplay is meant as an assist to avoiding adding multiple series for simple scripts to have intra-bar actions when in historical data but this does have limitations. Your script may not be able to take advantage of TickReplay when backtesting and I would recommend adding a 1 tick ask and 1 tick bid series.

        See the following for more details.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I can do a playback using the playback connection. I can't do back-tests using the StrategyAnalyzer. I rely heavily on Bid/ASK market data in hundreds of different places in a 200-Class Strategy.

          My strategy waits for a MarketDataType.Bid or Ask event - like would happen in live. But these events never fire during a Strategy Analyzer backtest so my strategy waits forever.

          What should I do?

          Comment


            #6
            Hello reach4thelasers,

            You should add a 1 tick ask series and 1 tick bid series. This will cause OnBarUpdate to trigger for the historical ask ticks and bid ticks.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks Chelsea, I will try that over the weekend

              Comment


                #8
                Chelsea,

                I've made a spike which tries to get Bid/Ask data during a backtest (Strategy Analyzer).

                The attached strategy subscribes to MarketData updates the only two ways I know how...

                - MarketDataUpdate Event on the Instrument class
                - Update Event on the MarketData class

                Neither of these events Fire at all - There is no printed output from all three methods, and breakpoints are not triggered.

                I've downloaded 3 months work of HistoricalData with Bid, Ask, Last and Tick checked.
                Attached Files

                Comment


                  #9
                  Hello reach4thelasers,

                  Adding a 1 tick ask series and 1 tick bid series will cause OnBarUpdate to trigger for the historical ask ticks and bid ticks.

                  I highly recommend you review the thread where I have detailed intra-bar granularity that I linked in post #4.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Did I not add 1 Tick Bid and ask series in that spike. I'm on my phone now so cant check

                    Comment


                      #11
                      Hello reach4thelasers,

                      From your previous post, it sounds as if you are expecting OnMarketData to trigger. This will trigger. Instead OnBarUpdate() will trigger for each tick.

                      May I confirm that you have added a print to OnBarUpdate to print the BarsInProgress and time and you are not getting prints for the ask tick data or bid tick data?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Ok this is going nowhere. You've clearly not read my posts properly or understood my issue.

                        I have a very complex strategy that utilises MarketData updates. I need the current market bid and ask in many many many different places in lots of interdependent classes. I don't need bar data I just occasionally need to know the current price. It's a trading platform and yet I can't get prices out of the damn thing.

                        To summarise the state of the various connection types in this regard:
                        • Live connection - provides Bid, Ask and Last Prices via market data updates
                        • Historical Data Playback Connection - provides Bid, Ask and Last Prices via market data updates
                        • MarketReplay Playback Connection - provides Bid, Ask and Last Prices via market data updates
                        • Simulated Data Feed - provides Bid, Ask and Last Prices via market data updates
                        • Strategy Analyser - DOES NOT provide Bid or Ask data via market data updates


                        Why?? Why is this different from EVERY other connection type. And what can I do about it without changing my entire implementation just so I can do back tests?
                        Last edited by reach4thelasers; 06-15-2017, 02:23 PM.

                        Comment


                          #13
                          Hello reach4thelasers,

                          I'm trying to convey that you would no longer use OnMarketData() as by adding the 1 tick ask and bid series this would put that same information in OnBarUpdate() so that this can be used by the Strategy Analyzer historically.

                          Lets say you want to know the current ask price halfway through the building of the primary bar. You would simply check the Close[0] when BarsInProgress is 1 any time while that bar is building and this will give you the current ask price.

                          You will not be able to get the ask and bid historically from OnMarketData. You must add a 1 tick ask series and 1 tick bid series and get the updates for these from OnBarUpdate.

                          The Strategy Analyzer is not a connection type.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            I understand your recommendation about using bar updates on a secondary series to obtain bid and Ask data. Message received and understood. However this requires me to change my entire implementation and maintain two versions of a very complex strategy. It is not an acceptable solution. Bid and Ask data are supposed to be supplied by the MarketData event.

                            And let's not get hung up on semantics. The internal working of a strategy Analyser session is basically a high speed historical Playback Connection. For all intents and purposes it's the same and should not have different behaviour. How on earth does a Simulator session provide fake bid and ask Data but a Market Analyser session provides nothing? It's a simple solution... Just make Bid Price equal to Last Price and add on a Tick for the Ask price and push it out on the MarketDataUpdate event. Better than providing nothing at all which is not realistic and breaks strategies and indicators that depend on these data.

                            I would like one of your P.Ms to take over this thread please and explain the rational behind this.
                            Last edited by reach4thelasers; 06-15-2017, 03:44 PM.

                            Comment


                              #15
                              Hello reach4thelasers,

                              The Strategy Analyzer uses a different data source than a real-time connection.

                              This is why you can backtest even if you are disconnected because this is read from historical data file only.

                              Historical data and real-time data are fundamentally different. The behavior is fundamentally different.

                              If you need your script to process historically, then add the tick series and don't use OnMarketData. Start the script this way because its necessary if this is what you want.

                              Its your choice how you code the script. However, if you want historical bid and ask prices, this is how its able to pull that information from historical data files.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              40 views
                              0 likes
                              Last Post alifarahani  
                              Started by Waxavi, Today, 02:10 AM
                              1 response
                              19 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X