Trading Algorithms VII – Liberal sequences and exact sequences
Translating ideas into a trading algorithm is not always easy. When examining a particular trade idea, we could find two cases:
- the signal can be described precisely in a consecutive sequence of trading facts, or
- Several conditions with variable steps among each condition need to be spotted.
The first class is easier to program. To this class belong any kind of crossovers:
- price to MA:
- MA to MA :
Similar conditions can be created with indicator crossovers and level breakouts.
Trading Signals Using Pivots
But what if the idea is more complex?. Let’s consider we want to catch pivot points in the direction of the trend. Let’s say we want to open a buy trade in the second pivot reversal. Let’s follow Pruitt’s example:
Buy on the second pivot pullback if
1.- The second pivot high is higher than the first pivot
2.- The pullback is larger than 2%
3.- The sequence takes less than 30 bars
The Flag Model
Since these conditions happen with variable price-action sequences programming, this kind of entry is much more difficult if we employ just If-then-else statements. The employment of flags to signal that a specific condition was met helps in the logic but is not the best solution.
The Finite State Machine
The second method to this kind of problem is the Finite State Machine (FSM). Basically, we want to detect certain states following others, defining a state when the needed condition is met. An FSM is a machine with finite states. The machine moves from state zero or START through several states until a final one, which defines the ACCEPT state.
We can imagine a state machine as a combination lock. We need to supply the lock with a combination of numbers until its final digit, which triggers its opening.
The first step is to create the states needed. Next, we create the conditions for the change from one state to other states. Once satisfied with the diagram, we can easily write the pseudo-code, or, even, the actual code directly.
As we can see here, the code is precisely subdivided into states, each state with the precise instructions to move to the next state or back to the start state. We can see also that this algorithm is executed from top to bottom on each new bar. We hope that this example will help you better understand how an entry algorithm can be created.
Stay tuned for more interesting videos on trading algos!