Mark
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Using OnMarketDataUpdate in backtesting
Collapse
X
-
Using OnMarketDataUpdate in backtesting
I have a lot of my strategy code placed in OnMarketDataUpdate and I noticed while backtesting that a print of MarketDataEventArgs m.Last was actually printing today's price instead of whatever the Last price was during the time of the backtest using tickreplay. I've also got logic depending on m.Bid and m.Ask in the backtest. Am I referencing the prices incorrectly? Can I use m.Last, m.Bid and m.Ask to reference whatever the values were at the time of the backtest?
MarkTags: None
-
Hello Mark,
Thanks for your post.
By default, the OnMarketData() method is not called on historical data (backtest), however, it can be called historically by using TickReplay.
Please note this information from the OnMarketData() help guide documentation: "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."
Tick Replay was only designed to work with MarketDataType.Last.
The Developing for Tick Replay help guide page contains sample code you could view demonstrating how to access historical Bid and Ask prices with Tick Replay.
See the help guide documentation below for more information.
OnMarketData(): https://ninjatrader.com/support/help...marketdata.htm
Historical Bid/Ask Series: https://ninjatrader.com/support/help..._ask_serie.htm
Developing for Tick Replay: https://ninjatrader.com/support/help...ick_replay.htm
Tick Replay: https://ninjatrader.com/support/help...<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
-
Thanks Brandon. So if my understanding is correct then something like this in a backtest using tick replay should print the last price at the time of the historical event:
protected override void OnMarketData(MarketDataEventArgs m)
{if (m.MarketDataType == MarketDataType.Last)print("Last Price traded: "+m.Last);
}
But during the backtest it's actually printing the price trading today:
02/02/2023 08:36:27 AM - Closing L-REG3-3117 on SHORT condition at 76.2
SHORT - 3118
------------------
Entry for S-REG3-3118 : 76.2
Target for S-REG3-3118 : 75.68
ATR: 43
Adjusting Stop for S-REG3-3118 on bar 11832
Last Price: 80.46
But 80.46 was actually the price for /CL from today morning when the backtest was running and not back on Feb 2nd, 2023. As you can see it entered when the price was 76.2. The adjustment and printout happened on the next 5K bar.
Mark
Comment
-
Hello Mark,
Thanks for your notes.
Yes, that would be the correct code to print out the last price from OnMarketData(). An example of this could be seen in the sample code in the OnMarketData() help guide page linked on post # 2
When creating a simple test script that prints out the last price in OnMarketData() and running the test script on the Strategy Analyzer with Tick Replay enabled, I am seeing that the last price information for the backtested dates is outputting to a New > NinjaScript Output window, not the current market price of today's date.
See this demonstration video showing that the OnMarketData() output is for the dates being backtested on, not the current market price for today's current bar:
<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
- Likes 1
Comment
-
Replayed the backtest over a year or so. m.Last *always* remained 81.4 even when price at the time of the backtest was about 108. Fortunately the integrity of the test was unaffected as it uses m.Bid or m.Ask to check stopout levels and those are correct. Here's a sample of the output showing this in action:
# 491. - SHORT -
------------------
Entry for S-REG3-491 : 109.5
...
Stop for order: S-REG3-491 : 110.5
...
Trail Stop for S-REG3-491 on bar 3343
Last Price: 81.4
Acceleration: 0.18
New Stop for S-REG3-491 is 108.21
Stopping out trade @ 108.24
Last Price: 81.45
...
For the entire period of the test, no matter what the last trade price at the time of the backtest, m.Last would either print "Last Price: 81.4", or "Last Price: 81.45". As I run this, markets are closed and 81.45 is the last price highlighted on the SuperDOM Friday evening @ 7:45 Mountain Time. Not sure why two values are being used.
Docs appear to suggest that for MarketDataEventArgs although Ask and Bid exist there actually is no parameter "Last" rather I should perhaps be using "m.Price" as "Price" is listed as a parameter:
https://ninjatrader.com/support/help...marketdata.htm
Perhaps m.Last has been misleading me as it appears to be an undocumented parameter that *always* returns the last realtime market trade price?
So MarketDataType uses "Last" as in "MarketDataType.Last" but passing MarketDataEventArgs m around would use m.Price.
Just checked your video as well after typing that conclusion out and indeed your code uses MarketDataUpdate (or "m" in my code) with ".Price".
Thanks for taking the time to make that!
Best,
MarkLast edited by markdshark; 08-18-2023, 07:52 PM.
Comment
-
Hello Mark,
Thanks for your notes.
That is correct. When accessing the Last price from OnMarketData() you should check if marketDataUpdate.MarketDataType == MarketDataType.Last and then access the marketDataUpdate.Price inside that condition to get the Last price in OnMarketData().
An example of this could be seen in the OnMarketData() help guide page: https://ninjatrader.com/support/help...marketdata.htm<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
61 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
134 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
75 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
45 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
50 views
0 likes
|
Last Post
|

Comment