Why my trailing stop not working? PLS need help

 

Hi all

I have a problem that I struggle with for the last two days with my trailing stop - it's not working and I can't figure why.

I'll appreciate any help, PLEASE.

This is my trailing stop code part :

extern bool                Use_TrailingStop=true;
extern double              StartTrailingAt=7;
extern double              TrailingStep=5;
extern double              TakeProfit=20;
extern double              StopLoss = 35;
extern int                 MagicNumber=1234;

void TrailingStop()
  {
   RefreshRates();

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)==false)
         continue;

      if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber && Use_TrailingStop==true)
        {
        Print("---TRAILINGSTOP step 1---");
        Print("---Bid price is : ", Bid);
        Print("---Open price price is : ", OrderOpenPrice());
        Print("---Current value is : ", Bid-OrderOpenPrice());
        Print("---Trailing start at is : ", point()*StartTrailingAt);
         if((Bid-OrderOpenPrice())>(point()*StartTrailingAt))
           {
            if(OrderStopLoss()<(Bid-(point()*TrailingStep)) || OrderStopLoss() == 0)
              {
               //--- modify order and exit
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),(Bid-(point()*TrailingStep)),OrderTakeProfit(),0,Green))
                  Print("OrderModify error ",GetLastError());
               return;                             
              }
           }
        }
      if(OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber && Use_TrailingStop==true)
        {
        Print("---TRAILINGSTOP step 1---");
        Print("---Bid price is : ", Ask);
        Print("---Open price price is : ", OrderOpenPrice());
        Print("---Current value is : ", OrderOpenPrice()-Ask);
        Print("---Trailing start at is : ", point()*StartTrailingAt);
         if((OrderOpenPrice()-Ask)>(point()*StartTrailingAt))
           {
            if(OrderStopLoss()>(Ask+(point()*TrailingStep)) || OrderStopLoss() == 0)
              {
               //--- modify order and exit
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),(Ask+(point()*TrailingStep)),OrderTakeProfit(),0,Red))
                  Print("OrderModify error ",GetLastError());
               return;                              
              }
           }
        }
     }
  }

double point(string symbol=NULL)  
{  
   string sym=symbol;
   if(symbol==NULL) sym=Symbol();
   double bid=MarketInfo(sym,MODE_BID);
   int digits=(int)MarketInfo(sym,MODE_DIGITS);
   
   if(digits<=1) return(1); 
   if(digits==4 || digits==5) return(0.0001); 
   if((digits==2 || digits==3) && bid>1000) return(1);
   if((digits==2 || digits==3) && bid<1000) return(0.01);
   
   return(0);
} 
 
  1. Lior Bercu:  it's not working and

    “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem


  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  3. extern double              StartTrailingAt=7;
                   if(!OrderModify(OrderTicket(),OrderOpenPrice(),(Bid-(point()*TrailingStep)),OrderTakeProfit(),0,Green))

    You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

    The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)

 
William Roeder #:
  1. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem


  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  3. You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

    The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)


Hi William,

Sorry, didn't pay attention - Not woking - The trailing stop function start but don't go over the :

if((Bid-OrderOpenPrice())>(point()*StartTrailingAt))

or

if((OrderOpenPrice()-Ask)>(point()*StartTrailingAt))

Also, the function does call RefreshRates();

I'll check the MODE_STOPLEVEL

 
Lior Bercu #:

Hi William,

Sorry, didn't pay attention - Not woking - The trailing stop function start but don't go over the :

if((Bid-OrderOpenPrice())>(point()*StartTrailingAt))

or

if((OrderOpenPrice()-Ask)>(point()*StartTrailingAt))

Also, the function does call RefreshRates();

I'll check the MODE_STOPLEVEL

I've checked the MODE_STOPLEVEL and it's 0.0014

So, I've changed 

StartTrailingAt=15;

And still nothing.

 
Lior Bercu #: And still nothing.

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Lior Bercu #:

I've checked the MODE_STOPLEVEL and it's 0.0014

So, I've changed 

And still nothing.

what is the number that your point() function is returning?