I am wondering if a state machine is a good pattern for this case.
Say I have the following Triggers:
enum Trigger
{
None,
RSIGt70,
RSILt30,
TrendUp,
TrendDown
}
And I have the following States
enum State
{
Flat,
ShortUnit1,
ShortUnit2,
LongUnit1,
LongUnit2
}
var position= new StateMachine<State, Trigger>(State.Flat);
position.Configure(State.Flat)
.Permit(Trigger.RSIGt70, State.Flat);
.Permit(Trigger.TrendingUp, State.Flat);
.Permit(Trigger.RSIGt70, State.ShortUnit1);
.Permit(Trigger.TrendingUp, State.ShortUnit1);
I am trying to get away from if statements and replace trading logic with a state machine. Not sure it works because it may be a confluence of triggers that transitions from one state to another. The questions is, how do State Machines deal with this situation? Are they rich enough or do I have to use a standard Turing Machine, i.e., if
statements with memory?
The problem is that two+ (in this case) conditions have to be true in order to transition from flat to one of the Long or Short positions.
So what I am saying is e.g., if both RSI > 70 and TrendingUp, then transition into a short position. Or if TrendingUp and RSI > 70, then transition into a short position. Since it doesn't matter which order the two conditions occurred. Vice versa for the other possible conditions. But if only one condition is true, then no position is triggered. But if I am in a state, how do I know if I came from None
which doesn't trigger a position, or from the other conditions that does trigger a position?
If the system becomes more complicated, I am trying to avoid error prone if/else statements. Not sure if this can be handled with a FSM since it depends on the history of how the current state came to be through time.