Reference Prior Trades
When creating a new position or adjusting an existing trade, there may be situations where you would like to reference specific attributes from existing positions / trades.
- Create a new option spread whose strikes overlap with the short strikes of an existing position
- Create a new option spread with the same expiration dates as an existing position
You can reference the attributes of existing positions using the SetVariable block combined with the Portfolio Studies block.
Example
We will use the following example to explain how to reference the attributes of an existing trade when entering a new trade.
- Buy a long put on the SPX
- When the long put has achieved 50% or greater profit, adjust the long put option into a Bear Put Spread by selling a further out-of-the money put with the following attributes:
- new short put should have the same expiration date of the existing long put
- new short put should be 20 points further out of the money from the existing long put
Run Example!
Setting Variables
- Use the Strategy Block to buy an at-the-money put on the SPX
- Use the SetVariable block to create variables referencing the attributes of the BuyPut order. We will reference these variables when constructing the new short put trade.
- StrikeVariable – reference the strike price of the long put
- ExpirationVariable – reference the expiration date of the long put
Variable Constraints
- When the Position Studies Block is assigned to a variable, the corresponding SetVariable Block must be enclosed inside a WHEN block with matching OrderID’s
- Create a WHEN block with matching OrderID’s, and place the variables inside the WHEN block.
Referencing Variables
- Create a Strategy block with strategy filters that use the strike / expiration variables above.
- Strategy: Sell Put
- DaysToExpiration: Use the ExpirationVariable to set the same expiration date as the long put
- StrikePrice: Use the (StrikeVariable – 20) to set the new strike price to be 20 points lower than the strike price of the existing long put
- Place the Strategy block inside the WHEN block, below the SetVariable block
- Combine the entry WHEN block with the adjusting WHEN block
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.