Click here to Skip to main content
13,046,087 members (66,873 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 5 Mar 2012

Converter to convert OHLC data to a larger timeframe

, 12 Mar 2012
Rate this:
Please Sign up or sign in to vote.
Converter to convert OHLC data to a larger timeframe


Function to covert an array of bars to bars of greater time interval. The bar can be immutable. 

It also adjusts the time on the output to match the boundary. 

For example: Convert an array of 1 min bars to 30 min or 1 hour.  Since the output is boundary adjusted, you can expect the output time to be 1:00, 1:30, 2:00 and so on for 30 min interval.   


Brokerage firms send stock bar information at various intervals. Automatic trading system could have strategies that depend on a different time interval compared to the ticks received. I wrote this function to covert a given array of bars to a larger time interval.

Using the code

Call the function below to get a converted arrary. Make sure the input is in DateTime ascending order. 

var time_increment = new TimeSpan(0, 30, 0); // 30 min bar
var converted_data = ConvertOpenHighLowCloseData.to_larger_timeframe(@bars_to_convert, time_increment); 

Here is the code that does the conversion 

// bars to convert should be order by date time
static public Collection<Bar> to_larger_timeframe(IEnumerable<Bar> bars_to_convert, TimeSpan time)
    var bars_converted = new Collection<Bar>();
    long current_tick_interval = -1;
    DateTime boundary_adjusted_time = default(DateTime);
    double current_bar_open = default(double);
    double current_bar_high = default(double);
    double current_bar_low = default(double);
    double current_bar_close = default(double);

    if (bars_to_convert.Count() == 0)
        return bars_converted;

    foreach (var bar in bars_to_convert)
        var this_tick_interval = bar.Time.Ticks / time.Ticks;
        if (this_tick_interval != current_tick_interval)
            if (current_tick_interval != -1)

                bars_converted.Add(new Bar( Time: boundary_adjusted_time, 
                    Open: current_bar_open, 
                    High: current_bar_high, 
                    Low: current_bar_low, 
                    Close: current_bar_close));
            current_tick_interval = this_tick_interval;
            boundary_adjusted_time = new DateTime(current_tick_interval * time.Ticks);
            current_bar_open = bar.Open;
            current_bar_high = bar.High;
            current_bar_low = bar.Low;
            current_bar_close = bar.Close;
            current_bar_high = bar.High > current_bar_high ? bar.High : current_bar_high;
            current_bar_low = bar.Low < current_bar_low ? bar.Low : current_bar_low;
            current_bar_close = bar.Close;
    // Add the final bar
    bars_converted.Add(new Bar(boundary_adjusted_time, current_bar_open, current_bar_high, current_bar_low, current_bar_close));
    return bars_converted;

Points of Interest

Happy Trading ... 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Xavier John
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to use this code? Pin
Member 1252247426-May-16 10:47
memberMember 1252247426-May-16 10:47 
QuestionI have literally been trying to fix this for hours and can't figure it out. Pin
scotty35626-Apr-16 16:09
memberscotty35626-Apr-16 16:09 
QuestionCan't get the last time period to display for all dates, only the last date Pin
scotty35626-Apr-16 16:07
memberscotty35626-Apr-16 16:07 
QuestionTops Pin
Member 1087648310-Jun-14 11:10
memberMember 1087648310-Jun-14 11:10 
Questionvery helpful Pin
Neeraj Kaushik 198016-Jan-13 1:30
memberNeeraj Kaushik 198016-Jan-13 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 12 Mar 2012
Article Copyright 2012 by Xavier John
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid