0

我正在摆弄 matlab(没有添加工具箱),并试图了解如何从交易数据库创建 x 分钟柱。

从 SQL 服务器访问数据,我得到一个元胞数组 Buy_Data:

    Attributes: []
          Data: {619134x2 cell}
DatabaseObject: [1x1 database]
      RowLimit: 0
      SQLQuery: [1x210 char]
       Message: []
          Type: 'Database Cursor Object'
     ResultSet: [1x1 com.microsoft.sqlserver.jdbc.SQLServerResultSet]
        Cursor: [1x1 com.mathworks.toolbox.database.sqlExec]
     Statement: [1x1 com.microsoft.sqlserver.jdbc.SQLServerStatement]
         Fetch: [1x1 com.mathworks.toolbox.database.fetchTheData]

现在,内容看起来像:

'2012-08-28 16:48:56.0'    [24800]
'2012-08-28 16:48:56.0'    [24800]
'2012-08-28 16:49:14.0'    [24800]
'2012-08-28 16:49:14.0'    [24800]
'2012-08-28 16:49:21.0'    [24799]
'2012-08-28 16:49:51.0'    [24800]
'2012-08-28 16:49:51.0'    [24800]
'2012-08-28 16:49:55.0'    [24800]
'2012-08-28 16:49:55.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 17:00:12.0'    [24839]

我如何将这些单元阵列中的 2 个组合成 x 分钟时间序列?

4

1 回答 1

1

好吧,组合单元阵列就像

new_Data = [Buy_data.Data; Buy_data2.Data]

显然,Buy_dataBuy_data2你的两个不同的结构。

将这些组合成 x 分钟烛台格式可以这样完成:

% example data
new_cell = {
    '2012-08-28 16:48:56.0'    [24800]
    '2012-08-28 16:48:56.0'    [24800]
    '2012-08-28 16:49:14.0'    [24800]
    '2012-08-28 16:49:14.0'    [24800]
    '2012-08-28 16:49:21.0'    [24799]
    '2012-08-28 16:49:51.0'    [24800]
    '2012-08-28 16:49:51.0'    [24800]
    '2012-08-28 16:49:55.0'    [24800]
    '2012-08-28 16:49:55.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 17:00:12.0'    [24839]
    };

% your period (in minutes)
x = 1; 



% convert dates to numbers and make sure dates are sorted
[dateNums, sortInds] = sort(datenum(new_cell(:,1)));
prices = [new_cell{:,2}].';
prices = prices(sortInds);

% find minimum date, maximum date
minDate = min(dateNums);
maxDate = max(dateNums);

% group data into bins
x = x/24/60;
numBins = ceil( (maxDate-minDate)/x );
[N,X] = hist(dateNums,numBins);

% create candlesticks
inds          = [1 cumsum(N)];
candles.min   = zeros(numBins,1);
candles.max   = zeros(numBins,1);
candles.open  = zeros(numBins,1);
candles.close = zeros(numBins,1);
candles.dates = cell(numBins,1);
for ii = 1:numBins

    % we already know the dates
    candles.dates{ii} = datestr(X(ii));

    % continue only if there's data    
    if N(ii)
        % extract the prices
        subSet = prices(inds(ii):inds(ii+1));
        % find max, min, open, close
        candles.open(ii)  = subSet(1);
        candles.close(ii) = subSet(end);
        candles.max(ii)   = max(subSet);
        candles.min(ii)   = min(subSet);        
    end    

end

请注意,您需要进行一些调整,因为这些烛台将是非常不具代表性的 :) 无论如何,这应该足以让您入门。

于 2012-08-29T10:55:18.407 回答