Download MetaTrader 5

Trailling Stop Problem...

To add comments, please log in or register
Frenchytrader
408
Frenchytrader  

Hi All,


It looks like it's only performing the first step and that's it.

the EA is handling 4 pairs.

So I have created 4 function for each pair:


Paremeters:

//-------------------Variables 
static int magicalNumber;
static string PipsDeclencheurArray[50];
static string StopLossArray[50];
static int PipsDeclencheurArrayValue[50];
static int StopLossArrayValue[50];

if(Digits == 3 || Digits == 5) {
      TrailingStopDeclencheur = P_TrailingStopDeclencheur * 10;
      TrailingStopDistance = P_TrailingStopDistance * 10;
      

   } else {
      TrailingStopDeclencheur = P_TrailingStopDeclencheur;
      TrailingStopDistance = P_TrailingStopDistance;
      
   }
   
   SplitString(PipsDeclencheur,";", PipsDeclencheurArray);
   SplitString(StopLossToSet,";", StopLossArray);
   
   int maxSize = ArraySize(PipsDeclencheurArray);
   ArrayResize( PipsDeclencheurArrayValue, maxSize);
   ArrayResize( StopLossArrayValue, maxSize);
   
   for(int i =0 ; i < maxSize ; i++) {
      if(Digits == 3 || Digits == 5) {
         PipsDeclencheurArrayValue[i] = StrToDouble(PipsDeclencheurArray[i]) * 10;
         StopLossArrayValue[i] = StrToDouble(StopLossArray[i]) * 10;
      } else {
         PipsDeclencheurArrayValue[i] = StrToDouble(PipsDeclencheurArray[i]);
         StopLossArrayValue[i] = StrToDouble(StopLossArray[i]);
      }
   }


The code:

    for (int z1=OrdersTotal()-1; z1 >= 0; z1 --) 
   {
      OrderSelect(z1, SELECT_BY_POS, MODE_TRADES);
      
      if ( Function_Close ) { 

       // ....Other pair

      
        // GBPNZD 
      if ( OrderSymbol() == "GBPNZD" && OrderMagicNumber() == MyMagicNum && TimeCurrent() - OrderOpenTime() >= Time_Function_Close * 60 &&
           OrderProfit()>0 ) 
           {

               updateTrailingStop_GBPNZD();
           Print ("TP is Low, protect profit");

                  // => It's printing... So the function is called.
           
           }
               
         }
      
      }


The function:

/*
   Trailing stop suiveur au pip près
*/
void updateTrailingStop_GBPNZD() {
        double ts;
        for (int i = OrdersTotal() - 1; i >= 0; i --) {
                OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        //      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MyMagicNum) {
                if (OrderMagicNumber() == MyMagicNum && OrderSymbol() == "GBPNZD") {

                        for (int j = ArraySize(PipsDeclencheurArrayValue) - 1; j >= 0 ; j-- ) {

                                //Trailing stop par palier mais inférieur au suiveur
                                if (OrderType() == OP_BUY && (MarketInfo("GBPNZD",MODE_BID) < (OrderOpenPrice() + TrailingStopDeclencheur*Point)) && (MarketInfo("GBPNZD",MODE_BID) > (OrderOpenPrice() + PipsDeclencheurArrayValue[j]*Point)) && OrderStopLoss() < OrderOpenPrice() + StopLossArrayValue[j]*Point) {
                                        OrderModifyReliable(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + StopLossArrayValue[j]*Point, OrderTakeProfit(), 0, White);
                                        break;
                                }

                                //Trailing Stop suiveur
                                if (OrderType() == OP_BUY && (MarketInfo("GBPNZD",MODE_BID) > (OrderOpenPrice() + TrailingStopDeclencheur*Point))) {
                                        ts = MarketInfo("GBPNZD",MODE_BID) - (Point * TrailingStopDistance); // calculate trailing stop
                                        if (OrderStopLoss() < ts) { // test if new high
                                                logData("BUY Trailing stop modification "+j);
                                                OrderModifyReliable(OrderTicket(), OrderOpenPrice(), ts, OrderTakeProfit(), 0, White);
                                                break;
                                        }
                                }

                                //Trailing stop par palier
                                if (OrderType() == OP_SELL && (MarketInfo("GBPNZD",MODE_ASK) > (OrderOpenPrice() - TrailingStopDeclencheur*Point)) && (MarketInfo("GBPNZD",MODE_ASK) < (OrderOpenPrice() - PipsDeclencheurArrayValue[j]*Point)) && OrderStopLoss() > OrderOpenPrice() - StopLossArrayValue[j]*Point) {
                                //      OrderModifyReliable(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - StopLossArrayValue[j]*Point, OrderTakeProfit(), 0, White);
                                        break;
                                }

                                //Trailing Stop suiveur
                                if (OrderType() == OP_SELL && (MarketInfo("GBPNZD",MODE_ASK) < (OrderOpenPrice() - TrailingStopDeclencheur*Point))) {
                                        ts = MarketInfo("GBPNZD",MODE_ASK) + (Point * TrailingStopDistance); // calcul trailing stop
                                        if (OrderStopLoss() > ts) { // test si nouveau plus bas
                                                logData("SELL Trailing stop modification "+j+" ask "+MarketInfo("GBPNZD",MODE_ASK)+" "+(OrderOpenPrice() - TrailingStopDeclencheur*Point));
                                                OrderModifyReliable(OrderTicket(), OrderOpenPrice(), ts, OrderTakeProfit(), 0, White);
                                                break;
                                        }
                                }

                        }
                }
        }
}




Thank you for helping

Frenchytrader
408
Frenchytrader  
picturePicture
To add comments, please log in or register