Bar Shift Problem?

 

Hi

I'm experiencing troubles with formulating the order condition to open trades.

condition 1) open long when the previous candle opened below a MA and the current candle opens above

Sometimes a position is opened even though the previous candle's open was not below the MA (condition not met).

condition 2) open short when an engulfing looking pattern occurs at the upper BB (= shift 2 candle opens below and closes above BB, shift 1 candle opens above and closes below BB)

The trigger candle should be the shift 1 candle opening above and closing below the BB, which means the EA should open a position at the open of the zero bar. But the EA opens the position one candle after the zero bar.

Please have a look at the mq4 below. If you need a better illustration of the problem run it in the strategy tester in visual mode (5min period EUR/USD between 2010.05.18 and 2011.05.31; the first three trades sum up the entire dilemma)


Thank you very much in advance.

Files:
 
Any suggestions?
 

Removed every mention of Bars or Barcount and replaced it with Time0 or Time[0] respectively.

Thx even though that didn't resolve the problem.

 

The problem might have its roots somewhere else. I simplified the order conditions so that the EA opens a position when one candle closes at the same value/price as its respective Bollinger Band.

e.g. the close of the candle was at 1.2292 so was the value of the Bollinger Band but no trade was triggered. So I thought it has something to do with spread or Bid/Ask difference or what not and so I modified the condition to -> close == Lower Bollinger Bands + Points * Spreadvalue with 1, 2, 3 up to 4 pips (we are talking about EUR/USD here in a time of low volatility). Still no trade.

What could it be?

 
Yojimbo:What could it be?
No mind readers here. Put some Print() statements around your IFs with the values of the variables and find out why.
if ((BBdis >= Point * BBdisval && prevMA21 > oldMA21 && prevOpen <= prevMA21
&& currOpen > currMA21 && OpenUBBdis > Point * OpenUBBdisval) || (BBdis >=
Point * BBdisval && (oldOpen > oldLBB && oldClose < oldLBB) && prevOpen <
prevLBB && currOpen > prevLBB)) Order = SIGNAL_BUY;
is unreadable, factor it out, make it self documenting
bool bbOK       = BBdis >= Point * BBdisval,
     prevOK     = prevMA21 > oldMA21,
     crossMA= prevOpen <= prevMA21 && currOpen > currMA21;
Print("bbOK=",bbOK," prevOK=",prevOK, "crossMA=",crossMA);
if ( (bbOK && prevOK && crossMA && OpenUBBdis > Point * OpenUBBdisval
     ) || (BBdis >= Point * BBdisval && (oldOpen > oldLBB && oldClose < oldLBB) &&
Reason: