Accurately mimic the network latency in Market Replay

I want to fully automate my E-mini and micro E-mini daily scalping strategy and run it on a VPS.

As there are definitely network latency between NinjaTrader sending market order to it is received by NinjaTrader’s server, I hope to mimic the latency in Market Replay.

I have been discuss this topic with Google Gemini (AI chat bot) for a while, Gemini believes that NinjaTrader directly access the Level 2 data and get the exact millisecond NinjaTrader send the order, and there is no any options to set a fixed delay time (such as 50 milliseconds) to access the order book a little bit later. Eventually Gemini came up with some solutions even itself believes that will consume a lot of computational resources. For example:

private DateTime _orderTriggerTime = DateTime.MinValue;
private bool _isOrderPending = false;

protected override void OnStateChange()
{
    if (State == State.Configure)
    {
        // Enforce that the strategy receives market depth events
        // Note: This requires full Market Replay data to be loaded
    }
}

// 1. SIGNAL GENERATION: Triggers on Level 1 or Bar Close
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
    // Generate your algorithmic trading signal here
    if (YourStrategySignalCondition() && !_isOrderPending)
    {
        _orderTriggerTime = marketDataUpdate.Time;
        _isOrderPending = true;
    }
}

// 2. HIGH-RESOLUTION TIMING CONTROL: Evaluates on Level 2 Order Book Changes
protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
{
    if (_isOrderPending)
    {
        // Because Level 2 updates stream constantly on liquid instruments like the S&P 500,
        // this block will evaluate almost instantly when the 50ms threshold is breached.
        if ((marketDepthUpdate.Time - _orderTriggerTime).TotalMilliseconds >= 50)
        {
            // Execute the market order immediately against the current Level 1 state
            SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "Delayed Latency Entry");
            
            // Reset the state machine
            _isOrderPending = false; 
        }
    }
}

In general, its idea is:

  1. Set a mark when you want to send the market order
  2. Use event listener such as “OnMarketDepth()” or “OnMarketData()” to see if the time in Market Replay has already slipped over the latency setting.
  3. Send the market order right after the time has slipped over the setting.

I am thinking that from a pure technical (Software Engineering) perspective, accessing the historical orderbook n milliseconds later should not be very difficult?

So I still wonder if there are any better ways to accurately mimic the latency with simpler code and less computation overhead.

This picture below saves my discussion with Gemini. The paragraphs starting with “Q:” and with gray background are my questions asked to Gemini. And you can see the responses are pasted right below the questions.

Ninjatrader or any system you program can handle it for you scalping strats.

Unless you’re running high frequency operations with field-programmable gate arrays, you’ll be fine.

You’re not

Thanks! So how to program the fixed look forward latency in Market Replay with Tick Replay enabled?

If you’re not a programmer and are unable to interact with the foundation AI tools, then it’ll be probably a challenge. But you can hire someone on upwork and elsewhere and begin the effor to converting yorur discretionary trading instructiosn to a diagram that someone can code for you.

Look up ninjaScript

I am a professional software engineer. I am just asking an API question.

Maybe this can help you:

or this bartimer

Hi marcus,’

I looked into the Bar Timer you provide. I think this is an indicator, instead of a Timer class similiar to “system.windows.forms.timer” but with underlying time replaced by NT’s bar time:

Could you explain to me how the “Bar Timer Hybrid” you provide can resolve my problem?

By the way, its funny that Gemini today changes its words and says sorry to me. It says that in Market Replay, NT fills the order at the next incoming tick:

So which is correct? (Market Replay fill the market order at current tick of sending order or next tick?)

Thanks,

Yaohua