Skip to main content

How to Use Variables as Temporary Memory in Your Bot

Learn how to use variables as temporary memory in your bot. Use 0/1 gates like cross_flag or MomentumGate, count candles with cross_age, wait for later pullbacks or confirmations, trigger the next logic, and reset the setup.

Use this article to understand how variables can help your bot remember that something happened earlier. This is useful when your strategy has multiple steps.

Example:

Trend → Momentum → Pullback → Confirmation → Entry

In real trading, these steps usually do not happen on the same candle. Momentum may happen first. The pullback may come a few candles later.

Variables let your EA remember the first condition, so the next part of the strategy can trigger later.

Also known as: variables, temporary memory, on/off switch, cross flag, cross age, momentum gate, setup active, wait for pullback, delayed entry, multi-step strategy logic.


Summary

A variable can work like temporary memory inside your EA.

In this example, we use two variables:

  • cross_flag

  • cross_age

cross_flag tells the EA whether the setup is active.

cross_age counts how many candles have passed since the setup became active.

Simple idea:

  • cross_flag = 0 means the setup is off

  • cross_flag = 1 means the setup is active

  • cross_age counts the candles after activation

This lets the bot wait for the next step instead of forcing everything to happen on one candle.


Why this is useful

Without variables, your conditions usually need to happen in the same chain.

Example:

Momentum happens
→ pullback happens
→ entry happens

But in many strategies, that is not realistic.

A pullback can happen one candle later, three candles later, or not at all.

Variables solve this by letting the EA remember:

“Momentum happened. Now I can wait for the pullback.”


The example strategy

In this example, we use three moving averages:

  • EMA 20 = fast moving average

  • EMA 50 = slower moving average

  • EMA 238 = trend filter

The idea:

  1. EMA 20 crosses above EMA 50.

  2. Price is still below EMA 238.

  3. The EA turns the setup on.

  4. The EA starts counting candles.

  5. After the defined candle count, the next logic can trigger.

  6. After the action is complete, the variables reset.


The two variables

cross_flag

This is the on/off switch.

  • cross_flag = 0 means no active setup

  • cross_flag = 1 means the setup is active

When the EMA crossover condition happens, the EA sets:

cross_flag = 1

That means:

“The signal happened. The next part of the strategy is now allowed to look for its condition.”


cross_age

This is the candle counter.

It starts at:

cross_age = 0

Every new candle, while cross_flag = 1, the EA increases it by 1.

Example:

  • after 1 candle: cross_age = 1

  • after 2 candles: cross_age = 2

  • after 3 candles: cross_age = 3

This tells the EA how old the active setup is.


Decision tree 1: Activate the setup

The first decision tree checks for the initial signal.

Example logic:

Run per Tick
→ EMA 20 crosses EMA 50
→ Price is below EMA 238
→ set cross_flag = 1
→ set cross_age = 0

Plain English:

When the crossover happens and price is still below the trend filter, the EA turns the setup on.

At the same time, it resets the candle counter to 0.

This is important because every new setup should start counting from zero.


Decision tree 2: Count candles while the setup is active

The second decision tree counts how many candles have passed since the setup became active.

Example logic:

Run per Candle
→ check cross_flag = 1
→ increase cross_age by 1

Plain English:

Every new candle, the EA checks whether the setup is active.

If cross_flag = 1, the EA adds 1 to cross_age.

This creates the waiting logic.

The EA now knows how many candles have passed since the original signal.


Decision tree 3: Trigger the next logic

The third decision tree uses the candle count.

Example logic:

Run per Candle
→ check cross_age >= 3
→ Buy Now
→ reset cross_flag = 0
→ reset cross_age = 0

Plain English:

Once the setup is at least 3 candles old, the EA allows the next action.

In this example, the next action is Buy Now.

After that, the EA resets both variables.

That reset is important. It tells the EA:

“This setup is finished. Wait for the next fresh signal.”


Why the reset matters

Do not skip the reset.

If you do not reset the variables, the EA may keep thinking the old setup is still active.

That can cause repeated entries or logic triggering from an outdated signal.

After the action is complete, reset:

cross_flag = 0
cross_age = 0

This prepares the EA for the next valid setup.


How this applies to momentum and pullback strategies

This same structure works well for strategies where one condition happens first and another condition happens later.

Example:

You want to buy when:

  1. Momentum appears first.

  2. Price later pulls back to EMA9.

  3. Price confirms support around EMA9.

  4. Then the EA enters.

You can use a variable like:

MomentumGate

Example:

Momentum condition is true
→ set MomentumGate = 1

Then, in another decision tree:

Run per Candle
→ check MomentumGate = 1
→ check pullback to EMA9
→ Buy Now
→ reset MomentumGate = 0

Plain English:

The EA remembers that momentum happened, then waits for the pullback.

This is much cleaner than trying to predict exactly which Candle ID the pullback will happen on.


Simple pullback example with EMA9

A possible pullback rule could be:

Candle Low ID 1 <= EMA9
AND
Candle Close ID 1 > EMA9

Plain English:

The candle moved down far enough to touch or move below EMA9, but then closed back above EMA9.

That can represent a pullback into EMA9 followed by buyer support.

This is only one possible definition. You can change it based on your own strategy logic.


Common mistakes

1. Forgetting that 0 and 1 are just states

0 means off.

1 means on.

The numbers are not magic. They simply tell the EA whether that part of the strategy should be active.


2. Not resetting the variable

If the setup is finished, reset the variable.

Otherwise, the EA may keep using old information.


3. Counting candles before the setup is active

Only increase cross_age when cross_flag = 1.

Otherwise, the counter runs even when there is no active setup.


4. Expecting all conditions to happen on one candle

Variables are useful because conditions do not always happen at the same time.

Momentum can happen first.

Pullback can happen later.

Confirmation can happen after that.

That is exactly why temporary memory is useful.


5. Making the logic too complex too early

Start simple.

First build:

Signal happens
→ variable turns on
→ next condition happens
→ action triggers
→ variable resets

Only add candle counters, extra filters, or multiple gates after the simple version works.


Best-practice structure

Use this structure for multi-step logic:

  1. Detect the first condition.

  2. Set a variable to 1.

  3. Let another tree check whether the variable is 1.

  4. Check the next condition.

  5. Trigger the action.

  6. Reset the variable to 0.

Example:

Momentum happens
MomentumGate = 1
→ pullback happens
→ Buy Now
MomentumGate = 0

If you also need a time limit, add a counter:

Momentum happens
MomentumGate = 1
MomentumAge = 0
→ every candle: MomentumAge + 1
→ if pullback happens within your allowed window: enter
→ reset both variables


Conclusion

Variables let your EA remember that something happened earlier.

This is useful for strategies where the setup develops in steps.

The main pattern is:

Turn the setup on
→ wait for the next condition
→ trigger the next logic
→ reset the setup

In this example:

  • cross_flag controls whether the setup is active

  • cross_age counts how many candles passed

  • the EA triggers the next logic after the required candle count

  • then both variables reset

This turns your strategy from a single-candle condition into a multi-step decision process.

Did this answer your question?