Take Profit, and Close trade position

Jmalad
10
Jmalad  

Hello MQL5 community

Can someone please help me, by example; how do i close an open trade, either through a function or TP amount?

I tried this:

double myAccountProfit = AccountInfoDouble(ACCOUNT_PROFIT);
double myAccountEquity = AccountInfoDouble(ACCOUNT_EQUITY);
if (myAccountEquity - myAccountBalance > 2)   
        //ClosePositions();
Vladimir Karputov
Moderator
257210
Vladimir Karputov  
Jmalad :

Hello MQL5 community

Can someone please help me, by example; how do i close an open trade, either through a function or TP amount?

I tried this:

1. Please insert the code correctly: when editing a message, press the button Codeand paste your code into the pop-up window. (The first time I corrected your message)

2. Nothing is clear from your explanation. Do you want to close all positions if the profit on your trading account is more than $ 2.0?

Mehmet Bastem
2551
Mehmet Bastem  
Jmalad:

Hello MQL5 community

Can someone please help me, by example; how do i close an open trade, either through a function or TP amount?

I tried this:

#include <trade/trade.mqh>


void OnStart()
  {
  PendingOrderDelete() ;
   MarketOrderClose();
  }
void MarketOrderClose()
  {
   CTrade trade;
   int i=PositionsTotal()-1;
   while (i>=0)
     {
      
      if (trade.PositionClose(PositionGetSymbol(i)))
      // ulong o_ticket = OrderGetTicket(i);
      //trade.OrderDelete(o_ticket);
       i--;
     }
     PendingOrderDelete() ;
  }
  
  void PendingOrderDelete() 
{  
         CTrade mytrade;
         int o_total=OrdersTotal();
         for(int j=o_total-1; j>=0; j--)
         {
            ulong o_ticket = OrderGetTicket(j);
            if(o_ticket != 0)
            {
             // delete the pending order
             mytrade.OrderDelete(o_ticket);
             Print("Pending order deleted sucessfully!");
          }
      }     
}