Open Limited Number of trades in trend, then stop

 

Good day, 

I am trying to write a code that counts the number of trades that have been opened in a new trend. That is, if there is a trend (e.g.  MA1 > MA2) the EA opens trades until the set number is reached, and then it stops and waits until there is a new trend. Then it starts counting again from 0.   

I have written the code below. It is in 2 parts.

Part 1 - checks that MA has crossed recently, and it is therefore a new trend. 

Part 2 - starts counting trades as soon as it is a new trend. 

The code works to a certain extent - it counts trades that have been opened since the new trend - usually up to 2 or 3, but then it just stops counting and then continues opening more trades. 

Please let me know what my mistake is. 

NB: I am a beginner and wrote this by researching a lot, so any pointers would be greatly appreciated.  


extern int     MaxTradesTrend=3;                         //Maximum Number of Trades Per Tren

bool IsNewTrend() 
{
   if((OrdersHistoryTotal()>=0))   
   { for(int c=0; c<=OrdersHistoryTotal(); c++)  
     { if(OrderSelect(c,SELECT_BY_POS,MODE_TRADES)==true)    
       { if(OrderSymbol() == Symbol() )      // check when MA bar crossed
         { if(OrderSymbol()!=Symbol())
            { continue; }
           if((OrderType() == OP_BUY) )    // check if MA bar crossed (less thank 6 bars ago)
            { if( (MA1b<MA2b) || (MA1c<MA2c) || (MA1d<MA2d) || (MA1e<MA2e) || (MA1f<MA2f) )    
                 { return true; }} 
           if((OrderType() == OP_SELL) )   // check if MA bar crossed (less thank 6 bars ago)
            { if( (MA1b>MA2b) || (MA1c>MA2c) || (MA1d>MA2d) || (MA1e>MA2e) || (MA1f>MA2f) )   
                 { return true; }} 
     }}}}  return false; //Not a new trend 
} 
//+----------------------------------------------------------------------------------------------------------------------------------------

int TradesInTrend()                      //Count Open Orders In Trend
{  int TotalTradesInTrend=0;
   if( IsNewTrend()  )
   {  for(int u=0; u<OrdersHistoryTotal(); u++)
      {  if(OrderSelect(u,SELECT_BY_POS,MODE_TRADES)==false)
            { continue; }
         if(OrderSymbol()!=Symbol())
            { continue; }
         TotalTradesInTrend++; 
      }} return(TotalTradesInTrend+OrdersTotal()); 
}
//+----------------------------------------------------------------------------------------------------------------------------------------

void Buy_Sell() 
{  
 if((TradesInTrend()<MaxTradesTrend) )   
   {  // open trades
}}
 

maybe this will help. EA opens the maximum number of trades entered in total ("OnlyOne"), or the maximum number of trades entered in direction ("OneDir").




 


enum onoff {Ano=1,Ne=0};

extern onoff OnlyOne=Ano;//Pouze jeden obchod celkem
extern onoff OneDir=Ano;//Pouze jeden obchod ve smeru
extern int MaxTrades=1;//Max obchodu
     

<Screenshot deleted>

 
Jiri Kasparek:

<Screenshot deleted>

Please do not show screenshots of code.

Copy and paste the code correctly, using the code button.

 
      }} return(TotalTradesInTrend+OrdersTotal()); 

Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select 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 programming forum 2013.02.15
          PositionClose is not working - MQL5 programming forum 2020.02.21
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles 24 July 2006
          Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 1 February 2011

 
Jiri Kasparek:

maybe this will help. EA opens the maximum number of trades entered in total ("OnlyOne"), or the maximum number of trades entered in direction ("OneDir").




<Screenshot deleted>

Hi there...

Please reshare your code? 

 

I have figured it out. 

Thanks though...

Reason: