# Week Open Price for indicator

968

Hi,

I try below code for Week Open Price on Indicator. I have not any error code but below code does not shows me correct time.

if(TimeDayOfWeek(Time[i]) != TimeDayOfWeek(Time[i+1]))

Any help would be better.

Best,

Max

968

I use below code for Day Open Price.

if(TimeDay(Time[i]) != TimeDay(Time[i+1]))

It does work for me. Even Month and Year, but Week is not.

Thanks,

Max

17954

Max-Enrik: I try below code for Week Open Price on Indicator. I have not any error code but below code does not shows me correct time.
if(TimeDayOfWeek(Time[i]) != TimeDayOfWeek(Time[i+1]))

Your code asks if bar i is the start of a new day not a new week. You want to find the "Week Open Price," here are three ways:

1. double wop = iOpen(NULL, PERIOD_W1, 0)
2. datetime  BOW = iTime(NULL, PERIOD_W1, 0);
int      iBOW = iBarShift(NULL,0, BOW);
double   wop  = Open[iBow];
3. Find the first bar of the new week: the DOW(Monday) < DOW(Friday)
int i=0;
while( TimeDayOfWeek(Time[i]) >= TimeDayOfWeek(Time[i+1]) ) ++i;
double wop = Open[i];

968

WHRoeder:

int i=0;
while( TimeDayOfWeek(Time[i]) >= TimeDayOfWeek(Time[i+1]) ) ++i;
double wop = Open[i];

That was awesome. Yeah, I found what I want. Thank you so much - @WHRoeder

Everything OK at any TimeFrames. But indicator Opened wrong Price & Time when I want to use it at the "W1 / Weekly" TimeFrame.

Best,

Max

968

WHRoeder:
Thanks for your help, that was helped me and I was solved my issue.
968

Hi,

I am using this code for my Daily Fibo object.
So, everything ok, but when I start MT4 platform I should restart once for my indicator will refresh (for works today, otherwise won't shows correctly for today).

I cannot find whats wrong with this code.

All the best.

int start()
{
int indicatorCounted = IndicatorCounted();
if( indicatorCounted < 0 ) { }
if( indicatorCounted > 0 ) { indicatorCounted--; }

int days = 0;
int limit = Bars - indicatorCounted;
for(int i = 0; i < limit; i++) {
if( TimeDay(Time[i]) != TimeDay(Time[i+1]) ) {

double   openDay   = Open[i];
datetime openTime  = Time[i];
datetime closeTime = Time[i] + 60 * 60 * 24;

string levelOpen = openDay + pips2dbl                                        ;
ObjectCreate(levelName, OBJ_FIBO, 0, openTime, levelOpen, closeTime, openDay );
...

968

I am still waiting good reply, thanks.

Moderator
15800

string levelOpen = openDay + pips2dbl

Why is this a string? It should be a double.

ObjectCreate(levelName, OBJ_FIBO, 0, openTime, levelOpen, closeTime, openDay );

Once this has been created it cannot be created again with the same name.

968

GumRai:

Once this has been created it cannot be created again with the same name.

As you mentioned, I has been fixed like below.

string levelName = "- Daily Open - " + TimeMonth( Time[0] ) + "/" + TimeDay(Time[0]);

I tested with 'Strategy Tester' right now yes new FIBO object opens, but previous day FIBOs does not remove even I choice History 1 like below code.

int History = 1;
...
days--;
}
if(days >= History ) { break; }

Moderator
15800

Please study this and fully understand what is happening in the code

static datetime bar_time=0;
if(bar_time==Time[0]) //Only check if a new bar///
return(0);
bar_time=Time[0];

static datetime openTime=0;

int i=0;

while(TimeDay(Time[i])==TimeDay(Time[i+1]))
i++;
if(openTime!=Time[i])
{
openTime=Time[i];
double   openDay   = Open[i];
datetime closeTime = Time[i] + 60 * 60 * 24;
double levelOpen=openDay+pips2dbl;

if(ObjectFind(0,levelName)!=0)
ObjectCreate(levelName,OBJ_FIBO,0,openTime,levelOpen,closeTime,openDay);
else
{
ObjectMove(0,levelName,0,openTime,levelOpen);
ObjectMove(0,levelName,1,closeTime,openDay);

}
}

The Fibo is only created once and then moved at the start of a new day.

968

Almost I will spend whole of my life for this code Man. I solved my issue with your brilliant code! I studied from your like an educational code.

Thank you so much more!