Detect the first Bar of Day, Week, Month

Created at 29 Mar 2013, 13:23
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TH

thakas

Joined 22.02.2013

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


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

thanks


@thakas
Replies

cAlgo_Fanatic
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        
    }
    //...


 

 


@cAlgo_Fanatic

jhtrader
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
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.


@atrader

hamsider
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