Need Help with my Grid Code

 

Hallo fellow community members,

I initially wrote this message in a genera section and was advised  to write it here. I also gave my code in sections that could not compile. So here my message again. 

I am trying to put up my very first EA but am kind of stuck with a certain part of the code. I want to have eight price levels at which I will place trades, more or less like a grid. Normally this would be done using pending orders but this option will complicate the code for a learner like me because the EA will have to keep finding and deleting pending orders which have been rendered irrelevant for the cycle. I therefore prefer a code that will wait until price reaches that point then places a market order. Here is what I have written so far. 

 I will place an order using my entry criteria - if the criteria signal a buy, I will place a buy market order. If the signal is for sell, I will place a sell market order. I have not included the criteria in the code because it is still a concept in my head and not part of the current problem. The list of errors in the ErrorDescription() function is also not exhaustive because that would make the code unnecessarily long.

This is the part I have a problem coding because what I have so far doesn't seem to work: Once I have a trade running, there will be eight different price levels, ich of which is "D" PIPs from the next (D is defined in the code). Trades will be entered at those levels depending on how the market behaves. If my trade is a buy, then its OrderOpenPrice() will be the 5th level, such that there will be  three levels above it and four below. The immediate lower level hedges it to stop further loss. If it is a sell, then the OrderOpenPrice() will be the 4th level such that there will be three below and 4 above, the immediate upper one being a hedge as well. Then I will have a code that says if price reaches level x, place a buy trade or a sell trade (depending on whether it is above or below the OrderOpenPrice()) Say, for instance that I want to enter hedge (buy and sell) at level 6 and only a sell at level 7. I have made an attempt using the following code but it doesn't seem to be working as I wish. 


   extern int     EA_MagicNumber =     5;
   extern int     EA_StandardGrid=    15;
   extern int     EA_MaximumSlip =     5;
   extern bool    EA_ManageMoney = false;
   extern double  EA_DefaultLots =  0.04;
   int    TheOrderType=OrderType();
   string TheChartSymbol=Symbol();
   double M_Ask=MarketInfo(Symbol(),MODE_ASK);
   double M_Bid=MarketInfo(Symbol(),MODE_BID);
   double D;
   static double U;
   static double L;
   double AlternativeD;
   static double Lots;

   void OnTick()
   {
/* -----------------------------------------------------------------------------------------------------------------------*/
      Lots=EA_DefaultLots; D=PIPstoDecimal(EA_StandardGrid); 
      

      
/* -----------------------------------------------------------------------------------------------------------------------*/
      if(MarketOrdersontheChart()==1)  {
      
      
      RefreshRates();
      if(Ask>=U+D)   {SendanOrder(OP_SELL,Lots,0,0);}
      if(Ask>=U+D*2) {SendanOrder(OP_SELL,Lots,0,U-D*3);}
      if(Bid<=L-D)   {SendanOrder(OP_BUY, Lots,0,0);}
      if(Bid<=L-D*2) {SendanOrder(OP_BUY, Lots,0,U+D*3);}
      }
/* -----------------------------------------------------------------------------------------------------------------------*/      
   }
   /* 
   Protocol to Account for Orders                                                                                         
   -----------------------------------------------------------------------------------------------------------------------*/
   int MarketOrdersontheChart()
   {
      int OrderIndex;
      int CountofOrdersontheChart=0;
      
      for(OrderIndex=OrdersTotal()-1; OrderIndex>=0; OrderIndex--)
        {
          if(!OrderSelect(OrderIndex,SELECT_BY_POS,MODE_TRADES)) continue;
          if(OrderSymbol()!=TheChartSymbol || OrderMagicNumber()!=EA_MagicNumber) continue;
          if(TheOrderType==OP_BUY || TheOrderType==OP_SELL)
          {CountofOrdersontheChart++;}
        }
      return(CountofOrdersontheChart); 
   } 
   /* 
   Protocol to Send an Order to Broker Server                                                                             
   -----------------------------------------------------------------------------------------------------------------------*/
   bool SendanOrder(int O_Type,double O_Lots,double O_SL,double O_TP)
   {
     double O_Price;
     switch(TheOrderType)
       {
            case  OP_BUY : O_Price=M_Ask; break;
            case  OP_SELL: O_Price=M_Bid; break;
         default         : Alert(Symbol()+   ":Unexpected Order Type Encountered. Check Code"); break;
       }
     double O_Ticket=OrderSend(Symbol(),O_Type,O_Lots,O_Price,EA_MaximumSlip,O_SL,O_TP,NULL,EA_MagicNumber,0,clrNONE);
     
     if(O_Ticket==-1) 
     {Alert(Symbol()+   ":Error Sending Market Order",ErrorDescription(GetLastError())); return(false);}
     
     return(true);
   }
   /* 
   Protocol to Initiate Loss Recovery Mode                                                                                
   -----------------------------------------------------------------------------------------------------------------------*/
    bool RecoveryModeInitiated()
    {
     int IndexofLoneOrder;
     D=PIPstoDecimal(EA_StandardGrid); 
     AlternativeD=PIPstoDecimal(MarketInfo(Symbol(),MODE_STOPLEVEL));
     if(D<AlternativeD) {D=AlternativeD;}
     
     for(IndexofLoneOrder=OrdersTotal()-1; IndexofLoneOrder>=0; IndexofLoneOrder--)
     {if(OrderSelect(IndexofLoneOrder,SELECT_BY_POS,MODE_TRADES)==true)   {
     if(OrderSymbol()==TheChartSymbol && OrderMagicNumber()==EA_MagicNumber)  
       {   Lots=EA_DefaultLots;
     
         if(OrderType()==OP_BUY)  
            {U=OrderOpenPrice(); L=M_Bid;
            if(OrderModify(OrderTicket(),U,L-D*3,U+D*3,0,clrNONE))
            {if(SendanOrder(OP_SELL,Lots,U+D*3,L-D*3)) {return(true);}}
            else Alert(Symbol()+   ":Lone Buy Order NOT Modified",ErrorDescription(GetLastError())); return (false);}
               
            else
      
         if(OrderType()==OP_SELL)
            {U=M_Ask; L=OrderOpenPrice();
            if(OrderModify(OrderTicket(),L,U+D*3,L-D*3,0,clrNONE))
            {if(SendanOrder(OP_BUY,Lots,L-D*3,U+D*3))  {return(true);}}
            else Alert(Symbol()+  ":Lone Sell Order NOT Modified",ErrorDescription(GetLastError()));  return(false);}
       
       }}} return(true);  
    }  
   
   
   /* 
   Protocol to Convert PIPs to Decimals
   This section of code translates user defined PIPs to a price value
   -----------------------------------------------------------------------------------------------------------------------*/
   double PIPstoDecimal(double PIPs)
   {
      double PricePoints=SymbolInfoDouble(Symbol(),SYMBOL_POINT);
      if(PricePoints==0.0001    || PricePoints==0.00001) {return PIPs*0.0001;}
      else if(PricePoints==0.01 || PricePoints==0.001)   {return PIPs*0.01;}
      else{return 0;}
   }  
  
  
   /* 
   Error Description Protocol                                                                                             
   -----------------------------------------------------------------------------------------------------------------------*/

   string ErrorDescription(int error_code)
   {
         string error_string;
   
         switch(error_code)
         {

   /*          
   ERRORS RETURNED FROM BROKER SERVER                                                                            */
            case 0:   error_string="no error";                                                   break;
            case 1:   error_string="no error, trade conditions not changed";                     break;
            case 2:   error_string="common error";                                               break;
            
            default:   error_string="unknown error";
         }
     
//---
         return(error_string);
   }
Once again, I will highly appreciate any pointers as to what I am doing wrong and how I can get on the right path.
 

OmondiLuoDollar:

I initially wrote this message in a genera section and was advised  to write it here. I also gave my code in sections that could not compile. So here my message again. 

   int    TheOrderType=OrderType();
   string TheChartSymbol=Symbol();
   double M_Ask=MarketInfo(Symbol(),MODE_ASK);
   double M_Bid=MarketInfo(Symbol(),MODE_BID);

I have made an attempt using the following code but it doesn't seem to be working as I wish.

  1. Your original post was moved. Don't double post.
  2. You can not use any Trade Functions unless you select an order first. That variable is not being updated. Move it in inside OnTick.
  3. That variable is not being updated. Move it in inside OnTick.
  4. That variable is not being updated. Move it in inside OnTick.
  5.       if(Ask>=U+D)   {SendanOrder(OP_SELL,Lots,0,0);}
    
    D and U have random values, your code is meaningless.
  6. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here. Print out your variables, and find out why.
Reason: