MACD Sample_v3

Profit factor:
0.79

Okay, here's a breakdown of what this MetaTrader script does, explained in a way that avoids technical jargon and focuses on the overall logic:

This script is like an automated assistant that watches the market and makes decisions about buying and selling a currency pair. It's based on a popular trading strategy using a technical indicator called MACD (Moving Average Convergence Divergence). It also uses Moving Averages.

Here's how it works, step by step:

  1. Initial Setup: The script starts by getting some basic information. It finds out the current price of the currency pair and sets up a few rules provided by the user, such as:

    • How much profit to aim for on each trade (Take Profit).
    • How much of the account to risk on each trade (Lots).
    • How far behind the price to move the stop-loss order to protect profits (Trailing Stop).
    • MACD levels for open and close orders.
    • Period for the moving average indicator.
  2. Checking Market Conditions: The core of the script involves monitoring the MACD indicator and a moving average (MA). The MACD helps identify potential buying and selling opportunities by comparing two moving averages of prices. It also looks at a signal line to confirm these opportunities. The moving average looks if the current trend is up or down.

  3. Looking for Trading Opportunities: The script constantly checks if the MACD and the moving average line are showing signals that line up with the user-defined rules.

    • Buying Signals: It looks for moments when the MACD suggests the price is about to go up, and also if the moving average is also uptrending, then the script considers buying.
    • Selling Signals: Conversely, it looks for when the MACD suggests the price is about to go down, and the moving average is also downtrending, signaling a possible selling opportunity.
  4. Opening Trades: If the script detects a valid buying or selling opportunity based on the MACD and moving average signals, it automatically places an order to open a trade.

  5. Managing Existing Trades: If the script finds no open trades it goes back to step 2. If it finds open trades then, the script constantly monitors any trades that are already open, and decides:

    • When to Close (Take Profit): It watches the MACD indicator. If the MACD signals that the price is likely to reverse direction, it automatically closes the trade to secure the profits.
    • Trailing Stop: It uses the trailing stop feature. This means that as the price moves in a favorable direction, the stop-loss order (an order to automatically close the trade if the price moves against you) is automatically adjusted to lock in profits.
  6. Repeating the Process: The script repeats steps 2-5 continuously, constantly watching the market, looking for new opportunities, and managing existing trades.

In short: This script automates a trading strategy that uses the MACD indicator and a moving average to identify and execute trades, aiming to make profits while managing risk using a take profit and a trailing stop feature.

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
MACD HistogramMoving average indicator
10 Views
0 Downloads
0 Favorites
MACD Sample_v3
//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;
double Points;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init ()
  {
   Points = MarketInfo (Symbol(), MODE_POINT);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double MacdCurrent=0, MacdPrevious=0, SignalCurrent=0;
   double SignalPrevious=0, MaCurrent=0, MaPrevious=0;
   int cnt=0, total;
// ïåðâè÷íûå ïðîâåðêè äàííûõ
// âàæíî óäîñòîâåðèòüñÿ ÷òî ýêñïåðò ðàáîòàåò íà íîðìàëüíîì ãðàôèêå è
// ïîëüçîâàòåëü ïðàâèëüíî âûñòàâèë âíåøíèå ïåðåìåííûå (Lots, StopLoss,
// TakeProfit, TrailingStop)
// â íàøåì ñëó÷àå ïðîâåðÿåì òîëüêî TakeProfit
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  // íà ãðàôèêå ìåíåå 100 áàðîâ
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // ïðîâåðÿåì TakeProfit
     }
// ðàäè óïðîùåíèÿ è óñêîðåíèÿ êîäà, ñîõðàíèì íåîáõîäèìûå
// äàííûå èíäèêàòîðîâ âî âðåìåííûõ ïåðåìåííûõ
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   MaCurrent=iMA(NULL,0,MATrendPeriod,MODE_EMA,0,PRICE_CLOSE,0);
   MaPrevious=iMA(NULL,0,MATrendPeriod,MODE_EMA,0,PRICE_CLOSE,1);
// òåïåðü íàäî îïðåäåëèòüñÿ - â êàêîì ñîñòîÿíèè òîðãîâûé òåðìèíàë?
// ïðîâåðèì, åñòü ëè ðàíåå îòêðûòûå ïîçèöèè èëè îðäåðû?
   if(OrdersTotal()<1) 
     {
      // íåò íè îäíîãî îòêðûòîãî îðäåðà
      // íà âñÿêèé ñëó÷àé ïðîâåðèì, åñëè ó íàñ ñâîáîäíûå äåíüãè íà ñ÷åòó?
      // çíà÷åíèå 1000 âçÿòî äëÿ ïðèìåðà, îáû÷íî ìîæíî îòêðûòü 1 ëîò
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money");
         return(0);  // äåíåã íåò - âûõîäèì
        }
      // ïðîâåðèì, íå ñëèøêîì ëè ÷àñòî ïûòàåìñÿ îòêðûòüñÿ?
      // åñëè ïîñëåäíèé ðàç òîðãîâàëè ìåíåå ÷åì 5 ìèíóò(5*60=300 ñåê)
      // íàçàä, òî âûõîäèì
      // If((CurTime-LastTradeTime)<300) { Exit }
      // ïðîâåðÿåì íà âîçìîæíîñòü âñòàòü â äëèííóþ ïîçèöèþ (BUY)
      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
         MathAbs(MacdCurrent)>(MACDOpenLevel*Points) && MaCurrent>MaPrevious)
        {
         OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Points,"macd sample",16384,0,Red); // èñïîëíÿåì
         if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
         return(0); // âûõîäèì, òàê êàê âñå ðàâíî ïîñëå ñîâåðøåíèÿ òîðãîâîé îïåðàöèè
            // íàñòóïèë 10-òè ñåêóíäíûé òàéìàóò íà ñîâåðøåíèå òîðãîâûõ îïåðàöèé
        }
      // ïðîâåðÿåì íà âîçìîæíîñòü âñòàòü â êîðîòêóþ ïîçèöèþ (SELL)
      if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
         MacdCurrent>(MACDOpenLevel*Points) && MaCurrent<MaPrevious)
        {
         OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Points,"macd sample",16384,0,Red); // èñïîëíÿåì
         if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
         return(0); // âûõîäèì
        };
      // çäåñü ìû çàâåðøèëè ïðîâåðêó íà âîçìîæíîñòü îòêðûòèÿ íîâûõ ïîçèöèé.
      // íîâûå ïîçèöèè îòêðûòû íå áûëè è ïðîñòî âûõîäèì ïî Exit, òàê êàê
      // âñå ðàâíî àíàëèçèðîâàòü íå÷åãî
      return(0);
     };
   // ïåðåõîäèì ê âàæíîé ÷àñòè ýêñïåðòà - êîíòðîëþ îòêðûòûõ ïîçèöèé
   // 'âàæíî ïðàâèëüíî âîéòè â ðûíîê, íî âûéòè - åùå âàæíåå...'
   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && // ýòî îòêðûòàÿ ïîçèöèÿ? OP_BUY èëè OP_SELL 
         OrderSymbol()==Symbol())    // èíñòðóìåíò ñîâïàäàåò?
        {
         if(OrderType()==OP_BUY)   // îòêðûòà äëèííàÿ ïîçèöèÿ
           {
            // ïðîâåðèì, ìîæåò óæå ïîðà çàêðûâàòüñÿ?
            if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
               MacdCurrent>(MACDCloseLevel*Points))
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // çàêðûâàåì ïîçèöèþ
                 return(0); // âûõîäèì
                };
            // ïðîâåðèì - ìîæåò ìîæíî/íóæíî óæå òðåéëèíã ñòîï ñòàâèòü?
            if(TrailingStop>0)  // ïîëüçîâàòåëü âûñòàâèë â íàñòðîéêàõ òðåéëèíãñòîï
              {                 // çíà÷èò ìû èäåì åãî ïðîâåðÿòü
               if(Bid-OrderOpenPrice()>Points*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Points*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Points*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
         else // èíà÷å ýòî êîðîòêàÿ ïîçèöèÿ
           {
            // ïðîâåðèì, ìîæåò óæå ïîðà çàêðûâàòüñÿ?
            if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
               MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Points))
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // çàêðûâàåì ïîçèöèþ
               return(0); // âûõîäèì
              }
            // ïðîâåðèì - ìîæåò ìîæíî/íóæíî óæå òðåéëèíã ñòîï ñòàâèòü?
            if(TrailingStop>0)  // ïîëüçîâàòåëü âûñòàâèë â íàñòðîéêàõ òðåéëèíãñòîï
              {                 // çíà÷èò ìû èäåì åãî ïðîâåðÿòü
               if((OrderOpenPrice()-Ask)>(Points*TrailingStop))
                 {
                  if(OrderStopLoss()==0.0 || 
                     OrderStopLoss()>(Ask+Points*TrailingStop))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Points*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
// the end.

Profitability Reports

USD/JPY Jul 2025 - Sep 2025
0.00
Total Trades 13
Won Trades 13
Lost trades 0
Win Rate 100.00 %
Expected payoff 2.61
Gross Profit 33.94
Gross Loss 0.00
Total Net Profit 33.94
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
3.09
Total Trades 10
Won Trades 9
Lost trades 1
Win Rate 90.00 %
Expected payoff 2.70
Gross Profit 39.86
Gross Loss -12.89
Total Net Profit 26.97
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.35
Total Trades 22
Won Trades 17
Lost trades 5
Win Rate 77.27 %
Expected payoff -3.73
Gross Profit 43.58
Gross Loss -125.70
Total Net Profit -82.12
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.27
Total Trades 6
Won Trades 5
Lost trades 1
Win Rate 83.33 %
Expected payoff -9.27
Gross Profit 20.20
Gross Loss -75.80
Total Net Profit -55.60
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.24
Total Trades 7
Won Trades 6
Lost trades 1
Win Rate 85.71 %
Expected payoff -9.39
Gross Profit 21.30
Gross Loss -87.00
Total Net Profit -65.70
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.43
Total Trades 7
Won Trades 6
Lost trades 1
Win Rate 85.71 %
Expected payoff -2.41
Gross Profit 12.66
Gross Loss -29.52
Total Net Profit -16.86
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.64
Total Trades 30
Won Trades 28
Lost trades 2
Win Rate 93.33 %
Expected payoff -1.49
Gross Profit 79.17
Gross Loss -124.01
Total Net Profit -44.84
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.53
Total Trades 16
Won Trades 15
Lost trades 1
Win Rate 93.75 %
Expected payoff -2.71
Gross Profit 49.10
Gross Loss -92.40
Total Net Profit -43.30
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.73
Total Trades 10
Won Trades 9
Lost trades 1
Win Rate 90.00 %
Expected payoff -1.07
Gross Profit 29.30
Gross Loss -40.00
Total Net Profit -10.70
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
0.60
Total Trades 58
Won Trades 55
Lost trades 3
Win Rate 94.83 %
Expected payoff -1.73
Gross Profit 147.92
Gross Loss -248.54
Total Net Profit -100.62
-100%
-50%
0%
50%
100%

Comments