Help! Close order and open opposite direction of trade on MQL5

 

Hi

I am trying to close an order and open an order in the opposite direction automatically on MQL5 under buy and sell signal that I have already created. So, if there is a buy signal, it should close the previous sell order and open buy order at the same time. Likewise, if there is a sell signal, it should close the previous buy order and open sell order. I am new to the program. Can anyone help me please?

Thank you.


#include<Trade/Trade.mqh>

CTrade trade;

void OnTick()
   {
     
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   string signal="";
   
   double PriceArray[];

   ArraySetAsSeries(PriceArray,true);
   
      int OsMADefinition=iOsMA(_Symbol,_Period,12,26,9,PRICE_CLOSE) ;

      CopyBuffer(OsMADefinition,0,0,3,PriceArray);
      
      double OsMAValue=(PriceArray[0]);
      
      double LastOsMAValue=(PriceArray[1]);
      
      if ((OsMAValue>0) && (LastOsMAValue<0))
      {
      signal=("buy");
      }
  
      if ((OsMAValue<0) && (LastOsMAValue>0))
      {
      signal=("sell");
      }
     
      //want to close all order and then open another direction order at the same time
      // buy 10 microlot
      if (signal =="buy")
      ...
      
      // sell 10 microlot
      if (signal =="sell")
      ...
   
   
      Comment("The current signal is: ",signal);
   }
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
Trade Orders in DOM - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Chakrit Dumrongthaveesak:
      //want to close all order and then open another direction order at the same time
      // buy 10 microlot
      if (signal =="buy")
      ...
      
      // sell 10 microlot
      if (signal =="sell")
      ...


Can you show us your attempt on your want?

 
Shengjie Mao:

Can you show us your attempt on your want?

      //want to close all order and then open another direction order at the same time
      // buy 10 microlot
      if (signal =="buy")
      		//close all sell position and open buy position instead with 0.1 lot, no TP&SL
      
      // sell 10 microlot
      if (signal =="sell")
      		//close all buy position and open sell position instead with 0.1 lot, no TP&SL
 
Show your attempt with code, not description.
Reason: