Ahmad Taufik
Ahmad Taufik

Reputation: 1

how to add a stoploss in mql5

I have a code that execute trading when 2 moving average crossover happen in mt5, the problem is I do not know how to code the stoploss which is the opposite of the crossover, let say i'm in a sell position with fast moving average is crossing below slow moving average , the position is kept open until new buy moving average crossover happen, then the sell position is closed with new buy position is open. anybody can help me?

'''

#include <Trade/Trade.mqh>

CTrade trade;

int OnInit() { Print("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, 200, 0, MODE_SMA, PRICE_CLOSE);
  double slowMaArray[];
  CopyBuffer(handleSlowMa, 0,1,2,slowMaArray);
  ArraySetAsSeries(slowMaArray, true);
  
  int handleFastMa = iMA(_Symbol,PERIOD_CURRENT, 20, 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 > slow ma");
     double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
     trade.Buy(0.1,_Symbol, ask, "This is a buy" );
   
  }
  
 
  if(fastMaArray[0] < slowMaArray[0] && fastMaArray[1] > slowMaArray[1]) {
     Print("Fast Ma is now < slow ma");
     double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
     trade.Sell(0.1,_Symbol, bid,"This is a sell" );
  }
  
  Comment("\nslowMaArray[0]: ",slowMaArray[0],
          "\nSlowMaArray[1]: ",slowMaArray[1],
          "\nfastMaArray[0]: ",fastMaArray[0],
          "\nfastMaArray[1]: ",fastMaArray[1]);
  

}

}

'''

Upvotes: 0

Views: 461

Answers (1)

Mido
Mido

Reputation: 1

you need first to make a void for loop to find position you will make two the first is for buy and the second is for sell

CLOSE BUY ORDERS

void CloseBuy() { for(int i=PositionsTotal()-1; i>=0; i--)
  {
   ulong PosTicket = PositionGetTicket(i);       
   string symbol = PositionGetSymbol(i);
   long PosDirection = PositionGetInteger(POSITION_TYPE);
   if(PosDirection == POSITION_TYPE_BUY)
   if(Symbol()==symbol)
     { MyTrade.PositionClose(PosTicket); }
  } }

CLOSE SELL ORDERS

void CloseSell() { for(int i=PositionsTotal()-1; i>=0; i--)
  {
   ulong PosTicket = PositionGetTicket(i);
   string symbol = PositionGetSymbol(i);       
   long PosDirection = PositionGetInteger(POSITION_TYPE);       
   if(PosDirection == POSITION_TYPE_SELL)      
   if(Symbol()==symbol)
     { MyTrade.PositionClose(PosTicket); }
  } }

then you need to edit your main code at ontick function

if(fastMaArray[0] > slowMaArray[0] && fastMaArray[1] < slowMaArray[1]) {
 Print("Fast Ma is now > slow ma");
 double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
 trade.Buy(0.1,_Symbol, ask, "This is a buy" ); }

else if(fastMaArray[0] < slowMaArray[0] && fastMaArray[1] > slowMaArray[1])
     { CloseBuy(); }

if(fastMaArray[0] < slowMaArray[0] && fastMaArray[1] > slowMaArray[1]) {
 Print("Fast Ma is now < slow ma");
 double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
 trade.Sell(0.1,_Symbol, bid,"This is a sell" ); }

else if(fastMaArray[0] > slowMaArray[0] && fastMaArray[1] < slowMaArray[1])
     { CloseSell(); }

Upvotes: -1

Related Questions