Limit orders in your EA

 

Hi Guys..

I am looking for a solution that could help very EAs.

Also i want to code expert advisor that could limited opening new positions for Each bar.

For example if we want to make new buy position by " if(High[0]>High[1]) ", then it allows to opening only 1 buy position for each Bar. On the other hand it is independed of other Bars, that means, when the Ea opens new buy position by current bar, it is possible to open another new buy position by the next bars if this condition is true. " if(High[0]>High[1]) "

I have earlier the below code in order to limit new orders, but it is not what i am looking for. because it limited the whole buy orders.

for(cpt = 0; cpt < OrdersTotal(); cpt++)
{
OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
{

if (OrderType() == OP_BUY)
{
NumBuys++;
}
else if (OrderType() == OP_SELL)
{
NumSells++;
}

//---------------

      if(High[0]>High[1] && NumBuys < 2)
      {
      
          OrderSend(Symbol(),OP_BUY,lot,Ask,Slippage,0,0,"",Magic,0,Navy);
      } 

//--------------- 

}
     
 
hmrt135:

Hi Guys..

I am looking for a solution that could help very EAs.

Also i want to code expert advisor that could limited opening new positions for Each bar.

For example if we want to make new buy position by " if(High[0]>High[1]) ", then it allows to opening only 1 buy position for each Bar. On the other hand it is independed of other Bars, that means, when the Ea opens new buy position by current bar, it is possible to open another new buy position by the next bars if this condition is true. " if(High[0]>High[1]) "

I have earlier the below code in order to limit new orders, but it is not what i am looking for. because it limited the whole buy orders.

If I understand you correctly you need a NewBar() function like this:

//<---- Check for new candle, returns true

bool NewBar()
{
        static datetime OldTime = 0;
        if(OldTime < Time[0])
        {
                OldTime = Time[0];
                return(true);
        }
        else
        {
                return(false);
        }
}

Then you could do something like this:

if(High[0]>High[1] && NewBar())  //<---- it will only do an OrderSend() if it is a NewBar and your condition is met So it should only do this once for each bar()
      {
      
          OrderSend(Symbol(),OP_BUY,lot,Ask,Slippage,0,0,"",Magic,0,Navy);  
      } 

Test the code no promises

 
danjp:

If I understand you correctly you need a NewBar() function like this:

Then you could do something like this:

Test the code no promises


thank you very much for your response. i will try it.
 
danjp:

If I understand you correctly you need a NewBar() function like this:

Then you could do something like this:

Test the code no promises


Unfortunatly it does not worked by my Ea, i Use the below code and it works perfect:

TRADE ONLY ONCE ON BAR

Up at the top where you declare your veriables put

static bool ITradedOnThisBar;


then where you send your order put

if(your critera && ITradedOnThisBar!=Bars)
{ticket=OrderSend(Symbol(),OP_BUY,... );
ITradedOnThisBar = Bars;}

Doing This will keep you from opening a trade on the same bar that you already opened a trade on, but more importantly you want to keep it from closing on the same bar so I would also add this code where you close your trade, for example:



if(Your Criteria && ITradedOnThisBar != Bars)
{
OrderClose(OrderTicket(),... ); // close position
}


This is probably the most simple way to do what you are trying to do.