trouble closing my trades

 

Hi everyone.

I am new to MQL5 but have managed to create some code that buy and sell as I want but I am having trouble with the closing of the trades.

I want the trade to close as soon as the next and oppisite trade opens, can anyone help?

TIA

#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

CTrade trade; // To send orders and close positions
CPositionInfo positionInfo; // to get info about current positions

int OnInit(){

   return INIT_SUCCEEDED;
  }

void OnTick(){
   
   static datetime timestamp;
   datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
   if (timestamp!= time){
       timestamp = time;

         static int handleSlowMa = iMA(_Symbol,PERIOD_CURRENT,35,0,MODE_SMA,PRICE_CLOSE); 
         double slowMaArray[]; 
         CopyBuffer(handleSlowMa,0,1,2,slowMaArray);
         ArraySetAsSeries(slowMaArray,true);
         
         static int handleFastMa = iMA(_Symbol,PERIOD_CURRENT,10,0,MODE_SMA,PRICE_CLOSE); 
         double fastMaArray[]; 
         CopyBuffer(handleFastMa,0,1,2,fastMaArray);
         ArraySetAsSeries(fastMaArray,true);
         
         
         if(fastMaArray[0] > slowMaArray[0] && fastMaArray[1] < slowMaArray[1]){
            Print("fast ma is now > than slow ma");
            double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
            trade.Buy(0.01,Symbol());
            
             
         }
         
         if(fastMaArray[0] < slowMaArray[0] && fastMaArray[1] > slowMaArray[1]){
            Print("fast ma is now < than slow ma");
            double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
            trade.Sell(0.01,Symbol());
           
             if(signal=="buy")
     {
      //First close all Sell positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if sell then close it
            if(positionInfo.PositionType()==POSITION_TYPE_SELL)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
      trade.Buy(0.01,Symbol());
     Comment ("The current signal is: ",signal);
     }

   if(signal=="sell")
     {
      //First close all Buy positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if buy then close it
            if(positionInfo.PositionType()==POSITION_TYPE_BUY)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
      trade.Sell(0.01,Symbol());
     Comment ("The current signal is: ",signal);
     }

              

         }
         
         
         
         Comment("\nslowMaArray[0]: ",slowMaArray[0],
                 "\nslowMaArray[1]: ",slowMaArray[1],
                 "\nfastMaArray[0]: ",fastMaArray[0],
                 "\nfastMaArray[1]: ",fastMaArray[1]);
                 
       }  

}
  
  
  
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
#include <Trade/Trade.mqh>

CTrade trade;
ulong posTicket;

int OnInit(){

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason){

   
  
  }

void OnTick(){




   static datetime timestamp;
   datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
   if (timestamp!= time){
       timestamp = time;

         static int handleSlowMa = iMA(_Symbol,PERIOD_CURRENT,35,0,MODE_SMA,PRICE_CLOSE); 
         double slowMaArray[]; 
         CopyBuffer(handleSlowMa,0,1,2,slowMaArray);
         ArraySetAsSeries(slowMaArray,true);
         
         static int handleFastMa = iMA(_Symbol,PERIOD_CURRENT,10,0,MODE_SMA,PRICE_CLOSE); 
         double fastMaArray[]; 
         CopyBuffer(handleFastMa,0,1,2,fastMaArray);
         ArraySetAsSeries(fastMaArray,true);
        
         
         if(fastMaArray[0] > slowMaArray[0] && fastMaArray[1] < slowMaArray[1]){
            Print("fast ma is now > than slow ma");
            double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
            trade.Buy(0.2,_Symbol,ask);
           
            if(posTicket>0 && PositionSelectByTicket(posTicket)){
            int posType = (int) PositionGetInteger(POSITION_TYPE);
            int(posType == POSITION_TYPE_BUY){
            trade.PositionClose(posTicket);
            posTicket = 0
            
            
         }
        
         if(fastMaArray[0] < slowMaArray[0] && fastMaArray[1] > slowMaArray[1]){
            Print("fast ma is now < than slow ma");
            double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
            trade.Sell(0.2,_Symbol,bid);
            
             if(posTicket>0 && PositionSelectByTicket(posTicket)){
            int posType = (int) PositionGetInteger(POSITION_TYPE);
            int(posType == POSITION_TYPE_SELL){
            trade.PositionClose(posTicket);
            posTicket = 0
         }
         
        
         
         Comment("\nslowMaArray[0]: ",slowMaArray[0],
                 "\nslowMaArray[1]: ",slowMaArray[1],
                 "\nfastMaArray[0]: ",fastMaArray[0],
                 "\nfastMaArray[1]: ",fastMaArray[1]);
}

So I changed my attempt at closing the trades but cant test it as it gives me a error saing unbalanced paretheses for the parentheses after OnTick

Does this code look like it  can work and if so can someone help to resolve the error?

Reason: