Need help for my expert advisior close position on opposite order

 

Hi

im new into programming and i trying to figure out how to close a position on the opposide signal but i my tries dont work i watched different videos but no reult.

i know the code is a bit messy but could someone help me please

#include <Trade/Trade.mqh>

input int TimeStartHour = 7;
input int TimeStartMin =10;

input int TimeEndHour = 22;
input int TimeEndMin =10;

input ENUM_TIMEFRAMES StochTimeframe = PERIOD_CURRENT;
input int StochK = 5;
input int StochD = 3;
input int StochSlowing = 3;
input double StochUpperLevel = 80;
input double StochLowerLevel = 20;

input double Lots = 0.1;


int totalBars;

int handleStoch;



CTrade trade;
ulong posTicket;


int OnInit(){

   Print("Inti");
   
   handleStoch = iStochastic(_Symbol,StochTimeframe,StochK,StochD,StochSlowing,MODE_SMA,STO_LOWHIGH);
   

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){
   Print("deinti");

}

void OnTick(){

   int bars = iBars(_Symbol,StochTimeframe);
   

   MqlDateTime structTime; 
   TimeCurrent(structTime);
   structTime.sec = 0;
   
   structTime.hour = TimeStartHour;
   structTime.min = TimeStartMin;
   datetime timeStart = StructToTime(structTime);
   
   structTime.hour = TimeEndHour;
   structTime.min = TimeEndMin;
   datetime timeEnd = StructToTime(structTime);
   
   bool isTime = TimeCurrent() >=timeStart && TimeCurrent() <timeEnd;
   
   Comment("\nServer Time:",TimeCurrent(),
           "\nTime Start Dt: ",timeStart,
           "\nTime End Input: ",timeEnd,
           "\nTime Filter: ",isTime);
   
   if(totalBars != bars){
      totalBars = bars;
   
      double stoch[];        
      CopyBuffer(handleStoch,MAIN_LINE,1,2,stoch);
      
      double stochSig[];        
      CopyBuffer(handleStoch,SIGNAL_LINE,1,2,stochSig);
      
      
      
      if(stoch[0] > StochUpperLevel && stochSig[0] > StochUpperLevel){
       
         if(stoch[0] < stochSig[0] && isTime && PositionsTotal()<1){   
            
         //short Signal
            trade.Sell(Lots,_Symbol);

         }
      }
      else if(stoch[0] < StochLowerLevel && stochSig[0] < StochLowerLevel){
         if(stoch[0] > stochSig[0] && isTime && PositionsTotal()<1 ){
            
         //long Signal
            trade.Buy(Lots,_Symbol);
            //trade.Buy(Lots,_Symbol);
            
         }
         
      
      }
      int posType = (int)PositionGetInteger(POSITION_TYPE);
      if(posType == POSITION_TYPE_BUY){
         if(stoch[0] > StochUpperLevel && stochSig[0] > StochUpperLevel && stoch[0] < stochSig[0]){
           trade.PositionClose(-1);
         }
      }else if(posType == POSITION_TYPE_SELL){
         if(stoch[0] < StochLowerLevel && stochSig[0] < StochLowerLevel && stoch[0] > stochSig[0]){
            trade.PositionClose(-1);
         }
      }
   
   }
}

 

Before you use PositionGetInteger you need to select this position. So first detect the signal for closing and when it’s confirmed – you need to go in a loop of all trades and close only the trades from opposite direction for example:

if(signal=”buy”){

for(int i=PositionsTotal()-1;i>=0;i--){

      if(uint ticket = PosititionGetTicket(i)){

            if(PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL){ //close sell on buy signal

               trade.PositionClose(ticket);

            }    

       }

}