Click here to Skip to main content
11,428,281 members (52,627 online)
Click here to Skip to main content

Tagged as

Converter to convert OHLC data to a larger timeframe

, 12 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Xavier John
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionTops Pin
Member 1087648310-Jun-14 12:10
memberMember 1087648310-Jun-14 12:10 
Questionvery helpful Pin
Neeraj Kaushik 198016-Jan-13 2:30
memberNeeraj Kaushik 198016-Jan-13 2:30 

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

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

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