Help adding MaxTradesPerBar to EA - page 2

 
serpentsnoir:

megamixx,

If you are still out there, I can suggest a few code changes that will do what you're looking for. Let me know if you are still interested.

Then recompile to make sure it is without error. Let me know when you have this and I'll paste the next change.

Got it and compiled successfully thanks sn.
 
megamixx:

Got it and compiled successfully thanks sn.


okay, good.

Next, there is a block of code like this

int openPositions()
{
//... blah...
}

copy the entire block, paste it right after and rename it to OpenTradesOnThisCandle

Then, recompile to make sure it is without error.

I'm off to work now, so you won't hear from me until later. Post the changes if you get stuck.

 

Copied this block and renamed it, now it looks like this:

int OpenTradesOnThisCandle( )
{ int op =0;
for(int i=OrdersTotal()-1;i>=0;i--) // scan all orders and positions...
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() != MagicNumber) continue;
if ( OrderSymbol()==Symbol() )
{
if ( OrderType() == OP_BUY ) op++;
if ( OrderType() == OP_SELL ) op++;
}
}
return(op);
}

Hope I did it right sn. I compiled and got no errors but warning:

Function "OpenTradesOnThisCandle" is not referenced and will be removed from exp-file
0 error(s), 1 warning(s)

 

Good. The message is just a warning, and will go away soon.

Now change the line where you see MODE_TRADES to be MODE_HISTORY in the new subroutine you created.

Now replace both of the two if statements with OrderType() to this

if (OrderOpenTime() > Time[0]) op++;

When you have that (and you will get the same warning) compile it to make sure it is error free.

 
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() != MagicNumber) continue;
if ( OrderSymbol()==Symbol() )
  1. What are Function return values ? How do I use them ? - MQL4 forum
  2. If there are any server calls in the loop You must count down AND check return codes Loops and Closing or Deleting Orders - MQL4 forum
 

This is how the block looks now:

int OpenTradesOnThisCandle( )
{ int op =0;
for(int i=OrdersTotal()-1;i>=0;i--) // scan all orders and positions...
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if (OrderMagicNumber() != MagicNumber) continue;
if ( OrderSymbol()==Symbol() )
{
if (OrderOpenTime() > Time[0]) op++;
if (OrderOpenTime() > Time[0]) op++;
}
}
return(op);
}

Compiled, 0 errors, 1 warning

 

Hi megamix,

I don't read the thread from beginning, however ...

1. Use SRC button when posting code

2. The return of OrderSelect() is either true or false, so this is the correct way of writing OrderSelect(),

int OpenTradesOnThisCandle( )
  { 
  int op =0;
  for(int i = OrdersTotal() - 1;i >= 0; i-- ) // scan all orders and positions...
    {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == true   // if order select is true ...
        && OrderMagicNumber() != MagicNumber                  // ... and magic number is our magic number ...
        && OrderSymbol()      == Symbol() )                   // ... and symbol is chart symbol
        { 
        if (OrderOpenTime() > Time[0]) op++;             
        if (OrderOpenTime() > Time[0]) op++; 
        }
    } 
    return(op);
  }

:D

 
onewithzachy:

Hi megamix,

I don't read the thread from beginning, however ...

1. Use SRC button when posting code

2. The return of OrderSelect() is either true or false, so this is the correct way of writing OrderSelect(),

:D


Thanks for the tip on posting code. Makes sense. As for point #2, I'll defer to sn as he's walking me through the steps and don't want to mess up his process. Much appreciated !

 
megamixx:


Thanks for the tip on posting code. Makes sense. As for point #2, I'll defer to sn as he's walking me through the steps and don't want to mess up his process. Much appreciated !


we will keep it simple for now. onewithzacky is correct, that checking return codes is a good habit.

if (OrderOpenTime() > Time[0]) op++; <<== this only needs to be there once. So take one out.

The next thing to do is to find this code and duplicate it right below.

     if(TradesInThisSymbol > 0) 
     {
      return(0);
     }

Then change

(TradesInThisSymbol > 0)

to

(OpenTradesOnThisCandle() > MaxTradesPerCandle)

Then compile - the error should go away.

 
serpentsnoir:


we will keep it simple for now. onewithzacky is correct, that checking return codes is a good habit.

if (OrderOpenTime() > Time[0]) op++; <<== this only needs to be there once. So take one out.

The next thing to do is to find this code and duplicate it right below.

Then change

(TradesInThisSymbol > 0)

to

(OpenTradesOnThisCandle() > MaxTradesPerCandle)

Then compile - the error should go away.


Done.

Deleted duplicate if (OrderOpenTime() > Time[0]) op++;

and found and changed

(TradesInThisSymbol > 0)

to

(OpenTradesOnThisCandle() > MaxTradesPerCandle)

Many thanks for your helpful assistance on this.