ReversWaves_TimeShift

Download script
Profit factor:
0.00

This automated trading script is designed to manage trades based on price direction, time constraints, and a specific recovery logic. Below is a non-technical breakdown of how the logic functions:

Core Operational Logic

1. Time Restrictions The script does not operate constantly. It is configured with specific start and end hours. If the current market time falls outside of this window (and there are no active trades resulting in profit), the script will remain idle and display a notification that it is currently "out of work" time.

2. Market Direction Analysis The script determines whether to buy or sell by looking at recent price action:

  • It compares the closing price of the previous candle to the opening price of the candle before it.
  • If the price closed higher than it opened, the script identifies a "Buy" opportunity.
  • If the price closed lower, it identifies a "Sell" opportunity.

3. Trade Execution and Safety Checks Before opening any positions, the script performs several background checks:

  • Connectivity: It ensures the platform is connected to the broker and trade permissions are active.
  • Account Balance: It verifies if there is enough "Free Margin" (available funds) to cover the cost of the trade.
  • Unique Identification: It uses a "Magic Number" to ensure it only manages trades that it opened itself, avoiding interference with other manual or automated trades.
  • Maximum Lot Limit: It includes a safety cap to prevent the software from opening an excessively large position size.

4. The Recovery Mechanism (Martingale-Style Logic) The most distinct feature of this script is how it handles losing trades. It employs a dynamic lot sizing strategy:

  • Standard Entry: When starting fresh, the script opens a trade with a standard, predefined volume.
  • Loss Detection & Escalation: If a trade is closed at a loss (specifically if it hits a certain "limit price" threshold), the script triggers a recovery mode.
  • Position Reversal: When a loss occurs, the script will attempt to enter a new position in the opposite direction of the losing trade.
  • Multiplier Effect: To recover the previous loss, the script multiplies the next trade's volume by a specific factor (determined by the "MM" setting). It continues to increase the size of subsequent trades until a profitable outcome is achieved or it hits the maximum allowed lot size.

5. Trade Management For every active trade, the script monitors:

  • Take Profit: Automatically sets a target price where the trade should close for a gain.
  • Stop Loss: Automatically sets a price point to exit the trade if the market moves against the position significantly.
  • Automated Exit: The script actively scans active trades to see if they have reached the predefined "Limit Price" or profit targets, closing them immediately when those conditions are met.
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
12 Views
0 Downloads
0 Favorites
ReversWaves_TimeShift
//+------------------------------------------------------------------+
//|                                                       Revers.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                         http://www.carbonclub.ru |
//+------------------------------------------------------------------+
 
 
extern double TakeProfit = 5;       // ÒÝÉÊÏÐÎÔÈÒ
extern double Lots = 0.02;          // Ñóììà ñòàâêè
extern double StopLoss = 70;        // ÑÒÎÏËÎÑÑ
extern double OrderLimitPrice = 5;  // Ïðîãðàììíûé ÑÒÎÏËÎÑÑ
extern int    StartHour = 23;       // Âðåìÿ íà÷àëà ðàáîòû 
extern int    EndHour = 5;          // Îêîí÷àíèå ðàáîòû  
extern int    MM = 2;               // Ìíîæèòåëü ñòàâêè
extern int    MagicNumber = 111;    // Magic number
extern int    MaxLot = 45;          // Ìàêñèìàëüíàÿ ñóììà ëîòà äëÿ îòáèâêè
 
int count=0;                        // Ãëîáàëüíàÿ ïåðåìåí.
bool BoSpos=0;                      // Ôëàã íà ïîêóïêó äëèíîé èëè êîðîòêîé ïîçèöèè.
int TicketOrder;                    // Íîìåð òåêóùåãî óáûòî÷íîãî îðäåðà
string Symb;                        // Íàèìåíîâàíèå êîòèðîâêè. USDEUR           
int Lpos,LposR,Count2;              // Ïîñëåäíÿÿ óáûòî÷íàÿ ñòàâêà. Íà SELL or BUY
int
 
   z=0,                             // ñ÷åò÷èê öèêëîâ  
   
   Clost,
   total,                           // Îáùåå êîë-âî îðäåðîâ
   POrder,                          // Íîìåð òåêóøåãî îòêðûòîãî îðäåðà
   TOrder,                          // Íîìåð  îòëîæåííîãî îðäåðà
   Total,                           // Êîëè÷åñòâî îðäåðîâ â îêíå 
   Tip=-1,                          // Òèï âûáðàí. îðäåðà (B=0,S=1)
   Ticket,                          // Íîìåð îðäåðà
   ticket;
   double
 
 //  Lots,                          // Ñòàâêà
   LPCOrder,                        // Îáú¸ì çàêðûòèÿ ïîñëåäíåãî óáûòî÷íîãî îðäåðà
   MA_1_t,                          // Çíà÷åí. ÌÀ_1 òåêóùåå
   MA_2_t,                          // Çíà÷åí. ÌÀ_2 òåêóùåå 
   Lot,                             // Êîëè÷. ëîòîâ â âûáðàí.îðäåðå
   Lts,                             // Êîëè÷. ëîòîâ â îòêðûâ.îðäåðå
   Min_Lot,                         // Ìèíèìàëüíîå êîëè÷åñòâî ëîòîâ
   Step,                            // Øàã èçìåíåíèÿ ðàçìåðà ëîòà
   Free,                            // Òåêóùèå ñâîáîäíûå ñðåäñòâà
   One_Lot,                         // Ñòîèìîñòü îäíîãî ëîòà
   PS_Lot,                          // Ïîñëåäíÿÿ ñòîèìîñòü ïðîéãðûøíîãî ëîòà
   Price,                           // Öåíà âûáðàííîãî îðäåðà
   SL,                              // SL âûáðàííîãî îðäåðà 
   TP;                              // TP âûáðàííîãî îðäåðà
   bool
   Work,
   Ans=false,                       // Îòâåò ñåðâåðà ïîñëå çàêðûòèÿ
   Cls_B=false,                     // Êðèòåðèé äëÿ çàêðûòèÿ  Buy
   Cls_S=false,                     // Êðèòåðèé äëÿ çàêðûòèÿ  Sell
   Opn_B=false,                     // Êðèòåðèé äëÿ îòêðûòèÿ  Buy
   Opn_S=false;                     // Êðèòåðèé äëÿ îòêðûòèÿ  Sell
   
 
 
 
 
//--------------------------------------------------------------------
int init()                                      // Ñïåö. ô-èÿ init()
  {
   Symb=Symbol();
                                 // Íàçâàíèå ôèí.èíñòð.
   return;    
  }
//--------------------------------------------------------------------
 
// -------------------------------------------------
// expert deinitialization function                                 
// -------------------------------------------------
int deinit()
  {
   return(0);
  }
//--------------------------------------------------------------------  
 
 
 
// -------------------------------------------------
// Ìåíÿåò ïîçèöèþ íà ïðîòèâîïîëîæíóþ
// -------------------------------------------------
int LastPosChg(int Lpos)
  {
   if (Lpos == OP_BUY)
     {Lpos=OP_SELL;
     }
   else
     {Lpos=OP_BUY;
     }
   return(Lpos);  
  }
////////////////////////////////////////////////////
 
 
//+------------------------------------------------------------------+
//| íå ïðåâûñèëè ëè ìû âðåìåííîé èíòåðâàë                            |
//+------------------------------------------------------------------+
bool CheckTimeLimit(int b, int e)
{
   bool isEnd=false;
   
      if (b<e)
     if (TimeHour(TimeCurrent())<b || TimeHour(TimeCurrent())>=e)
       {
        isEnd=true;
       }
     if (OrdersTotal()>0 || Work==true)
       {
        
        isEnd=false;
       }
     if (OrdersTotal()==0)
       {
           if (OrderSelect(TicketOrder, SELECT_BY_POS, MODE_HISTORY)==true)    // Áåð¸ì 1-ûé îðäåð 
             {
              if (OrderProfit()>0)                        // Ïðîâåðêà íà îòêðûòûå îðäåðà 
                {
                 isEnd=true;
                }
              else isEnd=false;   
              }  
       }
     if (b>e)
       {
        isEnd=true;
         if (TimeHour(TimeCurrent())>=b)
           {isEnd=false;}
         if (TimeHour(TimeCurrent())<e)
           {isEnd=false;}  
       }
     
   return (isEnd);   
}  
////////////////////////////////////////////////////////
 
 
 
int start()                                     // Ñïåö. ô-èÿ start()
  {
   //-------------------------------------
   // verify state
   //-------------------------------------
   if(!IsTradeAllowed()) return(0);
   if(!IsConnected()) return(0);
   if(IsStopped()) return(0);
   if(IsTradeContextBusy()) 
     {
      Print("Trade context is busy!");
      return(0);
     } 
 
   if (CheckTimeLimit(StartHour, EndHour)==true )
     {
       Comment("Åùå ïîêà íå âðåìÿ ðàáîòû ýêñïåðòà" ); 
       z = 0;
       return;
     }
   else
     {
       Comment("Ýêñïåðò ðàáîòàåò"+ "\n" + 
               "Work = ", Work);
     }
 
//  ------  Îïðåäåëåíèå äâèæåíèÿ êóðñà -----
double open  = Open[2];                // Óçíà¸ì öåíó îòêðûòèÿ 3-ãî áàðà.
double close = Close[1];               // Óçàí¸ì öåíó çàêðûòèÿ 2-ãî áàðà.
if(close>=open) BoSpos=0;
           else BoSpos=1; 
 
// Îïðåäåëÿåì è ïðèñâàèâàåì çíà÷åíèå ïåðåìåííîé. îðäåð íà BUY or SELL
if (BoSpos == 0) Lpos = OP_BUY;        // Ïåðåìåíîé ïðèñâàèâàåì çíà÷åíèå íà BUY
if (BoSpos == 1) Lpos = OP_SELL;       // Ïåðåìåíîé ïðèñâàèâàåì çíà÷åíèå íà SELL
  
 
/////////////////////////////////// N  |Àíàëèç îðäåðîâ|  /////////////////////////////////////////////
   total=OrdersTotal();
   Print("Îáùåå êîëè÷åñòâî îòêðûòûõ îðäåðîâ â òåðìèíàëå = ",total);
   //--------------------------------------------------------------- 4 --
     
    if (total!=0) 
     
     { Total=0;
      int SameOrder=0;                                  // Êîëè÷åñòâî îðäåðîâ
      for(int i=0; i<=OrdersTotal(); i++)          // Öèêë ïåðåáîðà îðäåð
      {
       // Print("Íà÷àëñÿ ",i,"-ûé öèêë ïîäñ÷¸òà îðäåðîâ ");
      if (OrderSelect(i,SELECT_BY_POS)==true)   // Åñëè åñòü ñëåäóþùèé
        { 
        // Print("Îðäåð N ",i," Åñòü â íàëè÷èè ");                                    
         if (Symb==OrderSymbol())
           {
           // Print("Îðäåð N ",i," Ñîâïàäåíèå ñèìâîëà ïàðû");
            if (MagicNumber==OrderMagicNumber()) 
              {
              // Print("Îðäåð N ",i," Ñîâïàäåíèå Magic Number");
               SameOrder=SameOrder+1;
              // Print("Ñ÷¸ò÷èê èòîãî : +",SameOrder);
              }
            else 
            {
            // Print("Íå Íàø îðäåð.");
            }
           }
         else
         {
         // Print("Ñîâïàäåíèé íåò. Ïðîäîëæàåì ïîèñê... ");
         }
        }
        else
        {
       // Print("Îðäåðîâ áîëüøå íåò");  
        }
      }
     if (SameOrder>=1) {total=1; Print("Åñòü ñîâïàäåíèÿ, áîëüøå ñîâåòíèêó íåëüçÿ îòêðûâàòü îðäåðà ïî äàííîé ïàðå, total =",total); }
     else {total=0; Print("Ñ÷¸ò÷èê ïóñò, ñîâïàäåíèé íåò total =",total);} 
     
     }
//--------------------------------------------------------------- 5 -- 
 
 
  
if(total<1)                            // Åñëè ëè îòêðûòûå îðäåðà?
  {                                                         
   if(AccountFreeMargin()<(1000*Lots)) // Åñòü ëè äåíüãè íà ñ÷åòó
     {
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);  
     }
    
 
        
// Ïðîâåðêà íà (BUY) ïîçèöèþ
   if(BoSpos == 0)
     { 
      if(Work == true) 
        {
         PS_Lot=Price*MM; 
         Lpos=LastPosChg(LposR);
         if (PS_Lot>=MaxLot) {Work=false; return(0);}
         TicketOrder=OrderSend(Symbol(),Lpos,PS_Lot,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point," =1= Order Profit BUY ",MagicNumber,0,Green);   
         Print("PS_Lot for BUY order  = ", PS_Lot);
         Print("COUNT = ",Clost);
         if (TicketOrder>0){Work=false;}
        }
      else
        {
         Clost=0;
         TicketOrder=OrderSend(Symbol(),Lpos,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"Standart Order BUY",MagicNumber,0,Green);
         Work=false;
        }
      }
      
// Ïðîâåðêà íà  (SELL) ïîçèöèþ
      if (BoSpos == 1)
        { 
         if (Work == true)
           {
            PS_Lot=Price*MM; 
            if (PS_Lot>=MaxLot) {Work=false; return(0);}
            TicketOrder=OrderSend(Symbol(),Lpos,PS_Lot,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"=1= Order Profit SELL",MagicNumber,0,Red);
            Print("PS_Lot for SELL order  = ", PS_Lot);
            Print("COUNT = ",Clost);
            if (TicketOrder>0){Work=false;}
           }       
         else
           {
            Clost=0;
            TicketOrder=OrderSend(Symbol(),Lpos,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Standart Order SELL",MagicNumber,0,Red);
            Work=false;
           }
        }
     }
       
    
      
        
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   
   for(count=0;count<total;count++)                       // Öèêë. Ïåðåáèðàåì îðäåðà
      {OrderSelect(count, SELECT_BY_POS, MODE_TRADES);    // Áåð¸ì 1-ûé îðäåð 
      if(OrderType()<=OP_SELL &&                          // Ïðîâåðêà íà îòêðûòûå îðäåðà 
         OrderSymbol()==Symb && MagicNumber==OrderMagicNumber())                        // Ïðîâåðêà ñîâïàäíèÿ ñèìâîëà ïàðû
        {
         if(OrderType()==OP_BUY)                         // Ïðîâåðêà íà  äëèííóþ ïîçèöèþ
           {
            TP=OrderProfit();
            // Ïðîâåðêà óñëîâèÿ íà çàêðûòèå îðäåðà
            double AskNow=Ask;
          
            if ( AskNow<(OrderOpenPrice()-OrderLimitPrice*Point )  || ( OrderOpenPrice()-OrderLimitPrice*Point)==AskNow )  
              {
               Count2=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);       // Çàêðûâàåì îðäåð
               Print("Ïîòåðè ñîñòàâëÿþò : ",TP); // Åñëè ïðîôèò ìåíüøå 0. Ñîîáùåíèå.
               Price=OrderLots();                // Áåð¸ì çíà÷åíèå òåêóùåé öåíû
               LposR=OrderType();                // Çíà÷åíèå ïîñëåäíåé ïðîéãðûøíîé ïîçèöèè BUY or SELL
               Work=true;                        // Ðàç ïðîôèò ìåíüøå 0 òîãäà çàâîäèì ñòàâêó â öèêë. Ïîêà íå ñûãðàåò ïðîôèò.
               TicketOrder=OrderTicket();        // Çàïîìèíàåì ïîðÿäêîâûé íîìåð îðäåðà 
               Print("Ticket óáûòî÷íîãî îðäåðà â öèêëå çàêðûòèÿ îðäåðîâ : ",TicketOrder);
               if(Count2>0)
                 {
                  if(OrderSelect(count,SELECT_BY_POS,MODE_TRADES))
                    { 
                     Print("BUY order closed : ",OrderClosePrice());
                    }
                 }
               Clost++;
               }    
               
                                                            
            }  
            // 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
            {// Ïðîâåðêà óñëîâèÿ íà çàêðûòèå îðäåðà
             double BidNow=Bid;
             if ( BidNow==(OrderOpenPrice()+OrderLimitPrice*Point) || BidNow>(OrderOpenPrice()+OrderLimitPrice*Point) )                    // should it be closed?*/
               {
                Count2=OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                Price=OrderLots();                                    // Áåð¸ì çíà÷åíèå òåêóùåé öåíû
                LposR=OrderType();                                    // Çíà÷åíèå ïîñëåäíåé ïðîéãðûøíîé ïîçèöèè BUY or SELL
                Work=true;                                            // Ðàç ïðîôèò ìåíüøå 0 òîãäà çàâîäèì ñòàâêó â öèêë. Ïîêà íå ñûãðàåò ïðîôèò.
                TicketOrder=OrderTicket();                            // Çàïîìèíàåì ïîðÿäêîâûé íîìåð îðäåðà 
                Print("Ticket óáûòî÷íîãî îðäåðà â öèêëå çàêðûòèÿ îðäåðîâ : ",TicketOrder);
                if(Count2>0)
                  {
                   if(OrderSelect(count,SELECT_BY_POS,MODE_TRADES))
                     {
                     Print("SELL order closed : ",OrderClosePrice());
                     }                 
                  }
                 
                Clost++;     
                }
            // 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);
}
// the end.

Comments