Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Converter to convert OHLC data to a larger timeframe

0.00/5 (No votes)
12 Mar 2012 1  
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. 

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;
        }
        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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here