SL and TP system - page 4

 

You must be getting the warning that the return value of OrderClose should be checked when compiling.

Do it.

 
Keith Watford:

You must be getting the warning that the return value of OrderClose should be checked when compiling.

Do it.

 bool result=true;
        result = OrderClose( OrderTicket(), LotsToTrade, MarketInfo(OrderSymbol(), MODE_BID), 0, clrAliceBlue);

Is this what you mean? I just tried this

 
Tiberious:

Is this what you mean? I just tried this

No.

OrderClose

Returned value

Returns true if successful, otherwise false. To get additional error information, one has to call the GetLastError() function.



Print the error if it fails.
 
Keith Watford:

No.

Print the error if it fails.

Im not sure how to correctly do that

 
This is what in various forms, but doesnt print or show error
void CloseAllTrades() {
   
   for (int i=OrdersTotal()-1; i>=0; i--) {
   
      if (OrderSelect(i, SELECT_BY_POS))
      
      if (OrderSymbol() == Symbol()) {
      
         int check = OrderClose( OrderTicket(), LotsToTrade, MarketInfo(OrderSymbol(), MODE_BID), 0, clrAliceBlue);
         
         if (check == false) {
         
            Print("The last ERROR was", GetLastError());
         
         }
    }
  }
}
 
Tiberious:
This is what in various forms, but doesnt print or show error

Then, as a coder you should be asking yourself "Why doesn't it print the error??"

The obvious answer is because there was no error!

If there was no error, then it can only be because there was no attempt to close any trade (or because a trade was closed).

What are the possible reasons for  there being no attempt to close any trade?

Only possible answers.......

There was not a trade open with the symbol.

The OrderSelect() failed.

The function was not called.

Add prints in your code to find the correct answer.

Documentation on MQL5: Common Functions / Print
Documentation on MQL5: Common Functions / Print
  • www.mql5.com
Print - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
This is what I have currently Ive tried printing out errors for the CloseAllOrders but they do not close. Any help is appreciated.
//+------------------------------------------------------------------+
//|                                                  EMA Cross 2.mq4 |
//|                                                        Tiberious |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Tiberious"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

double LotsToTrade = 0.1;     //Lot Size
int Magic = 500;
int MaxTrades = 1;
int MaxCloseSpreadPips = 7;

//Trade Time Delay
int TradeDelayTimeSeconds = (1 * 1 * 1 * 0);   // 0 second/minute delay
datetime LastTradePlacedTimeStamp = 0;

bool LongSetup = False;
bool ShortSetup = False;

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
      double LastSlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double LastFastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      
      if (GetTotalOpenTrades() < MaxTrades) {
      
         if ( (TimeCurrent() - LastTradePlacedTimeStamp) < TradeDelayTimeSeconds ) return;
      
          if ((LastFastEMA < LastSlowEMA)
                   &&(FastEMA > SlowEMA)) {
                   
            LongSetup = True;
      }
      
      if (LongSetup == True){
            
            Comment ("Buy");
            int OrderResultLong = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 0, 0, 0, "Buy Order", Magic, 0, clrGreen);
      }
      
      if (LongSetup == True && FastEMA < SlowEMA) CloseAllTrades();
         
         LongSetup = False;
         LastTradePlacedTimeStamp = TimeCurrent();
      
      
   }
 
      
      else if (GetTotalOpenTrades() < MaxTrades) {
      
         if ( (TimeCurrent() - LastTradePlacedTimeStamp) < TradeDelayTimeSeconds ) return;
            
       if ((LastFastEMA > LastSlowEMA)
               && (FastEMA < SlowEMA)) {
               
          ShortSetup = True;
       }
       
       if (ShortSetup == True) {
            
            Comment ("Sell");
            int OrderResultShort = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); 
      
      }
      
      if (ShortSetup == True && FastEMA > SlowEMA) CloseAllTrades();
      
         ShortSetup = False;
         LastTradePlacedTimeStamp = TimeCurrent();
 }
 
}

// Return total number of open trades
int GetTotalOpenTrades() {
      
      int TotalTrades = 0;
      
      //Loop through open orders and add them to TotalTrades
      for (int t=OrdersTotal()-1; t>=0; t--) {
      
         if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
            
            if(OrderSymbol() != Symbol()) continue;
            if(OrderMagicNumber() != Magic) continue;
            if(OrderCloseTime() !=0) continue;
            
            TotalTrades = (TotalTrades + 1);
      }
   }
      return TotalTrades;
 }

void CloseAllTrades() {

   int CloseResult = 0;
   
   for (int t=0; t<OrdersTotal(); t++) {
   
      if (OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
      
         if (OrderMagicNumber() != Magic) continue;
         if (OrderSymbol() != Symbol()) continue;
         if (OrderType() == OP_BUY) CloseResult = OrderClose(OrderTicket(), OrderLots(), Bid, MaxCloseSpreadPips, clrAliceBlue);
         if (OrderType() == OP_SELL) CloseResult = OrderClose(OrderTicket(), OrderLots(), Ask, MaxCloseSpreadPips, clrRed);
         
         t--;     
     } 
  }
  
  return;
}
   
 
Tiberious:
This is what in various forms, but doesnt print or show error


     There is no type of order to be closed in your code. (OP_BUY, OPSELL)

Tiberious:
This is what in various forms, but doesnt print or show error
int CloseAllTrades()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool asdsa=OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
     
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}
 
Mehmet Bastem:


     There is no type of order to be closed in your code. (OP_BUY, OPSELL)

I have since tried a different CloseAllOrders function and I tried to implement the code you shared, but orders are still not closing.

 
Tiberious:
This is what I have currently Ive tried printing out errors for the CloseAllOrders but they do not close. Any help is appreciated.

There is not even a single Print in your code!

Reason: