# How to open 1 trade per bar

Hello,

I was wondering if there is anyone kind enough to tell me how you would program an expert advisor to open one trade only per bar. What I mean for this is, I have programmed an expert advisor that opens a buy trade when the current MACD is greater than the previous MACD, but opens a trade every time the price moves. I only want it to open 1 trade per bar, (the important bit is the 1 trade per bar, as I want it to open a trade for the next bar)

ie)

else

( do nothing )

If anyone can help, it would be much appreciated !

Cheers, Anthony

You can use OrdersTotal() function and magic number and for or while loop.

Combine those and check if your magic number currently exists, if the answer will be true, then you should say to ea - do not open any trades now (eg. with "if")

hi,

thanks for your help, but I couldn't figure out how to do it with what you suggested. however i have made a similar solution by saying only open a new trade if the time of the last trade was 15 mins ago (ie 1 trade every bar for the 15 min timeframe).

It seems to work, but it's not as accurate.

If anyone can give an example of code, it would be appreciated !

Cheers, Anthony

bool opened_on_current_bar = false;

for (int i = OrdersTotal() - 1; i >=0; i-- ) {

if ( ! OrderSelect( i, SELECT_BY_POS ) )

continue;

if ( OrderOpenTime() >= Time[ 0 ] ) {

opened_on_current_bar = true;

break;

}

}

// Here the variable opened_on_current_bar is set appropriately

ralph.ronnquist:
bool opened_on_current_bar = false;

for (int i = OrdersTotal() - 1; i >=0; i-- ) {

if ( ! OrderSelect( i, SELECT_BY_POS ) )

continue;

if ( OrderOpenTime() >= Time[ 0 ] ) {

opened_on_current_bar = true;

break;

}

}

// Here the variable opened_on_current_bar is set appropriately

Nice code but the condition OrderOpenTime() >= Time[0] will not work.

First some basic information:

OrderOpenTime() and Time[] are int's and they are like timestamp.

Time[0] is the current time value.

But lets say that we will use normal hours just for education purposes.

Assuming that we are currently on bar 12.00 on 1h chart, we open trade at 12.01, and lets say that we are 1 minute after that time (12.02).

Now lets check this condition:

if 12.01 >= 12.02 opened_on_current_bar = true;

will this code work ? I don't think so. And where is the magic number condition? Hm... try again

could you do something like this or would it be bad practice?

{

if(logic)sell;

}

}[/CODE]

[CODE]bool NewBar()

{

static datetime lastbar = 0;

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return (true);

}

else

{

return(false);

}

}

Time[0] is the opening time of the current bar.

ralph.ronnquist:
Time[0] is the opening time of the current bar.

No, Time[0] is the current time.