Download MetaTrader 5

Retrieve information about number of trades opened

To add comments, please log in or register
marco_93
93
marco_93  

Hi,

I am trying to open at the beginning of a candle an order (buy/sell) with relative stop loss and take profit, after a generic signal is given at the end of the previous candle.

Clearly it should be opened only one trade in the candle after the signal. 

How can I tell the EA not to open potentially infinite trades in the same candle (due to the fact that the condition of the previous candle signal is constantly verified), I mean how can I write "don't open another trade in that candle because there was already opened a position in this current period"?

There is no limitation in the position that can be held. But there is a limitation in the position that can be opened 

in a candle ( one position after a signal is given, and for that period no more trades).


void OnTick()
  {
        stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);

	//what to put here
                
              sell = (AB(5,1)!=EMPTY_VALUE)&&(AB(1,1)!=EMPTY_VALUE);
              buy = (AB(5,1)!=EMPTY_VALUE)&&(AB(0,1)!=EMPTY_VALUE);
        
              if (buy)
              { 
                 slBuy= NormalizeDouble(AB(0,1),Digits);
                 OPBUY();     
              }
        
              if (sell)
              { 
                 slSell=NormalizeDouble(AB(1,1),Digits);
                 OPSELL(); 
              }              
}

Thanks for your help

whroeder1
15902
whroeder1  
marco_93: How can I tell the EA not to open potentially infinite trades in the same candle

Write an OrderSelect loop and count them. You should already have one, how else do you recover?

EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
marco_93
93
marco_93  
whroeder1:

Write an OrderSelect loop and count them. You should already have one, how else do you recover?

Thanks for your replay.


I have thought to this solution: the EA trade at the open of each new bar (only) by saving the timestamp of the current bar to a class variable.

I will use the price and indicators values of the previous bar to make trade decisions. So I have in this case skip the OrderSelect.


Create the CNewBar Class called Timer.mqh

class CNewBar
{
   private:
      datetime _time[], _lastTime;
      
   public:
      void CNewBar();
      bool CheckNewBar(string pSymbol, ENUM_TIMEFRAMES pTimeframe);
};

void CNewBar::CNewBar(void)
{
   ArraySetAsSeries(_time,true);
}

bool CNewBar::CheckNewBar(string pSymbol, ENUM_TIMEFRAMES pTimeframe)
{
   bool firstRun = false, newBar = false;
   CopyTime(pSymbol,pTimeframe,0,2,_time);
   
   if(_lastTime == 0) firstRun = true;
   
   if(_time[0] > _lastTime)
   {
      if(firstRun == false) newBar = true;
      _lastTime = _time[0];
   }
   
   return(newBar);

The EA

#include <C:\MQL4\Include\Timer.mqh>

CNewBar NewBar;
input bool TradeOnNewBar = true;
/*other input variables*/

void OnTick()
{
   bool newBar = true;
   int barShift = 0;

   if (TradeOnNewBar == true)
   {
        newBar = NewBar.CheckNewBar(_Symbol,_Period);
        barShift = 1;
   }
     
   if (newBar == true)
   {   
     stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
                
     sell = (AB(5,1)!=EMPTY_VALUE)&&(AB(1,1)!=EMPTY_VALUE);
     buy = (AB(5,1)!=EMPTY_VALUE)&&(AB(0,1)!=EMPTY_VALUE);
        
     if (buy)
     { 
       slBuy= NormalizeDouble(AB(0,1),Digits);
       OPBUY(); 
     }
        
     if (sell)
     { 
       slSell=NormalizeDouble(AB(1,1),Digits);
       OPSELL();  
     }
   }
}   

The problem is that the compiler gives me this error

'Period' - cannot convert enum 
referring to this line 
newBar = NewBar.CheckNewBar(_Symbol,_Period);
marco_93
93
marco_93  

Solved

just change 

ENUM_TIMEFRAMES

with

int

Thanks

whroeder1
15902
whroeder1  
Enumerations are your friend. Just change Period to ENUM_TIMEFRAMES(Period)
To add comments, please log in or register