Introduction
Creating and using variables in Quagensia® Algos is easy, and nearly every non-trivial Quagensia Algo will use variables to accomplish tasks such as getting data into the algo, storing intermediate results of complex calculations, storing the results of calculations made in the “When Bar Updates” section of a Quagensia Strategy on the current price bar or tick so that the results of the calculations can be used in the “When Bar Updates” section of the Quagensia Strategy on price bars that occur in the future, and getting data out of Quagensia Functions by passing variables into input parameters “by reference”.
All editions of Quagensia have two kinds of variables: input parameters and internal variables.
Quagensia Variable Special Cases
- Quagensia T Edition Expression Functions only have input parameters. They do not have internal variables. Workaround: If you need to create a Quagensia T Edition function that does calculations that must be stored in internal variables and then returns a result, you can create a Quagensia T Edition Action Function instead of an expression function, create an input parameter that can be set by reference, create variables in a Raw Code Output (Single or Multiple Statements) action’s text box by right clicking in the text box and selecting the variables to define, then store the results of the calculations in the variables, then return the result by setting the value of the input parameter by reference rather than returning the result as the return value of a Quagensia T Edition Expression Function.
- Quagensia N Edition Functions with a return value of “Indicator Time Series” are really only “wrappers” for NinjaTrader® indicators with no “Function Logic” section. Quagensia N Edition functions of this type have both input parameters and internal variables, but both variable types are used only for the purpose of quickly and easily getting a NinjaTrader® indicator into the Quagensia N Edition user interface so that the indicator can be used visually and codelessly. Since variables of both variable types are merely for configuring a “wrapper” for a NinjaTrader® indicator rather than being part of your own custom calculations and logic, much of the information below isn’t relevant to this special Quagensia Function type.
Input Parameters
The most common use of input parameters is to get data into a Quagensia Algo. For Quagensia Strategies, this means getting data from a trading platform’s user interface into the Quagensia Strategy. For Quagensia Functions, this means getting data from another Quagensia Algo, which could be either a Quagensia Strategy or another Quagensia Function, into the Quagensia Function.
Internal Variables
Internal variables are variables that are used internally inside of your Quagensia Algo. You can “initialize” their values to an initial value, then change them in the following ways:
- Set an internal variable to a new value using the “Set a Variable” action.
- Pass an internal variable into a Quagensia Function’s input parameter whose “Pass By Reference” check box is checked and let the internal logic of the Quagensia Function change the value of the internal variable by reference.
You can use internal variables to store intermediate results of complex calculations or to store the results of calculations made in the “When Bar Updates” section of a Quagensia Strategy on the current price bar or tick so that the results of the calculations can be used in the “When Bar Updates” section of the Quagensia Strategy on price bars that occur in the future.
You can create create internal variables in Quagensia Strategies, and in Quagensia N Edition Functions, by defining them inside of the “Internal Variables” section of a Quagensia Algo’s workspace.
Quagensia T Edition also allows you to define special kinds of variables in a Raw Code Output (Single or Multiple Statements) action’s text box by right clicking in the text box and selecting the variables to define. This method of defining variables becomes absolutely essential when creating Quagensia T Edition Functions, because the only way to create variables that store information in Quagensia T Edition Functions is by using this method in a Quagensia T Edition Action Function.
Special Kinds of Internal Variables
- Quagensia N Edition strategies allow to create an “Indicator Time Series” variable which allows you to set up either a simple NinjaTrader® indicator or a highly complex “nested indicator” (i.e. the 10-bar simple moving average of the 20-bar exponential moving average of the close price), plot the indicator, and either use the indicator as an entire time series or use the value of the indicator on the price bar a specified number of bars ago counting backward from the current bar throughout the strategy.
- Quagensia T Edition algos don’t have “Indicator Time Series” variables, but you can accomplish a similar thing using a “Read-Only Time Series” internal variable. Note that while Quagensia supports the ability to “nest” read-only time series variables, most Quagensia Functions that return read-only time series variables use actual EasyLanguage® functions, and it is considered best practice to not nest EasyLanguage® functions that return time series, but rather to create nested indicator time series by using multiple internal variables, with each successive internal variable using the internal variable directly above it as an input parameter. For example, you should first create a non-nested read-only time series internal variable, such as a 10-period simple moving average of the close price, and then create a second read-only time series internal variable below it, and use the above read-only time series internal variable as an the input into the second read-only time series internal variable. If you are an advanced user and are confident that it is safe to nest certain EasyLanguage® functions at the location in your EasyLanguage® strategy code that will be outputted by Quagensia T Edition, you can uncheck the check box titled “I Want to Use Best Practices When Using EasyLanguage® Functions (e.g. Indicators)”, which is located in the “Strategy Details” section of Quagensia T Edition Strategies and the “Function Details” section of Quagensia T Edition Functions.
- Quagensia Strategies allow you to create “Editable Time Series” internal variables as well. This variable type allows you to create a highly customized time series that represents something that can’t be represented by functions built into the trading platform, but rather must be calculated with more complicated logic. Note that while read-only time series variables get their data automatically from the trading platform, you are responsible for setting the values of an editable time series. You do this by setting the editable time series’ current bar’s value in every “When Bar Updates” event so that on bars in the future the editable time series will have values for price bars in the past.
- Quagensia N Edition strategies allow you to create an internal variable of type “Secondary Price Bars” that creates a secondary price bar time series in your NinjaTrader® strategy. A secondary price bar time series can have a different symbol, a different bar period, or a different symbol and bar period than the main set of price bars, i.e. the “primary price bars”, on which your strategy runs. Creating a secondary price bar time series for a TradeStation® EasyLanguage® strategy is done differently in Quagensia T Edition. Instead of creating an internal variable of type “Secondary Price Bars”, you can create an internal variable of type “Read-Only Time Series” and change its “Data Series Index” field to a whole number greater than 1. To use the secondary price bar time series configured in Quagensia T Edition, you need to add one or more additional time series to the TradeStation® chart to which the TradeStation® strategy is attached, so that the value entered in the “Data Series Index” field is less than or equal to the total number of time series on the chart. Failure to do this will cause an error to occur.
- You can create internal variables that store lists and single-dimensional and multi-dimensional arrays as well. Lists and arrays are collections of values of a certain data type, such as a collection of numbers or dates. Very few simple trading strategies use lists or arrays, but many non-trivial trading strategies depend heavily on lists, single-dimensional arrays, or multi-dimensional arrays. The more trading strategies you create using Quagensia, the more likely it is that you are going to need to use lists and arrays for more and more or your trading logic.
Using Input Parameters and Internal Variables
The input parameters and internal variables defined in a Quagensia Algo can be used like the many types of non-settable expressions in the code. Additionally, all settable internal variables as well as settable input parameters (i.e. those that are passed by reference into a Quagensia Function) can be used in locations where only settable expressions are allowed, including the left side of a “Set a Variable” action and as a settable variable that is passed into a Quagensia Function’s input parameter that requires its data to be passed by reference.
The list of input parameters and internal variables that are allowed at a location on your Quagensia Algo’s workspace are located in the context menu folder “Replace/Set Item >> Input Parameters and Internal Variables” of the context menu that opens when you click on a location where at least one input parameter or internal variable is allowed.
Passing Input Parameters “By Reference” into a Quagensia Function
A Quagensia Function’s input parameter whose “Pass By Reference” check box is checked can also be used to get data out of the Quagensia Function. You can set these kinds of input parameters in a “Set a Variable” action in the same way that you can set internal variables, and you can even pass these kinds of input parameters into other Quagensia Functions that have one of this kind of input parameter and set the input parameter’s value by reference in the second Quagensia Function. Getting data out of a Quagensia Function via an input parameter when the Quagensia Function is used in another Quagensia Algo is simple: once the Quagensia Function is called and the input parameter is set by reference from somewhere inside of the Quagensia Function, whatever variable you passed into the Quagensia Function’s input parameter by reference will have the value to which it was set inside of the Quagensia Function. You can use this variable’s changed value in the code that is called after the Quagensia Function was called. Note that the “Pass By Reference” check box is one of the “Advanced Fields” of an input parameter that you can view by clicking the “Advanced Fields” link button of any input parameter in the “Input Parameters” section of a Quagensia Function.
Notes about passing variables by reference to a Quagensia Function:
- Quagensia N Edition Function input parameters of type “Editable Time Series” are always editable in Quagensia N Edition Functions even though Quagensia N Edition Function authors will note that the “Pass By Reference” check box is disabled and unchecked for Quagensia N Edition Function input parameters of type “Editable Time Series”.
- Quagensia N Edition Function input parameters of type “Array” are automatically passed by reference and are always editable in Quagensia N Edition Functions.
- With the exception of Quagensia N Edition input parameters of types “Editable Time Series” and “Array”, as described above, authors of Quagensia Functions for all editions of Quagensia have a choice as to whether or not Quagensia Function input parameters of one of the three “editable collection” data types (i.e. “Editable Time Series”, “Array”, and “List”) should be passed by reference or not. However, even when an Array or List is not passed by reference, its items can still be modified from within a Quagensia Function in all ways except via the “Set a Variable” action, i.e. via all of the many Quagensia Action Functions that come pre-installed with Quagensia that change Arrays and Lists, and in the case of input parameters that are Lists and Arrays (except for Quagensia T Edition Arrays whose “Number of Items in Dimension 1 is Dynamic (i.e. Not a Fixed Constant)” check box is unchecked), items can be added and removed from the Array or List. The only difference between passing an editable collection by reference vs. not passing it by reference, for those editable collections that give you that choice, is that a “Set a Variable” action inside of a Quagensia Function will only let you modify an item in an editable collection if it is passed by reference.