Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Tip/Trick

Converter to convert OHLC data to a larger timeframe

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Mar 2012CPOL 21.7K   499   1   5
Converter to convert OHLC data to a larger timeframe

Introduction

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.   

Background  

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. 

C#
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 

C++
// 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;
        }
        else
        {
            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 ... 

License

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


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to use this code? Pin
Member 1252247426-May-16 10:47
Member 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
scotty35626-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
scotty35626-Apr-16 16:07 
QuestionTops Pin
Member 1087648310-Jun-14 11:10
Member 1087648310-Jun-14 11:10 
Questionvery helpful Pin
Neeraj Kaushik198016-Jan-13 1:30
Neeraj Kaushik198016-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.