Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

market replay and historical replay

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

    market replay and historical replay

    Hello i'd like to have a clarification in the difference between market replay and historical data.
    when i check for the last trade tick by tick can you confirm that they are the same?
    if not why they are different?

    Another thing is : when i run in market replay of historical i'm able to open and close position.
    when i run it in the strategy analyzer i cannot, do i have to be connected to what?
    I'm connected to my continuum account when running the analyzer but i don't get any trade even though in playback it would've traded a lot.
    (If i run MA strategy it works), but not even the initial print work.

    In the analyzer can i access file in my pc like i do for the playback or not? (StreamReader ecc)

    What i did in orde to make thing clearer is:
    else if (State == State.Configure)
    {
    AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1);
    }

    so i get the series

    and then what i do to access that data serie is :
    protected override void OnMarketData(MarketDataEventArgs e)
    {

    if (e.MarketDataType == MarketDataType.Last && BarsInProgress == 1)
    {
    bla bla
    }
    }

    i thought that by doing this i'm gonna run all like if it was tick by tick and by putting the principal graph to 1 minutes i would've been able to visualize bar at 1 minute but do the calculation at 1 tick.
    This is not the case because if i put everything at 1 tick it runs really slow of course, if i put principal at 1 day and the series added at 1 tick then the backtest goes really fast.

    about the analyzer i'm going to put some part of the code that may be helpful to answer my questions:

    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Download Data 2";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.ImmediatelySubmit;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;

    orderbook = new OrderBook();
    buckets = new List<Bucket>();
    hawkes = new Hawkes(0.5, 1.1, 0.5, 50); // alpha beta mu
    vwap = new VWAPCalculator(100);
    hint = new Hint();


    }



    protected override void OnMarketData(MarketDataEventArgs e)
    {

    if (e.MarketDataType == MarketDataType.Last && BarsInProgress == 1)
    {
    Print(e.Time.TimeOfDay);
    string side = "";

    if (e.Price >= e.Ask)
    {
    side = "B";
    }
    else if (e.Price <= e.Bid)
    {
    side = "S";
    }

    HERE I HAVE MY LOGIC OF THE STRATEGY AND I JUST DO :
    IF(I HAVE TO ENTER):


    SetProfitTarget(CalculationMode.Ticks, 25);
    SetStopLoss(CalculationMode.Ticks, 10);
    if (sign == 1) EnterLong();
    else EnterShort();​

    }​


    this works perfectly in playback. it doesn't in the analyzer


    thanks
    ​​

    #2
    Hello StefanoMuscariello,

    Thank you for your post.

    A backtest will process logic On Bar Close and use OHLC prices for fills vs. playback which processes logic based on the Calculate property (On Bar Close, On Price Change, or On Each Tick) and base order fills and prices on incoming replay data where the state is State.Realtime. Additionally, you do not need to use OnMarketData() to access an added data series. By default, OnMarketData() is not called historically (such as in a backtest) and would require TickReplay to be called historically. This is likely why you don't see anything from the logic inside of OnMarketData() when running a backtest.

    I suggest reviewing the following page for more details regarding the discrepancies between real-time (such as Playback) and backtest (such as Strategy Analyzer):Here is a page about OnMarketData() with some helpful notes to get a deeper understanding of the use cases for OnMarketData():You can certainly still use an added 1-tick data series for intrabar granularity in your backtests. This concept of adding a more granular data series is demonstrated in the following reference sample:When working with multiple series, you should keep the information on the following page in mind (including snippets about accessing the price data for different bars objects, which can be done in OnBarUpdate() and does not require OnMarketData()):If you are not connected to data while running the Strategy Analyzer, it will use any data stored locally in the repository. You can see what data is stored at the Control Center > Tools > Historical Data window. Otherwise, if you are connected when running a backtest, a request is made to your provider for the data needed to cover the instrument(s) and time range used in the backtest. Since you are seeing results from Sample MA Crossover, you likely have some data stored locally that is being used for the backtest.

    Please let us know if we may be of further assistance.

    Comment


      #3
      thank you for your answer.
      My question then is: if i implement the logic in the onbarupdate method, how can i access the last trade if i do not use marketdataupdate?
      thanks

      Comment


        #4
        Originally posted by StefanoMuscariello View Post
        thank you for your answer.
        My question then is: if i implement the logic in the onbarupdate method, how can i access the last trade if i do not use marketdataupdate?
        thanks
        I may need additional context; are you referring to getting the "Last" price in OnBarUpdate() or getting trade information from trades placed by the strategy? The multi-time frame & instruments page I linked has information about accessing price data in multi-bars scripts:You can either filter for a specific BarsInProgress that is calling OnBarUpdate() with a Last price for the data series and get the Close[barsAgo] price, or you could use Closes[barSeriesIndex][barsAgo] to specify the bars object you would like to get the Close price from for a specified number of bars ago:If this is not what you are looking for, please provide a more detailed description/example so I may better understand and assist you.

        Thank you for using NinjaTrader.

        Comment


          #5
          ok sorry i'll make it easier for you to understand.


          - First question is: can i access the granular information such as level 2 market depth and singular trades ( ALL the trades that are happening in the market, ergo the last executed trade in the market) in the strategy analyzer?
          If not there is no need to read the rest of the message .



          - Secondly i meant trades in the market executed from the other so i want to be able to analyze them.
          Example of how i did it is like this:


          protected override void OnMarketData(MarketDataEventArgs e)
          {

          string side = "";

          if (e.Price >= e.Ask)
          {
          side = "B";
          }
          else if (e.Price <= e.Bid)
          {
          side = "S";
          }

          then some logic ecc....

          Print(e.Time.TimeOfDay +" " + e.Price + " " + e.Volume + " " + side);

          }​

          So by doing this i'm sure i get the last price and last volume traded (it's tick by tick information so it's what i need)


          my question is: how can i reproduce this exact same code in onbarupdate?


          Also i have this


          protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
          {
          orderbook.Update(marketDepthUpdate);
          }

          here i have the single events so i keep track of the order book by myself and i want to be able to do these things at the same moment ( analyze the trades and implementing the order book)

          i want to be clear that it works in playback. but in analyzer no because of the onbarupdate, but in the on bar update how can i obtain the same granular information that i obtain in these 2 for trades and order book?

          if i add a 1 tick data series and in the onbarupdate i do : if(barsinprogress == 1) // so i'm in the 1 tick then how can i reconstruct the order book and analyze the trades?

          i hope i've been clear enough this time,

          Thanks​





          Last edited by StefanoMuscariello; 09-09-2023, 07:57 AM.

          Comment


            #6
            Hello StefanoMuscariello,

            Thank you for the clarification.

            If you would like OnMarketData() to process historically, such as when using the Strategy Analyzer, you must enable Tick Replay. Tick Replay will only replay the Last market data event. Please see the "Notes" section of the OnMarketData() page for additional information about this being a real-time data stream (playback works as if it were real-time) and how Tick Replay affects OnMarketData();
            • https://ninjatrader.com/support/help...marketdata.htm
              • "If used with TickReplay, please keep in mind 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(). More information can be found under Developing for Tick Replay.​"
            We have a page about developing for Tick Replay here:You could add a bid and ask series with AddDataSeries() for use in OnBarUpdate(), but the historical bid/ask series will hold all bid/ask events sent by the exchange which is not the same as the bid/ask at the specific time that a trade occurred. More info about this may be found here:
            OnMarketDepth() is a real-time only data stream that is not called on historical data.Please let us know if we may be of further assistance.

            Comment


              #7
              Thank you for your answer.
              by any chance do you have a sample script in which you add the series to manage the order book (more than just one level)
              And the trades through the analyzer?
              thanks

              Comment


                #8
                Hello StefanoMuscariello,

                Thank you for your reply.

                Level II market-depth data may only be accessed in real-time and may not be accessed historically, such as in the Strategy Analyzer. As far as I am aware, level II data is only available from data providers live or from downloaded Market Replay files that are used for the Playback connection. I am not aware of any data providers that provide level II historical data. Ultimately you could contact the support for each data provider to ask if they offer this or not.

                For an indicator that demonstrates accessing the bid/ask price at the time a trade is reported, see the BuySellVolume indicator. It uses OnMarketData() and works with the MarketDataType.Last. It then checks if the price is greater than/equal to the Ask or less than/equal to the Bid to determine if the trade was a buy or a sell. This indicator only calculates in real-time if Tick Replay is disabled, yet if you enable Tick Replay on a chart this indicator is then able to calculate historically. There is more information about Tick Replay indicators here:Please let us know if we may be of further assistance.

                Comment


                  #9
                  Hello,
                  also when trying to backtest now in the onbarupdate i get an error:

                  else if (State == State.Configure)
                  {
                  AddHeikenAshi(Instrument.FullName, BarsPeriodType.Minute, 4*60, MarketDataType.Last);
                  }
                  }

                  protected override void OnBarUpdate()
                  {
                  if(BarsInProgress == 1 && Opens[1].Count> 5)
                  {

                  List<double> temp= new List<double>();
                  temp.Append(Opens[1][0]);
                  temp.Append(Opens[1][1]);
                  temp.Append( Opens[1][2]);
                  temp.Append(Opens[1][3]);​
                  }


                  even though i do the check Opens[1].count i get a null reference excpetion because Opens[1] has not enough element. Why?
                  I'm in the analyzer

                  if i do just Opens[1][0] it works fine, as soon as i go behind it fails

                  Comment


                    #10
                    Hello StefanoMuscariello,

                    Thank you for your reply.

                    What are the results of printing the value of Opens[1].Count? Do you get the same results if you use CurrentBars[1] instead?Another item to keep in mind is it is difficult to get accurate results when using Heiken Ashi bars in the Strategy Analyzer. Heiken Ashi bars change the OHLC values of a series based on the Heiken Ashi moving average values; these values are not real prices so there are workarounds that could yield more accurate results such as using High order fill resolution, submitting orders to a single tick data series, or testing on Market Replay data with the Playback Connection. Please see "Backtesting Renko bars and HeikenAshi bars" on the following page:


                    Please let us know if we may be of further assistance.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    646 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    367 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    107 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    569 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    573 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X