CFDs and forex are complex instruments and come with a high risk of losing money rapidly due to leverage. Risk disclosure

Algorithmic trading in forex: how it works and where it fails

This guide walks through the path from a trading idea to a running expert advisor, the infrastructure it needs, and the ways automated systems lose money.

Extrade Pakistan editorial team ·

On this page
  1. What algorithmic trading is
  2. The workflow: rules, code, backtest, forward test
  3. Infrastructure: platform, VPS and monitoring
  4. How automated trading loses money
  5. Controls worth building in
  6. The legal position in Pakistan
  7. Frequently asked questions

What algorithmic trading is

Algorithmic trading (algo trading) means a computer program places, manages and closes trades according to fixed rules. The program reads prices, checks conditions and sends orders without a person clicking buttons.

In retail forex the common form is the forex robot, called an expert advisor (EA) in MetaTrader. An EA is a script attached to a chart that runs on every new price tick or every new bar. Other forms include indicator-based alerts that only notify you, and scripts that handle tasks such as closing all positions.

Automation does not create an edge. It carries out a set of rules faster and more consistently than a person can. If the rules lose money, the robot loses money faster and without hesitation. Leveraged forex and CFD trading can lose money quickly, and automated trading is no exception.

Past data (tuned)New datastart
Over-fitting: a strategy tuned to past data looks smooth on that data and often fails on data it has not seen. Test on a separate period and on a demo first. Illustrative example: made-up numbers for learning, not real prices or a broker's conditions.

The workflow: rules, code, backtest, forward test

Write the rules in plain language

Before any code, write the full system down. A complete rule set answers:

If a rule needs your judgement ("enter when the trend looks strong"), it cannot be coded yet. Turn it into something measurable. Our trading plan guide covers the same questions for manual trading.

Hypothetical example, for illustration only: buy when a fast moving average closes above a slow one, sell on the opposite cross. Place the stop a multiple of the Average True Range (ATR, a volatility measure) away from entry. Risk a fixed share of equity per trade. Skip trades when the spread is wider than a set limit. This describes structure only. It is not a recommendation and makes no claim about results. See MA crossover for how such signals behave.

Code it

MetaTrader EAs are written in MetaTrader's own language (MQL). Other setups use Python or another language connected to a platform through an API. Whatever the language, the code must handle more than signals:

Most real-money bugs come from this plumbing, not from the strategy logic.

Backtest

A backtest runs the rules over historical data. MetaTrader 5 has a built-in Strategy Tester for this. Read the results critically:

More detail is in Backtesting.

Forward test

Run the EA on a demo account, or with very small size, in real time for long enough to see different market conditions. This reveals execution delays, slippage and bugs a backtest cannot show. Compare live fills with what the backtest assumed. See Forward testing.

Infrastructure: platform, VPS and monitoring

An EA in MetaTrader only runs while the terminal is open and connected. Close your laptop and the robot stops, possibly with positions still open.

A VPS (virtual private server) is a rented remote computer that runs all the time. Traders put the terminal on a VPS to keep the EA online and to reduce delay to the broker's servers. Points to check:

Monitoring matters as much as uptime. Set alerts for disconnections, failed orders and unusual drawdown. Check the logs regularly. For platform background, see Platforms and apps.

How automated trading loses money

Overfitting

Tuning parameters until the backtest looks smooth usually fits past noise, not a repeatable pattern. The system then fails on new data. Warning signs: many parameters, results that collapse when a value is changed slightly, and strong in-sample but weak out-of-sample results.

Unrealistic costs

Spreads widen at rollover, around news and in thin sessions. Slippage on stops can be large in fast markets. A strategy with a small average gain per trade can turn negative once real costs are applied. Scalping robots are especially exposed; see Scalping and News trading.

Dangerous money management

Many commercial robots use grid or martingale logic: adding positions or doubling size after losses. The equity curve looks steady until a long trend against the positions triggers a margin call or stop-out and wipes out the account. Read Grid trading and Martingale strategy before running any EA that averages into losers.

Regime change

Markets move between trending, ranging and high-volatility phases. A rule set built for one phase can lose steadily in another. Rate decisions and data releases can change behaviour overnight.

Technical failure

Disconnections, VPS outages, platform updates, wrong symbol suffixes, and coding errors such as an order loop can all cause losses that have nothing to do with the strategy.

Purchased robots and "verified" results

Sellers often show selected backtests or short live records. Without the code, full trade history and realistic costs, you cannot judge the system. The same caution applies to signals and copy trading.

Controls worth building in

Position sizing for these limits can be checked with the calculators. General principles are in Risk management.

Automation does not change the legal status of the account the robot trades on. On platform status, per the SECP, offshore forex/CFD platforms operate without regulatory approval in Pakistan, and per the SECP, offshore platforms have no Pakistani licence or oversight and offer no legal protection or recourse. On payments and residents, per the SBP, remitting foreign exchange to overseas forex/margin/CFD platforms is not allowed through any payment channel (FERA s.5(1)), and per the SBP, the purchase of offshore forex/margin/CFD products by Pakistani residents violates section 4(1) of FERA 1947. Read Law and regulators before opening any account.

Frequently asked questions

Is a forex robot the same as an expert advisor?

In MetaTrader, yes. "Expert advisor" is the platform's name for a program that trades automatically. "Forex robot" is the informal term.

Do I need to know programming?

To build and fix your own EA, yes, or you need someone who does and whom you trust. Without reading the code, you cannot know what a robot will do in unusual conditions.

Can a good backtest guarantee live results?

No. Costs, slippage, execution and changing markets all differ from history. A backtest can rule out bad ideas, but it cannot prove a good one.

Do I need a VPS?

Only if the EA must run when your own computer is off or offline. Many traders forward test on their own machine first.

Is automated trading lower risk than manual trading?

Not by itself. It removes some emotional mistakes but adds technical risks, and a flawed rule set loses money without pausing. Leveraged trading can lose money fast whether a person or a program places the orders.

Sources

  1. Securities and Exchange Commission of Pakistan (press release, 09.04.2025) · secp.gov.pk › Press Release SECP Cautions Public Against Illegal Offshore Trading an — checked 2026-11-11
  2. State Bank of Pakistan, EPD Circular Letter No. 08 of 2022 · sbp.org.pk › Epd circular letter no 08 of 2022 — checked 2026-11-11

Related pages