🔂STATE MACHINE

Code Zero Visual Trading for TradingView

For your information

A state machine, also known as a state automaton, is a system that can be in one of several states. It changes its states in response to events or input signals.

Imagine a washing machine: it has different modes (states) - wash, rinse, spin. Depending on what you choose (event), it will switch to the required state and perform the necessary program.

Similarly, a state machine in programming or electronics "switches" between states according to predefined rules.

General description of the indicator

STATE MACHINE is a revolutionary tool for traders that allows the creation of strategies based on a sequence of events. It automates your trading by taking into account the time and conditions you set, turning your plan into profitable actions. This is your advantage in trading, embodying complex strategies effortlessly.

The STATE MACHINE module is like playing chess with your trading strategy. It enables you to "move" through different states (like positions on a chessboard), each corresponding to a specific moment or event in your strategy. You set the rules for when and how to transition from one state to another, up to the fifth.

If needed, you can pause for several bars before checking the conditions for the next move. And if your piece (state) cannot make a move within a certain time, it can return to the starting position (state 0). Thus, STATE MACHINE helps you create a strategy that responds to market changes over time and events, like an experienced chess player planning their moves in advance.

Important

You decide what should happen and when in your strategy.

The logic of states can best be understood with an example.

Let's imagine that we want to create a trading strategy using a STATE MACHINE that will generate a buy signal for a stock when several conditions are met one after another. Here is an example of a strategy with 3 states:

State 0: Startup.

  • The system is started and goes to State 1.

State 1: Market observation.

  • The system is on hold and observes the stock price.

  • If the closing price exceeds the average value of EMA(50), the system enters State 2.

State 2: Verification of additional conditions.

  • The system checks, RSI (Relative Strength Index) is less than 30, which may indicate that the stock is oversold.

  • If the RSI condition is satisfied now or within 5 bars, the system enters Condition 3.

  • If the condition is not satisfied within 5 bars, the system returns to Condition 0.

State 3: Generating a buy signal.

  • The system checks the trading volume - if it is higher than the average volume for the last 10 bars, it may indicate a growing interest in the stock.

  • If the trading volume is above average for 2 bars, the system generates a buy signal.

  • If the trading volume is not higher than average for 2 bars, the system returns to State 0

Thus, the STATE MACHINE automatically progresses step by step through the logical chain of conditions and waits for the necessary intervals of time between events until all the necessary criteria for purchase are satisfied. Once this happens, the trader receives a signal and can execute the purchase based on a predefined strategy.

All logic is based on the state. A machine can be in only one state. It can switch to another state only after an event. An event is the fulfillment of a certain condition. You can stay in each state indefinitely, waiting for the condition to be fulfilled, or set the number of bars to wait for this condition. If the condition is not fulfilled during this time, the automaton goes to state 0 and starts working again from the beginning.

The diagram shows an example.

State 0 - the machine is ready to work and is waiting for the condition [R1] to be fulfilled: RSI(14) < 30. Maximum waiting time: infinite.

State 1 - the machine has entered this state after fulfilling condition [R1] and is waiting for condition [R2] to be fulfilled: VOLUME > VOLUME MA(50) Maximum waiting time: 10 bars.

Now there is a situation of possible branching, where the machine will go next.

  1. If the condition [R2] is met, the machine will go to state 2.

  2. If 10 bars have passed and condition [R2] is not met, the machine will return to initial state 0 and start over.

State 2 - the machine has entered this state after [R2] is fulfilled and is waiting for [R3] to be fulfilled: close > open (Bullish bar) Maximum waiting time: infinite.

State 3 - the machine has entered this state after executing [R3], generates an output signal about the successful fulfillment of all conditions and goes to the initial state 0 and starts working again.

Thus, the machine is again waiting for the right situation in the market.

1. Interface

Cascading

The module can be connected in a queue of conditions. To do this, you need to select the input data source that should send a signal for activation (a value greater than 0) and enable cascading mode.

The condition (RULE) for each state is set in the same way as in the CONDITION MANAGER module. When it is fulfilled, the system moves to the next state and waits for its fulfillment.

When STATE MACHINE reaches the last step you set, it "says", "That's it, task complete!" and starts all over again.

As soon as you move to a new step, it will start waiting for a given condition to be met and will continue to do so until it is triggered. If such infinite waiting does not suit your strategy or if some delay needs to be introduced before executing a new step, you can always configure this.

NDescription

1

You can set the number of bars to wait before this step starts.

2

How many bars to wait for the condition to be met before it becomes irrelevant and STATE MACHINE needs to be restarted (go to state 0).

2. Transferring values between states

To make a decision based on data from the past, the module has special "memory" variables: S1_VAR, S2_VAR, S3_VAR, S4_VAR. They are like notes where important numbers from previous steps (states) are recorded.

Then, when you need to take a new step, you can look at these notes and use what is recorded to decide how exactly to move forward.

For example,

value of RSI(14) at transition from state 1 to state 2 is written to the special variable S1_VAR,

which can be used to move to state 4 to move to state 5: "RSI(14) at step 1 was 25?".

3. Output data

The STATE MACHINE module has three outputs:

  • A signal that all states have been passed successfully and we can work further (SIGNAL).

  • The state we are in now (CURRENT STATE).

  • The state we expect on successful completion (TARGET STATE).

4. Backtesting

For testing the STATE MACHINE module, the same Backtesting Line is used as in the CONDITION MANAGER. It shows how the logic you configured would have performed on historical data. You can sequentially check the operation of each state, which will help you understand how your settings respond to market conditions.

To see the results for a specific state, select it from the list of available states. Then on the chart, you will see a line with blue squares that will indicate the moments when the STATE MACHINE was activated in that state. This way, you can go through all the states and test them. This will give you a clear idea of when and why the module transitioned from one state to another and how it affected trading decisions.

Last updated