Author: Copyright � 2005-2006, RickD
Profit factor:
0.67

This script is designed to automatically trade on the financial markets based on the relationship between two moving averages. Here's a breakdown of its logic:

  1. Initialization: When the script starts, it doesn't perform any special setup tasks, indicated by the empty init() function.

  2. Moving Average Calculation: The core of the script relies on two types of moving averages, a "fast" one and a "slow" one, calculated using historical price data. Moving averages smooth out price fluctuations to identify trends. The "fast" moving average is more sensitive to recent price changes, while the "slow" moving average reflects a longer period and is less reactive. The script uses "Exponential Moving Averages" (EMA) calculated on the open price of each period.

  3. Order Counting: The script counts how many buy (OP_BUY) and sell (OP_SELL) orders are currently open for the specific financial instrument (e.g., EURUSD). It only considers orders that are related to the specific currency pair the script is attached to.

  4. Decision Making: The script compares the values of the fast and slow moving averages.

    • If the "fast" moving average is higher than the "slow" moving average, it indicates a potential upward trend. The script then closes all existing sell orders and, if there are no existing buy orders, opens a new buy order.

    • Conversely, if the "fast" moving average is lower than the "slow" moving average, it suggests a potential downward trend. The script closes all existing buy orders and, if there are no existing sell orders, opens a new sell order.

  5. Order Execution: The Buy() and Sell() functions handle the process of opening new trade orders. These functions specify the details of the trade, such as the size of the trade (Lots), the price, and acceptable slippage (the difference between the requested price and the actual execution price). They also include error handling and logging to monitor the trading process.

  6. Order Closing: The CloseOrders() function closes all existing orders of a specific type (buy or sell). It iterates through all open orders, checks if they match the specified type and currency pair, and then closes them using the CloseOrder() function. The CloseOrder() function sends a request to close an existing order. It includes error handling and logging to keep track of successful and failed attempts.

  7. Slippage: A slippage parameter is used to tolerate small differences between the requested execution price of an order and the price that is actually executed on the market. This helps to ensure that orders are more likely to be filled, even if the price fluctuates slightly.

In essence, the script attempts to capitalize on short-term price trends by buying when the fast moving average is above the slow moving average and selling when the fast moving average is below the slow moving average. It tries to ensure that only one order in each direction (buy or sell) is open at any given time.

Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
Moving average indicator
9 Views
0 Downloads
0 Favorites
2MA

#property copyright "Copyright © 2005-2006, RickD"
#property link      "http://e2e-fx.net"

#include <stdlib.mqh>
#include <stderror.mqh>

#define major   1
#define minor   0

extern double Lots = 0.1;
extern int Slippage = 3;

extern int FastP = 5;
extern int SlowP = 20;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init () {
}

void deinit() {
}

void start() {
  if (!IsTesting()) Comment("Version: ", major, ".", minor);
 
  double FastMA = iMA(NULL, 0, FastP, 0, MODE_EMA, PRICE_OPEN, 0);
  double SlowMA = iMA(NULL, 0, SlowP, 0, MODE_EMA, PRICE_OPEN, 0);


  int BuyCnt = 0;
  int SellCnt = 0;
  
  int cnt = OrdersTotal();
  for (int i = 0; i < cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }


  double price, sl, tp;
    
  if (FastMA > SlowMA) {
    CloseOrders(OP_SELL);

    if (BuyCnt == 0) {    
      price = Ask;
      sl = 0;
      tp = 0;

      Buy(Symbol(), Lots, price, Slippage, sl, tp, 0);
    }
  }

  if (FastMA < SlowMA) {
    CloseOrders(OP_BUY);

    if (SellCnt == 0) {    
      price = Bid;
      sl = 0;
      tp = 0;

      Sell(Symbol(), Lots, price, Slippage, sl, tp, 0);
    }
  }
}

