Download MetaTrader 5
To add comments, please log in or register
Unable to find the right application? Order it!
richy
14
richy 2015.04.27 01:35 

Hi everyone,

Just trying to fix a problem that I'm having with an EA of my friends'.

The EA has a what he calls a grid function, the way it functions is after an initial order is opened, the grid function (if turned on) will open a new order x pips from the original order which also remains open. Then the first Grid order will close before the 2nd grid order opens if x pips is reached so on.

Most the of the time everything work perfectly but we have noticed that if the Autotrading is left on over the weekend sometimes on Monday it will open random grid entries.

I believe the problem may have something to do with the use of ordercomment() used to determine if grids are open or not in the check history functions. From what I've read it can be changed which might be the issue. 

I'll post the relevant sections of code if anyone has any thoughts on a way around using ordercomment() let me know.

Cheers

Richy

//+------------------------------------------------------------------+
//| FUNCTIONS: Open grid trades                                      |
//+------------------------------------------------------------------+
void check_open_grid() 
  {
//----
 
   int index=0;
  
   
   if (stage=="BUY Cycle" && Ask>pprice)
     {
      double rindex=((Ask-pprice)/pnt)/GridPips;
      index=MathFloor(rindex);
      
      if (!found(index) && !foundhis(index) && index>0 && (index<=MaxGridOrders || MaxGridOrders==0)) opentrade(0,index,"G"); 
      else if (GridReEntry && !found(index+1) && foundhis(index+1) && index>=0 && (index<MaxGridOrders || MaxGridOrders==0)) opentrade(0,index+1,"G");
     }
   else if (stage=="SELL Cycle" && Bid<pprice)
     {
      rindex=((pprice-Bid)/pnt)/GridPips;
      index=MathFloor(rindex);
      if (!found(index) && !foundhis(index) && index>0 && (index<=MaxGridOrders || MaxGridOrders==0)) opentrade(1,index,"G");
      else if (GridReEntry && !found(index+1) && foundhis(index+1) && index>=0 && (index<MaxGridOrders || MaxGridOrders==0)) opentrade(1,index+1,"G");
     } 

//+------------------------------------------------------------------+
//| FUNCTIONS: Order excution                                        |
//+------------------------------------------------------------------+
void opentrade(int ordertype, int index, string type) 
  {
//----
   string typestring; 
   double price, lt, takeprofit, stoploss;
   color  clr=Lime;
   if (ordertype==1 || ordertype==3 || ordertype==5) clr=Red;


//----Check trading period

if(tframe!=Period())
   {return(0);}
   
//----Set OrderType
   
   if (ordertype==0) 
     {
      price=Ask;
      typestring=type+"_BUY ";
     }
   else if (ordertype==1) 
     {
      price=Bid;
      typestring=type+"_SELL ";   
     }
     
//----Define Lotsize for OrderType     
     
   if (type=="A") lt=(FirstLot*add.ltpercent/100);
   if (type=="P") lt=(FirstLot);
   if (type=="R") lt=(FirstLot);
   if (type=="G") lt=(GridLot);

//----
  
   if (lt>0 || UseMM==true) 
     {
      int ticket=OrderSend(Symbol(),ordertype,lt,price,Slippage,0,0,string(index),MagicNumber,0,clr);
      if(ticket>0 && OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
        {
         count++;
         if (GridTrading) infosend(typestring+" order ("+OrderTicket()+") opened: "+" @"+DoubleToStr(OrderOpenPrice(),dig)+", grid index: "+string(index));
         else infosend(typestring+" order ("+OrderTicket()+") opened: "+" @"+DoubleToStr(OrderOpenPrice(),dig));
         if (type=="P" && index==0)
           {
            if (ordertype==0) stage="BUY Cycle";
            else stage="SELL Cycle"; 
            cycletime=OrderOpenTime();
            pprice=OrderOpenPrice();
            invalidate_openlines(ordertype);
           }
         else if (type=="R" && index==0) {sig="Entered"; pprice=OrderOpenPrice();}
         else if (type=="A") 
           {
            if (ChangeStyle)
              {  
               ObjectSetInteger(0,add.name,OBJPROP_COLOR,INVALID_COLOR);
               ObjectSetInteger(0,add.name,OBJPROP_WIDTH,1);
              }
            ObjectSetString(0,add.name,OBJPROP_TEXT,"");
           }
         //----
         else if (type=="G" && TakeProfit && !HideTakeProfit)
           {  
            if (ordertype==0) takeprofit=OrderOpenPrice()+TakeProfitPips*pnt;
            else takeprofit=OrderOpenPrice()-TakeProfitPips*pnt;
            for (int j=10; j>0; j--) 
              {
               if (OrderModify(ticket,OrderOpenPrice(),0,takeprofit,0,CLR_NONE)) break;
               else Sleep(1000);  
               RefreshRates();
              } 
           }
         //----
         if (InitialStopLoss>0 || InitialTakeProfit>0)
           {
            stoploss=0;
            takeprofit=0;
            if (ordertype==0) 
              {
               if (InitialStopLoss>0) stoploss=OrderOpenPrice()-InitialStopLoss*pnt;
               if (InitialTakeProfit>0) takeprofit=OrderOpenPrice()+InitialTakeProfit*pnt;
              }
            else if (ordertype==1) 
              {
               if (InitialStopLoss>0) stoploss=OrderOpenPrice()+InitialStopLoss*pnt;
               if (InitialTakeProfit>0) takeprofit=OrderOpenPrice()-InitialTakeProfit*pnt;
              }
            if (takeprofit!=0 || stoploss!=0) 
              {     
               for (j=10; j>0; j--) 
                 {
                  if (OrderModify(ticket,OrderOpenPrice(),stoploss,takeprofit,0,CLR_NONE)) break;
                  else Sleep(1000);  
                  RefreshRates();
                 }
              }            
           }
        }
      else Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Error opening order : ",ErrorDescription(GetLastError())); 
     } 
  }    
   
  }
   
//******************************************************************************************************
//****************************************************************************************************** 
//+------------------------------------------------------------------+
//| FUNCTIONS: Check history                                         |
//+------------------------------------------------------------------+  
bool found(int index) 
  {
//----
   bool found=false;
   for (int i=OrdersTotal()-1; i>=0; i--) 
     {
      if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if (OrderSymbol()!=Symbol()) continue;
      if (OrderMagicNumber()!=MagicNumber) continue;
      if (OrderOpenTime()<ptime) continue;
      if (StringToInteger(OrderComment())==index) found=true;
      if (found) break;
     }
//----
   return (found);   
  } 
//+------------------------------------------------------------------+
//| FUNCTIONS: Check history                                         |
//+------------------------------------------------------------------+  
bool foundhis(int index) 
  {
//----
   bool found=false;
   for (int i=OrdersHistoryTotal()-1; i>=0; i--) 
     {
      if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if (OrderSymbol()!=Symbol()) continue;
      if (OrderMagicNumber()!=MagicNumber) continue;
      if (OrderOpenTime()<ptime) continue;
      if (StringToInteger(OrderComment())==index) found=true;
      if (found) break;
     }
//----
   return (found);   
  }  
whroeder1
13641
whroeder1 2015.04.27 13:18  
richy562: I believe the problem may have something to do with the use of ordercomment()
  1. But it's Not a good idea, brokers can change comments, including complete replacement. Instead I'd use a range of magic numbers.
  2. Since you don't show all the code there is nothing we help with. Put print statements (including variable values) before and inside your if's and track it down.
  3.    if (stage=="BUY Cycle" && Ask>pprice)
       else if (stage=="SELL Cycle" && Bid<pprice)
    Don't use strings when you mean enumerations:
    enum Cycles {BUY_CYCLE, SELL_CYCLE};
    Cycles stage = BUY_CYCLE;
    :
       if (stage==BUY_CYCLE && Ask>pprice)
    
  4.          if (type=="P" && index==0)
             else if (type=="R" && index==0) {sig="Entered"; pprice=OrderOpenPrice();}
             else if (type=="A") 
             else if (type=="G" 
    Likewise P, R, A, G are meaningless. If you can't speak you code aloud and have it make understandable English, change it.
  5.    if (ordertype==1       || ordertype==3            || ordertype==5          ) clr=Red;
       if (ordertype==OP_SELL || ordertype==OP_SELLLIMIT || ordertype==OP_SELLSTOP) clr=Red;
       if (ordertype % 2 ==OP_SELL                                                ) clr=Red; // Sell(market or pending)
    
    Don't hard code constants
/
To add comments, please log in or register