OK. Let's start with a simpler example.
Initially, state=0.
We want state=0 to change to state=1 when buy=TRUE. Suppose that
buy := Cross(Mov(C,n1,S),Mov(C,n2,S)
for some specified n1 ands n2.
Thereafter, state=1 until the following condition occurs:
sell := Cross(Mov(C,n2,S), Mov(C,n1,S)
Then state=1 changes to state=0. No other conditions can cause state=1 to change to state=0 or any other value.
With state=0 again, another buy signal will cause it to change to state=1. Alternatively, state=0 can change to state=-1 if the following condition occurs:
short := Cross(Mov(C,n3,S), Mov(C,n4,S)
for some specified n3 and n4.
Thereafter, state=-1 until the following condition occurs:
cover := Cross(Mov(C,n4,S), Mov(C,n3,S)
Then state=-1 changes to state=0. No other conditions can cause state=-1 to change to state=0 or any other value.
The objective of the code is to ensure that state always has the proper value in accordance with these rules with all possible combinations of the inputs buy, sell, short and cover.
We then display a chart of state in an inner window below the underlying security.
The important point is that the effect upon state of any input depends upon state's current value.
How can I most efficiently code this example, preferably without using PREV?
Thanks for your assistance.
Systematic