Quagensia N Edition makes it easy to create algorithmic trading strategies for NinjaTrader® 8 without code by simply clicking choices on a screen as you would verbally describe your trading ideas. You can then backtest and optimize your trading strategies for free, and trade them in live markets with NinjaTrader® 8.
In this video, we will build a simple algorithmic trading strategy in Quagensia N Edition without code by simply clicking choices on a screen as you would verbally describe the trading idea. Then, we will generate our strategy’s code and send it to NinjaTrader® 8 with the click of a button, then backtest and optimize our strategy for free on ten years of historical market data that comes free with NinjaTrader® 8. The goal of this video is to answer as many FAQs as possible in 57 minutes and onboard new users quickly and efficiently. The pace of the video is relatively fast, and most users will likely benefit from watching it a second time and rewatching certain sections multiple times. We recommend watching this video with Quagensia N Edition open and following along with the video by frequently pausing it and pointing and clicking in your own Quagensia Strategy to build the same trading logic without code.
Video Transcript: Create NinjaTrader® Strategies Using Point-&-Click with Quagensia N Edition – Full Tutorial
Quagensia N Edition makes it easy to create algorithmic trading strategies for NinjaTrader® 8 without code by simply clicking choices on a screen as you would verbally describe your trading ideas. You can then backtest and optimize your trading strategies for free, and trade them in live markets with NinjaTrader® 8.
In this video, we will build a simple algorithmic trading strategy in Quagensia N Edition without code by simply clicking choices on a screen as you would verbally describe the trading idea. Then, we will generate our strategy’s code and send it to NinjaTrader® 8 with the click of a button, then backtest and optimize our strategy for free on ten years of historical market data that comes free with NinjaTrader® 8.
This video is merely a “how to” tutorial that shows you how to do something very simple and basic rather than impress you with the extreme power of Quagensia. If you want to learn more about Quagensia’s advanced features and compare Quagensia to your current codeless strategy builder, please read the features list at the bottom of our home page and watch our other video tutorials.
Before we begin, note that once you install the Quagensia free trial, the clock starts ticking on your free trial period. The free trial is full-featured and the strategy code you generate with Quagensia is yours to keep and use on an unlimited number of computers even after your free trial ends. So once you install Quagensia you should use it as much as you can to make as many algos as you can before your free trial ends.
If you decide not to go with Quagensia at the end of your free trial, you won’t be able to edit your algos codelessly from within Quagensia, but you will still be able to open Quagensia and generate the code for algos, including the ones you built, the built-in strategies you see here, and the free algos you download from third parties.
If you’d like to follow along, you can pause this video, go to quagensia.com, quickly download and install the Quagensia N Edition free trial, and restart this video.
Let’s get started.
To create a new Quagensia Strategy, click on the “Plus” icon on the left side of the toolbar and click “Create New Quagensia N Edition Strategy”.
This creates a new Quagensia Strategy in a new tab.
Give your strategy a name. This will be its name in Quagensia N Edition.
Change its “Strategy Name Inside of NinjaTrader ® 8” to something relatively short. This will be its name in NinjaTrader ® 8.
Save this new strategy to your “My Algos” folder by clicking on the “Save” icon in the toolbar or by holding down the “Control” key, then pressing the “S” key.
Then click the “Save” button in the “Save As” dialog.
We now have a new strategy in our “My Algos” folder. Now let’s build out its trading logic by simply clicking choices on the screen.
Scroll down to the Strategy Details section.
Now, change the following properties:
First, check to make sure that the “Calculate” field’s drop-down list is set to “On Bar Close” because we only want to execute our trading logic when price bars close, not on each tick or on any other type of bar event.
Next, change the “Default Entry Order Quantity” to 100 since we will be using this strategy on equities for now.
Next, uncheck the “Exit on Session Close” check box, since we will be making a strategy that uses daily price bars and holds positions overnight.
Finally, change the “Slippage” to 2 so that our backtest results are less likely to be overly optimistic.
Note that most of the fields in the Strategy Details section, including the ones we just changed, are merely default values that can be overridden from within the NinjaTrader® user interface.
Next, scroll down to the “Input Parameters” section.
This section is where you define the input parameters of your strategy that you will be able to change from within the NinjaTrader® user interface.
We will be creating a basic moving average crossover strategy, so let’s create input parameters for the periods of each moving average.
Name the first one “Fast MA Period”, change its type to “Whole Number”, set its initial value to 3, and click the “Add Parameter” button.
Name the second one “Slow MA Period”, change its type to “Whole Number”, set its initial value to 6, and click the “Add Parameter” button.
Before we move on, notice the large number of data types that our input parameters can use. In addition to the basic ones that most codeless strategy builders support, Quagensia N Edition lets you pass input parameters of advanced data types from the NinjaTrader® user interface, including three kinds of dates and times, colors, many advanced numeric types, and a large number of “Lists of Choices” types such as the days of the week or months of the year, which show up as drop-down lists in both the NinjaTrader® and Quagensia user interfaces.
Now, scroll down to the “Internal Variables” section.
This section contains variable definitions used to store values that are part of the internal logic of our strategy.
Let’s create two internal variables of type “Indicator Time Series”, one for the slow moving average in our strategy and one for the fast moving average.
Name the first one “Fast MA” and change its type to “Indicator Time Series”.
Next, click on the “(Not Set)” label next to the “Indicator” label to open up a context menu containing the context-sensitive list of all of the allowable expressions we can use at this location. Then, roll your mouse over “Set Item” to see all the functions that can be used as NinjaTrader® indicators.
Before we choose an indicator, notice the very large number of indicators you can select from, and how they are nicely organized into categories for easy discovery. Note that if you cannot find a particular indicator you can add it to Quagensia the easy way by copying one of the over 120 built-in Quagensia N Edition Functions that you see here and making small changes to match your indicator’s name, input parameters, and other attributes.
Although Quagensia N Edition has over 1,900 functions, this context menu is only showing the subset of functions that can be used as indicators since the location we clicked only allows indicators. Some codeless strategy builders don’t filter their functions to only include those that are allowed at the location you want to add something. This wastes your time and mental energy by forcing you to browse through many functions that aren’t allowed at the location of interest. It also introduces a major source of errors because it makes it your responsibility to figure out what functions are valid at a given location and which functions are not. Quagensia limits the choices in its context-sensitive context menus to only the choices that are valid at the location you clicked, which stops you from making serious mistakes like putting text where a number should go or putting a number where an indicator should go.
If you want to learn more about an indicator, place your mouse pointer over the icon to the left of the menu item, read the tooltip that appears, and press F1 to go the documentation web page for that indicator. If you want to learn about other topics that are the most relevant to the location where you clicked, roll your mouse pointer over the “Help / Tutorials” menu item and click on any help topic you want to learn more about.
Let’s choose the weighted moving average whose NinjaScript® keyword is WMA from the large list of moving average indicators and click the “Add Variable” button to add it to our strategy.
One more note about getting context-sensitive help when you need it: Rolling your mouse pointer over icons, reading tooltips, and pressing F1 to get help also works for all gear icons that appear on the right side of any location in your strategy. For example, when you place your mouse pointer over the gear icon that appears when your mouse pointer is over the label “The Close”, an informative tooltip appears, and pressing F1 opens the “Using Primary Price Bars” help topic online. Clicking on the gear icon and rolling your mouse over “Help / Tutorials” displays other related help topics, such as the “Using Secondary Price Bars” help topic.
Note how moving our mouse pointer over another element in our strategy also refreshes the “Context-Sensitive Help” panel on the right side of the application’s main window to show the tooltip, the link to the most relevant help topic, and links to the related help topics. Clicking one of these links opens the help topic in your default browser.
Quagensia gives you the help you need, right when you need it, which is an indispensable time-saving feature.
If you want to view all our help topics, click the “Using Quagensia (The Main Help Page)” link at the bottom of the Context-Sensitive Help Panel, click on the appropriate link on the Start Page, or choose the appropriate item in the main menu’s “Help” menu. As you can see, Quagensia’s help documentation is quite extensive.
Let’s get back to building our strategy.
The “Input” field of our indicator defaults to “The Close”, but we can change it to a different property of the primary price bars data series, a property of a secondary price bars data series, a custom editable time series populated by our own custom calculations in our strategy, or another indicator to create a nested indicator.
Change the “Period” field from a hard-coded number to the input parameter we created called “Fast MA Period”.
Check the “Plot” check box so the indicator will appear on NinjaTrader® charts, uncheck the “Use Default Plotting Options” check box, and style the indicator’s plot as you wish. In addition to being able to change the color of the indicator, we can change other visual attributes of the plots as well.
Now, let’s repeat this process to add another internal variable of type “Indicator Time Series” named “Slow MA”. Instead of using the weighted moving average again, let’s use the triangular moving average instead. We already know that it can be found in the same subfolder as the weighted moving average but let’s pretend that we don’t know where to find it. If you can’t find the indicator you are looking for, enter part of its spoken name or even its keyword in NinjaScript® code in the text box at the bottom of the context menu, click the “Search for a Function…” button, and you should quickly be able to find the indicator you are looking for in the search window and choose to insert it at that location. To see how search works, just click on the gear icon to the right of the “(Not Set)” label next to the “Indicator” label, enter the word “triangular” into the search box, and click the “Search for a Function…” button. Our search found the triangular moving average. Note that the search window is context-sensitive in the same way that the context menus are, so the search results only include choices that are allowed at the location you clicked. For example, if there was a function with the word “triangular” in it that wasn’t an indicator, it wouldn’t show up in this search because you opened the search window from a location that only allows indicators. Also note that the search result shows us that we could have found this indicator in the context menu’s subfolder located at “Indicators” >> “Moving Averages” so we won’t need to search for it next time. Click on the search result for the triangular moving average and click “Add this function to your algo at the location you specified” and click the “Add Variable” button to add it to our strategy.
Change the “Period” field from a hard-coded number to the input parameter we created called “Slow MA Period” and plot this indicator as previously described, but this time choosing a different plot color or plot style.
Before we move on, notice the large number of additional data types that we can use as internal variables. In addition to the ones that most codeless strategy builders support, Quagensia N Edition lets you create and use internal variables of extremely powerful and highly advanced data types, including lists that can hold anywhere from zero values to millions of values, arrays which are like lists but can be single-dimensional or multi-dimensional, editable times series that allow you to create your own time series that you can plot and use in your trading logic similar to how you would use an indicator, secondary price bars that let you add time series for multiple instruments and multiple bar periods to your strategy, indicators, three kinds of dates and times, colors, many advanced numeric types, and a large number of “Lists of Choices” types.
Now that we have set everything up, it’s time to create the logic for our strategy in the “When Bar Updates” section. The logic in this section is executed on each eligible bar update event, which could be on each bar close, on each tick representing a trade in the market, or on each of another kind of bar update event, as determined by you in the NinjaTrader® user interface.
Click the “Add Trading Logic” link at the bottom of this section to open a context menu with all the actions that our strategy is allowed to take in this section. You will learn about the many kinds of powerful actions available to you in future videos. For now, just add a “Conditional Logic (If Block)” action.
Note that while some codeless strategy builders only support “If” blocks and do not support “Else If” or “Else” blocks, Quagensia allows you to add unlimited “Else If” blocks beneath each “If” block and add “Else” blocks as well.
Furthermore, while some strategy builders do not allow you to nest conditional blocks inside one another, Quagensia allows you to nest “If”, “Else If”, and “Else” blocks inside other “If”, “Else If”, and “Else” blocks which can be further nested inside even more “If”, “Else If”, and “Else” blocks.
Let’s enter a long position using a market order when a bunch of conditions are true, and we are not currently in a position.
Our first entry condition is that we can’t already be in a position. Let’s use context-sensitive search to find a function that sounds right then insert it at this location from the search window. Just click on the location where you want to insert a function and enter a search term. Whether you search for “Position”, “Flat”, “Short”, or “Long”, you won’t be able to miss multiple functions that will allow us to check if we are currently flat, but we will choose the function called “Current Market Position Is Flat (For Strategy)”.
Click on that row in the search results and click the context menu item to add it to our strategy at the location we specified, and let’s finish the condition by saying that this must be equal to True.
Once again, we’ve used context-sensitive search to find and insert a function without knowing how else to find it. When we roll our mouse pointer over the gear icon to the right of this function, we can see in the tooltip and the Context-Sensitive Help panel on the right of the application’s main window that its location in the context menu hierarchy is “More Expressions…” >> “Orders, Fills, and Positions” >> “Position Info”, and there it is. We could have inserted it by clicking on it from this context menu as well.
Now let’s add a moving average crossover condition. Click the condition we just made and click “Add Condition After Me Using AND”. Click on the comparison operator to change it to “Crossed Above a Time Series (While Time Series Shown on Left Going Up)”. Before we click on a choice, notice the large number of comparison operators that you can use in conditions. In addition to the ones that most codeless strategy builders support, Quagensia N Edition gives you many variations of “Crossed Above”, “Crossed Below”, “Is Between”, and “Is Within X Units of Y”.
Now let’s set the left side to our “Fast MA” indicator variable and the right side to our “Slow MA” indicator variable that we defined in the “Internal Variables” section.
Note the order of the three things we just did to create a condition. When creating a new condition, always start by choosing a comparison operator like we just did instead of filling in the left side or the right side of the comparison operator, because the choice for the comparison operator filters the choices in the context menus that appear when you click on the left side or the right side of it. To see an example of this, notice how there are a lot more choices when we click on the left side of the “Is Equal To” comparison operator than when we click on the left side of the “Crossed Above a Time Series (While Time Series Shown on Left Going Up)” operator, since only functions that return time series are allowed to go on the left side or the right side of a “Crossed Above” operator, but almost all functions return a value that can be equal to another value. Once you choose your comparison operator, you should build an expression on the left side first, because your choice for the left side further filters your choices on the right side. For instance, if you choose a numeric expression for the left side, the context menu that opens when you click on the right side will only have numeric expressions. If you then decided that you wanted to change the comparison to compare a date or time with another date or time, remember that you need to do this from the left side first, because in our example the right side will be filtered to only have numeric expressions whereas the left side will let you choose any expression that is compatible with the “Is Equal To” comparison operator, including date and time expressions.
In summary, to create a condition, first set the comparison operator in order to properly filter the context menu that opens from the left side, then choose an expression for the left side in order to properly filter the context menu that opens from the right side.
Now let’s add conditions that say we want the low of the price bar that just closed to be higher than the low of the previous price bar and the low of the previous price bar to be higher than the low of the price bar before it.
Click on the condition we just created and click “Add Condition After Me Using AND”. Change the comparison operator to “Is Greater Than” since we should always set the comparison operator before we set the left or right sides. Set the left side to “Price Data” >> “Low”, copy and paste the left side onto the right side, then on the right side change “The Current Bar” to “The Previous Bar”. Now let’s create a condition that compares the previous price bar to the bar two bars ago. Click on the condition we just created and click “Add Condition After Me Using AND”. Since this condition will be almost the same as the one we just made, let’s simply copy and paste the completed condition we just made on top of the empty condition we just added beneath it. Change the left side to “The Previous Bar” and change the right side to “The Bar 2 Bars Ago”.
Note that if instead of choosing “The Bar 2 Bars Ago” we chose “The Bar a Custom Number of Bars Ago” a text box appears that expects a whole number greater than or equal to 0. Use the number 0 to indicate “The Current Bar”, use 1 to indicate “The Previous Bar”, and so on. Unlike in some other codeless strategy builders, you can also replace this text box with any other expression that returns a whole number, including an input parameter, an internal variable, or a highly complex mathematical formula that combines functions, input parameters, and internal variables together so long as the end result is a whole number rather than a decimal number.
Now let’s add a condition merely for instructional purposes that says we only want to consider signals that occur on Monday or Tuesday. Click on the condition we just created and click “Add Condition After Me Using AND”. Click on the left side and search for “Day” or “Week”, and insert the “Day of Week” function from the search window. We want the day of the week of the current price bar, not a hard-coded date in a text box, so click on the gear icon to the right of the text box and choose “Price Data” >> “Time”.
Before we choose this option, notice how the context menu only has choices that are Date and Time expressions, so we don’t have to browse through the over 1,900 functions built into Quagensia N Edition to find what we are looking for. We need to set the right side to the hard-coded value “Monday”, so click on the right side and choose “Enter a Hard-Coded Value” >> “Select an Item from a List of Choices” >> “Day of Week (Selected From a List of Choices)”, and from the list of choices choose “Monday”. Before we move on, note that the list of days of the week is only one example of a “List of Choices” data type, of which there are many in Quagensia N Edition.
Now let’s also allow signals to occur on Tuesday. Click on the condition we just made and choose “Add Condition After Me Using OR”. Since the “Monday” filter is almost identical to the “Tuesday” filter, let’s just copy and paste the Monday filter on top of the empty condition beneath it and change “Monday” to “Tuesday”.
Let’s add parentheses around the Monday and Tuesday conditions so that they are evaluated as a group. Click on the Monday condition, click on “Add Parentheses”, then click on the right parenthesis below the Monday condition and click “Move Down”. Now the Monday and Tuesday conditions will be executed as a group instead of the Monday condition being executed in an AND’ed fashion with the conditions above it, and the Tuesday condition erroneously being considered by itself, which would likely cause an entry every single Tuesday.
Note that some codeless strategy builders only give you the choice of using either the “AND” operator or the “OR” operator in a given condition, but you cannot combine the two in the same condition. Quagensia lets you use the “AND” operator, the “OR” operator, and parentheses in the same condition to create much more advanced sets of conditions.
Furthermore, while some codeless strategy builders do not allow you to nest sets of conditions inside of one another, Quagensia allows you to nest sets of conditions inside of other sets of conditions which can be further nested inside of even more sets of conditions.
Let’s take a look at what we just built. We now have a condition set that is checked every bar close that says “If our strategy is not in a position, i.e. it is flat, our fast moving average indicator variable crossed above our slow moving average variable while our fast moving average was going up, the last two bars have higher lows than their previous bars and the day of the week is either Monday or Tuesday, then do something”.
Let’s make it do something when this condition set is true.
First, let’s enter a long position with a market order by clicking the “Add Trading Logic” link that is directly beneath the “Then do the following…” header and choosing “Submit an Order” >> “Enter Long” >> “Enter Long Position Using A Market Order”.
Second, let’s set the candle outline color of the signal bar to another color by clicking the “Add Trading Logic” link that is directly beneath the action we just added and choosing “Draw Something” >> “Colors” >> “Set Candle Outline Color”. Then click on the color button in the “Color” input parameter of this action and choose a different color.
Third, let’s draw a tiny diamond a little above the high of the signal bar by clicking the “Add Trading Logic” link that is directly beneath the action we just added and choosing “Draw Something” >> “Tiny Shapes” >> “Create New” >> “Draw Diamond”. Then click on the color button in the “Color” input parameter of this action and choose a different color.
To draw the diamond a little above the high of the signal bar, the “Y-Value” parameter needs to be set to the High of the Current Bar. If this is not already done by default, you can do this by choosing “Price Data” >> “High”.
Let’s multiply this number by 1.01 to draw the diamond 1% above the high. To do this, click on the gear icon to the right of “The High of The Current Bar” and choose “This Number * [A New Number]” and enter 1.01 in the text box. Note that while this arithmetic example is a simple multiplication, you can keep adding, subtracting, multiplying, and dividing terms together and use parentheses to change the order of operations of the arithmetic equation. Furthermore, each term in the arithmetic equation can be a very complex numeric expression made up of many expressions nested together, with each expression’s input parameters being set to other expressions whose input parameters are set to even more nested expressions, and where each expression can actually be a complex arithmetic combination of many terms, like the most complicated mathematical formulas that you’ve ever seen in any advanced math textbook. This ability to create incredibly complex expressions at a location where a single expression of a certain data type is expected works on all simple data types, such as dates, text, and Booleans, it works on time series, and it works on individual items in time series, lists, and arrays, not just numbers.
Let’s add one more action inside our “If” block. Let’s write a message to the NinjaScript® Output window that includes the timestamp and the close price of the signal bar. Writing the values of certain variables to the NinjaScript® Output window is one of the best ways to troubleshoot, or “debug”, strategies that aren’t working correctly inside of NinjaTrader®. So far we’ve always added actions by clicking the “Add Trading Logic” link at the end of all the other actions. This time, let’s insert this action above the “Set Candle Outline Color” action by clicking on the “Set Candle Outline Color” action and choosing “Add Item Before Me” >> “Output (Alerts and Logging)” >> “Print To Log / Output Window” >> “Print To Output Window (With Primary Data Series Time Stamp)”. This function automatically writes the timestamp, so we only need to write the close price of the signal bar.
First, type “Entry Close:” in the “Message” input parameter’s text box. To append the numeric close price of the signal bar as text to the end of the text “Entry Close:”, first click on the gear icon to the right of the text box and choose “This Piece of Text + [A New Piece of Text]”. A plus sign and an empty text box are added to the right of the first text box. To replace the empty text box with the close price of the signal bar, click on the gear icon to the right of the empty text box and select “Close” in the context menu under “Price Data”. When you do this, “The Close of The Current Bar” replaces the text box. Notice how “The Close of The Current Bar” on the canvas is wrapped inside of a function called “Convert Non-Text To Text”. Whenever you attempt to place a non-text expression, such as a number or date, at a location where text is expected, Quagensia will automatically wrap the non-text expression you selected inside of a “Convert Non-Text To Text” function so that your non-text expression will be automatically converted into text, allowing it to be used as text. One of the best things about Quagensia is that its validation system stops you from making serious mistakes like putting text where a number should go or putting a number where text should go. This is why whenever you click on a location that expects an expression of a certain data type, you will only see choices for expressions whose data types are compatible with the expected data type of the location, and Quagensia will automatically convert your selected expression into an expression with the expected data type of the location. If Quagensia didn’t do this, many bad things would frequently happen such as your strategies not compiling or not running within NinjaTrader®.
We’ve now finished our entry logic. Let’s create our exit logic, using copying and pasting as much as possible. First, we need an “Otherwise, If (Else If)” block beneath our “If” block by clicking on our “If” block and choosing “Add ‘Otherwise, If’ (Else If)” Block. This is different from adding another separate “If” block, because the condition set in “Else If” blocks are only considered if the conditions in the “If” block and the conditions in all other “Else If” blocks above the “Else If” block are false, whereas the condition set in another separate “If” block is checked unconditionally. An “Else If” block is good in this case because if the entry condition is true on a bar and an entry order is placed, we don’t even want to check for the exit condition. If we did, we might attempt to enter and exit a position on the same bar, and this could cause unexpected behavior and therefore possibly trading losses.
Copy and paste the “Crossed Above” condition from the “If” condition set into the “Else If” condition set. Make sure you click on the inner gear icon in the “Else If” condition set, because the inner gear icon is for the empty condition we want to paste on top of whereas the outer gear directly to its right is the gear icon for the entire condition set, which if clicked won’t give you the option to paste a single condition. Change the comparison operator to “Crossed Below a Time Series”. Add an AND’ed condition above the condition you just pasted, and let’s set it to check if our strategy’s current position is “Long”. On the left side, choose “More Expressions” >> “Orders, Fills, and Positions” >> “Position Info” >> “Current Market Position Is Long (For Strategy)” and change the right side to “True”.
That’s it for our exit criteria. To exit the entire long position with a market order if the exit condition set is true, click on the “Add Trading Logic” link beneath the “Then do the following…” header and choose “Submit an Order” >> “Exit Long” >> “Exit Entire Position (All Entries)” >> “Exit Entire Long Position Using A Market Order (All Entries)”.
Let’s get the other actions into the exit block the quick way using a multi-action copy and paste. To do this, in the “If” block, click on the “Print To Output Window (With Primary Data Series Time Stamp)” action, choose “Start Multi-Selection Here”, then click the “Draw Diamond” action and choose “Copy Selected Items”, then click on the “Add Trading Logic” link of the “Else If” block and choose “Paste”.
Note that while some codeless strategy builders have rather limited copy and paste functionality, Quagensia lets you easily copy and paste single expressions, single actions, and multiple actions within the same strategy, between two different strategies and between strategies and functions. In fact, the quickest way to make a Quagensia Function that you can use in all your Quagensia Strategies is to copy and paste some trading logic from a Quagensia Strategy into a new Quagensia Function.
When you do this, not only is the trading logic copied over, but even the input parameters of the Quagensia Strategy and most kinds of internal variables of the Quagensia Strategy that are used by the trading logic are copied over, making it really fast to create a reusable Quagensia Function out of logic in a strategy, and then you don’t even need to copy and paste anymore, because the Quagensia Function shows up in context menus when you click on locations where the trading logic is valid.
Back to our strategy.
Let’s just change the “Entry Close:” text in the first pasted action to say “Exit Close:” so we will know if the output is for an entry or an exit. To finish up, let’s draw the diamond 1% below the low of the exit signal bar by changing “The High” to “The Low” and changing 1.01 to 0.99.
Congratulations. You have now created a complete trading strategy. We are now ready to generate our strategy’s code and send it to NinjaTrader® 8 by clicking this button.
Before we do this, let’s open the NinjaTrader® 8 NinjaScript® Editor so that the code of our strategy will be automatically compiled by NinjaTrader® into a format that it understands, which will make our strategy ready to be backtested in the NinjaTrader ® 8 Strategy Analyzer.
To do this, first open up NinjaTrader® 8.
While NinjaTrader® is loading, it should be noted that Quagensia N Edition is a product of Quagensia, Inc., a company which has no affiliation with NinjaTrader, LLC.
Also, NinjaTrader® and NinjaScript® are registered trademarks of NinjaTrader, LLC.
Now that NinjaTrader® has opened, click “New” on the main menu and then “NinjaScript® Editor”.
Turn your sound up as well so that you can hear the sound that the NinjaScript® Editor makes when it successfully receives and automatically compiles the strategy code files you send it from Quagensia N Edition.
NinjaTrader® 8 is now ready to receive your strategy’s code file.
Now let’s generate our strategy’s code and send it to NinjaTrader® 8 by clicking this button. Now listen for the sound that the NinjaScript® Editor makes when it successfully receives and automatically compiles the strategy code files you send it from Quagensia N Edition.
What you just heard was the sound of success.
If you don’t have your sound up, you can make sure that your strategy code gets compiled by clicking the “Compile” button in the NinjaScript® Editor’s toolbar. You will see an animated icon in the lower left-hand corner of the NinjaScript® Editor that shows that compilation is in progress. When it goes away with no compilation errors displayed you know that the compilation was successful.
You don’t need to open your strategy code file from within NinjaTrader® 8, but if you do want to look at the code, you will find it under the “Strategies” folder of the NinjaScript® Editor. By default, all NinjaScript® strategy code files generated by Quagensia N Edition start with the prefix “Q underscore”. Double click your NinjaScript® strategy to open it within the NinjaScript® Editor.
Our trading strategy is now ready to be backtested and optimized from within NinjaTrader® using the NinjaTrader® 8 “Strategy Analyzer”.
Let’s also open the NinjaScript® Output window by clicking on the “NinjaScript® Output” icon in the NinjaScript® Editor’s toolbar. This is where our “Print To Output Window” actions will print messages.
If you haven’t already downloaded the market data for the instrument on which you want to backtest your strategy, connect to a market data feed by clicking the “Connections” main menu followed by the market data feed of your choice. For this tutorial we will choose the market data feed called “Kinetick – End of Day (Free)”. If the connection was successfully made, you will see a green circle in the bottom left-hand corner of the main window of NinjaTrader® 8 and you will hear a sound if your sound is turned up. If the connection was not made, try a different market data feed for the purposes of finishing this tutorial.
Now, open the Strategy Analyzer by clicking on “New” on the main menu and then “Strategy Analyzer”.
Set the “Backtest type” to “Backtest”. Set the “Strategy” drop-down list to your strategy. If the strategy names are cut off, you can make the properties grid panel wider by moving the splitter bar to the left.
Choose an instrument. We will use the symbol “SPY” for this tutorial. “SPY” is the symbol for the SPDR S&P 500 ETF, which is designed to track the S&P 500.
To run a backtest on daily bars, set the “Price based on” field to “Last”, the “Type” to “Day”, and the “Value” to “1”.
Choose a “Start date” about ten years in the past and set the “End date” to the current date.
Make sure that “Slippage” is set to “2”, the “Exit on session close” check box is unchecked, the “Set order quantity” is set to “Default Quantity”, and the “Default quantity” is set to 100.
Now, run the backtest by clicking the “Run” button.
Our backtest was successfully executed by NinjaTrader® 8, as can be seen by the fact that our “Total # of trades” was non-zero and the other fields are generally populated with non-zero data.
If all you see are zeros, something went wrong.
First, maybe the value of one of the fields in the Strategy Analyzer’s property grid is causing the problem. For instance, maybe you have no trades because there is no market data to backtest over. This could be the case if the “Instrument” field is set to one for which your installation of NinjaTrader® 8 has no market data between the “Start date” and “End date”, or maybe you attempted to run the backtest on 1-minute price bars instead of daily price bars and your market data consists of only daily bars, not intraday bars such as 1-minute bars. Please check all the fields in the properties grid carefully to look for issues.
If you can’t see an obvious problem, try manually charting the same market data that you tried to backtest over in a chart window that you can open by clicking “New” on the main menu then clicking “Chart”. If the market data in the chart looks good, then maybe something else is the problem.
For instance, maybe NinjaTrader® 8 encountered an error during the backtest, which you can determine by looking for errors in the “Log” tab of the main window of NinjaTrader® 8. If no errors are displayed in the log for your backtest, maybe the logic of your Quagensia N Edition Strategy is wrong. For instance, maybe the condition that must be true to enter a position will never be true in any scenario, or maybe the condition is too strict such that it is so rarely true that it wasn’t true even once between the “Start date” and “End date” of your backtest.
When you are trying to troubleshoot, or debug, your strategy’s trading logic, you can use “Print To Output Window” actions just like we did in our strategy to print output to the NinjaScript® Output window to first determine if and on what price bars or ticks a given “If”, “Else If”, or “Else” block’s condition set is true and second to determine the values of prices, indicators, and variables at a given point in your strategy’s logic on each price bar or tick where the “Print To Output Window” action was executed. When we look at the NinjaScript® Output window we can see the dates and times of the price bars when entries and exits occurred, and we can see the close prices of those bars.
Let’s take a quick look at the backtest results of our strategy, bearing in mind that they are only hypothetical performance results obtained with the benefit of hindsight and are not necessarily representative of the actual trading results that would be achieved by trading this strategy in actual markets. Also, note that we do not offer trading advice, so the example strategy we built for this tutorial and our commentary on its backtest results are meant for educational purposes only and should not be construed as a recommendation to trade using any particular strategy or as a recommendation to buy or sell any particular instrument. Finally, note that trading and investing in the financial markets can involve a high degree of risk of financial losses, including losing your initial investment or more.
Now, let’s look at the backtest results of the strategy we just built.
The backtest results screens of the NinjaTrader® 8 Strategy Analyzer are truly excellent and deserve many tutorial videos, but for the purposes of this video, we will only take a cursory look at a few of them.
First, the “Summary” screen gives us a high-level view of our backtest results. A key metric is “Profit Factor”. It is the ratio of gross profit and gross loss. If it is less than 1, our trading strategy lost money. And if it is barely above 1, the gross profit is barely above the gross loss on a percentage basis, so it lost almost as much as it gained.
Now, let’s look at the cumulative net profit of our strategy over time by choosing “Analysis” in the drop-down list in the upper left-hand corner of the Strategy Analyzer, then setting the “Graph” drop-down list on the Analysis screen to “Cumulative Net Profit”.
As you can see, while our strategy was profitable, it did not perform particularly well until near the end of the backtest period.
The Analysis screen contains a wealth of information about the backtest results of our strategy. When we change the values in the drop-down lists in this screen, we will get entirely new reports that will shed much light on the backtest results.
Finally, let’s look at a chart with our strategy’s trades displayed on it by choosing “Chart” in the drop-down list in the upper left-hand corner of the Strategy Analyzer. Here we can see that the indicators we configured in our Quagensia Strategy are displayed, and we can see that our strategy entered and exited long positions based on our criteria. Note that the positions are entered and exited on the price bar directly after the signal bar, not on the signal bar itself. This is correct, and follows the basic principle that if we submit a market order based on a signal occurring on a bar close or tick, our market order won’t get filled on that bar close or tick because the bar close or the tick occurred in the past. Instead, the soonest our market order could possibly be filled would be directly after the bar close or tick event occurred. In a backtest, this will be on the bar open of the next bar or the next tick. In actual markets, even this cannot be assumed.
Now that we have backtested our strategy using a fast moving average period of 3 and a slow moving average period of 6, let’s quickly run many backtests of our strategy, but using different values than 3 and 6 for our input parameters. To do this, change the “Backtest type” to “Optimization”, set the “Min” and “Max” values of “Fast MA Period” to 3 and 15, set the “Min” and “Max” values of “Slow MA Period” to 6 and 30, and click “Run”. That was fast. This video was not sped up. NinjaTrader® 8 just backtested our strategy with 325 combinations of fast and slow moving average lengths in under ten seconds.
Before we look at the optimization results, it must be stated that not only are these results hypothetical, but that the “most optimal” of the 325 variations of our strategy were determined by searching over a large number of strategy backtests. So even if our strategy is non-predictive it is likely that at least some of its 325 variations produced highly profitable backtest results merely by chance.
With that said, let’s view our optimization results. The top part of the Optimization screen shows the backtests with the best profit factors. Clicking on one of these backtests loads the bottom part of the screen with the backtest results screens that were previously discussed.
To get a quick picture of all of the backtest results for all 325 combinations of fast and slow moving average lengths, change the “Display” drop-down list to “Optimization Graph”, set “Parameter 1” to “Fast MA Period”, and set “Parameter 2” to “Slow MA Period”. You can change the values in the drop-down lists in the bottom panel to create different graphs to gain a better understanding of how changing the moving average period lengths changed the backtest results of our strategy.
Congratulations. You have now created, backtested, and optimized a complete trading strategy, albeit a very basic one.
Due to lack of time, we barely scratched the surface of the kinds of advanced algorithmic trading strategies you can easily build in Quagensia N Edition without code by simply clicking choices on the screen. Advanced features are covered in other tutorials.
It’s the advanced features of Quagensia N Edition that most set it apart from other codeless strategy builders.
For instance, while some codeless strategy builders don’t allow you to extend their functionality by creating your own functions without code, Quagensia makes this easy and even lets you create functions that call other functions that call even other functions, which allows you to build an increasingly powerful library of trading logic components that grows with you.
Also, while most codeless strategy builders don’t support loops, Quagensia makes it easy to create multiple types of loops without code, and you can even create loops inside of loops inside of other loops.
Also, most codeless strategy builders don’t have any support for lists, single-dimensional arrays, and multi-dimensional arrays of data, but Quagensia makes it easy to use these advanced data types without code by simply clicking choices on the screen. You may not start out using lists, arrays, and loops on day one, but soon enough you will probably find out that the use of lists, arrays, and loops is often the only way to accomplish an intermediate or advanced level task, and you will realize that the lack of support for lists, arrays, and loops would severely limit your ability to create many kinds of advanced trading strategies.
Also, unlike most codeless strategy builders, Quagensia N Edition’s innovative Quagensia Table System even makes it easy to codelessly load tables of lots of kinds of data, not just time series data, from data files on disk into in-memory data tables that you can query, modify, and then write back to data files on disk from your strategies. If you have valuable data in Microsoft Excel files, a database such as Microsoft SQL Server, SQLite, or MySQL, if you have access to exotic data via Bloomberg® Terminal®, Refinitiv® Eikon®, or another high end institutional platform, if you have tabular data output from MATLAB® or R, or if you have access to alternative data sources on the internet, it is generally very easy to get these kinds of data into comma-delimited, semicolon-delimited, or otherwise character-delimited data files. And once you do, it is easy to get all of this data, even entire databases of data, into your NinjaTrader® strategies by simply clicking choices on the screen in Quagensia N Edition. Since Quagensia N Edition also makes it easy to write data from in-memory data tables to data files on disk, this allows you to run a backtest or optimization of a NinjaTrader® 8 strategy that outputs a report containing tabular data that you can load into any database, use as input data in MATLAB® or R, or load into Microsoft Excel to manipulate further and use to create visually powerful charts and graphs that answer your questions about the markets in ways that backtest performance reports cannot, all done codelessly by simply clicking choices on the screen.
To learn more about the other powerful features of Quagensia N Edition, go to Quagensia.com, and watch our other videos where you will learn how to create strategies with far more advanced, interesting logic than the basic one we just built.
You will see how Quagensia N Edition really shines when it comes to enabling you to create advanced, complex trading strategies that are better able to model your most complex trading ideas.
If you haven’t downloaded and installed the Quagensia N Edition free trial, go to Quagensia.com and download it today.
Use the free trial period to create as many algos as you can because they are yours to keep after the free trial ends.
When you download the free trial you will also get all our free no-code trading strategies which you can use as-is and customize without code.
Have a great day.