Example #3: Three Bar Breakout Trading System
The Formula Wizard can also be used to set up more advanced formulas, such as a trading system. In this example, we will write a trading system formula that can be used in the Strategy Analyzer for back testing. The system will go long if the price exceeds the high of the three previous bars. The system will go short if the price goes lower than the low of the three previous bars. The stop is set at the low of the current bar minus $0.25. Successive signals in the same direction are ignored. If a short signal is received while long, but before the stop is triggered, the long will be exited and the trade reversed. The same applies for a long signal while the system is short. The price bar will be colored green while long, red while short and black otherwise.
As we are only coloring the price bars, and not plotting any values of our own, select a price study, and do not add any lines to the Data Plot table. Type in an appropriate study name and click on OK.
There are no studies used in this formula.
The system will go long only if it is not currently long, and if the price exceeds the high of the previous three bars. Add the following conditions to the first expression set:
Note the use of the negative offsets for the high() function. Negative offsets go ‘back’ bars, so the three lines test against the highs of the previous three bars.
Once these conditions are satisfied, we want to open a position in the long direction (thus closing out any existing short positions), and color the price bar green:
Note how we want the price bar to be colored every time, but the Strategy.doLong() only the first time. This stops us from adding to the position every time the price continues in our direction. When calling the Strategy.setStop() function, we are also passing in the low() - $0.25 value as our stop point.
The short conditions are the exact opposite of the long conditions. Add these to the 2 nd expression set:
Next, we add actions to color the price bar red, and to open the short position:
This looks similar to the long actions, note that we are using the high of the current bar as our stop loss.
The trading system is now complete. You can save the formula and run the resulting EFS in the Strategy Analyzer to evaluate its effectiveness.