How to have multiple separate criteria to close the same Ticket? Invalid Ticket for OrderClose Function.

 

Hello, I am trying to close my trades based on 2 separate types of criteria, 

1. Regardless of what the profit is, close the trade after 1020 seconds.

2. If my import value changes from -1 to 1 or vice versa, close it regardless of any other variables.

So far, my code works fine with 1 criteria, however when I add 2 if statements, MT4 does not close the trades and I get an error saying "Invalid Ticket for OrderClose Function"

My code is below.

//Global Variables
//int Value = ReadCSVFile and get cell value(Either -1 or 1)
int Ticket;
int TicketBuy;
datetime SwitchBuy
datetime SwitchSell

void OnTick()
  {
    //Open Trades based on Value from CSV and if No other buy orders open
    if(Value == 1){
    if(getBuyOrderCount(Symbol(),magic) < 1){
    Ticket == OrderSend(NULL,OP_BUY,0.01,Ask,2,0,0,NULL,magic,0,clrAliceBlue);
    SwitchBuy = TimeCurrent();
    }}

    if(Value == (-1)){
    if(SellOrderCount(Symbol(),magic) < 1){
     TicketSell = OrderSend(NULL,OP_SELL,0.01,Bid,2,0,0,NULL,magic,0,clrAliceBlue);
     SwitchSell = TimeCurrent();
    }}

    //Close Trades based on value in CSV file changing
    if(getBuyOrderCount(Symbol(),magic) == 1){
     if(Value == (-1)){
    CloseAllTradesBuy();
    Alert("Buy Trade Closed Because Probabilities Changed to -1!");
      } 
    }                
    

    if(SellOrderCount(Symbol(),magic)==1){
     if(Value == 1){
    CloseAllTradesSell();
    Alert("Sell Trade Closed Because Probabilities Changed to 1!");
     }
    }

    //Second Independent Criteria
    if((SwitchBuy+1020) < TimeCurrent()){
     if(OrderType() == OP_BUY){
     CloseAllTradesBuy();
      }
    }
     
     if((SwitchSell+1020) < TimeCurrent()){
     if(OrderType() == OP_SELL){
     CloseAllTradesSell();
      }
    }

  }

//Functions to Close Trades
double CloseAllTradesBuy(){
 for (int i =OrdersTotal(); i>=0;i--)
 {
   if(OrderSelect(i,SELECT_BY_POS)==true)
   if(OrderType()==OP_BUY)
   if (OrderSymbol()==Symbol())
   {
   OrderClose(Ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed);
   }
 }
}



double CloseAllTradesSell(){
 for (int i =OrdersTotal(); i>=0;i--)
 {
   if(OrderSelect(i,SELECT_BY_POS)==true)
   if(OrderType()==OP_SELL)
   if (OrderSymbol()==Symbol())
   {
   OrderClose(TicketSell,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed);
   }
 }
}

Is it even possible to do this?

Thank you.

 
  1. Rahul Shaji Parmeshwar: Is it even possible to do this?

    Yes.
         How To Ask Questions The Smart Way. (2004)
              Only ask questions with yes/no answers if you want “yes” or “no” as the answer.

  2. In your closeAll you select by position. What is the point of saving the ticket number? Just close by OrderTicket(), OrderLots(), and OrderClosePrice().

  3.      TicketSell = OrderSend(NULL,OP_SELL,0.01,Bid,2,0,0,NULL,magic,0,clrAliceBlue);

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020.07.25)
Reason: