[Algorithm Trading #6] Candle chart from historical data

By @algorithmtrader7/4/2017trading

Hi,

In this post, I will describe about how to plot candle chart from historical data.

I will plot the data in the struct json2data.candleStick

>> response = POLO_ChartData(pair, starttime, endtime, period);

json2data = loadjson(response);
>> json2data

json2data = 

  struct:

    candleStick: {1×1087 cell}
          range: 7819200

First, we need to count the number of candle stick using length() command.
Also, we need to define vectors to store each value.

nCandle = length(json2data.candleStick);

dateVal = zeros(nCandle,1);
highVal = zeros(nCandle,1);
lowVal = zeros(nCandle,1);
closeVal = zeros(nCandle,1);
openVal = zeros(nCandle,1);

Using for loop, we collect all data to desired variables.
Also, we define new struct called candleChart for convinience.

for i = 1:nCandle
    closeVal(i) = json2data.candleStick{i}.close;
    openVal(i) = json2data.candleStick{i}.open;
    highVal(i) = json2data.candleStick{i}.high;
    lowVal(i) = json2data.candleStick{i}.low;
    dateVal(i) = datenum([1970 1 1 0 0 json2data.candleStick{i}.date]);
end

candleChart.date = dateVal;
candleChart.high = highVal;
candleChart.low = lowVal;
candleChart.close = closeVal;
candleChart.open = openVal;
candleChart.ntime = nCandle;
candleChart.period = period;
candleChart.pair = pair;

MATLAB provides nice function called candle() to plot candle chart.
You could call following code.

candle(candleChart.high, candleChart.low, candleChart.close, ...
    candleChart.open, 'b', candleChart.date,'dd-mmm'); 
title('BTC XMR');
ylabel('satoshi');

And you may get chart as below:

chart_from_poloniex.PNG

Of course, MATLAB financial toolbox provides fints which is financial time series object.
However, I'm not familiar to the object yet.

14

comments