ZigAndZag_trader

Author: Copyright � 2008, MetaQuotes Software Corp.
Profit factor:
0.69

Here's a breakdown of what this trading script does, explained in a way that avoids technical jargon:

Overall Goal:

This script aims to automatically buy and sell currencies (or other assets) based on signals from a "ZigAndZag" indicator. It's designed to open a single trade at a time, and then close that trade when it receives another signal.

Here's the step-by-step logic:

  1. Setup:

    • The script starts by taking in some settings that you, the user, can adjust. These include:
      • Lots: How much of the currency to buy or sell in each trade.
      • ZZbar: Specifies which past bar, relative to the current one, the "ZigAndZag" indicator uses to generate buy/sell signals.
      • Closebar: Specifies which past bar, relative to the current one, the "ZigAndZag" indicator uses to generate close signals.
      • Maxord: (Currently set to 1) This limits the script to having only one open trade at any given time.
      • Sl (Stop Loss): The number of pips (a standard unit of measure in currency trading) away from the opening price that the trade will automatically close to limit losses if the price moves against the trade.
      • Tp (Take Profit): The number of pips away from the opening price that the trade will automatically close to secure profits if the price moves in favor of the trade.
      • Magic: A unique identifying number assigned to the trades opened by this script. This helps the script manage its own trades and not interfere with others.
  2. Waiting for a New Bar:

    • The script constantly checks if a new price bar has formed. It only acts when a new bar appears. This prevents the script from repeatedly executing the same actions on the same price data.
  3. Checking for Signals:

    • The script uses a custom indicator called "ZigAndZag" to generate trading signals. It looks at specific bars based on the ZZbar and Closebar settings to identify buy, sell, and close signals.
      • It checks three signals from the ZigAndZag indicator
      • If the buy signal is active it sets buy = true
      • If the sell signal is active it sets sell = true
      • if the close signal is active it sets close = true
  4. Opening a Trade:

    • If a "buy" signal is active and there are no trades currently open (OrdersTotal()<Maxord), the script opens a "buy" trade.
    • If a "sell" signal is active and there are no trades currently open, the script opens a "sell" trade.
    • When opening a trade, the script also sets:
      • Stop Loss (Sl): Automatically closes the trade to prevent a large loss.
      • Take Profit (Tp): Automatically closes the trade to secure a profit.
      • The amount to trade (Lots).
      • The script also checks to make sure the market allows trading, and waits if it doesn't.
  5. Closing a Trade:

    • If a "close" signal is active and there is an open trade, the script closes all trades opened by this script (identified by the "magic" number). It closes all trades to prevent potential loses from an unwanted trade
    • The script also checks to make sure the market allows trading, and waits if it doesn't.

In simple terms:

The script waits for a specific indicator ("ZigAndZag") to tell it to either buy or sell. When it gets a signal, it opens a trade. When the indicator tells it to close, it closes the trade. It does this automatically, aiming to profit from price movements identified by the "ZigAndZag" indicator. The stop loss and take profit settings are like safety nets, automatically closing the trade to limit losses or secure gains.

Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Miscellaneous
It plays sound alerts
3 Views
0 Downloads
0 Favorites
ZigAndZag_trader
//+------------------------------------------------------------------+
//|                                             ZigAndZag_trader.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//---- input parameters
extern double    Lots=0.1;//òîðãóåìûé ëîò
extern int       ZZbar=3;//áàðîâ íàçàä ñ êîòîðîãî áåðåì ñèãíàë ê òîðãîâëå
extern int       Closebar=3;//áàðîâ íàçàä ñ êîòîðîãî áåðåì ñèãíàë ê çàêðûòèþ 
extern int       Maxord=1;//êîëè÷åñòâî òîðãóåìûõ îðäåðîâ(ïîêà îñòàâèòü 1)âîìîæíî áóäåò ìóëüòèîðäåðíàÿ ñèñòåìà
extern int       Sl=0;//ñòîïëîñ ïîäáèðàåòñÿ ïðè îïòèìèçàöèè
extern int       Tp=0;//òåéê ïîäáèðàåòñÿ ïðè îïòèìèçàöèè
extern int       magic=78977;//ìàãèê
//-----------------------
static int prevtime = 0 ;
bool buy,sell,close,UseSound=false;
//+------------------------------------------------------------------+

int start()
  {
// Æäåì, êîãäà ñôîðìèðóåòñÿ íîâûé áàð
   if (Time[0] == prevtime) return(0);
      prevtime = Time[0];  
buy=false;sell=false;     
//-----------
 if(iCustom(NULL,0,"ZigAndZag",4,Closebar)!=0){close=true;}
 if(iCustom(NULL,0,"ZigAndZag",5,ZZbar)!=0){buy=true;}
 if(iCustom(NULL,0,"ZigAndZag",6,ZZbar)!=0){sell=true;}
 //Comment(close+"\n"+buy+"\n"+sell+"\n"+OrdersTotal());
//------------
 if(buy&&OrdersTotal()<Maxord){open(false,Sl,Tp,Lots);buy=false; } 
 if(sell&&OrdersTotal()<Maxord){open(true,Sl,Tp,Lots);sell=false; }
//------------------------------- 
 if(close&&OrdersTotal()>0){
  for(int i=0;i<OrdersTotal();i++){
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
    del(OrderTicket()); 
     close=false; 
     }}} 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//--------Ôóíêöèÿ îòêðûòèÿ îðäåðîâ-------------------------------------+
int open(bool tip,int Sl,int Tp,double lots)
{//tip = false => OP_BUYSTOP ; tip = true => OP_SELLSTOP;
   GetLastError();
   int err;
   double lastprise,prise,sl,tp; // ñàìàÿ ñâåæàÿ öåíà
   int ticket;
   int slip =(MarketInfo(Symbol(),MODE_SPREAD))*Point;//ìàêñ îòêëîíåíèå = ñïðåäó
   
//------   
   while (!IsTradeAllowed()){ Sleep(5000);}// åñëè ðûíîê çàíÿò òî ïîäîæäåì 5 ñåê
   if (tip == false)
    {
     prise = NormalizeDouble(MarketInfo(Symbol(),MODE_ASK),Digits);
     if(Sl!=0){sl = NormalizeDouble((MarketInfo(Symbol(),MODE_BID)-(Sl*Point)),Digits);}else{sl=0;}
     if(Tp!=0){tp = NormalizeDouble((MarketInfo(Symbol(),MODE_ASK)+(Tp*Point)),Digits);}else{tp=0;}
     for(int i=0;i<5;i++) 
      {
       RefreshRates();// îáíîâèì öåíó
        ticket = OrderSend(Symbol(), OP_BUY,lots ,prise, slip,sl,tp,NULL,magic,0, Blue);
         if (ticket < 0)
          {
           if(UseSound){PlaySound("timeout.wav");}
            Print("Öåíà ñëèøêîì áëèçêî!",prise,"  ",sl,"  ",tp,"  Íå ìîãó ïîñòàâèòü îðäåð BUY!");
             }
              else
               {
                break;
                 }
                  }
                   }
  if(tip==true)
   {
    prise = NormalizeDouble(MarketInfo(Symbol(),MODE_BID),Digits);
    if(Sl!=0){sl = NormalizeDouble((MarketInfo(Symbol(),MODE_ASK)+(Sl*Point)),Digits);}else{sl=0;}
    if(Tp!=0){tp = NormalizeDouble((MarketInfo(Symbol(),MODE_BID)-(Tp*Point)),Digits);}else{tp=0;}    
    for( i=0;i<5;i++) 
     {
      RefreshRates();// îáíîâèì öåíó
       ticket = OrderSend(Symbol(), OP_SELL, lots ,prise, slip,sl,tp,NULL,magic,0, Red);
        if (ticket < 0)
         {
          if(UseSound){PlaySound("timeout.wav");}
           Print("Öåíà ñëèøêîì áëèçêî!",prise,"  ",sl,"  ",tp,"  Íå ìîãó ïîñòàâèòü îðäåð SELL!");
            }
             else
              {
               break;
                }
                 }
                  }

return(ticket); 
 } 
//-------------------------------------------------------------------+
int del(int ticket)
   {
    int err;
        GetLastError();//îáíóëÿåì îøèêó
        OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
        string symbol = OrderSymbol();
        
        if(OrderType()==OP_BUY)
         {
          RefreshRates();
           double prise = MarketInfo(symbol,MODE_BID);
            OrderClose(ticket,OrderLots(),prise,3,Green);
             err = GetLastError();
             }
        if(OrderType()==OP_SELL)
         {
          RefreshRates();
           prise = MarketInfo(symbol,MODE_ASK);
            OrderClose(ticket,OrderLots(),prise,3,Green);
             err = GetLastError();
             }
        if (err == 0&&UseSound){PlaySound("expert.wav");} if (err != 0) {PlaySound("timeout.wav");Print(err);} 
        while (!IsTradeAllowed()){ Sleep(5000);}// åñëè ðûíîê çàíÿò òî ïîäîæäåì 5 ñåê 
    return(err);     
    }  

Profitability Reports

USD/CAD Jul 2025 - Sep 2025
0.54
Total Trades 28
Won Trades 6
Lost trades 22
Win Rate 21.43 %
Expected payoff -3.71
Gross Profit 121.21
Gross Loss -225.20
Total Net Profit -103.99
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.37
Total Trades 32
Won Trades 8
Lost trades 24
Win Rate 25.00 %
Expected payoff -9.83
Gross Profit 187.40
Gross Loss -502.00
Total Net Profit -314.60
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.31
Total Trades 34
Won Trades 8
Lost trades 26
Win Rate 23.53 %
Expected payoff -8.83
Gross Profit 136.60
Gross Loss -436.91
Total Net Profit -300.31
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
1.68
Total Trades 34
Won Trades 16
Lost trades 18
Win Rate 47.06 %
Expected payoff 3.95
Gross Profit 332.90
Gross Loss -198.50
Total Net Profit 134.40
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.69
Total Trades 27
Won Trades 8
Lost trades 19
Win Rate 29.63 %
Expected payoff -3.99
Gross Profit 235.90
Gross Loss -343.60
Total Net Profit -107.70
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
1.12
Total Trades 71
Won Trades 29
Lost trades 42
Win Rate 40.85 %
Expected payoff 2.01
Gross Profit 1298.78
Gross Loss -1156.07
Total Net Profit 142.71
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
0.73
Total Trades 77
Won Trades 23
Lost trades 54
Win Rate 29.87 %
Expected payoff -4.37
Gross Profit 912.72
Gross Loss -1249.53
Total Net Profit -336.81
-100%
-50%
0%
50%
100%
USD/CAD Jan 2025 - Jul 2025
0.52
Total Trades 70
Won Trades 22
Lost trades 48
Win Rate 31.43 %
Expected payoff -5.81
Gross Profit 435.75
Gross Loss -842.68
Total Net Profit -406.93
-100%
-50%
0%
50%
100%
NZD/USD Jan 2025 - Jul 2025
1.19
Total Trades 64
Won Trades 27
Lost trades 37
Win Rate 42.19 %
Expected payoff 2.57
Gross Profit 1020.80
Gross Loss -856.50
Total Net Profit 164.30
-100%
-50%
0%
50%
100%
GBP/USD Jan 2025 - Jul 2025
0.71
Total Trades 72
Won Trades 26
Lost trades 46
Win Rate 36.11 %
Expected payoff -6.92
Gross Profit 1227.70
Gross Loss -1725.80
Total Net Profit -498.10
-100%
-50%
0%
50%
100%

Comments