trailing stop problem

 

hi i have a problem with my trailing stop function in here

in the code below i just want to open a buy order then use the trailing stop in it but simply its not working and doesn't modify any orders. i would be thankful to pick your brain

input bool use_trailing=true;
input int when_to_trail=100;
input int trail_amount=100;
double pips;
int Magic=1234;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize==0.00001||ticksize==0.001)
   pips=ticksize*10;
   else pips=ticksize;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  adjust_trail();
   if(OrdersTotal()==0)
   int send=OrderSend(NULL,OP_BUY,0.02,Ask,5,Ask-50*pips,Ask+50*pips,NULL,Magic);
    
  }
//+------------------------------------------------------------------+
void adjust_trail()
{
for(int b=OrdersTotal()-1;b>=0;b--)
{
  if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
    if(OrderMagicNumber()==Magic)
          if(OrderSymbol()==Symbol())
                if(OrderType()==OP_BUY)
                   if(Bid-OrderOpenPrice()>when_to_trail*pips)
                     if(OrderStopLoss()<Bid-(pips*trail_amount))  
                      bool modify=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(pips*trail_amount),OrderTakeProfit(),0);
                        
}
}
 
saman89200: doesn't modify any orders. 
  1. Why do you expect it to trail starting at 100 pips when you have a take profit at 50 pips?
  2. int send=OrderSend(NULL,OP_BUY,0.02,Ask,5,Ask-50*pips,Ask+50*pips,NULL,Magic);
    
    bool modify=OrderModify
    You would know why, if you Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  3. Don't use NULL
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    • Zero is the same as PERIOD_CURRENT which means _Period. Don''t hard code numbers.
    • MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

  4. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  5.    if(OrdersTotal()==0)
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

Reason: