Evaluate Cryptocurrency Craze in MATLAB
Gunjan Gupta
Sr. Technical Expert, AI & ML at Volkswagen Group | Honored by PM Modi & Featured in Mann Ki Baat for Tellmate
Hi. Last week, I created a poll inquiring about the next live interactive session topic. The options from our side were Share Market, Cryptocurrency, Blockchain, Audio Processing, and Video Processing. On a different platform, different options were winners. The majority of votes came for Cryptocurrency and Audio Processing. We have chosen to work on both for future topics. Recently we had a practice run for Cryptocurrency. At the end of the discussion, I felt that most of the questions, including mine, targeted legality, pros, and cons instead of technical details where we should have focused more. What do you suggest, whether we should keep the discussion mixed with all kinds of questions or purely technical without involving questions like:
What question do you think we should include further? Do you have any questions for which you are looking for an answer? Share your questions here in the comments!
Disclaimer: You will not find any financial advice here or in our live session. We are no expert in Cryptocurrency, crypto art, NFTs, their trading, etc. This article and live session aim to increase awareness for education and research purposes. We wish to display some plots derived from data with our own subjective opinions. The data will be retrieved from CryptoCompare.com, a separate entity with its own policy and out of our control.
I am using cryptocompare.com for fetching various Cryptocurrencies. To get real-time data, we don't need API Key access. To retrieve Historial Data, we will need API Key. I will guide you for that in the coming paragraphs. But first, How do you fetch the Real-Time value of Bitcoin in INR or other currencies?
fprintf('Date & Time of Code Execution: %s IST\n',datestr(now));
%% Get Bitcoin in single currency: INR
url = 'https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=INR';
res = webread(url);
fprintf('\n1 Bitcoin = %.2f INR\n',res.INR)
Date & Time of Code Execution: 15-Mar-2022 10:07:48 IST
1 Bitcoin = 3139914.59 INR
If you want to get the data for Ethereum instead of Bitcoin, you can replace BTC with ETH in the URL. Now, if you wish to get the price of Bitcoin in multiple currencies together, How will you do it? Let's see the code below:
%% Get Bitcoin in multiple currencies: USD, INR, EUR
url = 'https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD,INR,EUR';
res = webread(url);
fprintf('\n1 Bitcoin = %.2f USD = %.2f INR = %.2f EUR\n',res.USD,res.INR,res.EUR)
1 Bitcoin = 38821.76 USD = 3139914.59 INR = 35371.95 EUR
Please note that these prices were obtained at my trial run on 15th March 2022. The price can be different when you try it in the future. Now let us try and get historical data for Bitcoin in INR since its beginning. We will need to create API Key for this. Go to https://min-api.cryptocompare.com/pricing and choose the free plan. Once your account is created, Go to "Create an API Key" under API Key Section. Give a suitable name with full Read/Write access.
Post that, your API key will be ready, and it could be copied to our MATLAB Program.
领英推荐
We will use the following code after replacing your own API Key.
%% Fetch Historical Data of Bitcoin in INR
api_key = '<YOUR KEY HERE>';
url ='https://min-api.cryptocompare.com/data/v2/histoday';
fsym = 'BTC'; % The cryptocurrency symbol of interest
tsym = 'INR'; % The currency symbol to convert into
limit = 2000; % The number of data points to return (Max: 2000)
toTs = -1; % Returns historical data before that timestamp.
data = [];
for i = 1:2 % To cover limitation of 2000 entries per fetch
???fullurl = [url '?fsym=' fsym '&tsym=' tsym '&limit=' num2str(limit),...
???????'&toTs=' num2str(toTs) '&api_key=' api_key];
???res = webread(fullurl); % Read data from web
???toTs = res.Data.TimeFrom; % For the next iteration
???s2t = struct2table(res.Data.Data); % Convert data in table form
???% Convert epoch time to datetime pattern of MATLAB
???s2t.time = datetime(s2t.time, 'convertfrom','posixtime');
???% Convert data in timetable format with time as reference
???tt = table2timetable(s2t,'RowTimes','time');
???data = [data; tt]; % Merge data of different iterations
end
data = retime(data,'daily'); % Remove duplicate dates and arrange properly
disp(tail(data))
As usual, I am not explaining the code, and I expect you to try it in your system and understand what has been done. If you have any doubt, you can post in the comments, and I will try to assist you! Now, if we want to plot this resulting data, We can do so:
figure; plot(data.time,data.close,'-.')
xlabel('Date'); ylabel('Price of Bitcoin in INR');
title('Historical: Bitcoin to INR')
Suppose we want to see the performance of Bitcoin in the last 1 year; how we will do that?
year = 1;
t = data.time(end-365*year:end); % time
p = data.close(end-365*year:end); % closing price
figure;?plot(t,p,'-o'); grid on;
xlabel('Date'); ylabel('Price of Bitcoin in INR');
title('Historical (Last 1 Year): Bitcoin to INR');
Since we achieved the objective of our article, I will end programming here and give you some tasks to do yourself. Here are your To-Dos:
I hope you guys will try the above tasks, and I would love to see your code or outcome in the comment section. I even gave you a couple of To-Dos in the previous week's article at Bollywood Movie Mania in MATLAB. I hope you will solve them if you have not tried yet. What do you think of these articles? Let me know in comments or get in touch with me at [email protected]
Happy MATLABing!