How to resolved TS,TP and SL

 

hi,

When I I reach if(currentProfit >= (profitTarget +tradeCosts)) 

First I want to remove all losses trades -its working with this code:

     double LossToClose = -0.5;













for (int i = OrdersTotal() - 1; i >= 0; i--) //loop all the orders

   {

   OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //select one after another

   //----

   if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) // check if it's the one u looking for

      {

      if (OrderProfit() <= LossToClose) // check if it's losing more or equal to what u decided

         {

         OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Red); // close it

         }

      }


After that I want Modify TP for  10 pips for each positive trades and use Trailing Stop for each positive trades for 30 points.

bool ModifyOpenOrder(double stopLossPips, double pipsProfit){
  bool modify = false;
  double   takeProfit;
  double   stopLoss;
  int orderType = OrderType();
  
  if(orderType == OP_BUY) {
    takeProfit = NormalizeDouble(Bid + pipsProfit, Digits);
    stopLoss = NormalizeDouble(Bid - stopLossPips, Digits);
    if(OrderStopLoss() < stopLoss) modify = true;
  }
  
  if(orderType == OP_SELL) {
    takeProfit = NormalizeDouble(Ask - pipsProfit, Digits);
    stopLoss = NormalizeDouble(Ask + stopLossPips, Digits);
    if(OrderStopLoss() > stopLoss) modify = true;
  }
  
  if(modify == true) {
    if(OrderProfit() < 0) {
       takeProfit = OrderTakeProfit();
    }
    
    bool result = OrderModify(OrderTicket(),OrderOpenPrice(),stopLoss,takeProfit,0,clrNONE);
    if(!result) {
      Print("Error in OrderModify. Error code=",GetLastError());
    }
    
    return result;
  }
  
  return false;
}


void tStop(string symb,int stop, int MN)// Symbol + stop in pips + magic number
  {
   double bsl=NormalizeDouble(MarketInfo(symb,MODE_BID)-stop*MarketInfo(symb,MODE_POINT),(int)MarketInfo(symb,MODE_DIGITS));
   double ssl=NormalizeDouble(MarketInfo(symb,MODE_ASK)+stop*MarketInfo(symb,MODE_POINT),(int)MarketInfo(symb,MODE_DIGITS));
  
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MN)
            if(OrderSymbol()==symb)

               if(OrderType()==OP_BUY && (OrderStopLoss()<bsl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),bsl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Buy's Stop Trailled to "+(string)bsl);
                       }else{
                     Print(symb+" Buy's Stop Trail ERROR");
                    }

               if(OrderType()==OP_SELL && (OrderStopLoss()>ssl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),ssl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Sell's Stop Trailled to "+(string)ssl);
                       }else{
                     Print(symb+" Sell's Stop Trail ERROR");
                    }
     }
  }


This is my function for calling when reach profit:

  ModifyOpenOrder(0,10*_Point); 
  tStop(_Symbol,30,MagicNumber);


In strategy tester not returns correct results for Trailing and TP.

How can I resolved this?

Reason: