Download MetaTrader 5

Is there a better way of programming an exit function?

To add comments, please log in or register
antony
549
antony  

Hello

I have a function on my EA that controls the exiting of trades:

void close(){
      if(OrderSelect(SELECT_BY_POS, SELECT_BY_POS)==true)
         if(GlobalVariableGet("period")!=Period() || !GlobalVariableCheck("symbol"+Symbol())){return;}
                                   
for(int i = OrdersTotal()-1; i>=0;i--){
                if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){

//+------------------------------------------------------------------+
if(OrderType() == OP_BUY){
      if(Rsi(0)>=70){OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),MarketInfo(Symbol(),MODE_SPREAD),Blue);} 
}

if(OrderType() == OP_SELL ){
      if(Rsi(0)<=30){OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),MarketInfo(Symbol(),MODE_SPREAD),Red);}
}
}
}
}

It seems even though my function looks for the correct Symbol() and Period() it seem that ticks from other chart times seem to trigger the closure of positions sometimes as soon as the trades are opened.

Can anyone see anything that I have missed?

My EA sets globalvariables as a trade is opened, and the close() function looks for the correct Symbol() and Period(), I run multiple timeframes and symbols so my EA must check that it is getting exit info from the same timeframe and chart that it made the entry on.

GlobalVariableSet("period",Period());GlobalVariableSet("symbol"+Symbol(),0);


Thanks


Antony

Keith Watford
Moderator
10566
Keith Watford  
GlobalVariableSet("symbol"+Symbol(),0)

 You are setting the value of the GV to zero, why?

If you are using the EA on more than 1 chart, why not simply assign a different magic number for each chart and check for that after OrderSelect? 

whroeder1
15574
whroeder1  
tonyjms2005: trigger the closure of positions sometimes as soon as the trades are opened.

  1. You know how to select by position
    for(int i = OrdersTotal()-1; i>=0;i--){
                    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
    
    What integer position are you selecting here?
          if(OrderSelect(SELECT_BY_POS, SELECT_BY_POS)==true)
    
  2. What filtering of all orders are you doing in your for loop? order accounting - MQL4 forum
To add comments, please log in or register