trade manager ea

 

Hi guys,

I know there are tons of these sorts of EA's out there but I wanted to code one to learn how to do it.

I have managed to put it together with very "childish" coding logic and skill. but it seems to work perfectly with the Sell orders but will not work at all on BUY orders...

can anyone help me understand why it closes the SELL orders perfectly according to the levels but as soon as I enter a BUY order it closes it immediately.

this confuses me as the logic for the BUY orders is the same as for the SELL (just in reverse) orders, so it should work exactly the same.

I would appreciate any assistance

thanks guys

//+------------------------------------------------------------------+
//|                                                TRADE MANAGER.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

extern double close_lot = 0.2;
int cnt,total;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
total = OrdersTotal();
   double stop1    = ObjectGet("stop1",OBJPROP_PRICE1);
   double stop2    = ObjectGet("stop2",OBJPROP_PRICE1);
   
   double target1  = ObjectGet("target1",OBJPROP_PRICE1);
   double target2  = ObjectGet("target2",OBJPROP_PRICE1);
   
   int win_idx =0; 
   
   if(ObjectFind("stop1")==win_idx){ // Open function
   ////////////////////////////////////////////////////////////////////////////////////////////
    
      ObjectSet   ("stop1"   ,OBJPROP_STYLE,STYLE_DASH);
      ObjectSet   ("stop1"   ,OBJPROP_COLOR,LightCoral);
      ObjectSet   ("stop1"   ,OBJPROP_WIDTH,1);
                                   }
   
   if(ObjectFind("stop2")==win_idx){ // Open function
   ////////////////////////////////////////////////////////////////////////////////////////////
    
      ObjectSet   ("stop2"   ,OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet   ("stop2"   ,OBJPROP_COLOR,Red);
      ObjectSet   ("stop2"   ,OBJPROP_WIDTH,2);
                                   }

  if(ObjectFind("target1")==win_idx){ // Open function
   ////////////////////////////////////////////////////////////////////////////////////////////
    
      ObjectSet   ("target1"   ,OBJPROP_STYLE,STYLE_DASH);
      ObjectSet   ("target1"   ,OBJPROP_COLOR,MediumSeaGreen);
      ObjectSet   ("target1"   ,OBJPROP_WIDTH,1);
                                   }
   
   if(ObjectFind("target2")==win_idx){ // Open function
   ////////////////////////////////////////////////////////////////////////////////////////////
    
      ObjectSet   ("target2"   ,OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet   ("target2"   ,OBJPROP_COLOR,Green);
      ObjectSet   ("target2"   ,OBJPROP_WIDTH,2);
                                   }
                                   
                                   
//----CLOSE TRADES
 if(ObjectFind("stop1")==win_idx){
      for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           { 
         if(Bid<=stop1)  {close_longs_stop1();}
      //   if(Bid<=stop2)  {close_longs_stop2();} 
      //   if(Bid>=target1){close_longs_target1();}
      //   if(Bid>=target2){close_longs_target2();}     
           }
         else // go to short position
           {
         if(Ask>=stop1)  {close_shorts_stop1();}
     //    if(Ask>=stop2)  {close_shorts_stop2();}   
     //    if(Ask<=target1){close_shorts_target1();}
     //    if(Ask<=target2){close_shorts_target2();}   
           }
        }
     }
 }
 
 if(ObjectFind("stop2")==win_idx){
      for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           { 
       //  if(Bid<=stop1)  {close_longs_stop1();}
        if(Bid<=stop2)  {close_longs_stop2();} 
      //   if(Bid>=target1){close_longs_target1();}
      //   if(Bid>=target2){close_longs_target2();}     
           }
         else // go to short position
           {
      //   if(Ask>=stop1)  {close_shorts_stop1();}
        if(Ask>=stop2)  {close_shorts_stop2();}   
     //    if(Ask<=target1){close_shorts_target1();}
     //    if(Ask<=target2){close_shorts_target2();}   
           }
        }
     }
 }
 
 if(ObjectFind("target1")==win_idx){
      for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           { 
       //  if(Bid<=stop1)  {close_longs_stop1();}
      //   if(Bid<=stop2)  {close_longs_stop2();} 
         if(Bid>=target1){close_longs_target1();}
      //   if(Bid>=target2){close_longs_target2();}     
           }
         else // go to short position
           {
      //   if(Ask>=stop1)  {close_shorts_stop1();}
     //    if(Ask>=stop2)  {close_shorts_stop2();}   
         if(Ask<=target1){close_shorts_target1();}
     //    if(Ask<=target2){close_shorts_target2();}   
           }
        }
     }
 }
 
 if(ObjectFind("target2")==win_idx){
      for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           { 
       //  if(Bid<=stop1)  {close_longs_stop1();}
      //   if(Bid<=stop2)  {close_longs_stop2();} 
      //   if(Bid>=target1){close_longs_target1();}
         if(Bid>=target2){close_longs_target2();}     
           }
         else // go to short position
           {
      //   if(Ask>=stop1)  {close_shorts_stop1();}
     //    if(Ask>=stop2)  {close_shorts_stop2();}   
     //    if(Ask<=target1){close_shorts_target1();}
        if(Ask<=target2){close_shorts_target2();}   
           }
        }
     }
 }
