open and close values in array always identical

 

Hello,

find below a smal ea which demonstrates the problem: When using CopyOpen and CopyClose, values of open and close of last index (highest) always identical. 

int minbarcount=10;
int lastbarcount=0;

int OnInit()
{
        return(0);
}
void OnDeinit(const int reason)
{
}
void OnTick()
{

        if (Bars(_Symbol, _Period) > lastbarcount) 
        {
                lastbarcount = Bars(_Symbol, _Period);
        }
        else
                return;
        
        if(lastbarcount > minbarcount)
        {
                double open[];
                double close[];
                //ArraySetAsSeries(open,true);
                //ArraySetAsSeries(close,true);
                int copied_o=CopyOpen(Symbol(),PERIOD_CURRENT,0,minbarcount,open);
                int copied_c=CopyClose(Symbol(),PERIOD_CURRENT,0,minbarcount,close);
                for(int i=0;i < minbarcount;++i)
                        Print(StringFormat("o[%d]=%.5f c[%d]=%.5f",i,open[i],i,close[i]));
        }
}

 

Can anyone explain why this is so, or what's the sense or is there a mistake in the code.

Thanks in advance 

 
chinaski:

Hello,

find below a smal ea which demonstrates the problem: When using CopyOpen and CopyClose, values of open and close of last index (highest) always identical. 

 

Can anyone explain why this is so, or what's the sense or is there a mistake in the code.

Thanks in advance 

Open[minbarcount-1] and Close[minbarcount-1] are the values of open and close for the current bar (the last bar in the chart). As your code is only executed when there is a new bar Open=Close (and also =High=Low).

I also draw your attention to the fact that it is not reliable to use Bars () to detect a new candle/bar.

 
angevoyageur:

Open[minbarcount-1] and Close[minbarcount-1] are the values of open and close for the current bar (the last bar in the chart). As your code is only executed when there is a new bar Open=Close (and also =High=Low).

I also draw your attention to the fact that it is not reliable to use Bars () to detect a new candle/bar.

Thanks. God explanation. Makes sense.

So what do you suggest to detect new bar ? Obiviously it works, when open and close always same for latest...? 

 
chinaski:

Thanks. God explanation. Makes sense.

So what do you suggest to detect new bar ? Obiviously it works, when open and close always same for latest...? 

See this code as example.
 
angevoyageur:
See this code as example.
Thank you for code. You are a living manual.
 
chinaski:
Thank you for code. You are a living manual.
Thank you, but to be honest I was just reading an article on this subject when I saw your question ;-)
 
angevoyageur:
Thank you, but to be honest I was just reading an article on this subject when I saw your question ;-)
Lucky man. I have no talent in searching this forum database.
 

But please do not understand this wrong. I do not expect other people to search for me.

My hope is always somebody has a solution in mind or minds a ->direct<- link to an article where this problem was discussed or solved. 

 
chinaski:

But please do not understand this wrong. I do not expect other people to search for me.

My hope is always somebody has a solution in mind or minds a ->direct<- link to an article where this problem was discussed or solved. 

Don't worry, ask your questions. So if I think it is easy to find I will send you packing ;-)
 
angevoyageur:
Don't worry, ask your questions. So if I think it is easy to find I will send you packing ;-)

Thank you angevoyageur. Right now, having next issue.

Reason: