My question may seem stupid, but I’d like to know if the indicators calculated in State.DataLoaded and those calculated in the OnBarUpdate() have the same value.
Because I tested a strategy with the same indicators and “parameters” as those in my indicator where I simulate trading, and the results are frankly different.
Thank you for your help.
Wouldn’t the OnBarUpdate indicators be updated more frequently therefore change from the DataLoaded state?
DataLoaded is only called once before all historical data is processed.
What calculations are you trying to do in there? Nothing related to the bars will work. other than Count I think.
You shouldn’t really be doing any calculations in the DataLoaded state since it’s only called once as others mentioned. I use for initializing variables, objects and indicators. Example:
else if (State == State.DataLoaded)
{
var config = new StrategyAnalyzerExporterConfig
{
EnableWriteToDatabase = EnableWriteToDatabase,
EnablePrintDataBar = EnablePrintDataBar,
DatabasePath = DatabasePath,
UseFloat32 = UseFloat32,
TableName = TableName,
// Optimized batch settings for better performance
FlushSize = 50_000, // Larger batches for better throughput
FlushIntervalSeconds = 60,
// More aggressive commit settings for real-time performance
CommitEveryRows = 25_000, // Smaller commits for lower latency
MaxTxDurationSeconds = 30, // Prevent long-running transactions
IdleTailCommitSeconds = 15, // Quick commits when idle
CheckpointEveryCommits = 10, // More frequent checkpoints
};
var FeaturesEngineeringConfig = new FeaturesEngineeringConfig
{
TickSize = TickSize,
BarsRequiredToTrade = BarsRequiredToTrade,
LookbackPeriod = 9,
LookbackPeriodSlow = 14,
};
_parsedTimeStart = int.Parse(TimeStart);
_parsedTimeEnd = int.Parse(TimeEnd);
_FeaturesEngineeringService = new FeaturesEngineeringService(FeaturesEngineeringConfig);
_eventManager = new EventManager();
// Only create DB manager if writing is enabled
if (EnableWriteToDatabase)
_databaseManager = new ExporterDatabaseManager(config, _eventManager, StrategyType);
// Indicators
_movingAverage = EMA(BarsArray[PRIMARY_SERIES], 9);
_slowMovingAverage = EMA(BarsArray[PRIMARY_SERIES], 20);
_ATR = ATR(BarsArray[PRIMARY_SERIES], 14);
// Timing init
_histStopwatch = new Stopwatch();
_histStarted = false;
_histEnded = false;
_histProcessedBars = 0;
_eventManager.OnPrintMessage += HandlePrintMessage;
}
Thank you for your answers.
@ [WaleeTheRobot](Profile - WaleeTheRobot - NinjaTrader Community Forum) But here, all EMA and ATR values are calculated for PRIMARY_SERIES, right? My question is whether calculating here will produce the same results as if you calculated them in the onbarupdate for each PRIMARY_SERIES index.
Here you are essentially assigning the EMA and ATR to PRIMARY_SERIES which is what I have set to 0. So if you want a different data series then you need to change that.
NinjaTrader controls when the indicators are calculated. All we know in a strategy is that when we are in OnBarUpdate of the strategy that the indicator values for the current and previous bars will be available if that bar has been processed. However, if the lookback is set to 256, then you’ll only have the previous 256 bar values for the indicator.
I’d like to know if the indicators calculated in State.DataLoaded and those calculated in the OnBarUpdate() have the same value
This is not an option. Indicators are not calculated in one place or the other. They may be accessed in both places, but we don’t control where they are calculated when running a strategy. Normally, the indicator should only be accessed in OnBarUpdate. I don’t think you can expect the indicator values to be populated yet or accurate if trying to access in State.DataLoaded. Not 100% sure about that, but I never do it. I only access indicators in OnBarUpdate.