//----END 
   return(0);
  }
//+------------------------------------------------------------------+

void close_longs_target1()
{
/////////////////////////
OrderClose(OrderTicket(),close_lot,Bid,3,Violet); // close half position
                 return(0); // exit
///////////////////////////
}

void close_longs_target2()
{
/////////////////////////
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
///////////////////////////
}

void close_shorts_target1()
{
//////////////////
OrderClose(OrderTicket(),close_lot,Ask,3,Violet); // close half position
               return(0); // exit
/////////////////
}

void close_shorts_target2()
{
//////////////////
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
/////////////////
}

void close_longs_stop1()
{
/////////////////////////
OrderClose(OrderTicket(),close_lot,Bid,3,Violet); // close half position
                 return(0); // exit
///////////////////////////
}

void close_longs_stop2()
{
/////////////////////////
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
///////////////////////////
}

void close_shorts_stop1()
{
//////////////////
OrderClose(OrderTicket(),close_lot,Ask,3,Violet); // close half position
               return(0); // exit
/////////////////
}

void close_shorts_stop2()
{
//////////////////
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
/////////////////
}
 

The first obvious thing, your loops . . .

for(cnt=0;cnt<total;cnt++)

. . . if you are closing orders within these loops you MUST count down not up.

How close are you stop and TP ?

 

thanx Raptor Ill adjust that.

the SL and TP are further than 10 pips as a standard

 
mqlearner:

thanx Raptor Ill adjust that.

the SL and TP are further than 10 pips as a standard

10 pips or 10 points ? are you taking into account your Broker being 5 digits ? is you Broker 5 digits ?
 

sorry i didnt make myself clear...i am using in excess of 10 pips on a 5 digit broker.

i draw the lines approx 10-15 pips for target 1 and 20-30 pips for target 2

same R:R for the SL's

 

the strange thing is it closes the sell orders perfectly according to the target1&2 and stop1&2 levels...

works no problem... but wont do the same for buy orders... it just closes them the moment i open the buy order

 
mqlearner:

the strange thing is it closes the sell orders perfectly according to the target1&2 and stop1&2 levels...

works no problem... but wont do the same for buy orders... it just closes them the moment i open the buy order

Did you re-arrange the objects on the screen? For sell positions, Stop2 has to be the greatest, then Stop1, then current price, and targets under it. For buy positions, Target2 has to be the greatest, then Target1, then current price, and stops under it. You should check it inside the EA before run the closing, and alert the user that EA has been stopped (or, much better, re-arrange / create levels automatically by some smart way, when a new position opened).
 

ill try that thank you Erzo. I hope it fixes the issue.

strange thing is when i changed the ++ to -- in the for loop...now it wont do anything at all.

maybe the fact that it closed sell orders was a fluke ...im going to work thru the code again and see if i can fix it with all your suggestions.

thanx guys

 
mqlearner:

ill try that thank you Erzo. I hope it fixes the issue.

strange thing is when i changed the ++ to -- in the for loop...now it wont do anything at all.

You need to do a little more than that . . .

for(cnt=0;cnt<total;cnt++)

to  


for(cnt=total-1; cnt>=0; cnt--)
 

thank you, i was just searching thru one of my MQL tutorials looking for just that...i remembered there was something else to do in the for loop, couldnt remember exactly what is was tho

much appreciated

Reason: