Orders not placing with the correct conditions.

 

I was trying to build an EA by using Envelop indicator and EMA indicator. But when I run the code it some orders were placed with wrong conditions.

My Strategy is shown in the figure.


I assumed Period 1 of EMA is equal to line chart.

SELL ORDER

- When downward  line chart cross with lower envelop place sell order. No SL and TP.

-This sell order is profitable or not I want to close the order when line chart cross again with the lower envelop.

BUY ORDER

- When upward  line chart cross with upper envelop place buy order. No SL and TP.

-This buy order is profitable or not I want to close the order when line chart cross again with the upper envelop.


I add trailing stop as well.

Envelop settings

  • period- 10
  • Deviation - 0.1

EMA settings

  • period-  1


My code posted below. But it have some issues. I want to fix those issues. Please anyone can help me.

 
#property copyright "Copyright © 20107 Sonali"
#property link      "chathu.sonali19@gmail.com"
//--------------------------------------------------------------------
extern int     stoploss             = 0,
               takeprofit           = 500,
               risk                 = 10,
               Magic                = 1995;
extern bool    CloseCounter         = true;
extern double  Lot                  = 0.01;
extern int     TrailingStop         = 500;    
extern int     TipFrorCandl         = 1;     
extern int     Period_ENV=10;      // Period of Envelop
                                            
//--------------------------------------------------------------------
double SL,TP,LOT;
int TimeBar;
//--------------------------------------------------------------------
int start()
{
   if (TimeBar==Time[0]) return(0);
   double EMA1 = iMA(NULL,0,1,0,MODE_EMA, PRICE_CLOSE,0);
   double WMA1 = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,1);
   double ENVUP=iEnvelopes(NULL,0,Period_ENV,MODE_EMA,0,PRICE_CLOSE,0.1,MODE_UPPER,0);
   double ENVDO=iEnvelopes(NULL,0,Period_ENV,MODE_EMA,0,PRICE_CLOSE,0.1,MODE_LOWER,0);
   
   if ( ENVUP<EMA1 && WMA1<ENVUP)
   {
      TimeBar=Time[0];                            
      if (takeprofit!=0) TP  = Ask + takeprofit*Point;
      if (stoploss!=0)   SL  = Ask - stoploss*  Point;     
      LOT = LOT(risk,1);
      if (CloseCounter);
      openorder ("Buy");
   }
   if ( EMA1<ENVDO && ENVDO<WMA1 )
   {
      TimeBar=Time[0];                            
      if (takeprofit!=0) TP = Bid - takeprofit*Point;
      if (stoploss!=0)   SL = Bid + stoploss*  Point;            
      LOT = LOT(risk,1);
      if (CloseCounter);
      openorder ("Sell");
   }
   TrailingStop();
   checkforclose();
return(0);
}
//--------------------------------------------------------------------
void checkforclose()
{
   
   double EMA1 = iMA(NULL,0,1,0,MODE_EMA, PRICE_CLOSE,0);
   double WMA1 = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,1);
   double ENVUP=iEnvelopes(NULL,0,Period_ENV,MODE_SMMA,0,PRICE_CLOSE,0.1,MODE_UPPER,0);
   double ENVDO=iEnvelopes(NULL,0,Period_ENV,MODE_SMMA,0,PRICE_CLOSE,0.1,MODE_LOWER,0);
   for (int i=0; i<OrdersTotal(); i++)
   {                                               
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if (OrderSymbol()!=Symbol()||Magic!=OrderMagicNumber()) continue;
      if (OrderType()==OP_BUY )
      {   
      if ( EMA1<ENVUP && ENVUP<WMA1 )  OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
      }
      if (OrderType()==OP_SELL  )
      {
      if ( ENVDO<EMA1 && WMA1<ENVDO)   OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
      }   
   }
}
//--------------------------------------------------------------------
void openorder(string ord)
{
   int error;
   
   if (ord=="Buy" ) error=OrderSend(Symbol(),OP_BUY, LOT,Ask,2,SL,TP,"EMA WMA RSI",Magic,3);
   if (ord=="Sell") error=OrderSend(Symbol(),OP_SELL,LOT,Bid,2,SL,TP,"EMA WMA RSI",Magic,3);
   if (error==-1)   ShowERROR(error,0,0);
return;
}                  
//--------------------------------------------------------------------
void ShowERROR(int Ticket,double SL,double TP)
{
   int err=GetLastError();
   switch ( err )
   {                  
      case 1:                                                                               return;
      case 2:   Alert("Íåò ñâÿçè ñ òîðãîâûì ñåðâåðîì   "              ,Ticket," ",Symbol());return;
      case 130: Alert("Error áëèçêèå ñòîïû   Ticket ",                 Ticket," ",Symbol());return;
      case 134: Alert("Íåäîñòàòî÷íî äåíåã   ",                         Ticket," ",Symbol());return;
      case 146: Alert("Error Ïîäñèñòåìà òîðãîâëè çàíÿòà ",             Ticket," ",Symbol());return;
      case 129: Alert("Error Íåïðàâèëüíàÿ öåíà ",                      Ticket," ",Symbol());return;
      case 131: Alert("Error Íåïðàâèëüíûé îáúåì ",                     Ticket," ",Symbol());return;
      default:  Alert("Error  " ,err,"   Ticket ",                     Ticket," ",Symbol());return;
   }
}
//--------------------------------------------------------------------
double LOT(int risk,int ord)
{
   if (Lot!=0) return(Lot);
   double MINLOT = MarketInfo(Symbol(),MODE_MINLOT);
   LOT = AccountFreeMargin()*risk/100/MarketInfo(Symbol(),MODE_MARGINREQUIRED)/ord;
   if (LOT>MarketInfo(Symbol(),MODE_MAXLOT)) LOT = MarketInfo(Symbol(),MODE_MAXLOT);
   if (LOT<MINLOT) LOT = MINLOT;
   if (MINLOT<0.1) LOT = NormalizeDouble(LOT,2); else LOT = NormalizeDouble(LOT,1);
   return(LOT);
}
//--------------------------------------------------------------------
void TrailingStop()
{
   int tip,Ticket;
   bool error;
   double StLo,OSL,OOP;
   for (int i=0; i<OrdersTotal(); i++) 
   {  if (OrderSelect(i, SELECT_BY_POS)==true)
      {  tip = OrderType();
         if (tip<2 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
         {
            OSL   = OrderStopLoss();
            OOP   = OrderOpenPrice();
            Ticket = OrderTicket();
            if (tip==OP_BUY)             
            {
               StLo = SlLastBar(1,Bid,TipFrorCandl,TrailingStop);        
               if (StLo <= OOP) continue;
               if (StLo > OSL)
               {  error=OrderModify(Ticket,OOP,NormalizeDouble(StLo,Digits),
                  OrderTakeProfit(),0,White);
                  Comment("TrailingStop ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
                  Sleep(500);
                  if (!error) Comment("Error order ",Ticket," TrailingStop ",
                              GetLastError(),"   ",Symbol(),"   SL ",StLo);
               }
            }                                         
            if (tip==OP_SELL)        
            {
               StLo = SlLastBar(-1,Ask,TipFrorCandl,TrailingStop);  
               if (StLo==0) continue;        
               if (StLo >= OOP) continue;
               if (StLo < OSL || OSL==0 )
               {  error=OrderModify(Ticket,OOP,NormalizeDouble(StLo,Digits),
                  OrderTakeProfit(),0,White);
                  Comment("TrailingStop "+Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
                  Sleep(500);
                  if (!error) Comment("Error order ",Ticket," TrailingStop ",
                              GetLastError(),"   ",Symbol(),"   SL ",StLo);
               }
            } 
         }
      }
   }
}
//--------------------------------------------------------------------
double SlLastBar(int tip,double price, int tipFr, int tral)
{
   double fr;
   int jj,ii,delta=5;
   if (tral!=0)
   {
      if (tip==1) fr = Bid - tral*Point;  
      else fr = Ask + tral*Point;  
   }
   else
   {
      if (tipFr==0)
      {
         if (tip== 1)
         for (ii=1; ii<100; ii++) 
         {
            fr = iFractals(NULL,0,MODE_LOWER,ii);
            if (fr!=0) if (price-delta*Point > fr) break;
            else fr=0;
         }
         if (tip==-1)
         for (jj=1; jj<100; jj++) 
         {
            fr = iFractals(NULL,0,MODE_UPPER,jj);
            if (fr!=0) if (price+delta*Point < fr) break;
            else fr=0;
         }
      }
      else
      {
         if (tip== 1)
         for (ii=1; ii<100; ii++) 
         {
            fr = iLow(NULL,0,ii);
            if (fr!=0) if (price-delta*Point > fr) break;
            else fr=0;
         }
         if (tip==-1)
         for (jj=1; jj<100; jj++) 
         {
            fr = iHigh(NULL,0,jj);
            if (price+delta*Point < fr) break;
            else fr=0;
         }
      }
   }

   return(fr);
}
I'm not a good coder.
Thank you.
Files:
GBPUSD1.PNG  16 kb