Good Day i need help in closing a trade on a specific condition

 
  if(OrdrTotal==1 && OrderSelect(SELECT_BY_POS,MODE_TRADES))
     {
     if(OrderMagicNumber()==2000 && OrderSymbol()==_Symbol)
        {
      if(OrderType()==OP_BUY)
        if(Ask>OrderOpenPrice()+CLP*_Digits)closed=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite);
      else
         {
           
         }
        }
      if(OrderType()==OP_SELL)
        if(Bid<OrderOpenPrice()+CLP*_Digits)closed=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite); //CLP user can input desired pips
      else
      {
      
      }
    }
   
  }

Please I'm Trying to say this; 2 trades place same time a buy and a sell  with Take Profit 25 Stop Loss 20, so i want if maybe a sell hit take profit a close order function be call only when second order drops back to 17pips if did not hit take profit, but this way i write my logic not working that way if any other way i can get current trade information's and close it at a particular price help please

 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. if(OrdrTotal

    We have no idea what that variable is.

  3. OrderSelect(SELECT_BY_POS,MODE_TRADES))

    Invalid call. Don't post code that will not even compile.

  4. CLP*_Digits)

    You probably don't mean 5 CLP.

  5. if(Ask>OrderOpenPrice()+CLP*_Digits)closed=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite);

    You buy at the Ask and sell at the Bid. You can't close your buy orders at the Ask.

  6. if(Bid<OrderOpenPrice()+CLP*_Digits)closed=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite); //CLP user can input desired pips
    Do you really want to close your sell order at a loss?
 
William Roeder:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. We have no idea what that variable is.

  3. Invalid call. Don't post code that will not even compile.

  4. You probably don't mean 5 CLP.

  5. You buy at the Ask and sell at the Bid. You can't close your buy orders at the Ask.

  6. Do you really want to close your sell order at a loss?

Thanks very much i'm still new i'm yet to figure out how to post to specific sections,

1. i have bookmarked the link to mt4 section now

2. i post complete code now you can see now

3. i don't know how you mean but i have posted complete code that compiles now

4. the CLP is price level the close order is called user can input desired pips

5. i get i corrected that statement now

6. i don't want to close at loss now understand what i mean now, 2 trades a buy and a sell placed the same time with SL 20 and TP 40 so if either a buy hits a Stop Loss the close order function is only called when the sell did not get to TP so user can set maybe at 17pips close the order not allowing the sell to return back to loss after the buy have hit the Stop Loss. thanks

//+------------------------------------------------------------------+
//|                                                     DMF Real.mq4 |
//|                 Copyright 2021, ITace Inc. (Marve)Software Corp. |
//|                                             https://www.ITace.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, ITace Inc. (Marve)Software Corp."
#property link      "https://www.ITace.com"
#property version   "1.00"
#property strict
input string TradingBetween = "16:00-23:59";
input double SL= 100;
input double TP= 50;
input double LotSize= 1.6;
input double CLP=5;
int OnInit()
{
   int AccountLists_AcceptData[];
   int number_of_accounts = 3;
   ArrayResize(AccountLists_AcceptData, number_of_accounts);
   AccountLists_AcceptData[0] = 1326923; 
   AccountLists_AcceptData[1] = 2200659; 
   AccountLists_AcceptData[2] = 47809298;
   
   bool isAccountNumberOK = false;
   for (int i = 0; i < number_of_accounts; i++)
   {
      if (AccountNumber() == AccountLists_AcceptData[i])
      {
         isAccountNumberOK = true;
         break;
      }
   }

   if (!isAccountNumberOK)
   {
      Print("License is not authorized! Contact Itace Inc. (Marve)");
      return(INIT_FAILED);
   }
   else
   {
      return(INIT_SUCCEEDED);
   }
}
void OnTick()
{
      bool validTime = isValidTime2Trade(TradingBetween);
      double ask = MarketInfo ( Symbol(), MODE_ASK ),bid = MarketInfo ( Symbol(), MODE_BID );
      bool closed;
      int ticket1, ticket2,OrdrTotal = OrdersTotal();
 if(OrdrTotal<1 && validTime==true)
   {
    ticket1=OrderSend(Symbol(),OP_SELL, LotSize, NormalizeDouble(bid,Digits),0,NormalizeDouble(bid+SL,Digits),NormalizeDouble(bid-TP,Digits),NULL,2000,0,clrRed);
    ticket2=OrderSend(Symbol(),OP_BUY ,LotSize, NormalizeDouble(ask,Digits),0,NormalizeDouble(ask-SL,Digits),NormalizeDouble(ask+TP,Digits),NULL,2000,0,clrGreen);
   }
   if(OrdrTotal==1 && OrderSelect(SELECT_BY_POS,MODE_TRADES))
     {
     if(OrderMagicNumber()==2000 && OrderSymbol()==_Symbol)
        {
      if(OrderType()==OP_BUY)
        if(Ask>OrderOpenPrice()+CLP*_Digits)closed=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite);
      else
         {
         
         }
        }
      if(OrderType()==OP_SELL)
        if(Bid<OrderOpenPrice()+CLP*_Digits)closed=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite);
      else
      {
      
      }
    }
   
  }
  #define HR2400 86400
#define SECONDS uint
SECONDS    time(datetime when=0) {return SECONDS(when == 0 ? TimeCurrent() : when) % HR2400;               }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime   date(datetime when=0) {return datetime((when == 0 ? TimeCurrent() : when) - time(when));      }
bool isValidTime(SECONDS t0, SECONDS t1, datetime when=0) {SECONDS now = time(when); return t0 < t1 ? t0 <= now && now < t1  : !isValidTime(t1, t0, when);}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void TradingTimeSplit(string str,int &t0,int&t1)
  {
   string res[];
   StringSplit(str,StringGetCharacter("-",0),res);
   string T0=res[0],T1=res[1];
   int H0,M0,H1,M1;
   ArrayFree(res);
   StringSplit(T0,StringGetCharacter(":",0),res);
   H0=StrToInteger(res[0]);
   M0=StrToInteger(res[1]);
   t0=H0*60*60+M0*60;
   ArrayFree(res);
   StringSplit(T1,StringGetCharacter(":",0),res);
   H1=StrToInteger(res[0]);
   M1=StrToInteger(res[1]);
   t1=H1*60*60+M1*60;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool isValidTime2Trade(string str)
  {
   int t0,t1;
   TradingTimeSplit(str,t0,t1);
   return(isValidTime(t0,t1));
  }
//+------------------------------------------------------------------+
 
Marvelous Agbor:

I have deleted your 2 new topics.

Do not start new topics to discuss slightly different aspects of the same code.

Keep all your posts about the same code here.

 
Keith Watford:

I have deleted your 2 new topics.

Do not start new topics to discuss slightly different aspects of the same code.

Keep all your posts about the same code here.

please why i'm i been hunted here and have not achieved any help i posted 2 topics with different codes ans problems i'm surprise you view them same 
 
Marvelous Agbor:
please why i'm i been hunted here and have not achieved any help i posted 2 topics with different codes ans problems i'm surprise you view them same 

They were basically the same.

Reason: