The “When Advanced NinjaScript® Events Occur” Section (N Edition)

You can add no-code trading logic to the ten subsections of the “When Advanced NinjaScript® Events Occur” section of a Quagensia N Edition Strategy that will be executed when the NinjaScript® strategy’s corresponding event-driven method is executed.

NinjaScript® strategies have event-driven methods (a method is another name for a function) such as the ones named “OnOrderUpdate()” and “OnExecutionUpdate()” that are called by the NinjaTrader® platform while the NinjaScript® strategies are running.

The no-code trading logic in a given subsection in the “When Advanced NinjaScript® Events Occur” section of a Quagensia® N Edition Strategy is executed when its corresponding event-driven method is executed. For example, the trading logic in the “When OnOrderUpdate Occurs” subsection of the “When Advanced NinjaScript® Events Occur” section is executed when its corresponding event-driven method, “OnOrderUpdate()”, is executed. Note that the mechanism in a NinjaScript® strategy that causes the trading logic in the “When NinjaTrader® ATM Strategy Callback Occurs” section to be executed is not technically an “event-driven method”, and instead could be called an “event-driven callback” or just a “callback”, but you can think of it in the same way you think of the other nine subsections for the purposes of using it in a Quagensia N Edition Strategy.

To learn more about each of these event-driven methods and the supporting no-code logic components and no-code data types built into Quagensia N Edition you can click the links in the yellow section at the top of this web page.

You can access the arguments, or “input parameters”, passed to an event-driven method by the NinjaTrader® platform from the corresponding subsection of the “When Advanced NinjaScript® Events Occur” section without code.

What follows is an example of how you can do this for one of the subsections of the “When Advanced NinjaScript® Events Occur” section, the “When OnOrderUpdate Occurs” section. To learn about the slight variations of how to do this for the other nine subsections of the “When Advanced NinjaScript® Events Occur” section, please click the relevant links for each of the subsections in the yellow section at the top of this web page.

The method signature for the OnOrderUpdate() method is as follows:

protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)

You can add one of the input parameters of the OnOrderUpdate() method signature above to the no-code logic in your strategy’s “When OnOrderUpdate Occurs” section by clicking in this section at a location where an expression of the specific input parameter’s data type is allowed and selecting it from the “When OnOrderUpdate Occurs” folder of the context menu that opens.

The first input parameter of the OnOrderUpdate() method is an advanced NinjaScript® “Order” object. You can access it in the same way as you can access the non-advanced input parameters as described above. Quagensia N Edition has a data type called “Order NinjaScript® Object” which allows you to create some very powerful trading logic both in the “When OnOrderUpdate Occurs” section and in other sections. You can read the help page titled Using Advanced NinjaScript® Objects in Quagensia® N Edition to learn more about using advanced NinjaScript® objects such as the Order NinjaScript® Object.

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 seven of the ten subsections of the “When Advanced NinjaScript® Events Occur” section that otherwise could not accommodate such trading logic.

The following seven subsections of the “When Advanced NinjaScript® Events Occur” section are 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 these sections without an extra layer of protection:

  1. When OnOrderUpdate Occurs
  2. When OnExecutionUpdate Occurs
  3. When OnPositionUpdate Occurs
  4. When OnAccountItemUpdate Occurs
  5. When OnConnectionStatusUpdate Occurs
  6. When NinjaTrader® ATM Strategy Callback Occurs
  7. When OnOrderTrace Occurs

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 these subsections 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”.

The “When OnOrderTrace Occurs” section in particular is a special case in that it is neither guaranteed to have accurate market data and indicator data by default nor is it even guaranteed to only be executed after the strategy is configured and ready to accept market data. The “Use TriggerCustomEvent to Sync Logic on Data Series” Action block allows no-code functions and features that use market data or that require that the strategy is configured and ready to accept market data to be used in the “When OnOrderTrace 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.

Entering a number greater than 0 in the “Once all data series have at least the following number of bars…” field of the “Use TriggerCustomEvent to Sync Logic on Data Series” Action block ensures that the specified number of bars will be present in both the primary data series and all secondary data series before the actions inside this Action block will be executed. This is very important because unlike the “When Bar Updates” section, which by default only executes once the number of bars to which the “Bars required to trade” field in the NinjaTrader® user interface is set are present in both the primary data series and all secondary data series, all ten advanced sections (even those that are guaranteed to have accurate price and indicator data by default) are completely unfiltered and will execute whenever the underlying NinjaScript® “event-driven method” is executed. Out of an abundance of caution, it is recommended that you treat all ten of the advanced sections of a Quagensia N Edition Strategy as having the potential to be called at any time after the strategy is configured and is ready to accept market data and before the strategy has received any market data. And for the special case of the most advanced and probably least useful “When OnOrderTrace Occurs” section, you should treat it as having the possibility of executing even before the strategy is configured and is ready to accept market data.

The three subsections of the “When Advanced NinjaScript® Events Occur” section that do not need or allow “Use TriggerCustomEvent to Sync Logic on Data Series” Action blocks also will execute whenever their underlying NinjaScript® event-driven methods are executed, and just like the other seven subsections this can occur before all the data series of the strategy have at least the number of bars to which the strategy’s “Bars required to trade” field in the NinjaTrader® user interface is set.

Unlike the “When Bar Updates” section, which only executes once the number of bars to which the “Bars required to trade” field in the NinjaTrader® user interface is set are present in both the primary data series and all secondary data series, the “When OnMarketData Occurs”, “When OnMarketDepth Occurs”, and “When OnFundmentalData Occurs” subsections are completely unfiltered and will execute whenever the underlying OnMarketData(), OnMarketDepth(), or OnFundamentalData() NinjaScript® event-driven methods are executed.