void CloseOrders(int type) {

  int cnt = OrdersTotal();
  for (int i=cnt-1; i>=0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    
    int type2 = OrderType();
    if (type != type2) continue;
    
    if (type == OP_BUY) CloseOrder(OrderTicket(), OrderLots(), Bid, Slippage);
    if (type == OP_SELL) CloseOrder(OrderTicket(), OrderLots(), Ask, Slippage);
  }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int SleepOk = 2000;
int SleepErr = 6000;

int Buy(string symbol, double lot, double price, int slip, double sl, double tp, int magic, string comment="") {
  RefreshRates();  
  int dig = MarketInfo(symbol, MODE_DIGITS);

  price = NormalizeDouble(price, dig);
  sl = NormalizeDouble(sl, dig);
  tp = NormalizeDouble(tp, dig);
    
  string _lot = DoubleToStr(lot, 1);
  string _price = DoubleToStr(price, dig);
  string _sl = DoubleToStr(sl, dig);
  string _tp = DoubleToStr(tp, dig);

  Print("Buy \"", symbol, "\", ", _lot, ", ", _price, ", ", slip, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");

  int res = OrderSend(symbol, OP_BUY, lot, price, slip, sl, tp, comment, magic);
  if (res >= 0) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("Error opening BUY order: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (-1);
}

int Sell(string symbol, double lot, double price, int slip, double sl, double tp, int magic, string comment="") {
  RefreshRates();  
  int dig = MarketInfo(symbol, MODE_DIGITS);

  price = NormalizeDouble(price, dig);
  sl = NormalizeDouble(sl, dig);
  tp = NormalizeDouble(tp, dig);
    
  string _lot = DoubleToStr(lot, 1);
  string _price = DoubleToStr(price, dig);
  string _sl = DoubleToStr(sl, dig);
  string _tp = DoubleToStr(tp, dig);

  Print("Sell \"", symbol, "\", ", _lot, ", ", _price, ", ", slip, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");
  
  int res = OrderSend(symbol, OP_SELL, lot, price, slip, sl, tp, comment, magic);
  if (res >= 0) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("Error opening SELL order: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (-1);
}

bool CloseOrder(int ticket, double lot, double price, int slip) {
  RefreshRates();  
  int dig = MarketInfo(OrderSymbol(), MODE_DIGITS);

  string _lot = DoubleToStr(lot, 1);
  string _price = DoubleToStr(price, dig);

  Print("CloseOrder ", ticket, ", ", _lot, ", ", _price, ", ", slip);
  
  bool res = OrderClose(ticket, lot, price, slip);
  if (res) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("CloseOrder failed: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (false);
}

Profitability Reports

USD/CHF Jul 2025 - Sep 2025
0.53
Total Trades 100
Won Trades 24
Lost trades 76
Win Rate 24.00 %
Expected payoff -6.86
Gross Profit 782.54
Gross Loss -1468.68
Total Net Profit -686.14
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.95
Total Trades 68
Won Trades 20
Lost trades 48
Win Rate 29.41 %
Expected payoff -0.43
Gross Profit 546.04
Gross Loss -575.02
Total Net Profit -28.98
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
1.04
Total Trades 74
Won Trades 20
Lost trades 54
Win Rate 27.03 %
Expected payoff 0.36
Gross Profit 646.30
Gross Loss -619.70
Total Net Profit 26.60
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.86
Total Trades 80
Won Trades 20
Lost trades 60
Win Rate 25.00 %
Expected payoff -2.31
Gross Profit 1123.60
Gross Loss -1308.80
Total Net Profit -185.20
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.91
Total Trades 75
Won Trades 19
Lost trades 56
Win Rate 25.33 %
Expected payoff -1.11
Gross Profit 858.56
Gross Loss -941.54
Total Net Profit -82.98
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.71
Total Trades 81
Won Trades 21
Lost trades 60
Win Rate 25.93 %
Expected payoff -4.59
Gross Profit 922.11
Gross Loss -1294.10
Total Net Profit -371.99
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.00
Total Trades 12
Won Trades 2
Lost trades 10
Win Rate 16.67 %
Expected payoff -8804.44
Gross Profit 31.60
Gross Loss -105684.90
Total Net Profit -105653.30
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.41
Total Trades 80
Won Trades 20
Lost trades 60
Win Rate 25.00 %
Expected payoff -9.01
Gross Profit 491.90
Gross Loss -1212.60
Total Net Profit -720.70
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
1.01
Total Trades 192
Won Trades 60
Lost trades 132
Win Rate 31.25 %
Expected payoff 0.26
Gross Profit 3326.66
Gross Loss -3277.59
Total Net Profit 49.07
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
0.91
Total Trades 194
Won Trades 53
Lost trades 141
Win Rate 27.32 %
Expected payoff -1.39
Gross Profit 2753.97
Gross Loss -3022.73
Total Net Profit -268.76
-100%
-50%
0%
50%
100%

Comments