Author: Coders Guru
Profit factor:
2.65

This automated trading script is designed to navigate the financial markets by identifying specific momentum shifts using two moving averages. It acts as a systematic way to enter and manage trades based on the interaction between short-term and long-term price trends.

Core Trading Logic

The primary "engine" of this script relies on the EMA Cross methodology. It calculates two different Exponential Moving Averages (EMAs):

  • Short-term EMA: Tracks recent price fluctuations closely.
  • Long-term EMA: Represents the broader trend over a longer period.

The script monitors the relationship between these two lines. When the short-term line crosses over the long-term line, it signals a change in momentum. Specifically:

  • Buy Signal: Triggered when the short-term average moves above the long-term average, suggesting upward momentum.
  • Sell Signal: Triggered when the short-term average moves below the long-term average, suggesting downward momentum.

Trade Execution and Management

Once a cross is detected, the script manages the lifecycle of a trade based on several predefined rules:

  • Entry Rules: The script will only open a new trade if there are currently no active positions. This prevents the system from over-leveraging or opening multiple simultaneous trades. It automatically places a "Buy" order when an upward cross occurs and a "Sell" order when a downward cross occurs.
  • Risk Protection: Every trade is automatically equipped with two essential safety features:
    • Take Profit: A pre-set target where the script will automatically close a winning trade to lock in gains.
    • Stop Loss: A protective limit that automatically closes a trade if the market moves against the position, preventing significant losses.
  • Trailing Stop: To maximize profit during favorable market movements, the script includes a "Trailing Stop" feature. If a trade moves into a profitable state beyond a certain threshold, the script will dynamically move the Stop Loss closer to the current price. This allows the trade to "breathe" while ensuring that if the market suddenly reverses, the gains are preserved.

Operational Safeguards

Before beginning any operations, the script performs basic health checks to ensure it has enough historical data to calculate the averages accurately and verifies that your profit targets are set to realistic values. It is designed to run autonomously, constantly scanning for these moving average intersections and adjusting its active trades in real-time.

Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicator
11 Views
0 Downloads
0 Favorites
EMA-CROSS
//+------------------------------------------------------------------+
//|                                                    EMA_CROSS.mq4 |
//|                                                      Coders Guru |
//|                                         http://www.forex-tsd.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| TODO: Add Money Management routine                               |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link      "http://www.forex-tsd.com"
//---- input parameters
extern double    TakeProfit=130;
extern double    StopLoss = 100;
extern double    Lots=1;
extern double    TrailingStop=20;
extern int ShortEma = 12;
extern int LongEma = 20;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
int Crossed (double line1 , double line2)
   {
      static int last_direction = 0;
      static int current_direction = 0;
      
      //Don't work in the first load, wait for the first cross!
      static bool first_time = true;
      if(first_time == true)
      {
         first_time = false;
         return (0);
      }
      
      if(line1>line2)current_direction = 1; //up
      if(line1<line2)current_direction = 2; //down
      if(current_direction != last_direction) //changed 
      {
            last_direction = current_direction;
            return (last_direction);
      }
      else
      {
            return (0); //not changed
      }
   }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   int cnt, ticket, total;
   double SEma, LEma;
   
   
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
        
   SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0);
   LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,0);
   
   
   static int isCrossed  = 0;
   isCrossed = Crossed (LEma,SEma);
   
   total  = OrdersTotal(); 
   if(total < 1) 
     {
       if(isCrossed == 1)
         {
            //ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"EMA_CROSS",12345,0,Green);
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"EMA_CROSS",12345,0,Green);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
              }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0);
         }
         if(isCrossed == 2)
         {
            //ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"EMA_CROSS",12345,0,Red);
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"EMA_CROSS",12345,0,Red);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
              }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0);
         }
         return(0);
     }
     
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      //OrderPrint();
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
           
           /* REMOVED - Trailling stop only close
           if(isCrossed == 2)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
           */
           
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            // should it be closed?
            
            /* REMOVED - Trailling stop only close
            if(isCrossed == 1)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
            */
            
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
 

Profitability Reports

PERFORMANCE BY SYMBOL

Expected payoff

Average expected payoff across the available backtests for each symbol.

