Wikipedia – "An open-high-low-close chart (also OHLC chart, or simply bar chart) is a type of chart typically used to illustrate movements in the price of a financial instrument over time."
Sombody on the forums recently asked how the data for these charts could be calculated. Rx handles this type of problem perfectly via the "BufferWithTime" operator.
from window in prices
.Timestamp()
.BufferWithTime(TimeSpan.FromSeconds(1))
select new
{
Start = window.First().Timestamp.DateTime,
Open = window.First().Value,
High = window.Max(ts => ts.Value),
Low = window.Min(ts => ts.Value),
Close = window.Last().Value
}
We are currently building charting functionality into our SDP. This could be useful! Here is a program that uses the query with a simulated price tick source.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
var interval = TimeSpan.FromSeconds(0.1);
var rand = new Random();
var prices = Observable.GenerateWithTime(100, _ => true, i => i, _ => interval, i => rand.Next(i - i/10, i + i/10));
var query =
from window in prices
.Timestamp()
.BufferWithTime(TimeSpan.FromSeconds(1))
select new
{
Start = window.First().Timestamp.DateTime,
Open = window.First().Value,
High = window.Max(ts => ts.Value),
Low = window.Min(ts => ts.Value),
Close = window.Last().Value
};
query.Subscribe(Console.WriteLine);
Console.ReadKey();
}
}
}
Enjoy!
Comments