[Algorithm Trading #6] Candle chart from historical data

in #trading7 years ago

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.

Sort:  

Just get to the machine learning lessons already! :P

Congrats! You've made a step forward for future tech!

What do you mean "candle chart"? Can you explain it more in detail? and what is advantage of this candle chart?

Trading orders are kind of continuous time discrete event. So we may need a lot of data to represent the all order history.
Candle chart is a way of compacting the order history, in such a way that for a certain time (say from 1:00~2:00 am), we record most expensive price, cheapest price and open, close price and trading volume within the time.
There are other kinds of chart such as bar chart or - * chart but nowadays candle chart is most popular.

Congratulations @algorithmtrader! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

The Steem community has lost an epic member! Farewell @woflhart!
SteemitBoard - Witness Update
Do not miss the coming Rocky Mountain Steem Meetup and get a new community badge!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!