Click here to Skip to main content
Click here to Skip to main content

Converter to convert OHLC data to a larger timeframe

By , 12 Mar 2012
 

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, 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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionvery helpfulmemberNeeraj Kaushik 198016 Jan '13 - 1:30 

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

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