Detect the first Bar of Day, Week, Month

thakas since: 22 Feb 2013;

  29 Mar 2013, 13:23
Detect the first Bar of Day, Week, Month

I would like to inform me how can I detect the first bar of the day, week, month.

thanks

cAlgo_Fanatic's avatar

cAlgo_Fanatic since: 10 Jan 2013;

  29 Mar 2013, 14:54

Hello,

Try this code:

public override void Calculate(int index)
{
    if (index == 0) return;

    DateTime currentTime = MarketSeries.OpenTime[index];
    DateTime previousTime = MarketSeries.OpenTime[index-1];

    if(currentTime.Month != previousTime.Month)
    {
        // first bar of the month           
    }
    if(currentTime.DayOfWeek == DayOfWeek.Monday && previousTime.DayOfWeek != DayOfWeek.Monday)
    {
        // first bar of the week        
    }
    if(currentTime.Date != previousTime.Date)
    {
        // first bar of the day        
    }
    //...


 

TRADERS FIRST™
jhtrader's avatar

jhtrader since: 15 Oct 2013;

  06 Nov 2013, 18:31
RE:Time

Hi,

If we wanted to use this in a robot you cant use calculate method...

Is this equivalent to

index = MarketSeries.OpenTime[MarketSeries.OpenTime.Count - 1];

Also what UTC timeframe does the code assume.. if I use UTC - 5 do I have to change it to Sunday?

cAlgo_Fanatic said:

Hello,

Try this code:

public override void Calculate(int index)
{
    if (index == 0) return;

    DateTime currentTime = MarketSeries.OpenTime[index];
    DateTime previousTime = MarketSeries.OpenTime[index-1];

    if(currentTime.Month != previousTime.Month)
    {
        // first bar of the month           
    }
    if(currentTime.DayOfWeek == DayOfWeek.Monday && previousTime.DayOfWeek != DayOfWeek.Monday)
    {
        // first bar of the week        
    }
    if(currentTime.Date != previousTime.Date)
    {
        // first bar of the day        
    }
    //...


 

atrader's avatar

atrader since: 10 Oct 2012;

  07 Nov 2013, 17:32
RE: RE:Time

jhtrader said:

Hi,

If we wanted to use this in a robot you cant use calculate method...

Is this equivalent to

index = MarketSeries.OpenTime[MarketSeries.OpenTime.Count - 1];

Also what UTC timeframe does the code assume.. if I use UTC - 5 do I have to change it to Sunday?

yes and yes. you can use MarketSeries.OpenTime.LastValue for convenience. You can also set the robot timezone.

"A lie gets halfway around the world before the truth has a chance to get its pants on." - Sir Winston Churchill

hamsider since: 16 Sep 2021;

  21 Sep 2021, 12:47
RE:

cAlgo_Fanatic said:

Hello,

Try this code:

public override void Calculate(int index)
{
    if (index == 0) return;

    DateTime currentTime = MarketSeries.OpenTime[index];
    DateTime previousTime = MarketSeries.OpenTime[index-1];

    if(currentTime.Month != previousTime.Month)
    {
        // first bar of the month           
    }
    if(currentTime.DayOfWeek == DayOfWeek.Monday && previousTime.DayOfWeek != DayOfWeek.Monday)
    {
        // first bar of the week        
    }
    if(currentTime.Date != previousTime.Date)
    {
        // first bar of the day        
    }
    //...

Any idea how to get the actual index of Day start, Week start and Month start?

I'm confused there should be a quick way to get them. help is highly appreciated. 

thank you