我有4个excel文件分别保存着科大讯飞、三六零、同花顺和昆仑万维这四只股票的数据,使用matlab内置的candle函数,绘制出2023年1月31日以来,科大讯飞、三六零、同花顺和昆仑万维这四只股票的蜡烛图,并将这四张蜡烛图绘制在同一张图中。
时间: 2023-06-14 22:03:46 浏览: 206
首先需要将这四个excel文件导入到MATLAB中,可以使用`xlsread`函数。假设这四个文件分别为`kdxfl.xlsx`、`360.xlsx`、`ths.xlsx`、`klww.xlsx`,并且都有四列数据分别为日期、开盘价、最高价和最低价。代码如下:
```matlab
[kdxfl_date, kdxfl_open, kdxfl_high, kdxfl_low] = xlsread('kdxfl.xlsx');
[360_date, 360_open, 360_high, 360_low] = xlsread('360.xlsx');
[ths_date, ths_open, ths_high, ths_low] = xlsread('ths.xlsx');
[klww_date, klww_open, klww_high, klww_low] = xlsread('klww.xlsx');
```
接下来,需要找到2023年1月31日在数据中的位置。假设日期数据是以序列号的形式保存的,可以使用`datenum`函数将日期转换为序列号。代码如下:
```matlab
target_date = datenum('2023-01-31');
kdxfl_target_idx = find(kdxfl_date >= target_date, 1);
360_target_idx = find(360_date >= target_date, 1);
ths_target_idx = find(ths_date >= target_date, 1);
klww_target_idx = find(klww_date >= target_date, 1);
```
现在,我们已经找到了2023年1月31日在数据中的位置。接下来,可以使用`candle`函数绘制蜡烛图。代码如下:
```matlab
figure;
subplot(2,2,1);
candle(kdxfl_open(kdxfl_target_idx:end), kdxfl_high(kdxfl_target_idx:end), kdxfl_low(kdxfl_target_idx:end));
title('科大讯飞');
subplot(2,2,2);
candle(360_open(360_target_idx:end), 360_high(360_target_idx:end), 360_low(360_target_idx:end));
title('三六零');
subplot(2,2,3);
candle(ths_open(ths_target_idx:end), ths_high(ths_target_idx:end), ths_low(ths_target_idx:end));
title('同花顺');
subplot(2,2,4);
candle(klww_open(klww_target_idx:end), klww_high(klww_target_idx:end), klww_low(klww_target_idx:end));
title('昆仑万维');
```
最后,使用`hold on`命令将这四张蜡烛图绘制在同一张图中。代码如下:
```matlab
hold on;
candle(kdxfl_open(kdxfl_target_idx:end), kdxfl_high(kdxfl_target_idx:end), kdxfl_low(kdxfl_target_idx:end));
candle(360_open(360_target_idx:end), 360_high(360_target_idx:end), 360_low(360_target_idx:end));
candle(ths_open(ths_target_idx:end), ths_high(ths_target_idx:end), ths_low(ths_target_idx:end));
candle(klww_open(klww_target_idx:end), klww_high(klww_target_idx:end), klww_low(klww_target_idx:end));
title('四只股票蜡烛图');
legend('科大讯飞', '三六零', '同花顺', '昆仑万维');
```
这样就可以在同一张图中绘制出四只股票的蜡烛图了。完整代码如下:
```matlab
[kdxfl_date, kdxfl_open, kdxfl_high, kdxfl_low] = xlsread('kdxfl.xlsx');
[360_date, 360_open, 360_high, 360_low] = xlsread('360.xlsx');
[ths_date, ths_open, ths_high, ths_low] = xlsread('ths.xlsx');
[klww_date, klww_open, klww_high, klww_low] = xlsread('klww.xlsx');
target_date = datenum('2023-01-31');
kdxfl_target_idx = find(kdxfl_date >= target_date, 1);
360_target_idx = find(360_date >= target_date, 1);
ths_target_idx = find(ths_date >= target_date, 1);
klww_target_idx = find(klww_date >= target_date, 1);
figure;
subplot(2,2,1);
candle(kdxfl_open(kdxfl_target_idx:end), kdxfl_high(kdxfl_target_idx:end), kdxfl_low(kdxfl_target_idx:end));
title('科大讯飞');
subplot(2,2,2);
candle(360_open(360_target_idx:end), 360_high(360_target_idx:end), 360_low(360_target_idx:end));
title('三六零');
subplot(2,2,3);
candle(ths_open(ths_target_idx:end), ths_high(ths_target_idx:end), ths_low(ths_target_idx:end));
title('同花顺');
subplot(2,2,4);
candle(klww_open(klww_target_idx:end), klww_high(klww_target_idx:end), klww_low(klww_target_idx:end));
title('昆仑万维');
hold on;
candle(kdxfl_open(kdxfl_target_idx:end), kdxfl_high(kdxfl_target_idx:end), kdxfl_low(kdxfl_target_idx:end));
candle(360_open(360_target_idx:end), 360_high(360_target_idx:end), 360_low(360_target_idx:end));
candle(ths_open(ths_target_idx:end), ths_high(ths_target_idx:end), ths_low(ths_target_idx:end));
candle(klww_open(klww_target_idx:end), klww_high(klww_target_idx:end), klww_low(klww_target_idx:end));
title('四只股票蜡烛图');
legend('科大讯飞', '三六零', '同花顺', '昆仑万维');
```
阅读全文