Positive Negative
17 reports
EUR/USD Jul 2025 - Sep 2025
37.20
Total Trades 2094
Won Trades 2026
Lost trades 68
Win Rate 96.75 %
Expected payoff 117.57
Gross Profit 252994.00
Gross Loss -6800.00
Total Net Profit 246194.00
-100%
-50%
0%
50%
100%
EUR/USD Oct 2025 - Feb 2026
0.87
Total Trades 304
Won Trades 214
Lost trades 90
Win Rate 70.39 %
Expected payoff -3.93
Gross Profit 7806.00
Gross Loss -9000.00
Total Net Profit -1194.00
-100%
-50%
0%
50%
100%
AUD/USD Oct 2025 - Feb 2026
0.79
Total Trades 275
Won Trades 197
Lost trades 78
Win Rate 71.64 %
Expected payoff -5.95
Gross Profit 6090.00
Gross Loss -7727.00
Total Net Profit -1637.00
-100%
-50%
0%
50%
100%
USD/JPY Jul 2025 - Sep 2025
0.74
Total Trades 183
Won Trades 114
Lost trades 69
Win Rate 62.30 %
Expected payoff -6.69
Gross Profit 3463.69
Gross Loss -4687.24
Total Net Profit -1223.55
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
0.73
Total Trades 184
Won Trades 134
Lost trades 50
Win Rate 72.83 %
Expected payoff -7.32
Gross Profit 3653.00
Gross Loss -5000.00
Total Net Profit -1347.00
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.70
Total Trades 152
Won Trades 104
Lost trades 48
Win Rate 68.42 %
Expected payoff -9.35
Gross Profit 3379.00
Gross Loss -4800.00
Total Net Profit -1421.00
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.63
Total Trades 167
Won Trades 112
Lost trades 55
Win Rate 67.07 %
Expected payoff -8.91
Gross Profit 2515.57
Gross Loss -4003.42
Total Net Profit -1487.85
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.61
Total Trades 185
Won Trades 110
Lost trades 75
Win Rate 59.46 %
Expected payoff -15.62
Gross Profit 4611.00
Gross Loss -7500.00
Total Net Profit -2889.00
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
0.57
Total Trades 153
Won Trades 93
Lost trades 60
Win Rate 60.78 %
Expected payoff -20.96
Gross Profit 4293.15
Gross Loss -7500.05
Total Net Profit -3206.90
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.53
Total Trades 136
Won Trades 90
Lost trades 46
Win Rate 66.18 %
Expected payoff -15.93
Gross Profit 2433.00
Gross Loss -4600.00
Total Net Profit -2167.00
-100%
-50%
0%
50%
100%
USD/CAD Oct 2024 - Jan 2025
0.36
Total Trades 91
Won Trades 53
Lost trades 38
Win Rate 58.24 %
Expected payoff -19.05
Gross Profit 959.30
Gross Loss -2692.73
Total Net Profit -1733.43
-100%
-50%
0%
50%
100%
AUD/USD Oct 2024 - Jan 2025
0.31
Total Trades 106
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -28.69
Gross Profit 1359.00
Gross Loss -4400.00
Total Net Profit -3041.00
-100%
-50%
0%
50%
100%
GBP/AUD Oct 2025 - Feb 2026
0.31
Total Trades 354
Won Trades 156
Lost trades 198
Win Rate 44.07 %
Expected payoff -27.19
Gross Profit 4359.13
Gross Loss -13984.74
Total Net Profit -9625.61
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.30
Total Trades 239
Won Trades 111
Lost trades 128
Win Rate 46.44 %
Expected payoff -24.21
Gross Profit 2526.12
Gross Loss -8312.39
Total Net Profit -5786.27
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.26
Total Trades 122
Won Trades 66
Lost trades 56
Win Rate 54.10 %
Expected payoff -33.83
Gross Profit 1473.00
Gross Loss -5600.00
Total Net Profit -4127.00
-100%
-50%
0%
50%
100%
GBP/CAD Oct 2025 - Feb 2026
0.18
Total Trades 237
Won Trades 72
Lost trades 165
Win Rate 30.38 %
Expected payoff -40.61
Gross Profit 2171.46
Gross Loss -11796.53
Total Net Profit -9625.07
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.00
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%

Comments