Buying/Selling only once per Bar

 

Hi all,

I have been working on a simple support/resistance EA. The EA places a buy or sell order when the price
meets the support/resistance line and the preceeding bar meets certain criteria. The problem I have is that
if I close out the position (for whatever reason), the EA creates another buy/sell because the conditions are
still being met (eg, if I am on a 15 min chart and close the order 6 mins after the new bar started, the conditions
are still being met for the next 9 mins and the EA will therefore open another order).

I have tried to get the EA to only carry out the Technical Analysis when the next bar opens, but this doesnt seeem to
stop it from opening another order once one is close. To accomplish this, I have been using the following code :

datetime BarTime1 = 0;

if (BarTime1 < Time[0])

BarTime1 = Time[0];

TechnicalAnalysis()

Is this the correct method of instructing the EA to carry out the TechnicalAnalysis only on every new bar ?
Is the there any other way of stopping the EA opening a new order once one is closed (as described above) ?

Thanks for taking the time to help a noob ! ;)

 
Use a bool variable to record the fact that an order has been placed during a bar . . . when you get a new bar reset that variable. Check the state of this variable prior to placing an order.
 

Ok great thanks for that RaptorUK...that makes sense. I will give that a go.

 
JoBlo:

datetime BarTime1 = 0;

if (BarTime1 < Time[0])

BarTime1 = Time[0];

TechnicalAnalysis()


  1. BarTime1 must be static or global.
  2. use != not < (in case server resets their clocks)

static datetime Time0;
bool newBar = Time0 != Time[0];  Time0 = Time[0];
TechnicalAnalysis();
 
WHRoeder:

  1. BarTime1 must be static or global.
  2. use != not < (in case server resets their clocks)


Thank you WHRoeder for the help...that has sorted the problem.
Reason: