I am stuck how to close a single buy order for expert advisor

1
I would like to learn the code how to close a single buy/sell order when indicator conditions met instead setting SL/TP criteria.
I wrote just a simple example conditions to open a buy order. Below the line "// Close Buy position" i have difficulties to find the right code, how to
close the buy order. I tried various ways like to set expiry time 20 minutes 20*60 sec if the time i must set is in sec. Also i found code where i create function
to close multiple orders but in this simple buy example does not work ( if also the code i found is right).
I make some attempts without result.



Below is the code:

Code: Select all

// We must download Trade.mqh file and add it in include folder
 
 // Import Trade library
 #include <Trade.mqh>

 // Create an instance of Ctrade called trade
 CTrade trade; 
  
// Open per bar
datetime Time0 = 0;





void OnTick()
  {
  
  if (Time0 != Time[0]){  // Start of Time

   
  
  ///////////////////////////////////////////////////////////
  
  double fastMA = iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE,0);
  double fastMA_old = iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE,3);
  
  double slowMA = iMA(_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE,0);
  double slowMA_old = iMA(_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE,3);
  
  
  double CCI = iCCI(_Symbol,_Period,9,PRICE_CLOSE,0);
  
  
  // We create a string variable for the signal
  string signal="";
  
  // Buy conditions
  if(fastMA>slowMA && fastMA_old< slowMA_old)
  {
  signal="buy";
  }

  // Open a Buy order if Buy conditions met
  if(signal=="buy")
  {
  OrderSend(_Symbol,OP_BUY,0.10,Ask,3,0,0,NULL,0,0,Green); // After Slippage=3, next is stop loss and next the TP
  }
  
  
  
  // Close Buy position
  if(CCI<=-100 && signal=="buy"){
                                // CloseAllPositions();
                                //OrderModify(OrderTicket(),OrderOpenPrice(),0,0,1200,clrAliceBlue);
                                //trade.PositionClose(_Symbol);
                                //OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,clrAliceBlue);                
                                } 
                     
                          
                                 
                                                    
                             
  
  Time0 = Time[0];
  }// end of Time
 
  } // End OnTick()


 //void  CloseAllPositions()
 //{
 
         //     for(int i=PositionsTotal()-1;i>=0;i--)
        //      {
        //      int ticket = PositionGetTicket(i);
        //      trade.PositionClose(i);
              
        //      }
 
 
 //}


Who is online

Users browsing this forum: No registered users and 13 guests