Most Quagensia N Edition users will never need to add logic to the “When NinjaTrader® ATM Strategy Callback Occurs” section.
The “When NinjaTrader® ATM Strategy Callback Occurs” section is located in the “When Advanced NinjaScript® Events Occur” section of Quagensia® N Edition Strategies and is meant exclusively for advanced NinjaScript® programmers working on behalf of Quagensia N Edition users who need a Quagensia N Edition Strategy that has fundamentally different functionality than the built-in Quagensia N Edition Strategy template that allows you to enter a position based on automated entry logic you create without code and then manages the open position using one of your pre-existing NinjaTrader® ATM Strategies that you can manually interact with the way you normally interact with ATMs.
Most Quagensia N Edition users will never need to use this section. Instead, most users will likely only customize ATM-based Quagensia N Edition Strategies whose original author added the appropriate advanced logic to this section. To see an example of how this section can be used by a NinjaScript® programmer to set up a Quagensia N Edition Strategy template that other Quagensia N Edition users can make copies of and add their own custom trading logic to the less advanced sections of the copied Quagensia N Edition Strategy template, open the built-in Quagensia N Edition Strategy template that mentions ATMs in its title from the Algo Explorer on the left side of Quagensia N Edition’s main window, scroll down to the “When Advanced NinjaScript® Events Occur” section of the Quagensia N Edition Strategy, expand it to see the contracted “When NinjaTrader® ATM Strategy Callback Occurs” section, and expand the “When NinjaTrader® ATM Strategy Callback Occurs” section to view the logic in it that makes up part of the necessary structure of this strategy.
Technical Details About the “When NinjaTrader® ATM Strategy Callback Occurs” Section
The “When NinjaTrader® ATM Strategy Callback Occurs” section contains logic that is executed when the NinjaTrader® ATM Strategy technology reports back, or “calls back”, to the Quagensia® N Edition Strategy as to whether or not a NinjaTrader® ATM Strategy was successfully created by the NinjaScript® method (which is also called a “function”) named “AtmStrategyCreate()”.
The NinjaScript® AtmStrategyCreate() method is called by the no-code Quagensia N Edition Function named “Submit Entry Order with ATM Strategy Attached (Strategy’s ‘State’ Must Be ‘Real-Time’) (Advanced)”. The “Submit Entry Order with ATM Strategy Attached (Strategy’s ‘State’ Must Be ‘Real-Time’) (Advanced)” no-code function is located in the “Submit an Order >> ATM Strategies (Advanced)” folder of the context menu that appears when you click on a location where this Action function is allowed, such as anywhere in the “When Bar Updates” section of a Quagensia N Edition Strategy where an Action is allowed.
Read the AtmStrategyCreate() method’s help page at ninjatrader.com to learn more about this essential NinjaScript® function.
The NinjaScript® AtmStrategyCreate() method has the following method signature:
AtmStrategyCreate(OrderAction action, OrderType orderType, double limitPrice, double stopPrice, TimeInForce timeInForce, string orderId, string strategyTemplateName, string atmStrategyId, Action<ErrorCode, string> callback)
It is the last parameter of the AtmStrategyCreate() method, which is named “callback” in the method signature above and on the help page linked to above, that has a data type of “Action<ErrorCode, string>” and which the Quagensia N Edition Function named “Submit Entry Order with ATM Strategy Attached (Strategy’s ‘State’ Must Be ‘Real-Time’) (Advanced)” sets to a function in the generated NinjaScript® strategy code of the Quagensia N Edition Strategy. The trading logic in the “When NinjaTrader® ATM Strategy Callback Occurs” section ends up in the function definition of this function which is passed as the “callback” parameter of the NinjaScript® AtmStrategyCreate() method.
You can access the “ErrorCode” and “string” properties of the “Action<ErrorCode, string> callback” parameter from the “When NinjaTrader® ATM Strategy Callback Occurs” section without code by clicking in this section at a location where an expression of the specific property’s data type is allowed and selecting it from the “When NinjaTrader® ATM Strategy Callback Occurs” folder of the context menu that opens.
The “string” property can be accessed from the no-code function named “ATM Strategy Unique ID of ATM Strategy Passed to the ‘When NinjaTrader® ATM Strategy Callback Occurs’ Event (Advanced)” and is allowed at locations where expressions of the “Text” data type is allowed.
The “ErrorCode” property can be accessed from the no-code function named “Error Code Passed to the ‘When NinjaTrader® ATM Strategy Callback Occurs’ Event (Advanced)” and is allowed at locations where expressions of the “List of Choices” data type named “Error Code Type” is allowed.
The “Use TriggerCustomEvent to Sync Logic on Data Series” Action block makes it possible to use no-code functions and features that require accurate market data and indicator data in the “When NinjaTrader® ATM Strategy Callback Occurs” section.
The “When NinjaTrader® ATM Strategy Callback Occurs” section is not guaranteed to have accurate market data and indicator data by default, and runtime errors would occur for various reasons if no-code functions and features that use market data were allowed to be used in this section without an extra layer of protection. To fix this, Quagensia N Edition has a Use TriggerCustomEvent to Sync Logic on Data Series Action block that allows no-code functions and features that use market data to be used in the “When NinjaTrader® ATM Strategy Callback Occurs” section so long as the no-code functions and features are used inside of a “Use TriggerCustomEvent to Sync Logic on Data Series” Action block, just like no-code trading logic can be added inside of an “If Block”.
You may need to use a “Lock Critical Block of Code (lock(object){…})” Action block in the “When NinjaTrader® ATM Strategy Callback Occurs” section and other sections to ensure that certain blocks of trading logic are never executed at the same time on two different threads, but instead “take turns” executing.
The NinjaTrader® Desktop platform is a multi-threaded application that is able to run the NinjaScript® strategies you create using Quagensia N Edition on multiple threads at once. For instance, while the trading logic in your Quagensia N Edition Strategy’s “When NinjaTrader® ATM Strategy Callback Occurs” section is running, NinjaTrader® can process information about your NinjaScript® strategy’s market data, orders, fills, positions, account, and much more on multiple separate threads at the same time. As another example, when you click a Button Control that your Quagensia N Edition Strategy adds to the NinjaTrader® chart on which it is applied, the trading logic that is in the Button Control’s “When Button Control is Clicked” section can be executed at the same time that everything described in the previous sentence is occurring.
If you have multiple blocks of no-code trading logic in a Quagensia N Edition Strategy that absolutely must not be executed at the exact same time as each other on two different threads, you can accomplish this by putting each of the multiple blocks of code inside of their own Lock Critical Block of Code (lock(object){…}) Action blocks, similar to how you can put trading logic into an “If Block”.