The advanced rules syntax introduced in the early versions of QuantShare is the perfect tool to create advanced rules and outputs that are very difficult and almost impossible to create using vector-based functions. Even using the .Net languages, implementing those rules would be too difficult and very complicated.
There are two kinds of advanced rules, the first ones are used to detect advanced patterns, while the second ones are used to create advanced outputs and simulate various strategies.
The second category, we are dealing with there, uses a syntax that starts with "SET". These rules can be created with the vector-based formulas, and are mainly used to assign a value (output) to each trading bar (or to trading bars that satisfy specific conditions).
One of the simplest output simulates a strategy that buys/shorts a security and exits it after N number of bars. This rule or output could be translated in:
Set _PERF(OPEN) WHERE _DIS(0) > 50
The syntax is: Set [output] WHERE [condition2] THEN [condition3] THEN [condition4]...
The above formula calculates the performance of a security using the open price, from the current bar (CB) to the bar number "CB + 50".
What you need to know is that advanced rules use the vector-based language functions as well as its own functions. Example:
_dis: Calculates the distance in bars, between the current condition (the bar at which the condition is met) and an older condition bar.
_perf: Calculates the return (ROC: rate of change) of the specified time-series between two condition bars.
NB: The first condition, in a "SET" rule, is the current bar.
You can get a full list of all functions in the documentation document.
How to simulate options strategies:
Let us create a more advanced output. This output simulates a strategy that is used by traders to protect their portfolios from decline using options.
The first step would be to create your rules using the "Rules Manager". After you click on "Analyze", select "Outputs", then click on "Select Outputs". In the "Output" form, select "Custom output", click on "Add", and then click on "Edit" (third column in the grid).
The type of strategy we are going to use is: Buy a security and hedge it with a put option that has the following characteristics: Expiration in 50 days, strike at -20% below the current security price and a cost of 2% (2% of the current security price).
Here are the different scenarios at the expiration:
The security return is lower or equal to -20%: The strategy maximum loss is -20%.
The security return is higher than -20%: The strategy return will be the security return minus the cost of the option (2%).
These scenarios are translated in the following formula:
_iff(_perf(open) > -20, _perf(open) - 2, -20)
In addition, since the option expires in 50 days, we should set the following function: _dis(0) >= 50 after the "WHERE" keyword.
The complete output formula is: Set _iff(_perf(open) > -20, _perf(open) - 2, -20) WHERE _dis(0) >= 50