Skip to main content

Counting Candlesticks as Time Variable

Use a candlestick counter to simulate time in backtests. Map candles to hours, reset daily, and unlock time-based trading logic.

Updated over 2 months ago

Most trading bots use time-based blocks like Run at Time or Run in Session. But these rely on real clock time, which can’t be backtested directly in MetaTrader. To solve this, you can make your bot “count candlesticks” and use that counter as a time replacement.

How It Works

  • Create a variable called Counter.

  • Start it at 0 every day.

  • Every new candlestick, add +1 to the counter.

  • On the 1H timeframe, this means:

    • Counter = 1 → 01:00

    • Counter = 8 → 08:00

    • Counter = 16 → 16:00 (4PM)

At midnight, reset the counter to zero. That way, each new day starts fresh.

This gives you a simple system where candles = hours, enabling you to build and test strategies based on “time” inside Profectus.AI.

Example: Counting Logic

  1. Add a decision tree running Run per Candle (set to 1H).

  2. Add a Formula Block: Counter = Counter + 1.

  3. Add a reset rule: at midnight, set Counter = 0.

Now your bot will keep track of time as long as it runs.

Example: Using the Counter

Let’s say you want to open a trade exactly at 4PM (16:00):

  • Start a new decision tree with Run per Candle (1H).

  • Add a Trade Rule Block: Is Counter = 16?

  • Add another Trade Rule Block: Are there no trades open?

  • If both conditions are true → trigger Buy Now block.

That’s it! You’ve replaced a fixed clock input with a variable you can backtest.

Why This Matters

  • You can now backtest time-based ideas in MetaTrader.

  • Flexible: Counter can be applied to any timeframe (e.g., 5M candles → Counter 48 = 4 hours).

  • Reusable: Plug the Counter into other logic, like session filters or custom rules.

Download this example also shown below in the screenshot.

Did this answer your question?