HELP with my CODE please? :)

 
Hi am new here and new at coding at MT5. So basically the code wants to look at all my positions and see if it is a SELL for USDCHF, USDCAD, USDJPY and would close it if it is. Am having troubles on getting a return on my position type, it returns 1 and i need to be a SELL. That's why it is not closing my open SELL positions.

Here's the code:
if(id == CHARTEVENT_OBJECT_CLICK){
      Print("The ", sparam, " was clicked");
      if(sparam == BtnClose){
         for(int i = PositionsTotal()-1; i>=0; i--){
            ulong posTicket = PositionGetTicket(i);
            int posType = (int)PositionGetInteger(POSITION_TYPE,posTicket);    //<----this returns 1 so it would not satisfy the if condition below to close the position
            string symbol = PositionGetSymbol(posTicket);
            if(posType == POSITION_TYPE_SELL && (symbol == "USDCHF" || symbol == "USDCAD" || symbol == "USDJPY")){
               if(trade.PositionClose(posTicket)){
                  Print("Position #",posTicket," was closed...");
               }
               else{
                   Print("Failed to close position #",posTicket);
                   Print(posTicket);
                   Print(posType);
                   Print(symbol);
             }
           }
         }
         ObjectSetInteger(0,BtnClose,OBJPROP_STATE,false);
         ChartRedraw();       
     }
   }
}


I hope you guys can help me :)


Thanks in advance!

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
  • www.mql5.com
Types of Chart Events - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
 
Alain Verleyen #:


Apologies about that. I have edited it. Thanks! :)

 

After a little search for "closing open positions" and filtering for CodeBase: https://www.mql5.com/en/search#!keyword=closing%20open%20positions&module=mql5_module_codebase&method=2

I found: https://www.mql5.com/en/code/39835.

At the very< end is an example function that closes all position - I think you easily can change it according to your needs.

Reason: