Download MetaTrader 5

HELP. cycle through orders to determine ten pip gap

To add comments, please log in or register
Subscribe to signals right in MetaTrader. It is easy!
diggity dogg
25
diggity dogg 2016.03.31 12:20 

Hi,

Ive made a simple EA that will open a buy/sell above or below a moving average. Once the initial trade is open I would like the EA to open additional orders 10 pips above the already open orders or ten pips below the already open orders. I have searched the internet and this forum for answers but I still need a little help as it just doesnt work how I want it to. The EA will compile with no errors but it will just open and close one trade at a time without placing any additional orders. I would like the EA to cycle through the open orders and place additional orders so there will possibly be more than one trade open at a time. There is either a problem with the open buy/sell rule or a problem with the cyle through open orders part of the code.. Im not sure where Ive gone wrong..

extern int MagicNumber=10001;
extern double Lots =0.01;
extern double StopLoss=20;
extern double TakeProfit=10;
extern int TrailingStop=0;
extern int Slippage=3;
//+------------------------------------------------------------------+

int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
 
 //-------------------------------------------------------------------------------------------------------------------------------------
  if(OrdersTotal()==0) 
  {
     int result=0;
     if((Open[0]>iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0))) 
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"DiggityEA",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((Open[0]<iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0))) 
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"DiggityEA",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
 
 //----------------------------------------------------------------------------------------------------------------------------------------------
  
  if( OrdersTotal()>0 )
  {
    
      for (int x=OrdersTotal()-1;x>0;x--)       
     {                                        
      OrderSelect(x,SELECT_BY_POS);
        {  if   (OrderOpenPrice()+(10*MyPoint)<Ask)                                 
            {
             result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"DiggityEA",MagicNumber,0,Blue);
             if(result>0)
             {
              TheStopLoss=0;
              TheTakeProfit=0;
              if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
              if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
              OrderSelect(result,SELECT_BY_TICKET);
              OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
             }
             return(0);
            }
            
         }
       }   
     
       
       
        for (int z=OrdersTotal()-1;z>0;z--)       
     {                                        
      OrderSelect(z,SELECT_BY_POS);
        {  if   (OrderOpenPrice()-(10*MyPoint)>Bid)                               
            {
             result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"DiggityEA",MagicNumber,0,Red);
             if(result>0)
             {
              TheStopLoss=0;
              TheTakeProfit=0;
              if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
              if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
              OrderSelect(result,SELECT_BY_TICKET);
              OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
             }
          return(0);
            }
           }   
          
       }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 



           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
  
  }
}


diggity dogg
25
diggity dogg 2016.03.31 12:33  
I plan on adding a hedging system to this EA so that the losing trades hedge until breakeven, this is why its necessary to have more orders opening while the losing trades are in recovery mode. I am well aware of the pitfalls of this strategy.... However I need to learn how to cycle through orders and write a rule to add additional orders so that I might be able to write a more sophisticated EA in the future.. Im just learning to code at the moment. I must be close to what I want to achieve,- Ive been studying other EA's that work and using them as a basis for this code but when i compile and put into strategy tester it just doesnt do what I want it to
To add comments, please log in or register