Trailingstop EA won't work - page 3

 
FMIC: Some advice; don't use "Ask" or "Bid" - use "OrderClosePrice()" instead. It is a real-time value irrespective of it being a Buy or Sell order.
Not real time.
  1. With Ask and Bid you must RefreshRates() after delays, i.e. between multiple server calls, after Sleep or a long computation.
  2. With OrderClosePrice, it gets updated only with a OrderSelect. Thus after Sleep or a long computation (or keeping an order selected between OnTick calls,) you must re-select.
    OrderSelect(OrderTicket(), SELECT_BY_TICKET); // Refresh OrderClosePrice
 
WHRoeder:
Not real time.
  1. With Ask and Bid you must RefreshRates() after delays, i.e. between multiple server calls, after Sleep or a long computation.
  2. With OrderClosePrice, it gets updated only with a OrderSelect. Thus after Sleep or a long computation (or keeping an order selected between OnTick calls,) you must re-select.

Thank you for the info. I was not aware that "OrderClosePrice()" was buffered. So I guess that neither "OrderClosePrice()" nor the "Ask"/"Bid" is the ideal real-time solution.

However, do you agree that "OrderClosePrice()" is a better overall solution than the use of "Ask"/"Bid" after the "OrderSelect()" is made?

And for a Real-time solution, if it is indeed needed, then I guess we can fallback on the alternatives:

  • Reselecting the Order with "OrderSelect()" (EDIT: forgot to list this one in the initial post, even thou you pointed it out)
  • "RefreshRate()" with "Ask"/"Bid"
  • "MarketInfo()" with "MODE_ASK"/"MODE_BID" (old MQL4 Style)
  • "SymbolInfoDouble()" with "SYMBOL_ASK"/"SYMBOL_BID" (new style MQL4+ and MQL5)
  • "SymbolInfoTick()" (which I already use in most of my EA's anyway)
 
FMIC: However, do you agree that "OrderClosePrice()" is a better overall solution than the use of "Ask"/"Bid" after the "OrderSelect()" is made?
absolutely.
 

Why are you trying to invent the wheel again?

Look here - you'll find everything you need.

If not start amending its source code.

 

Thank you for Everyone's help so far.  I think I have now resolved the error 130 issue, but there is something seriously wrong with my OrderSelect function, which is most likely why it only sometimes modify some of the orders.  Below is a list of things that I printed.  The OrderTicket # is completely wrong.  That ticket number belong to a trade I have open for the EURAUD.  While I did not include all the results, there are actually multiple Pairs where the OrderTicket numbers are not corresponding with the pair in the trade terminal.  

 Will someone please have a look and let me know what's wrong with OrderSelect? Thank you

 

2016.04.18 16:04:38.602 Trailing_v19 EURUSD,M15: ticket = 0    I think that ticket should print the same value as OrdersTotal, so it appears that my counter doesn't work.

2016.04.18 16:04:38.602 Trailing_v19 EURUSD,M15: OrderSelect = true

2016.04.18 16:04:38.602 Trailing_v19 EURUSD,M15: OrdersTotal = 11

2016.04.18 16:04:38.202 Trailing_v19 EURUSD,M15: Selectbypos = 0

2016.04.18 16:04:38.202 Trailing_v19 EURUSD,M15: Selectbyticket = 1

2016.04.18 16:04:38.202 Trailing_v19 EURUSD,M15: OrderTicket = 50213357

extern int TrailingStart=12;
extern int TrailingStop=5;
double stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
int TS=TrailingStart-TrailingStop;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init(){
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit(){
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   double Pip=Point*10;
   int ticket=0;
   if(TS<stoplevel) TrailingStart=(int)stoplevel+TrailingStop+2;
   
   Print("OrdersTotal = ",OrdersTotal());
   Print("OrderSelect = ",OrderSelect(OrderTicket(),SELECT_BY_TICKET));
   Print("ticket = ",ticket);                     
   Print("OrderTicket = ",OrderTicket());                     
   Print("Selectbyticket = ",SELECT_BY_TICKET);                     
   Print("Selectbypos = ",SELECT_BY_POS);                     
                        
   {for(int i=OrdersTotal()-1; i>=0; i--){ //I added more curly brackets{}
      if(OrderSelect(i,SELECT_BY_POS)){
      ticket++;{
         if(OrderSymbol()==_Symbol){
            if(OrderType()==OP_BUY){
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if((Bid-OrderOpenPrice())>(TrailingStart*Pip)){ //For now, I have changed back to Bid and Ask and will include OrderClosePrice() later on.
                     if(TrailingStop*Pip<Bid-(TrailingStop*Pip)){ //I have added TrailingStop*Pip before the '<'
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailingStop*Pip),OrderTakeProfit(),Blue)) //I removed the !
                        return(0); //I replaced the Getlasterrror with a return value
                        RefreshRates();
                        }     
                    }  
                }     
            }  
            if(OrderType()==OP_SELL){
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if((OrderOpenPrice()-Ask)>(TrailingStart*Pip)){
                     if(TrailingStop*Pip>Ask+(TrailingStop*Pip)){
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailingStop*Pip),OrderTakeProfit(),Red))
                        return(0);
                        RefreshRates();
                       }
                    }
                }  
            }
         }
      } 
   }
}  
  return(0);
}
//+------------------------------------------------------------------+

 



 

