How to close positions or orders?

Like when after sell order is triggered I like to close Buy order if any order has been executed and when buy order is triggered, I like to close Sell order if any order has been present.

Can’t understand where to put close trade code and also how to use closetrade code in mql5. I don’t want any stop-loss and take profit methods and tickets all that. I want simple direct method.

...
#include <Trade/Trade.mqh>

input double Lots=0.01;

input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;

input double Step = 0.04;

input double Maximum = 0.2;

int parabolicSarHandle;


int barsTotal;


CTrade trade;

int OnInit()

 {
  parabolicSarHandle = iSAR (_Symbol,Timeframe,Step,Maximum);


  barsTotal = iBars(_Symbol,Timeframe);

 
   return(INIT_SUCCEEDED);

  }

void OnDeinit(const int reason)
  
{
   
  }

void OnTick()

    {

   double val [];


   CopyBuffer (parabolicSarHandle,0,0,2,val);

   
   int bars = iBars(_Symbol,Timeframe);


   if (barsTotal != bars)
{
      
      double close0 = iClose(_Symbol,Timeframe,0);


      double close1 = iClose(_Symbol,Timeframe,1);

   
   if  (close0 >val [1] && close1 <val [0])
 {
      
      Print (__FUNCTION__ ">Buy Signal");

      trade.Buy(Lots,_Symbol,0,0,0,"SAR BUY");

      barsTotal = bars;

    } 

   else if (close0 < val [1] && close1 > val [0])

    
{
      
      
      Print (__FUNCTION__ ">Sell Signal");

      trade.Sell(Lots,_Symbol,0,0,0,"SAR SELL");

      barsTotal = bars;

    }

   
   Comment  ("\nParabolic SAR[0]: ",DoubleToString(val[0],_Digits),


              "\nParabolic SAR[1]: ",DoubleToString(val[1],_Digits));

    }

 }

After trade.Buy execution, I want to close Sell order of the same and when trade.Sell is executed, I want to close Buy order automatically.

How do i do it?

Like trade.CloseOrder(trade.Buy) or trade.ClosePosition(trade.Sell) ?

When and where and what to write?

I used claude (dot) ai
which is like chatGPT to get the following input and output:

In Metatrader 5, I want to have a function: void closeBuys() to close all Buy, BuyStop, and BuyLimit orders with a function and another function: void closeSells() that will close all Sell, SellStop and SellLimit orders.

SP

Here are functions to close all buy and sell orders in MetaTrader 5:

cpp

Copy code

void closeBuys(){
  for(int i=OrdersTotal()-1; i>=0; i--){
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
      if(OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT){
        OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);
      }
    }
  }
}

void closeSells(){
  for(int i=OrdersTotal()-1; i>=0; i--){
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
      if(OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT){
        OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);
      }
    }
  }
}

The closeBuys() function loops through all open orders and closes any BUY, BUYSTOP, or BUYLIMIT orders.

The closeSells() function does the same for SELL, SELLSTOP, and SELLLIMIT orders.

OrderClose() is used to close each order, passing in the ticket, lots, close price, and slippage.

These can be called anytime you want to close all orders of those types, for example on a new bar or during a custom indicator event.

Copy

Can you include the closeBuys() and closeSells() to trigger on parabolic SAR and debug the following code to do this?

#include <Trade/Trade.mqh>

input double Lots=0.01;

input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;

input double Step = 0.04;

input double Maximum = 0.2;

int parabolicSarHandle;

int barsTotal;

CTrade trade;

int OnInit()

{

parabolicSarHandle = iSAR (_Symbol,Timeframe,Step,Maximum);

barsTotal = iBars(_Symbol,Timeframe);

return(INIT_SUCCEEDED);

}

void OnDeinit(const int reason)

{

}

void OnTick()

{

double val [];

CopyBuffer (parabolicSarHandle,0,0,2,val);

int bars = iBars(_Symbol,Timeframe);

if (barsTotal != bars)

{

double close0 = iClose(_Symbol,Timeframe,0);

double close1 = iClose(_Symbol,Timeframe,1);

if (close0 >val [1] && close1 <val [0])

{

Print (FUNCTION “>Buy Signal”);

trade.Buy(Lots,_Symbol,0,0,0,“SAR BUY”);

barsTotal = bars;

}

else if (close0 < val [1] && close1 > val [0])

{

Print (FUNCTION “>Sell Signal”);

trade.Sell(Lots,_Symbol,0,0,0,“SAR SELL”);

barsTotal = bars;

}

Comment ("\nParabolic SAR[0]: ",DoubleToString(val[0],_Digits),

"\nParabolic SAR[1]: ",DoubleToString(val[1],_Digits));

}

}

SP

Here is one way to modify the code to close all buy or sell orders when the parabolic SAR signals a change in trend:

cpp

Copy code

#include <Trade/Trade.mqh>

input double Lots=0.01;

input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT; 

input double Step = 0.04;

input double Maximum = 0.2;

int parabolicSarHandle;

int barsTotal;

CTrade trade;

void closeBuys(){
  // Close all buy orders 
}

void closeSells(){
  // Close all sell orders
}

int OnInit()
{
  parabolicSarHandle = iSAR (_Symbol,Timeframe,Step,Maximum);

  barsTotal = iBars(_Symbol,Timeframe);

  return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
}

void OnTick()
{
  double val[];
  
  CopyBuffer (parabolicSarHandle,0,0,2,val);
  
  int bars = iBars(_Symbol,Timeframe);

  if (barsTotal != bars)
  {
    double close0 = iClose(_Symbol,Timeframe,0);
    double close1 = iClose(_Symbol,Timeframe,1);
    
    if (close0 > val[1] && close1 < val[0]) {
      Print(__FUNCTION__ ">Buy Signal");
      trade.Buy(Lots,_Symbol,0,0,0,"SAR BUY");
      barsTotal = bars;
      
      // Close sell orders
      closeSells();
      
    } else if (close0 < val[1] && close1 > val[0]) {
      Print(__FUNCTION__ ">Sell Signal");
      trade.Sell(Lots,_Symbol,0,0,0,"SAR SELL");
      barsTotal = bars;
      
      // Close buy orders
      closeBuys();
    }
    
    Comment ("\nParabolic SAR[0]: ",DoubleToString(val[0],_Digits),
             "\nParabolic SAR[1]: ",DoubleToString(val[1],_Digits));
  }
}

The key changes:

  • Added closeBuys() and closeSells() functions
  • Call closeSells() when buy signal triggers
  • Call closeBuys() when sell signal triggers

This will close out any existing opposite orders when the SAR signals a trend change. Let me know if you have any other questions!

Not bad for AI, but it would’ve had it covered totally if it added a Magic Number check too.