QF Workshop 2026
Current location: Home > Workshops > QF Workshop 2026

QF Workshop #6: Programming Trading using MT5 / MQL5

Posted:2026-10-29 14:41:47 Click:190

 

QF Workshop #6: Programming Trading using MT5 / MQL5

Duration: ~1 hour  |  Instructor: Dr. Raymond S. T. Lee

Learn how to design and implement automated trading programs (Expert Advisors) in MQL5 — the foundation for Quantum Trading strategies.

1. Order Types and Trade Execution (12 min)

  • Market Order: Immediate execution at current Bid/Ask
  • Pending Orders: Buy/Sell Limit, Buy/Sell Stop
  • CTrade class: Recommended way to send orders in MQL5
#include <Trade\Trade.mqh>
CTrade trade;

void OnTick()
{
   if(PositionsTotal() == 0)
   {
      trade.Buy(0.1, _Symbol, 0, 0, 0, "QF Buy");
   }
}

2. Position Management and Risk Control (15 min)

// Check open positions
for(int i = PositionsTotal()-1; i >= 0; i--)
{
   ulong ticket = PositionGetTicket(i);
   if(PositionSelectByTicket(ticket))
   {
      double profit = PositionGetDouble(POSITION_PROFIT);
      if(profit > 50.0)
         trade.PositionClose(ticket);
   }
}

// Set Stop Loss / Take Profit
trade.Buy(0.1, _Symbol, 0, 
          SymbolInfoDouble(_Symbol, SYMBOL_BID) - 50*_Point,
          SymbolInfoDouble(_Symbol, SYMBOL_ASK) + 100*_Point);

3. Simple Moving Average Crossover EA (15 min)

#include <Trade\Trade.mqh>
CTrade trade;
int ma_fast, ma_slow;

int OnInit()
{
   ma_fast = iMA(_Symbol, PERIOD_H1, 10, 0, MODE_SMA, PRICE_CLOSE);
   ma_slow = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE);
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   double fast[], slow[];
   ArraySetAsSeries(fast, true);
   ArraySetAsSeries(slow, true);
   CopyBuffer(ma_fast, 0, 0, 2, fast);
   CopyBuffer(ma_slow, 0, 0, 2, slow);

   if(fast[1] <= slow[1] && fast[0] > slow[0] && PositionsTotal()==0)
      trade.Buy(0.1, _Symbol);
   if(fast[1] >= slow[1] && fast[0] < slow[0] && PositionsTotal()>0)
      trade.PositionClose(_Symbol);
}
Quantum Finance Connection: Later workshops will replace simple MA crossovers with Quantum Price Levels (QPLs) as dynamic support/resistance for more scientific entry and exit rules.

Workshop Assignment (6 questions)

  1. Write an EA that opens a Buy position of 0.1 lot when price crosses above a 50-period SMA on H1, and closes it when price crosses below. Include basic error checking.
  2. Add a Stop Loss of 30 pips and Take Profit of 60 pips to the EA above.
  3. Modify the EA so that it only trades between 08:00 and 18:00 server time.
  4. Explain the difference between trade.Buy() and a pending Buy Limit order. When would you use each in a quantum trading strategy?
  5. What information does PositionGetDouble(POSITION_PROFIT) return? How can it be used for dynamic exit based on Quantum Price Levels?
  6. Backtest your EA in the Strategy Tester for at least 3 months on EURUSD H1 and report the net profit, number of trades, and maximum drawdown.

QF Workshop #6 – Programming Trading with MT5/MQL5 | Dr. Raymond S. T. Lee

到价提醒[关闭]