The OnMarketData(), OnMarketDepth(), and OnFundamentalData() NinjaScript® event-driven methods are not called for historical bars on a chart, only real-time bars, so if you are applying your strategy to a chart with enough historical price bars to handle the number of bars back that your trading logic requires you should not experience issues due to the trading logic in the “When OnMarketData Occurs”, “When OnMarketDepth Occurs”, and “When OnFundamentalData Occurs” subsections being executed for every execution of the OnMarketData(), OnMarketDepth(), and OnFundamentalData() NinjaScript® event-driven methods. For instance, if your trading logic requires data from only one bar ago and you apply your strategy to a chart with five historical bars, you should not experience any issues due to this behavior of the “When OnMarketData Occurs”, “When OnMarketDepth Occurs”, and “When OnFundamentalData Occurs” subsections.

That said, to reduce the likelihood that your strategy will ever experience a runtime error or a logic error due to this issue, it is recommended that if your logic needs to access any market data other than that which is in the “MarketDataEventArgs marketDataUpdate” input parameter of the OnMarketData() method, the “MarketDepthEventArgs marketDepthUpdate” input parameter of the OnMarketDepth() method, or the “FundamentalDataEventArgs fundamentalDataUpdate” input parameter of the OnFundamentalData() method, you should put that logic in an “If Block” that has a condition that checks to ensure that there are enough price bars. You can create such a condition in multiple ways, including by setting the condition’s comparison operator to “Is Equal To”, then clicking on the location on the left side of the “Is Equal To” and selecting either the no-code function named “Not Enough Price Bars (Checks Primary And Secondary Data Series)” or the no-code function named “Not Enough Price Bars (Checks Primary Data Series Only)” from the “Price Data >> Price Bar Metadata” folder of the context menu that opens, then entering the required number of bars into that no-code function’s single input parameter, then clicking on the right side of the “Is Equal To” and selecting “False” from the “Hard-Coded Value (True, 42, “Hello”, List of Choices)” folder of the context menu that appears.

You may need to use “Lock Critical Block of Code (lock(object){…})” Action blocks in the subsections of the “When Advanced NinjaScript® Events Occur” 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 the various subsections of the “When Advanced NinjaScript® Events Occur” section of your Quagensia N Edition Strategy section are 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”.

Submit your high-quality Quagensia tutorials for consideration for free inclusion on our website.

If you publish high-quality Quagensia tutorials on a social media platform like YouTube, please consider submitting them to us. We may promote them for free by linking to them from relevant pages on our website.

To submit your Quagensia tutorials for consideration for free inclusion on our website, use the How to Submit Your Quagensia-Enhanced Content page. We are available by phone or email to help you create, publish, and promote Quagensia-enhanced content. You can also get questions answered in our Content Creator Support Center and in the official Quagensia® Discord community’s content-creators channel.

Thanks for Sharing!

Ready to get started?

Download the Quagensia no-code strategy builder free trial and build your trading strategies now.

Under a dollar a day.

Priced to be an easy decision for most traders.

Join Us on Discord

Build point-&-click strategies for NinjaTrader® and TradeStation® using Quagensia®. Collaborate with other traders to get and give help, build and backtest strategies together, and grow your skills.

Subscribe to our Newsletter

We'll send you product updates and company news from Quagensia as well as curated lists of Quagensia-enhanced content.

Welcome
Content Creators

Quagensia enables content creators to make interactive content. We invite you to create Quagensia-enhanced content and share it on YouTube and other social media channels. You may submit your high-quality Quagensia-enhanced content for consideration for free inclusion on our website.

Screenshots

For larger and additional screenshots, see large Quagensia N Edition screenshots (for NinjaTrader®) and large Quagensia T Edition screenshots (for TradeStation®).

For additional screenshots, see the list of Strategies Built into Quagensia N Edition (for NinjaTrader®) and Strategies Built into Quagensia T Edition (for TradeStation®). Select a built-in strategy that interests you and scroll down for the screenshot. Then scroll up to the top of the page and click the “Overview” link to watch the video on how to use that built-in strategy.

This is a screenshot of the point-and-click trading strategy you will build in Quagensia N Edition (for NinjaTrader®) if you follow along with the video titled Create NinjaTrader® Strategies Using Point-&-Click with Quagensia N Edition – Full Tutorial.

This is a screenshot of the point-and-click trading strategy you will build in Quagensia N Edition (for NinjaTrader®) if you follow along with the video titled "Introduction to Quagensia N Edition".

Click the gear icon that appears when you place your mouse pointer over the location to which you would like to add trading logic. A context menu will appear with filtered options to choose from.

Click the gear icon that appears when you place your mouse pointer over the location to which you would like to add trading logic. A context menu will appear with filtered options to choose from.

Thanks for Sharing!

Ready to get started?

Download the Quagensia no-code strategy builder free trial and build your trading strategies now.

Under a dollar a day.

Priced to be an easy decision for most traders.

Join Us on Discord

Build point-&-click strategies for NinjaTrader® and TradeStation® using Quagensia®. Collaborate with other traders to get and give help, build and backtest strategies together, and grow your skills.

Subscribe to our Newsletter

We'll send you product updates and company news from Quagensia as well as curated lists of Quagensia-enhanced content.

Welcome
Content Creators

Quagensia enables content creators to make interactive content. We invite you to create Quagensia-enhanced content and share it on YouTube and other social media channels. You may submit your high-quality Quagensia-enhanced content for consideration for free inclusion on our website.