Replies: 9 comments
-
Hi @dkharrat Which bar timestamps are you referring to, Nautilus is built firstly to run on order book and tick level data, when you use bars to simulate and process execution there is some information loss, such as we don't know whether the high traded before the low or vis vera, and also when during the bars time interval the aggregated OHLC was hit. Even with bar data, Nautilus maintains an orderbook per instrument per venue, and a bars OHLC prices are converted into ticks and iterated through the matching engine in an orderly way, there is also a synchronisation of time so that no events can occur out of order in terms of timestamps.
If your limit order is working in the market already, then if any of the bars prices trade through the order then it is matched and it will be executed - somewhere in that 5 minute interval (at either the open, high, low, or close - and in that order since this information has been lost already [edit: you can actually randomize this but thats another topic]). It would be rather unrealistic if we treated this bar as a discrete unit of time during which no matching occurs because we're stepping through a backtest bar-by-bar with an expectation that an order won't execute until the next bar, regardless of where the market price is? If you meant, on submit you wouldn't expect the order to be executed until at least 5 minutes have passed, that doesn't seem realistic if your latency is milliseconds or better? Does your bar data have opens which exactly match the last close, or are there gaps? So I think I need more detail here to explain what might be going on, was your order working already, or are you submitting your order expecting it won't be executing until the next bar at It may help to review the code around how:
Look forward to your response and we can continue to discuss bar execution dynamics. This probably deserves some more docs as well, so the specifications and expectations are clearly laid out. |
Beta Was this translation helpful? Give feedback.
-
I should have re-read your initial post, and noticed this:
Scenario and question Stated another way, (and in the same scenario above) if your bar data gaps and the next open steps up to 102 - would you expect the market order to be executed at 102? If this is the case, then I see this as a data issue, because the information loss of aggregating bar data has caused gaps, and in reality you're not going to achieve the price of 102 as clearly some time has passed for the market to move to this new price, and notably with bars we don't even know when the market was exactly quoting 102. I think this is the key difference in expectations here: the platform will simulate the market at the current bid/ask prices, and for an aggressive order will process this immediately + any defined latency. Your expectation is that the order won't be executed until the next bar comes in, which should have an open at the close of the last bar anyway?
For this one, if your limit order is marketable then it should execute at current bid/ask price? if its not marketable, then I agree and this is the current behaviour? Hopefully this helps to clarify the discussion a little more. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the details @cjdsellers! For I'm using BarDataWrangler and just noticed it accepts a By the way, I provide both 5-min and 1-min bar data, and I noticed Nautilus uses the lowest granularity available (i.e.1-min bar data) to determine the order fill time, which is pretty nice, as that makes the fills more accurate. Regarding your question:
Yes, exactly, that's my expectation. Isn't a gap-up realistically possible even in tick-level data? By the time the bar completed, the ask might not be at the bar's close anymore. I think Nautilus should use the next tick's ask price to be as realistic as possible (in my case, it would the next 1-min bar's open). |
Beta Was this translation helpful? Give feedback.
-
Hi @dkharrat There is an expectation that
I think we'll have to disagree on this point, to be as realistic as possible the fill will occur wherever the market is based on the timestamps in the data, and any latency modelling - i.e. how fills will actually be determined in an order book or matching engine. So the issue for you here is twofold:
I think what you're attempting to do here is simulate latency by using the inherent gaps in your bar data? To help illustrate this, consider the original scenario and this time with tick level data and latency modeled. Lets say the next quote tick has a best ask of 102 and a timestamp of 00:05:01.000, and you're simulating your one-way latency at 00:00:00.100 (100ms). You send your market order which is going to hit the matching engine at 00:05:00.100 where the best ask is currently still at 101 per the data - and this is what Nautilus will simulate. To achieve the functionality you're after would compromise the realism of backtesting on order book and tick level data, and so can't be changed. We're also wanting to improve the fill dynamics and modeling options here, which would be done prior to considering any sort of configuration for bar execution. If you were to step through the code in the links I provided, I think you would find we're providing maximum realism - and I'm happy to discuss any individual points along that code path. |
Beta Was this translation helpful? Give feedback.
-
Hi @cjdsellers, thanks for the details. It helps understand where you're coming from.
That's good to know. This would be good to document, as it's not clear. However, as far as I know, most data sources consider the timestamp to be opening bar time. Wouldn't make sense to be consistent with the conventions used by most data sources?
You're reasoning make sense to me and I agree with the behavior you described in your example scenario. I think the difference in how I'm thinking about it is I'm thinking about the bar data as "trade ticks" whereas you're thinking about it as "quote ticks" (correct me if I'm misunderstanding here). If we're considering the bar data as quote ticks, then I would agree with your reasoning. However, if we treat bar data as trade ticks, then it would make sense to consider the next "trade tick" for the matching engine, not the last one, since the price at the last trade tick will probably not match the quote tick. Do you agree with my thinking here? I did notice that there are data wranglers for trade ticks. How does Nautilus process trade ticks? Are they converted to quote ticks for the matching engine? |
Beta Was this translation helpful? Give feedback.
-
@dkharrat If you load 1-minute bar data into Nautilus, you can then use internally aggregated 5-minute bars for your strategy. This approach guarantees that your strategy operates with 5-minute bars as intended, while the Matching Engine improves the realism by making use of higher-resolution data. |
Beta Was this translation helpful? Give feedback.
-
Hi @dkharrat I agree that its not so clear the expectation for bars is a timestamp on close. I wouldn't say most data sources timestamp on open in my experience, in my opinion it seems more logical to timestamp on the close - as that's when the bar is fully formed and represents a data event where an aggregation is completed - both are valid approaches anyway. For the discussion on bar execution dynamics, I don't think it makes a difference whether we're considering quotes or trades. You could rewrite my scenario above as a trade tick, with the information we have available from bars only - until that trade hits the ask the market has not moved. Trade messages also occur in order book data, where there is a precise timestamp when the trade occurred - and we can also see where the market was quoting around these point too. So I think you're wanting to infer this trade to mean that the ask had to move up to that price at some point not visible in your data?
I don't think this makes sense, you said it yourself with "will probably not match the quote tick", its the lack of granularity available with the bar data which forces us make this assumption either one way or the other. Nautilus will process quote ticks and trade ticks in the same way through the matching engine - only that quote ticks have additional information on both the bid and ask side, where as trade ticks have the single trade price and so that is where the market must be at that exact point in time. Bar data is also only single prices per OHLC point, either bid, ask for quotes, or last for trades, in any of those cases I think we can both agree that processing bar data for execution should behave like trades? It's valid to run backtests on all of the following types of data in combination or isolation, and these are listed in descending order of granularity:
Where I think we could converge on agreement is for the behavior of bar data treated as trade ticks, to simulate the market lacking the granularity of the other data types, you could take the next trade tick to mean either:
I can see how you might want to configure bar execution for a) which I think is your preference, and it might seem like the most realistic choice to "fill in the gaps" - but if this became the default it would degrade the realism of backtests running on order book + quote + trade data or any combination of these, which Nautilus is build for firstly. I also think my point still stands that your bar close should match the next open? otherwise where was the market trading in between? it could have been far outside the next bars OHLC for all we know, or we could make an assumption that it was somewhere between the close and open - but again, we're not sure with bars. If the close did match the next open, then the current implementation would also meet your expectations. I'm aware and understand that bar data often has a difference between these prices though. So having said all of the above, I still think its not a case of "the Nautilus implementation is wrong and introduces look-ahead bias", its more how one prefers to represent the lack of information with bar data to simulate execution. We could probably introduce additional configuration options for processing bar data to cater for these differences in assumption, but as per one of my other messages:
Does this make sense so far? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the detailed thoughts @cjdsellers! It's helpful and I think we're converging on the expected behavior.
You're actually right. After looking into it more, it does look like different data sources have different conventions, and the recommendation is to use the close time, as you suggested, as it reflects when the bar has been completed and would have prevented bugs that introduce look-ahead bias, like the one I ran into.
Yes, that's right. I'm basically just trying to make the most conservative assumption on where the ask/bid would be at a specific instant of time, absent the quote tick information. My assumption is that a trade tick represents a completed execution at a specific time and it's possible the bid/ask has moved after that trade tick. Whereas with quote ticks for the same instant of time, you have the exact bid/ask to match with.
Making it configurable makes sense. Perhaps, you could provide two configurations:
I do agree that you want to default to whatever configuration provides the most realism. When you have quote ticks or orderbook deltas, I agree that
Why is it not possible for there to be gaps? I can see that happening even at the highest resolution data. This could happen for very illiquid stocks or options. For an extreme example, check
I agree. After I've set |
Beta Was this translation helpful? Give feedback.
-
Hi @dkharrat I'm glad we were able to get to the bottom of it, that it's simply a matter of what assumptions are being made about the bar based execution / trade tick only data - I agree that configurations would make sense to cater for different preferences and data type combinations for backtesting.
This is probably another case of assumptions made in bar aggregation. If the close timestamp is bar open + bar interval, then wouldn't this be exactly at, or very close to the open of the next bar? (some bars will close just before the next open, in which case - sure - there may be a movement up or down on the next trade tick.) This example you're giving for I'm glad you're getting better results now, closer to what you expect. If you get tired of waiting and wanted to attempt some bar execution configuration then I can help guide any PR you put up. I'll also move this to discussions now - as its rather lengthy, and we can open a more specific issue for bar execution configuration with better specifications. |
Beta Was this translation helpful? Give feedback.
-
I have been developing a strategy and backtesting it extensively. Results looked very promising. But after deploying it live, I was dismayed to see a significant discrepancy in the results. After investigation, it turns out the backtest results were inaccurate and misleading due to orders getting filled at an unrealistic time.
I have a strategy that uses 5-min bars. However, when submitting a limit order, the order is filled at some price within the same bar, which is unrealistic. To reflect real-world results, it should fill in the next 5-min bar (if the limit price happens to be within the bar price range).
More specifically: If I have a 5-min bar that opened at time
t
, the strategy would have received the bar when it's complete (i.e. at timet+5
), so the order should execute fromt+6
the earliest. Currently, the order is executed at timet
.I tried to workaround this using the LatencyModel, but that introduced a lot of indeterminism in the backtesting results, so it doesn't work.
Expected Behavior
Market orders are executed at the open of the next bar. Limit orders are executed within the next bar's price range.
Actual Behavior
Market orders are executed at the close of the same bar. Limit orders are executed within the same bar price range.
Specifications
nautilus_trader
version: 1.88.0Beta Was this translation helpful? Give feedback.
All reactions