My backtest require me to load my data from a file. This data is just a dictionary containing some double datatype. It's NOT a data series. This data sits in a file, namely data_yyyy-MM-dd.txt Depending on the start date, it will need load the appropriate data. For example, if the start date of my backtest is 2023-12-22, then it should look for the file named data_2023-12-22.txt. Where should I load this data? Should it be in DataLoaded or Transition or Historical. This data only needs to load once at the beginning of the backtest and it needs to know the start date of the backtest so that it can read the correct filename with a backtest startdate attached to it. Where/How can I get the backtest startdate?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
best way to run a backtest when there is an external data needed at the beginning
Collapse
X
-
best way to run a backtest when there is an external data needed at the beginning
Hi,
My backtest require me to load my data from a file. This data is just a dictionary containing some double datatype. It's NOT a data series. This data sits in a file, namely data_yyyy-MM-dd.txt Depending on the start date, it will need load the appropriate data. For example, if the start date of my backtest is 2023-12-22, then it should look for the file named data_2023-12-22.txt. Where should I load this data? Should it be in DataLoaded or Transition or Historical. This data only needs to load once at the beginning of the backtest and it needs to know the start date of the backtest so that it can read the correct filename with a backtest startdate attached to it. Where/How can I get the backtest startdate?Tags: None
-
Hello affilife,
Thank you for your post.
Although this is not documented in the help guide, there are properties called From and To that you can access when the strategy is in the Strategy Analyzer to get the from date and to date selected. Here is an example of a print of From and To that you could use to test:
Since the data only needs to be loaded once, you could likely access From and To in State.DataLoaded and load the data from your file there as well.Code:else if (State == State.DataLoaded) { if (IsInStrategyAnalyzer) Print("From date: " + From + " To date: " + To); }
Please let us know if we may be of further assistance.
-
One follow up question. Can I use IsInStrategyAnalyzer when State == State.Configure to AddDataSeries depending on whether IsInStrategyAnalyzer true or false? Will IsInStrategyAnalyzer available in State Configure?
Edit: tried it and it works tooLast edited by affilife; 02-24-2024, 11:32 PM.
Comment
-
Although you have tested this and it works, you must be careful with the use of AddDataSeries(). Please see the following warning in the help guide:Originally posted by affilife View PostOne follow up question. Can I use IsInStrategyAnalyzer when State == State.Configure to AddDataSeries depending on whether IsInStrategyAnalyzer true or false? Will IsInStrategyAnalyzer available in State Configure?
Edit: tried it and it works too- "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."
Please don't hesitate to reach out with any additional questions or concerns.
Comment
-
if I don't use IsInStrategyAnalyzer to dynamically load my data, how can I avoid issue where I don't need so much historical when I enable it to trade realtime? Because it takes 15-30min to enable the strategy to go fully active if I don't separate out the logic of what barsToLoad when adding the series.
Also, the code below utilize another instrument when I passed in a variable for instrument name. How do I properly add the corresponding micro instrument for the mini instrument in the primary
series with using the recommended hardcoded that official support provide?
Code:else if (State == State.Configure) { if (!IsInStrategyAnalyzer) { AddDataSeries( null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1 }, 2000, null, null); AddDataSeries( null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 60 }, 200, null, null); AddDataSeries( null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, 5, null, null); string microName = "M" + Instruments[0].FullName; AddDataSeries( microName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1 }, 2000, null, null); } // AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay) else if (IsInStrategyAnalyzer) // for strategy analyzer { Print("adding 1 tick series for fill accuracy"); AddDataSeries(Data.BarsPeriodType.Tick, 1); // 2nd series AddDataSeries(Data.BarsPeriodType.Minute, 60); AddDataSeries(Data.BarsPeriodType.Day, 1); string microName = "M" + Instruments[0].FullName; AddDataSeries(microName, Data.BarsPeriodType.Tick, 1); } }
Comment
-
Hello affilife,
Thank you for your reply.
When you enable a strategy in real-time from the Strategies tab, there is a "Days to load" property that you can set to the desired amount of historical data. If you load a strategy and enable it from a chart, it will use the historical data available on the chart. This would be based on the "Days to load" in the chart's Data Series window. You could reduce the amount of data in the data series to load less historical data when enabling your strategy.
There are also some tips on how to speed up the loading of data here:
For example, you could keep a Market Analyzer or a chart window open with the desired instruments and data series loaded (such as 1-tick) to have that data saved to your repository.
The use of microName for the instrument is not supported. You would have to hard-code the instrument name. You could create copies of your strategy that add the desired micro instrument and load the corresponding strategy based on the mini instrument you plan to enable the strategy on. For example, if your strategy is named MyStrategy you could create a copy called MyStrategyES and it can call AddDataSeries() to add the MES, and another copy called MyStrategyNQ could add MNQ, etc.Code:AddDataSeries(microName, Data.BarsPeriodType.Tick, 1);
Please let us know if we may be of further assistance.
Comment
-
This is definitely a hack. Maintaining code base like this with multiple copies is definitely not a good practice. Why can't we support AddDataSeries() with run-time variables? Can we ask dev team to support AddDataSeries() with run-time variables as this would be a scalable approach?
Comment
-
I certainly understand your concern and appreciate your feedback. There is an existing feature request to support the dynamic handling/loading of additional data series, and I will add your vote to the request. The internal tracking number for this feature request is SFT-882. Please reference this internal tracking number when contacting Platform Support if you ever have questions regarding this feature request.Originally posted by affilife View PostThis is definitely a hack. Maintaining code base like this with multiple copies is definitely not a good practice. Why can't we support AddDataSeries() with run-time variables? Can we ask dev team to support AddDataSeries() with run-time variables as this would be a scalable approach?
When a feature request is implemented, you'll find a description of the new feature in the release notes:Thank you for using NinjaTrader.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
38 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
124 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
64 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
41 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
46 views
0 likes
|
Last Post
|

Comment