Single Trade Entery per Candle or Bar - How To

 

Does anyone know what code to insert and where to place it, to limit one entry per a candle within an EA.


I've searched the forum and I've found some code pieces, but not where to insert them.


What is the best code for this function that would work on any selected timeframe?


I've attached a sample EA for this to be inserted into. However I would like to do this for other EA's as well.


Thanks!

Files:
 
phorexpress:

Does anyone know what code to insert and where to place it, to limit one entry per a candle within an EA.


I've searched the forum and I've found some code pieces, but not where to insert them.


What is the best code for this function that would work on any selected timeframe?


I've attached a sample EA for this to be inserted into. However I would like to do this for other EA's as well.


Thanks!

bool NewBar()

{

static datetime lastbar = 0;

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return (true);

}

else

{

return(false);

}

}


if(NewBar())

{

//open trade here

}

 
robofx.org:

bool NewBar()

{

static datetime lastbar = 0;

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return (true);

}

else

{

return(false);

}

}


if(NewBar())

{

//open trade here

}

Thanks I will try this

 

This does not work.

Here is what I have done:

I copied the code into an .mqh file (NewBar.mqh).

Next, I entered an include statement in my .mq4 file, #include <NewBar.mqh>.

The following logic does not work:

if(condition1 && condition2 && NewBar())

// open trade

However, the following logic does work:

if(condition1 && condition2)

// open trade

Where am I going wrong?

 
rayeni wrote >>

This does not work.

Here is what I have done:

I copied the code into an .mqh file (NewBar.mqh).

Next, I entered an include statement in my .mq4 file, #include <NewBar.mqh>.

The following logic does not work:

if(condition1 && condition2 && NewBar())

// open trade

However, the following logic does work:

if(condition1 && condition2)

// open trade

Where am I going wrong?

I think I found my error:

It should be: if(condition1 && condition2 && NewBar() == true)

 
rayeni:

I think I found my error:

It should be: if(condition1 && condition2 && NewBar() == true)

Try changing

#include <NewBar.mqh>

to

#include "NewBar.mqh"

 

this is O.K. but if we need to make one trade every 4 bars? how we do it ?

Thank you.

 
  1. rayeni:

    I think I found my error:

    It should be: if(condition1 && condition2 && NewBar() == true)

    if NewBar() returns a bool then 'if (aBool == true)' and 'if (aBool)' are equivalent
  2. but if we need to make one trade every 4 bars? how we do it ?
    static datetime lastTrade;
    if (... 
    && Time[0] >= lastTrade + 4*Period()*60){ // Only once every 4 bars
       if (OrderSend(...) < 0) Alert("orderSend failed: ",GetLastError());
       else                    lastTrade = Time[0];
    }