我的問題是基於這樣的前提: Group OHLC-Stockmarket Data into multiple timeframes with T-SQL蜱蜱在C#燭臺/ OHLC數據/ NET
像這個問題的提問者,我的目標是從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毫秒,但數據中沒有固定或一致的滴答數。