Entry Issue - Donchian Channel on Bar Close

 
My entry technique: I intend to enter long or short when the current price bar closes above or below a Donchian Channel (50 day market high or low)

......

double DonchianLowerEntry = iCustom(NULL,0,"DonchianChannel2",2400,3,-2,0,100,0,0); // Entry for Lower Donchian Channel - 50 days, 2400
double DonchianUpperEntry = iCustom(NULL,0,"DonchianChannel2",2400,3,-2,0,100,1,0); // Entry for Upper Donchian Channel - 50 days, 2400
double ClosingPrice = iClose(NULL,0,0); //Closing Price of current bar

......

if(ClosingPrice>DonchianUpperEntry) // Closing price is greater than the Donchian upper channel
{
if(trend>0) // Trend is up - MACD indicator filter
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-TrailingStop,0,"Alcatraz: Long Order Placed",16384,0,Green); // Open Long order

.....

Expert Advisor is opening a trade when bar penetrates Donchian Channel, but does not close above the Channel. It should be opening trade only when it CLOSES above the channel. Does anyone know why this might be happening?
 
Current bar is not finished, and the "close" of bar zero is the (still changing) current bid.
 
My understanding is that the expert advisor should only run when a new tick comes through. Is there a way to code only for entry on closing price (30 min chart)?
 
A new tick starts a run through your code.
Look at your code above, the last parameter - 0 - is a bar index. If 0 then you are calculating the using the unfinished current bar. Change it to 1 to calculate on the most recently completed bar.
 
Changing the bar index parameter worked perfectly. Thanks, Phy.
Reason: