web analytics
< All Topics
Print

Variable Block

Variable block

The variable blocks allow you to assign the value / output of a method to a variable that can be reused throughout your strategy. 

For example, you can assign the value of a technical study (i.e. Historical Volatility) in the Market Studies Block to a variable. Or assign the value of a portfolio study (i.e. returnOnMargin) in the Portfolio Studies Block to a variable. You can also perform mathematical operations (i.e. addition, subtraction, etc..) and assign the result to a variable. 

Why Variables

Variables can help reduce the complexity in your strategy by allowing you to reuse the variable throughout your strategy. This reduces repetitiveness and enables you to make changes to your strategy easier. 

Variables can also be used to select positions / strikes that are relative to the value of technical studies (i.e. select strikes 10 points above 50-Day Moving Average – see example below)

Variables can also be used to select positions / strikes relative to the position of prior trades. (i.e. select strikes 10 points from existing long put with same expiration).

Example

The strategy below repeatedly uses the 50-Day Moving Average study from the Market Studies Block.

When the 50-Day Moving Average crosses above the 200-Day Moving average, buy a call whose strike price is within 10 points from the 50-Day Moving Average. But when the 50-Day Moving Average crosses below the 200-Day Moving Average, buy a put whose strike price is within 10 points from the 50-Day Moving Average.

Run Example!

If you wanted to change the 50-Day Moving Average to the 10-Day Moving Average, you would need to update six different blocks.

But by assigning the Moving Average studies into variables, your strategy becomes easier to read and update. In the example below, the Moving Average studies have been assigned to 2 variables (ShorterTermMovingAverage, LongerTermMovingAverage).

Now you only need to update one block (ShorterTermMovingAverage) if you wanted to change the 50-day Moving Average to 10-day Moving Average.

Variable Constraints

If the Portfolio Studies Block is assigned to a variable, the corresponding SetVariable Block must be enclosed inside a WHEN block with matching OrderID’s.

Generally, this occurs when a new position is trying to reference the attributes of an existing trade. (i.e. select strike that is 15 points from existing short leg)

Debugging Variables

Since the state of the variables is dynamic and can change constantly over the course of a backtest, debugging a strategy that use variables is slightly more involved.

To debug your strategy, you should examine the state / value of the variables. Fortunately, you can use the Log block to print the value of the variables. The values of the variables will be outputted into the Log tab where you can view the state of the variables over time. 

To preview trades which use the variables inside the Strategy Block / Strategy Filters, please print the value of the variables using the Log Block, and temporarily set the variables to the values printed in the Logs.  Be sure to set the preview date to correspond to the entries in the Logs. 

Please see https://www.optionstack.com/kb/debug-your-strategy/ for more information. 

Table of Contents