Using Advanced Events in a Strategy with Highly Advanced Code Injection (Point-and-Click NinjaTrader® Strategy)
Original Trading Strategy
Peer Reviews, Modifications & Responses
Thanks for Sharing!
Trading Strategy Summary
This advanced strategy is almost the same as the strategy named “Turn Indicator Entry Filters On and Off and Don’t Plot Off Indicators” that is built into Quagensia N Edition and that can be opened from the “Algos from Quagensia > Strategies > Strategies for New Users” folder of the Algo Explorer on the left side of Quagensia N Edition’s main window and whose Strategy Home Page is https://www.quagensia.com/trading-idea/turn-indicator-filters-on-and-off-and-dont-plot-off-indicators-n. In fact the trading logic of this strategy is identical to that one, and only differs in what it outputs to the NinjaScript® Output Window. To read about the strategy upon which this one is based, please read the documentation on its Strategy Home Page linked to above.
The purpose of this tutorial strategy is to demonstrate three things:
1. This strategy demonstrates how to insert raw NinjaScript® into your Quagensia N Edition Strategy from within Quagensia N Edition using the “Raw Code Output (Single or Multiple Statements)” Action. You can add a “Raw Code Output (Single or Multiple Statements)” Action to a strategy by clicking on a location where an Action can go then select the menu item located at “More Actions > Raw Code Output (Single or Multiple Statements)” in the context menu that opens. The “Raw Code Output (Single or Multiple Statements)” Action allows advanced users to add a single statement or multiple statements of valid NinjaScript® code at a given location in their trading logic by providing them with a text box where they can write valid NinjaScript® code. We at Quagensia have used this feature many times since most of the “Actions” you see in the context menu when you click on a location where an Action can go are Quagensia N Edition Functions that required the use of a “Raw Code Output (Single or Multiple Statements)” Action to connect the no-code environment offered by Quagensia N Edition with the NinjaScript® application programming interface (API) so that the code that Quagensia N Edition outputs for your Quagensia N Edition Strategy will be a syntactically correct, compilable NinjaScript® strategy.
2. This strategy demonstrates how to insert raw NinjaScript® into your Quagensia N Edition Strategy from within Quagensia N Edition using the “Raw Code Output” Expression. You can add a “Raw Code Output” Expression to a strategy by clicking on a location where an Expression can go then select the menu item located at “More Expressions > Raw Code Output Expression” in the context menu that opens. The “Raw Code Output” Expression allows advanced users to add an expression of valid NinjaScript® code at a given location in their trading logic by providing them with a text box where they can write valid NinjaScript® code. We at Quagensia have used this feature many times since most of the “Expressions” you see in the context menu when you click on a location where an Expression can go are Quagensia N Edition Functions that required the use of a “Raw Code Output” Expression to connect the no-code environment offered by Quagensia N Edition with the NinjaScript® application programming interface (API) so that the code that Quagensia N Edition outputs for your Quagensia N Edition Strategy will be a syntactically correct, compilable NinjaScript® strategy.
One powerful feature of “Raw Code Output (Single or Multiple Statements)” Actions and “Raw Code Output” Expressions is that you can insert a reference to an input parameter or an internal variable of the Quagensia N Edition Strategy or Function in which you place the “Raw Code Output (Single or Multiple Statements)” Action or “Raw Code Output” Expression by right clicking in its text box at the desired location in the code inside of the text box, then selecting the input parameter or internal variable whose reference you want to insert at that location from the context menu that appears.
To learn more about “Raw Code Output (Single or Multiple Statements)” Actions, you can read the “Using the ‘Raw Code Output (Single or Multiple Statements)’ Action” help page at https://www.quagensia.com/help/raw-code-action.
To learn more about “Raw Code Output” Expressions, you can read the “Using the ‘Raw Code Output’ Expression” help page at https://www.quagensia.com/help/raw-code-expression.
3. Our most advanced users that understand NinjaScript® at an intermediate level or higher will appreciate the specific problem that this strategy solves, which is how to add use a combination of “Raw Code Output (Single or Multiple Statements)” Actions and no-code point-and-click trading logic to the bottom of the “When Bar Updates” section in a way that makes it possible to write code in advanced NinjaScript® strategy events such as the following from within a Quagensia N Edition Strategy:
OnAccountItemUpdate():
https://ninjatrader.com/support/helpGuides/nt8/onaccountitemupdate.htm
OnExecutionUpdate():
https://ninjatrader.com/support/helpGuides/nt8/onexecutionupdate.htm
OnOrderUpdate():
https://ninjatrader.com/support/helpGuides/nt8/onorderupdate.htm
OnPositionUpdate():
https://ninjatrader.com/support/helpGuides/nt8/onpositionupdate.htm
The highly advanced, highly powerful but error-prone technique demonstrated four separate times in the “When Bar Updates” section of this strategy, one for each of these four advanced events, mixes point-and-click logic with actual NinjaScript® code. Please read the comments in the yellow “Comment in Code” text boxes, as well as the comments (the lines that start with “//”) in the Raw Code Output Actions that have them, that you will find throughout the “When Bar Updates” section of this strategy to learn how to use this highly advanced technique.
Before you run this strategy by applying it on a chart or running it in the NinjaTrader® Strategy Analyzer, be sure to open the NinjaScript® Output Window to see the outputted report that is printed from these advanced events (one row per event that occurs) and from the “OnBarUpdate()” event (two rows per “OnBarUpdate()” event, one directly before and one directly after the market order to enter a position is submitted).
You can open the NinjaScript® Output Window from NinjaTrader®’s main window by clicking on “New” on the main menu and then “NinjaScript® Output”, or you can open it by clicking on the “NinjaScript® Output” icon in the NinjaScript® Editor window’s toolbar.
To get the report into Excel, open Excel, copy the report’s output from the NinjaScript® Output window, select the entire first column in the Excel file’s main worksheet, then paste the contents of the clipboard into the first column of the Excel worksheet by holding down the “Control” key then pressing the “V” key while the entire first column is selected. If a dialog box appears that says something like “The data you’re pasting isn’t the same size as your selection. Do you want to paste anyway?” click its “OK” button. Then, select the first column again, click the “DATA” or “Data” menu and click the “Text to Columns” button in the ribbon bar that appears directly beneath Excel’s main menu. When you do this, the numbers and charts may update automatically in which case there is nothing left to do, or the “Convert Text to Columns Wizard” may appear. If the wizard appears, follow these steps:
- In Step 1 of the wizard, select the “Delimited” radio button and click the “Next” button.
- In Step 2 of the wizard, there are some check boxes in a group with the title “Delimiters”. Uncheck any checked check boxes in this group. Then, check the check box in this group of check boxes that represents the character used to separate the fields in the report, which you will be able to see in the “Data preview” at the bottom of the dialog box. Since this report uses the semicolon ( ; ) character, check the “Semicolon” check box. Then click the “Next” button.
- In Step 3 of the wizard, click the “Finish” button. A dialog box may appear that asks you if you want to replace the data already in the worksheet. Click its “OK” button. At this point, the wizard will close and the numbers should update automatically in which case there is nothing left to do.
Copyright © 2024 Quagensia, Inc. All rights reserved.
How to Get the Quagensia Strategies
This point-and-click trading strategy is built into Quagensia N Edition (for NinjaTrader®).
Customize Quagensia Strategies with Your Own Ideas
Tweaking Quagensia Trading Strategies using point-and-click with the Quagensia Desktop Application is easy for non-programmers. If you get stuck you can usually find the answer you need in our online help documentation or you can ask a question in our friendly Discord community.
If you don’t want to tweak a Quagensia Trading Strategy with point-and-click to add your own proprietary trading logic, you can still download a Quagensia Trading Strategy file, open it up in the Quagensia Desktop Application, and generate its code, then backtest and optimize the trading strategy on different instruments, different bar periods (weekly bars, daily bars, hourly bars, 15-minute bars, etc.), or different bar types (time-based bars, volume-based bars, tick-based bars, etc.), and use different start and end dates.
Some tweaks you can make to the Quagensia Trading Strategies you download include:
Modify the entry & exit logic.
- Add more conditions, remove conditions, or change them by choosing from among a very large number of components, including many exotic indicators.
- Add or modify stop losses, trailing stops, and profit targets. Make them tighter, less tight, or based on an entirely different calculation.
- Add or modify time stops. For example, exit after a certain number of bars either unconditionally or only if the post-entry price action did or did not exhibit certain characteristics.
Enhance the output of the strategy to go beyond simply placing orders.
- Draw lines, shapes, and text on the chart. For instance, you can mark times or prices where each entry or exit condition of a multi-condition entry or exit was true, even if all the necessary entry or exit conditions were not true at the same time so an entry or exit did not occur.
- Write information to NinjaTrader®’s NinjaScript® Output window or TradeStation®’s EasyLanguage® Print Log window.
- Write information to a file. You can even output a report that can be opened in Microsoft Excel or consumed by another application that reads comma-delimited, semicolon-delimited, or otherwise character-delimited tabular data files.


