Locate and Rank Top X Bars and Output a Report (Uses Tables of Data) (Point-and-Click NinjaTrader® Strategy)
Original Trading Strategy
Links to the section of the video titled “Using & Customizing the Built-In Strategies in Quagensia N Edition” in which this built-in strategy is covered.
Peer Reviews, Modifications & Responses
Thanks for Sharing!
Trading Strategy Summary
No-code strategy building tools that lack features like tables of data, lists, arrays, and loops make it tremendously difficult or impossible to create trading strategies that output many kinds of advanced reports to tabular data files on disk.
Quagensia N Edition’s innovative table system makes it easy to codelessly use point-and-click to create and output advanced reports by allowing you to create in-memory data tables, then populate them with data, then create, modify, and delete rows from these in-memory data tables and sort and filter their data, then write that data to tabular data files on disk. This allows you to run a backtest or optimization of a NinjaTrader® 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.
This Quagensia Strategy uses tables of data to get the list of the “top X” specified number of bars with the highest values for the metric of interest between the backtest start and end date, write the bar end time and the value for the metric of interest for each of the “top X” bars to a tabular data file using the Quagensia Table System’s advanced tabular data file writing system. It also marks them up on the chart to make it easy to quickly identify them when scrolling backward over all the bars between the backtest start and end date.
When run as-is, without customizations, this report’s “metric of interest” is “% Change (From Bar Open Price To Bar Close Price)”, and every bar between the backtest start and end date is considered for inclusion in the “top X” bars for that metric. However, this report can be easily customized without code. You can easily change the “metric of interest” by changing a single calculation, and you can plug in your own conditions for which price bars should be considered for inclusion in the “top X” bars for that metric instead of considering all the bars for inclusion.
To customize this report without code, scroll down to the two yellow “comments” that start with the following message and follow their instructions:
”
********************************************************************
********************************************************************
Attention paid users
********************************************************************
********************************************************************
”
This Quagensia Strategy demonstrates how to do the following powerful things with tables of data:
1. Create an in-memory table of data that is initially empty and then populate the table of data from calculations in the strategy instead of loading the table of data from a tabular data file on disk.
2. Query and manipulate the data using 1) sorting and 2) extracting a number of rows from the top of a data table.
3. Do queries whose results are copied into a new table of data rather than overwriting the queried table of data.
4. Loop over the rows of a table of data and get the values of the fields in each row.
5. Write a table of data to a tabular data file on disk.
NOTE #1: If you will only be running this report as-is, i.e. if you will be changing neither the criteria for what price bars will be included in the results nor the bar metric being measured and ranked, the “Bars required to trade” field in the NinjaTrader® user interface can be as low as 0, since by default this report considers only the current price bar. If you will be running this report with your own criteria for what price bars will be included in the results or your own calculation for the bar metric being measured and ranked, the “Bars required to trade” property will likely have to be larger to account for things like a 10-period simple moving average needing at least 10 price bars of initial data before it can provide a valid result for a given price bar. The larger you make this number, the less likely you will experience run-time errors or logic errors, but the more initial price bars will be required before the first price bar will be considered for inclusion in this report. If you use a really big number for the “Bars required to trade” property, like 200, you will notice that the “Start Date” displayed in the report results will be the bar end time for the price bar approximately 200 bars after the first price bar occurring on or after the “Start date” that you set in the NinjaTrader® Strategy Analyzer, because in the example just given, the first 200 trading bars of data would be skipped before the logic in the “When Bar Updates” section below would be executed for each price bar in the backtest that creates the report.
NOTE #2: This strategy uses Quagensia N Edition’s Quagensia Table System, which is also called the “tables of data” feature. This feature only works properly if the proper versions of the QuagensiaNEditionRuntime.dll, QuagensiaNEditionRuntime.cs, and QuagensiaRuntime.dll files are present in the NinjaTrader® Bin-Custom folder and the QuagensiaNEditionRuntime.dll and QuagensiaRuntime.dll files are properly “referenced”, all of which is accomplished by importing the QuagensiaNEditionRuntime.zip NinjaScript® add-on file that comes with Quagensia N Edition into NinjaTrader®. If the proper versions of the QuagensiaNEditionRuntime.dll, QuagensiaNEditionRuntime.cs, and QuagensiaRuntime.dll files are not present in the NinjaTrader® Bin-Custom folder when you generate the code for a Quagensia N Edition Strategy that uses the “tables of data” feature, the Quagensia N Edition Runtime Setup Assistant will automatically open and will guide you through the quick and easy process of importing the QuagensiaNEditionRuntime.zip NinjaScript® add-on file that comes with Quagensia N Edition into NinjaTrader®.
Quagensia N Edition’s innovative Quagensia Table System makes it easy to load tables of many kinds of data, not just time series data, from tabular data files into in-memory data tables that you can query, modify, and then write back to tabular 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 tabular 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.
You can also create in-memory data tables and populate them with data from scratch, i.e. not from tabular data files, and then create, modify, and delete rows from these in-memory data tables.
And as this strategy demonstrates, regardless of how you create your in-memory data tables, Quagensia N Edition makes it easy to write that data to tabular data files on disk.
All features just described work with data columns of 73 different types of data including: 10 numeric data types, 3 date/time data types, colors, Booleans (true or false), text, and 57 “lists of choices” data types such as the months of the year or the days of the week.
Copyright © 2023 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.


