勾选到C#/ .NET中的烛台/ OHLC数据

时间:2013-08-05 18:32:23

标签: c# sql datetime group-by finance

我的问题是基于这个前提: Group OHLC-Stockmarket Data into multiple timeframes with T-SQL

与该问题的提问者一样,我的目标是从SQL源构建烛台数据,但完全在C#中。换句话说,我想首先将滴答数据全部下载到我的C#程序中,然后在程序中对其进行操作以得到如下结果:

TimeStamp          | Price 
2012-02-17 15:15:0 | 102  
2012-02-17 15:16:0 |108   
2012-02-17 15:17:0 | 101  
2012-02-17 15:18:0 |105   
2012-02-17 15:19:0 |107   
2012-02-17 15:20:0 |115   

Desired Query Result (5 minutes):

Timestamp       |Open|High|Low|Close
2012-02-15:19:0 |102 |108 |101|107
2012-02-15:24:0 |115.|....|...|...
2012-02-15:29:0 |....|....|...|...

(部分从上述问题转载)

我在C#中的代码使用相关的连接字符串按刻度数据下载整个刻度,并将数据存储在数组中。

//SQL connection string
SqlConnection o_sqlConnection = new SqlConnection();
o_sqlCommand.CommandText = "(see query below)";
SqlDataReader o_sqlDataReader;
...
//Storage of query result in arrays
pricetmp = Convert.ToDouble(o_sqlDataReader["Price"]);
            priceList.Add(pricetmp);
voltmp = Convert.ToDouble(o_sqlDataReader["Volume"]);
            volList.Add(voltmp);
//along with date, time and stock symbol
//eventually
double[] priceArray = price.ToArray();
DateTime[] timestampArray = timestamp.ToArray();
...

我需要帮助编写代码的一部分,在上述问题的答案中产生与SQL查询相同的结果,即

(reprinted from the link)
select  min(timestamp) as Time
,       max(Price) as Highest
,       min(Price) as Lowest
,       min(case when rn_asc = 1 then [Price] end) as first
,       min(case when rn_desc = 1 then [Price] end) as Last
from    (
    select  row_number() over (
                partition by cast(cast(timestamp as float) * 24 * 12 as int)
                order by timestamp) as rn_asc
    ,       row_number() over (
                partition by cast(cast(timestamp as float) * 24 * 12 as int)
                order by timestamp desc) as rn_desc
    ,       where Stocksymbol = 'abc123'
    ,       *
    from    @t
    ) as SubQueryAlias
group by
    cast(cast(timestamp as float) * 24 * 12 as int)

注意:我已经修改了上面的查询以符合我的目的 - 即直接将刻度数据(而不是更短的时间帧OHLC数据)转换为OHLC数据。

作为C#的新手,我很欣赏一些关于如何编写模仿上述查询结果的函数的建议。 以下是我到目前为止的情况:

int j = 0; //n is length of data per expected number of OHLC bars
double[] open = new double[n];
double[] close = new double[n];
double[] high = new double[n];
double[] low = new double[n];
int i = 0; int ratio = priceData.Length/n;
while (i < priceData.Length)
{
    open[i/ratio] = priceData[i];
    close[i/ratio] = priceData[i];
    high[i/ratio] = priceData[i];
    low[i/ratio] = priceData[i];

    if (i != -1)
    {
        for (j = i + 1; j < i + ratio; j++)
        {
        if (priceData[j] > high[i/ratio])
            high[i/ratio] = priceData[j];
        if (priceData[j] < low[i/ratio])
            low[i/ratio] = priceData[j];
        close[i/ratio] = priceData[j];
        }
    }

    i += ratio;
}
//output open, high, low, close

如何根据预期的时间范围为n值选择参数? (1分钟,2分钟,15分钟等)(在SQL数据中很容易)?或者更具体地说,如何推广此代码以输入所需的时间范围并使用它来计算OHLC数据?我如何结合使用DateTime数组(此示例代码忽略)来对数据进行排序?

这似乎更容易在SQL中进行,因为产生5分钟数据的24 x 12计算可以修改为其他数字(例如,15分钟数据为24 x 4,2分钟数据为24 x 30等)针对不同的时间段。但我需要在C#中完全做到这一点。

我的滴答时间通常是每3毫秒,但数据中没有固定或一致的滴答数。

1 个答案:

答案 0 :(得分:0)

为什么你不使用图表系列的DataManipulator.Group方法?

你可以这样做:

    chart.DataManipulator.Group("HiLoOpCl", 5, IntervalType.Minutes, "series1", "series2")

我认为它更好。

相关问题