Our partnerSkilling
  • Spreads from 0.1 pips
  • 1,000+ instruments
  • Free demo account

65% of retail investor accounts lose money when trading CFDs with this provider.

Open account
Module 2 · Programming for traders

2.2 How a robot is built

Lesson 5 of 19Reading time: 2 min

A robot is not a program that runs from top to bottom and stops. It waits for events – and the platform calls the right function when something happens.

OnInit / OnStartOnce: create indicatorsOnTickOn every price changeOnDeinit / OnStopOnce: clean upNew candle?No → trailing stop onlyRead signalIndicators, candles 1 and 2Check filtersTrend, time, spreadCalculate sizeRisk in % and SLSend orderWith SL and TPYes
The events in a robot. Most things happen in OnTick – but we only look for new signals when a new candle starts.

The events

EventMetaTrader (MQL4/MQL5)cTrader (C#)
The robot startsOnInit()OnStart()
New priceOnTick()OnTick()
New candleMust be checked yourself in OnTick()OnBar()
The robot stopsOnDeinit()OnStop()

New candle in MetaTrader

MetaTrader has no separate event for a new candle. So we check ourselves whether the time of candle 0 has changed:

MQL5
datetime lastBarTime = 0;

void OnTick()
{
   datetime barTime = iTime(_Symbol, _Period, 0);
   if(barTime == lastBarTime) return;   // same candle as last time: do nothing
   lastBarTime = barTime;

   // here we look for new signals – once per candle
}

return means “leave the function here”. So the code below only runs once per candle.

New candle in cTrader

In cTrader it is simpler. OnBar() is called automatically every time a new candle starts:

C# (cTrader)
protected override void OnBar()
{
    // here we look for new signals – once per candle
}

What goes where?

  • OnInit / OnStart: Create indicators and check settings.
  • OnTick: Things that must happen continuously – for example a trailing stop.
  • New candle: Read signals and open trades.
  • OnDeinit / OnStop: Clean up.

Why not look for signals on every price?

There can be many price changes per second. If the robot looks for signals on every one of them, it can open several trades on the same signal – and the result depends on how many price changes arrived. By only looking for signals on a new candle, the robot becomes predictable, and the tests become more reliable.

In plain words

A trading robot is like a recipe. OnInit is getting the ingredients out, OnTick is checking the pot every moment, and OnDeinit is cleaning the kitchen afterwards.

Try it yourself

Find OnInit, OnTick and OnDeinit in an MQL5 robot. Write one sentence about what each part does.

Key takeaways

  • A robot reacts to events: start, new price, new candle and stop.
  • In MetaTrader you check yourself whether a new candle has started. In cTrader OnBar is used.
  • Look for signals once per candle – that makes the robot predictable.
Practise what you have learned – without riskTry it on a free demo account with Skilling, with real prices and virtual money.
Open a free demo account

65% of retail investor accounts lose money when trading CFDs with this provider.