[Coding Issues] Changed broker, EA stopped closing positions

 

Hiya all,

So I had made this -for me - really handy EA. If you load it, you can chose from the four pending order types, and three lines are drawed on the screen: the Triggerline, TP and SL. As soon as the price gets triggered the order opens; the TP and SL can be changed realtime: The TP and SL-levels are determined from the height of the line (where they are on the y-axis).

Anyhow, I just switched brokers, but now my EA does not close orders anymore. The only difference with this broker, is that each symbol is followed by an 'm'. So EURUSDm instead of EURUSD, and USDJPYm instead of USdJPY, but this should't make a difference if I am correct. I do not know what it is, but maybe anyone of you is willing to help me. If so, I'd be really grateful! 

 

//+------------------------------------------------------------------+
//|                                                         TPSL.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#include <stdlib.mqh>

extern double Lots=0.10;
extern int TakeProfit=75;
extern int StopLoss=75;
extern int Order1=1;
extern string Order1Order2="BuyStop--SellLimit";
extern string Order3Order4="BuyLimit--SellStop";

// Non-external variables
bool StopEA=false,RemoveObjects=false;
int Slippage=30;
double OpenPriceLevel,StopLossLevel,TakeProfitLevel;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {  
 if(Order1==1 || Order1==3)
  {
// ---Open_Price_Level
        OpenPriceLevel = Bid;
        if ( OpenPriceLevel <= 0 )
                { OpenPriceLevel = Bid + MarketInfo( Symbol(), MODE_STOPLEVEL )*Point; }
// ---Stop_Loss_Level
        StopLossLevel = OpenPriceLevel - StopLoss * Point;
// ---Take_Profit_Level
        TakeProfitLevel = OpenPriceLevel + TakeProfit * Point;
  }

if (Order1==2 || Order1==4 )
  {
// ---Open_Price_Level
        OpenPriceLevel = WindowPriceOnDropped();
        if ( OpenPriceLevel <= 0 )
                { OpenPriceLevel = Ask - MarketInfo( Symbol(), MODE_STOPLEVEL )*Point; }
// ---Stop_Loss_Level
        StopLossLevel = OpenPriceLevel + StopLoss * Point;
// ---Take_Profit_Level
        TakeProfitLevel = OpenPriceLevel - TakeProfit * Point; 
  }  
  
// Line creation:
        ObjectCreate( "OpenPriceLine", OBJ_HLINE, 0, 0, OpenPriceLevel, 0, 0, 0, 0 );
        ObjectSet( "OpenPriceLine", OBJPROP_COLOR, White );
        ObjectSetText( "OpenPriceLine", "OpenPriceLine", 6, "Arial", White );

        ObjectCreate( "StopLossLine", OBJ_HLINE, 0, 0, StopLossLevel, 0, 0, 0, 0 );
        ObjectSet( "StopLossLine", OBJPROP_COLOR, Red );
        ObjectSetText( "StopLossLine", "StopLossLine", 6, "Arial", Red );

        ObjectCreate( "TakeProfitLine", OBJ_HLINE, 0, 0, TakeProfitLevel, 0, 0, 0, 0 );
        ObjectSet( "TakeProfitLine", OBJPROP_COLOR, Lime );
        ObjectSetText( "TakeProfitLine", "TakeProfitLine", 6, "Arial", Lime );
        
if(Order1==1)
   {
   ObjectCreate( "Type", OBJ_LABEL, 0,0,0,0,0,0,0);
        ObjectSet( "Type", OBJPROP_CORNER, 1);
        ObjectSet( "Type", OBJPROP_XDISTANCE, 15);
        ObjectSet( "Type", OBJPROP_YDISTANCE, 20);
        ObjectSetText(  "Type", "Buy Stop", 14, "Arial", White);
   }
   
else if(Order1==2)
   {
   ObjectCreate( "Type", OBJ_LABEL, 0,0,0,0,0,0,0);
        ObjectSet( "Type", OBJPROP_CORNER, 1);
        ObjectSet( "Type", OBJPROP_XDISTANCE, 15);
        ObjectSet( "Type", OBJPROP_YDISTANCE, 20);
        ObjectSetText(  "Type", "Sell Limit", 14, "Arial", White);
   }
      
else if(Order1==3)
   {
   ObjectCreate( "Type", OBJ_LABEL, 0,0,0,0,0,0,0);
        ObjectSet( "Type", OBJPROP_CORNER, 1);
        ObjectSet( "Type", OBJPROP_XDISTANCE, 15);
        ObjectSet( "Type", OBJPROP_YDISTANCE, 20);
        ObjectSetText(  "Type", "Buy Limit", 14, "Arial", White);
   }
   
else if(Order1==4)
   {
   ObjectCreate( "Type", OBJ_LABEL, 0,0,0,0,0,0,0);
        ObjectSet( "Type", OBJPROP_CORNER, 1);
        ObjectSet( "Type", OBJPROP_XDISTANCE, 15);
        ObjectSet( "Type", OBJPROP_YDISTANCE, 20);
        ObjectSetText(  "Type", "Sell Stop", 14, "Arial", White);
   }

// Pop up window
        int  Answer = MessageBox( "For order placement drag the lines and press OK", "Pending Order placement", 0x00000001);
        if ( Answer != 1 )
            {
            StopEA=true;
            RemoveObjects=true;
            }  
                 
//End Init function
   return(0);
  }

int deinit()
  {
        ObjectDelete( "OpenPriceLine" );
        ObjectDelete( "StopLossLine" );
        ObjectDelete( "TakeProfitLine" );
        ObjectDelete( "Type" );
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  if(OrdersTotal()>0)
    OrderManagement();

 // Objects removed after order placal
  if(RemoveObjects==true)
    {
        ObjectDelete( "OpenPriceLine" );
        ObjectDelete( "StopLossLine" );
        ObjectDelete( "TakeProfitLine" );
        ObjectDelete( "Type" );         
    }
    
  if(StopEA==true)
    {
    return(0);
    } 
    
    Check();
     
  return(0);    
  }

//+-------------------------------------------------------------------+
//| Signal Check                                                      |
//+-------------------------------------------------------------------+
double Check()
   { 
   // Open_Price
        OpenPriceLevel = NormalizeDouble( ObjectGet( "OpenPriceLine", OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) );

     if(Order1==1) // Buy Stop
          {
           if(OpenPriceLevel<=Bid)
             {
             StopEA=true;
             OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",111,0,White);   
             ObjectDelete( "OpenPriceLine" ); 
             }
          }
          
     else if(Order1==2) // Sell Limit    
          {
           if(OpenPriceLevel<=Bid)
             {
             StopEA=true;
             OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",111,0,White);  
             ObjectDelete( "OpenPriceLine" );               
             }
          }    
                  
     else if(Order1==3) // Buy Limit    
          {
           if(OpenPriceLevel>=Bid)
             {
             StopEA=true;
             OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",111,0,White);   
             ObjectDelete( "OpenPriceLine" );              
             }
          }
     else if(Order1==4) // Sell Stop
          {
           if(OpenPriceLevel>=Bid)
             {
             StopEA=true;
             OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",111,0,White);  
             ObjectDelete( "OpenPriceLine" );                         
             }
          }
          

                     
   return(0);
   }


//+------------------------------------------------------------------+
//| Order Management Function                                        |
//+------------------------------------------------------------------+
double OrderManagement()
   {
//Make sure selected order is the correct one(to close)     
     for(int i = (OrdersTotal()-1); i >= 0; i --)        
     OrderSelect(i,SELECT_BY_POS);

// Stop_Loss
        StopLossLevel = NormalizeDouble( ObjectGet( "StopLossLine", OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) );
// Take_Profit
        TakeProfitLevel = NormalizeDouble( ObjectGet( "TakeProfitLine", OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) );

       if(OrderType()==0) // Type = Buy
           {       
            if(Bid>=TakeProfitLevel)
               {
               RemoveObjects=true;
               OrderClose(OrderTicket(),Lots,TakeProfitLevel,Slippage,Green);
               }
            
            else if(Bid<=StopLossLevel)
               {
               RemoveObjects=true;
               OrderClose(OrderTicket(),Lots,StopLossLevel,Slippage,Red);           
               }
           }   
           
     else if(OrderType()==1) // Type = Sell
           {           
            if(Bid<=TakeProfitLevel)
               {
                RemoveObjects=true;  
                OrderClose(OrderTicket(),Lots,TakeProfitLevel,Slippage,Green);
               }
               
            else if(Bid>=StopLossLevel)
               {
                RemoveObjects=true;
                OrderClose(OrderTicket(),Lots,StopLossLevel,Slippage,Red);           
               }
           }   
                       
      return(0);
   }
 
Maarten91: I just switched brokers, but now my EA does not close orders anymore. 
use What are Function return values ? How do I use them ? - MQL4 forum and find out why.
 
Cool, thanks for the quick reaction and I'll read myself into it!
 

So I added the last two lines (this is a snippet from the order management function)

       if(OrderType()==0) // Type = Buy
           {       
            if(Bid>=TakeProfitLevel)
               {
                RemoveObjects=true;
                OrderClose(OrderTicket(),Lots,TakeProfitLevel,Slippage,Green);   
                if(OrdersTotal()!=0)
                Alert("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
               }

Which resulted in a pop-up with error 129 (invalid price)

So I figured that I should've put 'Bid' or 'Ask' in the price-tab instead of 'TakeProfitLevel'

 

Thanks for pointing me in the right direction, but still letting me figure it out. I really learned some usefull stuff! 

 
Maarten91:

So I added the last two lines (this is a snippet from the order management function)

Which resulted in a pop-up with error 129 (invalid price)

So I figured that I should've put 'Bid' or 'Ask' in the price-tab instead of 'TakeProfitLevel'

 

Thanks for pointing me in the right direction, but still letting me figure it out. I really learned some usefull stuff! 

 


with closing trades you can use instead of Bid or Ask also OrderClosePrice()
 
Maarten91:

So I added the last two lines (this is a snippet from the order management function)

Which resulted in a pop-up with error 129 (invalid price)

So I figured that I should've put 'Bid' or 'Ask' in the price-tab instead of 'TakeProfitLevel'

 

Thanks for pointing me in the right direction, but still letting me figure it out. I really learned some usefull stuff! 

 


You need to read this,  actually read it and understand it,  it's obvious from your code change that you didn't,  you are still not checking the return value from the OrderClose()  why would you check for an error if the OrderClose() has worked ?

What are Function return values ? How do I use them ? 

Reason: