MACD_503_no_trade

Author: Sanyooooook
Profit factor:
0.00
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
MACD Histogram
1 Views
0 Downloads
0 Favorites
MACD_503_no_trade
//+------------------------------------------------------------------+
//|                                                      MACD_50.mq4 |
//|                                                      Sanyooooook |
//|                                              sanyooooook@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Sanyooooook"
#property link      "sanyooooook@mail.ru"

//---- input parameters
//extern double    Lots=0.1;
extern int       fast_ema_period=12;
extern int       slow_ema_period=26;
extern int       signal_period=4;
extern int       applied_price=4;

extern double StopLost=30;
extern double Lot=0.1;
extern double TakeProfit=30;
double Slippage=3;
bool Long=true;
//extern int MAGIC=20100930;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
double Level1;
double Level2;
double LevelExtr;
int MAGIC=20101009;
int init()
  {
//----
   Level1=0.0;
   Level2=0.0;
   start();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
/*double Extr_Price(int Extr,int start,int end)
{
   Price=Close[start];
   for(int i=start;i>=end;i--)
   {
      if(Extr==0)//åñëè èùåì ìàêñèìóìû
      {
        if(Price<=High[i])
           Price=High[i];
      }
      else//åñëè èùåì ìèíèìóìû
      {
        if(Price>=Low[i])
           Price=Low[i];
      }
   }
   return(Price);
}*/
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   double MACD_Buff_High[100];
   double MACD_Buff_Low[100];
   ArrayInitialize(MACD_Buff_High,0.0);
   ArrayInitialize(MACD_Buff_Low,0.0);
   double MACD=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,1);
   double MACD_Sig=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,1);
   double MACD1=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,1+1);
   double MACD_Sig1=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,1+1);
   int i=0;
   int H;
   int Stop=0;
   int Start;
   double Price_Low=Low[0];
   double Price_High=High[0];
   if(((MACD<MACD_Sig)&&(MACD1>MACD_Sig1))||((MACD>MACD_Sig)&&(MACD1<MACD_Sig1)))
   {
      if(MACD>0)
      {
         Comment("MACD>0");
         while(Stop<2)
         {
            MACD=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,i);
            MACD_Sig=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,i);
            if(MACD<0&&Stop==0)
            {
               Stop=1;
               Start=i;
            }
            if(Price_Low>=Low[i]&&Stop==1)Price_Low=Low[i];
            if(MACD>0&&Stop==1)Stop=2;
            i++;
         }
         MACD_Buff_High[0]=Price_Low;
         H++;
         i=Start;
         MACD_Buff_High[H]=Low[i];
         while(i>=0)
         {
            MACD=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,i);
            MACD_Sig=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,i);
            MACD1=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,i+1);
            MACD_Sig1=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,i+1);
            if(Price_Low<=High[i])
            {
               Price_Low=High[i];
            }
            if((MACD-MACD_Sig)*(MACD1-MACD_Sig1)<0&&MACD<MACD_Sig&&Price_Low>MACD_Buff_High[H-1])
            {
               MACD_Buff_High[H]=Price_Low;
               H++;
            }
            i--;
         }
      }
      else
      {
         Comment("MACD<0");
         while(Stop<2)
         {
            MACD=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,i);
            MACD_Sig=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,i);
            if(MACD>0&&Stop==0)
            {
               Stop=1;
               Start=i;
            }
            if(Price_High<=High[i]&&Stop==1)Price_High=High[i];
            if(MACD<0&&Stop==1)Stop=2;
            i++;
         }
         MACD_Buff_Low[0]=Price_High;
         H=1;
         i=Start;
         MACD_Buff_Low[H]=High[i];
         while(i>=0)
         {
            MACD=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,i);
            MACD_Sig=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,i);
            MACD1=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_MAIN,i+1);
            MACD_Sig1=iMACD(NULL,0,fast_ema_period,slow_ema_period,signal_period,applied_price,MODE_SIGNAL,i+1);
            if(Price_High>=Low[i])
            {
               Price_High=Low[i];
               //MACD_Buff_Low[H]=Low[i];
               //Print("1");
            }
            if((MACD-MACD_Sig)*(MACD1-MACD_Sig1)<0&&MACD>MACD_Sig&&Price_High<MACD_Buff_Low[H-1])
            {
               MACD_Buff_Low[H]=Price_High;
               H++;
               //Print("2");
            }
            i--;
         }
      }
   }
   double Aver;
   double Extremum;
   int del;
   if(H>1)
   {
      i=1;
      if(MACD_Buff_High[0]>0)
      {
         Aver=MACD_Buff_High[0];
         for(del=ObjectsTotal()-1;del>=0;del--)
         {
            if(StringFind(ObjectName(del),"SanyokLine")>=0)
            {
               ObjectDelete(ObjectName(del));
            }
         }
         while(i<H)
         {
            Aver=(Aver+MACD_Buff_High[i])/2;
            if(i+1!=H)
            ObjectCreate("SanyokLine"+i,OBJ_HLINE,0,0,Aver);
           // Print(Aver);
            i++;
         }
         Extremum=MACD_Buff_High[H-1];
      }
      else
      {
         if(MACD_Buff_Low[0]>0)
         {
            Aver=MACD_Buff_Low[0];
            for(del=ObjectsTotal()-1;del>=0;del--)
            {
               if(StringFind(ObjectName(del),"SanyokLine")>=0)
               {
                  ObjectDelete(ObjectName(del));
               }
            }
            while(i<H)
            {
               Aver=(Aver+MACD_Buff_Low[i])/2;
               if(i+1!=H)
               ObjectCreate("SanyokLine"+i,OBJ_HLINE,0,0,Aver);
               //Print(Aver);
               i++;
            }
         Extremum=MACD_Buff_Low[H-1];
         }
      }
   }
   if(Aver>0)
   {
      if(ObjectFind("SanyokLine")==-1)
      {
         ObjectCreate("SanyokLine",OBJ_HLINE,0,0,Aver);
         Level1=NormalizeDouble(Aver,Digits);
      }
      else
      {
         if(MathAbs(ObjectGet("SanyokLine",OBJPROP_PRICE1)-Aver)>Point)
         {
            //ObjectDelete("L");
            ObjectSet("SanyokLine",OBJPROP_PRICE1,Aver);
            Level2=Level1;
            Level1=NormalizeDouble(Aver,Digits);
            //Comment (Level1," ",Level2);
         }
      }
   }
   if(Extremum>0)
   {
      if(ObjectFind("Extr")==-1)
      {
         ObjectCreate("Extr",OBJ_HLINE,0,0,Extremum);
         Level1=NormalizeDouble(Extremum,Digits);
      }
      else
      {
         if(MathAbs(ObjectGet("Extr",OBJPROP_PRICE1)-Extremum)>Point)
         {
            //ObjectDelete("L");
            ObjectSet("Extr",OBJPROP_PRICE1,Extremum);
            //Level2=Level1;
            //Level1=NormalizeDouble(Extremum,Digits);
            //Comment (Level1," ",Level2);
         }
      }
   }
   /*
   int TotalBuy=0,TotalSell=0;
   int Total=0,res;
   int otl,o;
   double SL,TP;
   for(i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true&&OrderMagicNumber()==MAGIC&&OrderSymbol()==Symbol())
      {
         int cmd=OrderType();
         if(cmd==OP_BUY)
         {
            TotalBuy=OrderTicket();
            Total++;
            if(ObjectFind("SL")>-1)
            {
               if(MathAbs(ObjectGet("SL",OBJPROP_PRICE1)-OrderStopLoss())>Point)
               OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(ObjectGet("SL",OBJPROP_PRICE1),Digits),OrderTakeProfit(),0);
            }
         }
         if(cmd==OP_SELL)
         {
            TotalSell=OrderTicket();
            Total++;
            if(ObjectFind("SL")>-1)
            {
               if(MathAbs(ObjectGet("SL",OBJPROP_PRICE1)-OrderStopLoss())>Point)
               OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(ObjectGet("SL",OBJPROP_PRICE1),Digits),OrderTakeProfit(),0);
            }
         }
         if(cmd==OP_BUYSTOP||cmd==OP_SELLSTOP)
         {
            o=1;
            Total++;
         }
      }
   }
   if(TotalBuy>0)otl=TotalBuy;
   if(TotalSell>0)otl=TotalSell;
   if((Total>0&&TotalBuy==0&&TotalSell==0))
   {
      CloseAllOrders();
      ObjectDelete("SL");
      return;
   }
   /*if(Ask>Level1&&Bid<Level1&&Close[0]>Open[0])
      Long=false;
   else
   {
      if(Ask>Level1&&Bid<Level1&&Close[0]<Open[0])
      Long=true;
   }*/
   /*if(((Ask>Level1&&Bid<Level1)||(Ask>Extremum&&Bid<Extremum))&&Close[0]<Open[0]&&Total<1)
   {
      //if(iVolume(Symbol(),0,0)>1)return;
      //if(DayOfWeek()==1||DayOfWeek()==5)return;
      res=OpenOrders(OP_BUY,NormalizeLots (Lot,MarketInfo(Symbol(),MODE_LOTSTEP)),Slippage,StopLost,0,MAGIC," BUY "," ");
   }
   else
   {
      if(((Ask>Level1&&Bid<Level1)||(Ask>Extremum&&Bid<Extremum))&&Close[0]>Open[0]&&Total<1)
      {
      //if(iVolume(Symbol(),PERIOD_D1,0)>1)return;
         //if(DayOfWeek()==1||DayOfWeek()==5)return;
         res=OpenOrders(OP_SELL,NormalizeLots (Lot,MarketInfo(Symbol(),MODE_LOTSTEP)),Slippage,StopLost,0,MAGIC," SELL "," ");
      }
   }
   if(TotalBuy>0&&Total<2)
   {
      OrderSelect(TotalBuy,SELECT_BY_TICKET,MODE_TRADES);
      SL=StopLost;
      TP=TakeProfit;
      if (SL<MarketInfo(Symbol(),MODE_STOPLEVEL)&&SL!=0)SL=MarketInfo(Symbol(),MODE_STOPLEVEL);
      if (TP<MarketInfo(Symbol(),MODE_STOPLEVEL)&&TP!=0)TP=MarketInfo(Symbol(),MODE_STOPLEVEL);
      double S1=NormalizeDouble(OrderStopLoss()+SL*Point,Digits);
      double P1=NormalizeDouble(OrderStopLoss()-TP*Point,Digits);
      if(!Long)P1=0;
      res=OrderSend(Symbol(),OP_SELLSTOP,NormalizeLots(OrderLots()*2,MarketInfo(Symbol(),MODE_LOTSTEP)),OrderStopLoss(),Slippage,0,0," ",MAGIC,0);
      OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES);
      OrderModify(OrderTicket(),OrderOpenPrice(),S1,P1,0);
      return;
   }
   if(TotalSell>0&&Total<2)
   {
      OrderSelect(TotalSell,SELECT_BY_TICKET,MODE_TRADES);
      SL=StopLost;
      TP=TakeProfit;
      if (SL<MarketInfo(Symbol(),MODE_STOPLEVEL)&&SL!=0)SL=MarketInfo(Symbol(),MODE_STOPLEVEL);
      if (TP<MarketInfo(Symbol(),MODE_STOPLEVEL)&&TP!=0)TP=MarketInfo(Symbol(),MODE_STOPLEVEL);
      double S=NormalizeDouble(OrderStopLoss()-SL*Point,Digits);
      double P=NormalizeDouble(OrderStopLoss()+TP*Point,Digits);
      if(Long)P=0;
      res=OrderSend(Symbol(),OP_BUYSTOP,NormalizeLots(OrderLots()*2,MarketInfo(Symbol(),MODE_LOTSTEP)),OrderStopLoss(),Slippage,0,0," ",MAGIC,0);
      OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES);
      OrderModify(OrderTicket(),OrderOpenPrice(),S,P,0);
   }*/
return(0);
}
//+------------------------------------------------------------------+
int OpenOrders(int cmd,double lot,int slip,double SL,double TP,int MG,string PP,string coment)
{
   double PriceSL;
   double PriceTP;
   int ticket=-1;
   RefreshRates();
   if (SL<MarketInfo(Symbol(),MODE_STOPLEVEL)&&SL!=0)SL=MarketInfo(Symbol(),MODE_STOPLEVEL);
   if (TP<MarketInfo(Symbol(),MODE_STOPLEVEL)&&TP!=0)TP=MarketInfo(Symbol(),MODE_STOPLEVEL);
   Print(coment);
   while(ticket<=0)
   {
      while (IsTradeContextBusy())
      {
          Sleep(100);
      }
      RefreshRates();
      if (cmd==OP_SELL)
         ticket=OrderSend(Symbol(),cmd,lot,NormalizeDouble(Bid,Digits),slip,0,0,coment,MG);
      if (cmd==OP_BUY)
         ticket=OrderSend(Symbol(),cmd,lot,NormalizeDouble(Ask,Digits),slip,0,0,coment,MG);
      if (ticket<=0)
      {
         int err=GetLastError();
         Print("Îøèáêà îòêðûòèÿ îðäåðà #",err,PP);
         if (err!=129||err!=135||err!=136||err!=138)return;
      }
      else
      {
         if (TP==0&&SL==0)return(ticket);
         if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==false)return(ticket);
         while (IsTradeContextBusy())
         {
            Sleep(100);
         }
         if (cmd==OP_SELL)
            TP=TP*(-1);
         if (cmd==OP_BUY)
            SL=SL*(-1);
         if (SL>0||SL<0)PriceSL=NormalizeDouble(OrderOpenPrice()+SL*Point,Digits);
         else PriceSL=0;
         if (TP>0||TP<0)PriceTP=NormalizeDouble(OrderOpenPrice()+TP*Point,Digits);
         else PriceTP=0;
         if (PriceTP==OrderTakeProfit()&&PriceSL==OrderStopLoss())
         {
            return(ticket);
         }
         else
         {
            bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),PriceSL,PriceTP,0);
            if (mod==false)
            {
               Print("Îøèáêà ìîäèôèêàöèè îðäåðà #",GetLastError(),PP);
            }
         }
      }
   }
return(ticket);
}
double NormalizeLots (double lots, double step) {
   return (MathRound(lots/step)*step);
}
void CloseAllOrders()
{
   for (int k=OrdersTotal()-1;k>=0;k--)
   {
      RefreshRates();
      if (OrderSelect(k,SELECT_BY_POS,MODE_TRADES)==true&&OrderMagicNumber()==MAGIC&&OrderSymbol()==Symbol())
      {
         if(OrderType()>1)
         {
            OrderDelete(OrderTicket());
         }
         if (OrderType()==OP_BUY)
         {
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slippage);
         }
         if (OrderType()==OP_SELL)
         {
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slippage);  
         }
      }
   }
}

Comments