Hi Everyone.  Thank you for all the help.  I found the mistake and the issues have been resolved.  Since I do not use a Stoploss, the value of OrderStopLoss() is always 0.  This is not a problem when modifying the buy order because 0 is always less than Bid-TrailingStop*Pip.  But with the sell order 0 can never be more than Ask+Trailingstop*Pip.  So I had to add the or(|| OrderStopLoss()) to this line:

if(OrderType()==OP_SELL){
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if((OrderOpenPrice()-Ask)>(TrailingStart*Pip)){
                     if(OrderStopLoss()>Ask+(TrailingStop*Pip) || OrderStopLoss()==0){ //Added OrderStopLoss()==0
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailingStop*Pip),OrderTakeProfit(),Red))



 

 
Trader3000 That ticket number belong to a trade I have open for the EURAUD.
No filtering on your OrderSelect loop. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
 
WHRoeder:
Trader3000 That ticket number belong to a trade I have open for the EURAUD.
No filtering on your OrderSelect loop. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum

Thank you very much for the help.  Yes, there is definitely an issue with OrderSelect because it only works on some charts.  The other problem is that I cannot include a MagicNumber because the trades are placed manually.  I used the portion of the code that I think is relevant, but I cannot get it to compile.  It shows this error:

'MySelect' - function can be declared only in the global scope Trailing_v26.mq4 31 13

and line 31 is this one:

bool    MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){

 Here is the whole EA:

#property strict;
extern int TrailingStart=10;
extern int TrailingStop=5;
double stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
int TS=TrailingStart-TrailingStop;
bool    MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES);
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init(){
   if(TS<stoplevel){
      MessageBox("Please note: Your inputs for TrailingStart and/or TrailingStop cannot"+
                 "\nbe less than the minimum levels required by your broker and the"+
                 "\nTrailingStart has been increased automatically to "+StringConcatenate(stoplevel+TrailingStop)+" pips");
     } 
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit(){
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   double Pip=Point*10;
   if(TS<stoplevel) TrailingStart=(int)stoplevel+TrailingStop;
           
    bool    MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){
    if (!OrderSelect(iWhat, eSelect, ePool))     return (false);
    if (OrderSymbol()      != _Symbol)           return (false);
    if (ePool != MODE_HISTORY)                   return (true);
    return(OrderType() <= OP_SELL);
}
    for(int i = OrdersTotal()-1; i >= 0; i--)
    if (MySelect(i, SELECT_BY_POS)){

    for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--)
    if (MySelect(iPos, SELECT_BY_POS, MODE_HISTORY)){
        int nextTkt = OrderTicket();
     
              if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if((Bid-OrderOpenPrice())>(TrailingStart*Pip)){
                     if(OrderStopLoss()<Bid-(TrailingStop*Pip)){
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailingStop*Pip),OrderTakeProfit(),Blue))
                        Print("Buy = ",GetLastError());
                        return(0);
                        RefreshRates();
                        }     
                     }  
                  }     
               }
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if((OrderOpenPrice()-Ask)>(TrailingStart*Pip)){
                     if(OrderStopLoss()>Ask+(TrailingStop*Pip) || OrderStopLoss()==0){
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailingStop*Pip),OrderTakeProfit(),Red))
                        Print("Sell = ",GetLastError());
                        return(0);
                        RefreshRates();
                        }
                     }
                  }  
               }
            
         
         
  return(0);
}
//+------------------------------------------------------------------+

 



 
Trader3000:

Yes, there is definitely an issue with OrderSelect because it only works on some charts.  The other problem is that I cannot include a MagicNumber because the trades are placed manually.  I used the portion of the code that I think is relevant, but I cannot get it to compile.

I gave you a perfectly good skeleton code on which to build on, which fixes your problems and even listed them prior to that, yet you choose to ignore it and continue to struggle wondering why your code is not doing what you want!

Why? Do you want your code not to work? Use the example and test it yourself!

 
He thinks he can just cut and paste and doesn't have to learn to code. He can't even fix this simple error without asking:
'MySelect' - function can be declared only in the global scope Trailing_v26.mq4 31 13
Reason: