Having trouble second set of entry conditions

 

Hi All,

I would like to get some advice on an issue I'm currently having with coding the logic for my EA.

The EA is is to open a position based on a specified signal which it does. It is also to exit said position based on one of two exit signals. The problem is with a third exit signal. This third signal should close the position and immediately open the opposite position and is intended to correct false entries

Without the order reversals, and if all three exit conditions are checked simultaneously, each exit condition is capable of being called and closing the respective buy/close order (as determined by print messages). However for some reason when the EA is rewritten (as it is below) condition 3 no longer functions and the respective OrderSend is not called either. maybe there is an issue with the way the loop has been structured.

Below is EA body which I hope has been sufficiently annotated and I apologize in advance for any (likely) poor coding practices as I'm quite new to programming EAs. My area of expertise involves more organic programming/reprogramming procedures :S Any assistance with the above issue regarding the position reversal would be greatly appreciated. Thank you in advance

totalorders=OrdersTotal();
if (totalorders<1)
   {//BUY positions available?
    if(BOcondition1==true)
    {
    result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Ask+TakeProfit,"MAmacd_V2",MagicNumber,0,Green);
    if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) Print ("BUY_order_opened_under_CONDITION_1:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening BUY order : ",GetLastError());
       return(0); 
    }
    if(SCcondition3==true && closeSELLrev==true)  //condition 3 has closed a Sell order....I THINK THE ISSUE LIES IN THIS IF STATEMENT
    {
     result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Ask+TakeProfit,"MAmacd_V2",MagicNumber,0,Green);  //open a Buy order thus reversing a Sell order closed under condition 3
     if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) Print ("BUY_order_opened_under_CONDITION_2:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening BUY order : ",GetLastError());
       return(0); 
    }    
    
    
   
   //SELL positions available?
    if(SOcondition1==true)
    {
    result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,Bid-TakeProfit,"MAmacd_V2",MagicNumber,0,Green);
    if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) Print ("SELL_order_opened_under_CONDITION_1:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening SELL order : ",GetLastError());
       return(0); 
    }
    if(BCcondition3==true && closeBUYrev==true)  //condition 3 has closed a Buy order.....I THINK THE ISSUE LIES IN THIS IF STATEMENT
    {
    result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,Bid-TakeProfit,"MAmacd_V2",MagicNumber,0,Green); //open a Sell order thus reversing a Buy order closed under condition 3
    if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) Print ("SELL_order_opened_under_CONDITION_2:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening SELL order : ",GetLastError());
       return(0); 
    }  
   }   


//Exit strategies
   
for(cnt=OrdersTotal()-cnt; cnt>=0; 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 found
         {
            // Close BUY conditions-------
            if(BCcondition1==true || BCcondition2==true)
            {
              close=OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
              if (close==true && BCcondition1==true) //order closed under condition 1
               {   
                Print ("Condition_1_closed_BUY_order:_", OrderTicket());
               }
              if (close==true && BCcondition2==true)
               {
                Print ("Condition_2_closed_BUY_order:_", OrderTicket());  
               }
              return(0);
            }   
            if(BCcondition3==true)
               {
                closeBUYrev=OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position....THERE MAY ALSO BE AN ISSUE WITH THE VARIABLE FOR THE CLOSED ORDER
                if (closeBUYrev==true && BCcondition3==true) //order closed under condition 3
                                     {
                                      Print ("Condition_3_closed_BUY_order:_", OrderTicket());
                                     }
                               return(0);
                              } 
            
            // Check for trailing stop-----------
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
           else //go to SELL positions---------
           {
         // Close SELL conditions-------
            if(SCcondition1==true || SCcondition2==true)
            {
              close=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
              if (close==true && SCcondition1==true) //order closed under condition 1
               {   
                Print ("Condition_1_closed_SELL_order:_", OrderTicket());
               }
              if (close==true && SCcondition2==true)
               {
                Print ("Condition_2_closed_SELL_order:_", OrderTicket());  
               }
              return(0);
            }
            
            if(SCcondition3==true)
            {
              closeSELLrev=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position...THERE MAY ALSO BE AN ISSUE WITH THE VARIABLE FOR THE CLOSED ORDER
              if (closeSELLrev==true && SCcondition3==true) //order closed under condition 3
               {   
                Print ("Condition_3_closed_SELL_order:_", OrderTicket());
               }
              return(0);
            }
            
            // Check for trailing stop-----------
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if(OrderStopLoss()<Ask+Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
        }
   }
   return(0);


PS: I have gone with this method as order comments are both unreliable (as discussed numerous time on this forum) and cannot be edited after the order has been made (as this was a thought to identify orders closed under condition 3).

I had also tried doing this by setting bool variable for open/close (based of the "simple EA" example given in the mql4 book. However that effort was met with much less success than the above one.

Cheers

 

When you have comments etc in your code that makes the code very wide, it does not fit on a single page. This also makes your post very wide. This makes your post and code very hard to read because we have to keep scrolling left and right.

To be honest with you, if I have to keep scrolling left and right to read a post, I can't be bothered. I doubt that I am alone with this and it could be the reason that you have not had any replies.

It's not your fault, it is a forum failure, but for future reference, if a single line of code is too long, put it on 2 lines. Maybe more people will read your post :)

 
  1. Are your books one column but two feet wide? No because that would be unreadable. Most are six inches wide, some are two columns or more, so they are readable. Your code should be the same readable.
  2. closeSELLrev=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
    if (closeSELLrev==true && SCcondition3==true) //order closed under condition 3
    
    What are Function return values ? How do I use them ? - MQL4 forum
  3. You would never write IF( (2+2 == 4) == true ) would you? So don't write IF( boolean == true ). IF( boolean ) and IF( !boolean ) are sufficient and if you name your variables to be readable clearer: IF( isSell3 ) ...
 

Hi GumRai and WHRoeder,


Thank you for taking the time to reply to what is seems is an extremely poorly formatted post.

To make things a little clearer and so I can edit the post appropriately, can either of you please post a screen capture of what you see or re-post the longest line correctly formatted. The reason I ask is because I am not experiencing any difficulties in displaying the script on either my work or home PC. That said, both stations use wide-screen displays thus allowing much wider text to be displayed than the conventional 4:3 format monitor.

WHRoeder, I will try calling some function return values for the bool variables closeSELLrev and SCcondition3 later tonight when I get home as I''m unable to run MT4 on my work PC. As for bool satements, well..that makes perfect sense when you put it like that. Thank you. Scripting written in the manner described both cuts down clutter and makes things much easier to read.

If you (or anyone) else for that matter is wondering, there was a rather convoluted reasoning behind the naming of closeSELLrev (and its opposing variable; closeBUYrev). The variable was intended to store a value which would indicate that a SELL order was closed under condition 3 (SCcondition3). This variable would then be called to determine if the opposite order (a BUY) should be opened. This order 'reversal' under condition 3 is printed in the log as "....opened under condition 2".

Orders opened under condition 2 would then be closed under a fourth condition which has not been scripted in yet. I think once I resolve the posted issue I should be able to script the alternate close positions without too many problems.

Again, sorry for the noobieness of the posted format. I can remedy this once I know what people are dealing with at their end.


Cheers.

 

This is what I see on my screen

Often posts are too wide because of long //comments and this can be rectified by placing comments on the next line

     result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Ask+TakeProfit,"MAmacd_V2",MagicNumber,0,Green);
     //open a Buy order thus reversing a Sell order closed under condition 3
   
 
GumRai:

This is what I see on my screen

Often posts are too wide because of long //comments and this can be rectified by placing comments on the next line


Wow that is annoying. Sorry about that. Here is the ea (reformatted) to (hopefully) be displayed properly.


//Opening strategies----------------

totalorders=OrdersTotal();
if (totalorders<1)
   {//BUY positions available?
    if(opnBUY1)
    {
    result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Ask+TakeProfit,"MAmacd_V2",MagicNumber,0,Green);
    if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) 
       Print ("BUY_order_opened_under_CONDITION_1:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening BUY order : ",GetLastError());
       return(0); 
    }
    if(clsSELL3 && clsSELLforReversal>0)  
    //condition 3 has closed a Sell order
    {
     result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Ask+TakeProfit,"MAmacd_V2",MagicNumber,0,Green);  
     //open a Buy order thus reversing a Sell order closed under condition 3
     if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) 
       Print ("BUY_order_opened_under_CONDITION_2:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening BUY order : ",GetLastError());
       return(0); 
    }    
    
    
   
   //SELL positions available?
    if(opnSELL1)
    {
    result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,Bid-TakeProfit,"MAmacd_V2",MagicNumber,0,Green);
    if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) 
       Print ("SELL_order_opened_under_CONDITION_1:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening SELL order : ",GetLastError());
       return(0); 
    }
    if(clsBUY3 && clsBUYforReversal>0)  //condition 3 has closed a Buy order
    {
    result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,Bid-TakeProfit,"MAmacd_V2",MagicNumber,0,Green); 
    //open a Sell order thus reversing a Buy order closed under condition 3
    if (result>0)
      {
       if (OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) 
       Print ("SELL_order_opened_under_CONDITION_2:_#",OrderTicket(),"@",OrderOpenPrice());
      }
       else Print ("Error opening SELL order : ",GetLastError());
       return(0); 
    }  
   }   

//Exit strategies
   
for(cnt=OrdersTotal()-cnt; cnt>=0; 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 found
         {
            // Close BUY conditions-------
            if(clsBUY1 || clsBUY2)
            {
              close=OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
              if (close>0 && clsBUY1) //order closed under condition 1
               {   
                Print ("Condition_1_closed_BUY_order:_", OrderTicket());
               }
              if (close>0 && clsBUY2)
               {
                Print ("Condition_2_closed_BUY_order:_", OrderTicket());  
               }
            }   
            if(clsBUY3)
               {
                clsBUYforReversal=OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position due to false entry
                if (clsBUYforReversal>0 && clsBUY3) //order closed under condition 3
                                     {
                                      Print ("Condition_3_closed_BUY_order:_", OrderTicket());
                                     }
                                    if (clsBUYforReversal<0 && clsBUY3)
                                     {
                                      Print ("Error_closing_BUY_for_reversal:_",GetLastError());
                                     }  
                              } 
             return(0);
            // Check for trailing stop-----------
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
           else //go to SELL positions---------
           {
         // Close SELL conditions-------
            if(clsSELL1>0 || clsSELL2>0)
            {
              close=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
              if (close>0 && clsSELL1>0) //order closed under condition 1
               {   
                Print ("Condition_1_closed_SELL_order:_", OrderTicket());
               }
              if (close>0 && clsSELL2>0)
               {
                Print ("Condition_2_closed_SELL_order:_", OrderTicket());  
               }
            }
            
            if(clsSELL3)
            {
              clsSELLforReversal=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position due to false entry
              if (clsSELLforReversal>0 && clsSELL3>0) //order closed under condition 3
               {   
                Print ("Condition_3_closed_SELL_order:_", OrderTicket());
               }
              if (clsSELLforReversal<0 && clsSELL3)
                                   {
                                    Print ("Error_closing_SELL_for_reversal:_",GetLastError());
                                   }    
            }
            return(0);
            // Check for trailing stop-----------
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if(OrderStopLoss()<Ask+Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
        }
   }  
   return(0);
}           
 
WHRoeder:
  1. Are your books one column but two feet wide? No because that would be unreadable. Most are six inches wide, some are two columns or more, so they are readable. Your code should be the same readable.
  2. What are Function return values ? How do I use them ? - MQL4 forum
  3. You would never write IF( (2+2 == 4) == true ) would you? So don't write IF( boolean == true ). IF( boolean ) and IF( !boolean ) are sufficient and if you name your variables to be readable clearer: IF( isSell3 ) ...

Please see the changes I made to the script above to make it display better and hopefully easier to read (with renaming of some variables)

I had an attempt at using the return values though I'm none the wiser as to what is going on. Though I have the feeling I've missed the point of the return values.

 
SteepCurve: Though I have the feeling I've missed the point of the return values.
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
Obviously.
 
WHRoeder:
Obviously.


The trailing stops were taken from the MACD sample EA on MT4 and seem to operate normally. I couldn't see the point in utilising the return values generated from the OrderModify function in this instance.

From what I understand, which it seems is becoming less and less, if return(0); is replaced with return; this will exit the EA start function, not just the function preceeding it, ie OrderModify.

I have read the post on return values and a number of pages regarding function returns and still have problems getting my head around it.

That said, is the OrderModify return values really going to help in this situation. I have attempted to use return values in the areas of the EA which seemed to be causing the problems.

 
SteepCurve:
I have read the post on return values and a number of pages regarding function returns and still have problems getting my head around it.
That said, is the OrderModify return values really going to help in this situation.
  1. OrderModify returned WHAT? You have NO idea.
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,...
    return(0);
    What are Function return values ? How do I use them ? - MQL4 forum
    if( !OrderModify(...) ){ ...// OrderModify returned false
    return(0);
  2. You do NOT know wither you called orderModify or not. If you logged the error you would know, A) modify failed and WHY, or B) you didn't call it AT ALL because the stop hadn't moved and there was no error message so the problem is your conditional logic above.
 
WHRoeder:
  1. OrderModify returned WHAT? You have NO idea.
    What are Function return values ? How do I use them ? - MQL4 forum
  2. You do NOT know wither you called orderModify or not. If you logged the error you would know, A) modify failed and WHY, or B) you didn't call it AT ALL because the stop hadn't moved and there was no error message so the problem is your conditional logic above.


Hmm it seems that OrderModify is not being called as I couldn't get any error return message. So I guess that means there's an error with the structure of the logic preceeding the order modify commands.

I'm a bit miffed as to what it is though. I mean, the EA is a dead copy of one of the examples from mt4 which happily modified orders. I have added a second section of code for opening/closing buy/sell orders and was careful to not add any extra return functions which would lead to premature termination of the script.


PS: I have resolved the issue of 'reversing' orders though there is still no functioning OrderModify (funnily enough because it was built from this EA).

I would obviously still like to resolve this issue.

Cheers.

Reason: