code_snippet_DelayTrading





//+------------------------------------------------------------------+
//|                                                steinitz_test.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern int     MagicNumber =  30000;

extern string  hd = " --Delay time after last trade--";
extern bool     useDelayAfterTrade = true;
extern int     MinutesToDelay = 30;

extern string  to="---Text Object Settings---";
extern int 		CommentTxtSize = 10;
extern color 	CommentColor   = White;

int            Comment1Y;
string         Comment1Label;

datetime StopTime;                 // Time to wait after a trade is stopped out

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----

     DeleteExistingLabels();
     SetupLabels();
     ClearLabels();
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

   ClearLabels();
   DeleteExistingLabels();
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   int NumOrders = 0;
   
// Reset labels in case of disconnect from server

   if (Comment1Y < 10)
   {
     SetupLabels();
     ClearLabels();
     DeleteExistingLabels();
     SetupLabels();// Make sure label settings are OK
   }

   NumOrders = CalculateCurrentOrders(MagicNumber);
   if(NumOrders == 0)
   {
// Check if last trade stopped out

      if (useDelayAfterTrade == 1)
      {
        StopTime = LastTradeStoppedOut();
        if  (TimeCurrent() < StopTime)
        {
          OutputComment1ToChart("No Trades Until : " + TimeToStr(StopTime,TIME_DATE|TIME_MINUTES));
         return(0);
        }
        OutputComment1ToChart("Trading Resumed ");
      }
      
// Your code for placing trades

   }
   else
   {
      RefreshRates();
      
// Your code to handle open trades

   }
   
   return(0);
}


//+------------------------------------------------------------------+
//| LastTradeStoppedOut                                              |
//| Check History to see if last trade stopped out                   |
//| Return Time for next trade                                       |
//+------------------------------------------------------------------+
  
datetime LastTradeStoppedOut()
{
   int cnt, total;
   bool Stopped;
   datetime NextTime;
   
   NextTime = 0;
   total = OrdersHistoryTotal();
   for (cnt = total - 1; cnt >= 0; cnt--)
   {
      OrderSelect (cnt, SELECT_BY_POS, MODE_HISTORY);
      
      if(OrderSymbol()!=Symbol()) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
      
      if (TimeCurrent() - OrderCloseTime() > MinutesToDelay*60) continue;
      
        Stopped = true;
   }
   
   if (Stopped)
   {
      NextTime = OrderCloseTime() + MinutesToDelay*60;
   }
   
   return (NextTime);
}

int CalculateCurrentOrders(int MagicNumber)
  {
   int buys = 0, sells = 0, num = 0;
   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!= Symbol()) continue;
      if (OrderMagicNumber()!= MagicNumber) continue;
      if(OrderType() == OP_BUY)  buys++;
      if(OrderType() == OP_SELL) sells++;
     }
     
   num = buys + sells;
   return(num);
  }

void ClearLabels()
{
   string mComment = " ";
   
   OutputLabelToChart(Comment1Label, Comment1Y, CommentTxtSize, CommentColor, mComment, " ");
}

void DeleteExistingLabels()
{
   int objLabels = ObjectsTotal(OBJ_LABEL);
   string objName;
   
   if (objLabels > 0)
   {
      for (int i = objLabels; i >= 0;i--)
      {
         objName = ObjectName(i);
         if (StringFind(objName,Symbol() + "Comment1", 0) >= 0) { ObjectDelete(objName); continue; } 
      }
   }
}

void SetupLabels()
{
     Comment1Y = 12;
	  Comment1Label = Symbol() + "Comment1";
}

void OutputLabelToChart(string LabelName, int LabelY, int LabelTxtSize, color LabelColor, string LabelStr, string MarkString)
{
//  string mtime=Month()+"/"+Day()+" "+Hour()+":"+Minute()+":"+Seconds()+"  ";

  	if(ObjectFind(LabelName) != 0)
   {
   ObjectCreate(LabelName, OBJ_LABEL, 0, 0, 0);
	ObjectSet(LabelName, OBJPROP_CORNER, 0);
	ObjectSet(LabelName, OBJPROP_XDISTANCE, 20);
	ObjectSet(LabelName, OBJPROP_YDISTANCE, LabelY);
	}
	ObjectSetText(LabelName, LabelStr, LabelTxtSize, "Arial Bold", LabelColor);
}

void OutputComment1ToChart(string mComment)
{
   OutputLabelToChart(Comment1Label, Comment1Y, CommentTxtSize, CommentColor, mComment, "*");
}

//+------------------------------------------------------------------+



Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:
Checks for the total of closed orders

Checks for the total of open orders

Other Features: