Trailing stop function

To add comments, please log in or register
KNHConsultants
34
KNHConsultants  

Hello All,


i am a little new to m4l programming but i have some programming background. someone wrote a trailing stop function for me but i'm not sure if its working properly or maybe its where the function call is in the onTick function. here's the trailing stop code below. Please let me know if it is ok or needs tweaking


//+------------------------------------------------------------------+
//|                     Trailing Stop function                       |
//+------------------------------------------------------------------+
void trailingStop()
  {

   for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
     {
      if(OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {                                                    // Analysis of orders:
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magicNumber)
           {
            double SL=OrderStopLoss();                       // SL of the selected order
            bool Modify=false;                            // Not to be modified

            if(OrderType()==OP_BUY)
              {
               if(NormalizeDouble(SL,Digits)<NormalizeDouble(Bid-TralStep*Point,Digits) && (NormalizeDouble(Ask-OrderOpenPrice(),Digits)>=NormalizeDouble(TralStart*Point,Digits)))
                 {
                  SL=Bid-TralStep*Point;                               // then modify it
                  Text="Buy ";                                         // Text for Buy 
                  Modify=true;                                         // To be modified
                 }
              }
            else if(OrderType()==OP_SELL)
              {
               if((NormalizeDouble(SL,Digits)>NormalizeDouble(Ask+TralStep*Point,Digits) || NormalizeDouble(SL,Digits)==0) && (NormalizeDouble(OrderOpenPrice()-Bid,Digits)>=NormalizeDouble(TralStart*Point,Digits)))
                 {

                  SL=Ask+TralStep*Point;                               // then modify it
                  Text="Sell ";                                        // Text for Sell 
                  Modify=true;                                         // To be modified
                 }
              }

            if(Modify==true)
              {
               if(OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,clrDarkRed)==true)
                 {
                  Print("Trailing stop: Order ",Text,OrderTicket()," is modified successfully:)");

                 }
               else
                 {
                  Print("Trailing stop: Order ",Text,OrderTicket()," modification failed:)");

                 }
              }

           }
        }
     }
  }
  
Seng Joo Thio
578
Seng Joo Thio  
KNHConsultants:

i am a little new to m4l programming but i have some programming background. someone wrote a trailing stop function for me but i'm not sure if its working properly or maybe its where the function call is in the onTick function. here's the trailing stop code below. Please let me know if it is ok or needs tweaking

Have you run it? Did it behave as expected? If not, any error message?

It's easy to miss out errors by reading through codes alone, it's best to run and observe.

To add comments, please log in or register