🔍
Plot TradingView Charts with Own Data - YouTube
Channel: unknown
[3]
Few months ago, I made a video on how we can
programmatically open tradingview chart and
[8]
in that video, i showcased how we can embed
tradingviewcharts in your projects and one
[14]
of the comment that I received on that video
is if it’s possible to plot tradingview
[19]
charts with your own data and it's it's possible
but it's not possible with the approach that
[25]
I discussed in that video.
[26]
so, In today’s session, we are going to
see how we can plot tradingview charts with
[31]
your own data.
[33]
Tradingview offers three different charting
libraries that allow you to plot charts with
[37]
your own data.
[38]
The first one is a lightweight chart which
is quite easy to use and it's quite easily
[43]
accessible.
[44]
You just have to include the cdn file in your
HTML document and you can start using it and
[49]
it's free and open source and it also has
quite a bit of features, quite a few it also
[53]
loaded with a lot of features.
[55]
so, and it’s also quite easy to use.
[57]
The only downside is that it doesn't have
the capability of adding the tooltip or drawing
[63]
something on the charts or being able to include
indicators, but other than that it is very
[69]
capable.
[70]
tradingview also offers couple of other libraries,
but these are a bit difficult to get your
[73]
hands on.
[74]
You have to fill in a form and submit some
details and then request for access.
[79]
so, it's not as easily accessible as the lightweight
chart.
[82]
so, I'm not going to cover the other two in
this video and if you scroll down you can
[87]
see comparison between all the three libraries
and you can see that, the candlestick charts
[93]
available in all the three libraries.
[95]
You can also plot area and line charts and
the main difference is the capability to add
[100]
technical indicators.
[102]
we can see that the indicators option is only
available for the second and third libraries.
[106]
so, if indicators is your requirement, then
what I'm going to discuss in today's session
[112]
may not be may not suffice your requirement
but if you just want to plot the charts with
[118]
your own data without the need to reinvent
the wheel then you may follow along.
[124]
This is where you can find the documentation
for the light weight chart.
[126]
If you scroll down you can see all the different
features.
[129]
so, it supports both Static and live charts
which is great and it’s also quite lightweight
[134]
and it’s open source which is nice and it
also gives you all the interactive features
[140]
like zooming and panning and scrolling around
and all the stuff and if you scroll down,
[144]
you can see they also provide so many examples
and if you click on this icon, you can get
[150]
the code for each example.
[152]
So, I'll leave a link for this site in the
description box.
[157]
You can check it out and explore if you want
to know further.
[160]
In today's video, we’re going to cover three
different examples: the first one is the static
[165]
chart, where we’re going to download the
candlestick data for a particular coin from
[169]
binance and just plot it as is using the lightweight
charting library, and second we’re going
[175]
to create a live chat where I am go to use
a socket API to get the real-time candlestick
[180]
data for a particular coin and then plug it
to this library and we’re going to plot
[185]
it.
[186]
It’s going to be a live chart and third
we’re going to combine the above two examples
[190]
and create a live chart with some history.
[192]
To follow along you need to have a basic understanding
of the following concepts so you need to have
[197]
basic knowledge of HTML, JavaScript and if
you know how to use which API and sockets
[202]
API it does come in handy but even if you
don't know these two then it's fine.
[206]
I am going to show you how to do it.
[208]
it's very simple we can do it in like 5 or
6 lines of code.
[211]
Alright!
[212]
I have created a folder called tradingview
custom data and I already have two other folders
[217]
which I want to talk about later.
[218]
so, first i am going to create a new folder
for static charts.
[221]
I’m going to call it static charts.
[224]
Ok. and inside that folder i’m going to
create index.html file and then i’m going
[232]
to create index.js file.
[235]
Let’s initiate initialise the HTML file.
[239]
I’m going to set the title to Static chart
and then i’m going to put a header.
[248]
I’m just going to say binance.
[251]
BTCUSDT chart.ok. and then i’m going to
create a container to hold the chart and i’m
[257]
going to call it TVchart.
[262]
I'm not going to set the dimensions here.
[264]
we are going to pass it in the code and finally
let’s include the javascript file
[274]
and we also need to include the tradingview
chart in the library.
[278]
So, let’s go to the documentation and here
is where we can find the cdn file.
[282]
Just copy that and paste it here.
[284]
that’s it.
[285]
Let’s move on to the JavaScript file.
[287]
Before we start writing the code, I want to
first go through the pseudo code so that you
[291]
understand the whole process.
[293]
so, first we are going to define the chart
properties where we’re going to set the
[296]
width and height of the chart & some other
properties.
[299]
Then we are going to create the chart with
the defined properties and bind it to the
[303]
DOM element which is the <div> element that
we just created and then we’re going to
[308]
add the candlestick series and then set the
data to that candlestick series and render.
[314]
Let’s write the code.
[317]
First, I'm going to create reference to the
console log.
[321]
Then, i’m going to create a variable called
chart properties.
[331]
You set the width to 1500 pixels and then
the height to 600 pixels and we also need
[346]
to pass the time scale and in a Time Scale
we say time visible is equal to true and seconds
[359]
visibleis equal to false.
[363]
So, when you need to pass time visible and
second visible because we need to send timestamp
[368]
in milliseconds and i got to know these fields
then the field names by looking at the documentation.
[373]
Ok.
[374]
So, first step is done.
[376]
now, let's move on to the second step.so,
first I’m going tocreate a reference to
[381]
the TV chart <div> element.
[388]
So, i say domElement = document.getelement.id.
[402]
Then, we’re going to create a chart.
[409]
Const chart is equal to.
[422]
Because we included the library we can use
this function directly.
[425]
so lightweight chart.create chart.
[428]
this takes in two parameters.
[429]
The first one is Dom element and the second
one is chart properties.
[434]
Next let’s move on to the step 3 which is
creating the candlestick series.
[443]
Alright!
[449]
Step 3 is finished.
[463]
Now let’s get to the final step which is
getting the data and rendering the charts.
[467]
So, we’re going to use BTCUSDT candlestick
data from binance and use that to render the
[474]
chart.
[475]
So. this is the API to get the candlestick
information.
[479]
You can see that the response is in the form
of an array and each element in the array
[484]
is also another array which contains information
for each candlestick.
[489]
As I mentioned earlier, i’m going to use
fetch API to get the data.
[492]
So, let’s write the code.
[494]
i’m going to copy paste this link.
[515]
I need to invoke this function to convert
the data in the appropriate format and it's
[519]
also an asynchronous function so it returns
the promise.
[523]
So, then so, this is where we have the data.
[531]
now keep in mind that in order to set the
data to the candlestick series we need to
[535]
create an Array in ascending order and ascending
order from from a timeframestandpoint.
[541]
so the oldest candles come first and the newer
ones come at the end and each element in the
[546]
array is a JSONwith the following fields:
time open high low close.
[551]
So’ let’s create that mapping.
[553]
i’m going
to use a map function to do that and return
[565]
time is equal to zero and let’s let’s
first write down all the fields.
[575]
Ok.
[581]
Now timestamp should be passed in seconds.
[583]
ok. and the timestamp that you see here is
the first element in the array and its provided
[588]
in milliseconds so we have to convert that
to seconds by dividing by 1000.
[592]
So, we’ll say d[0] divided by 1000.
[597]
Ok . and the next value is the open price.
[600]
But you can see that it is in a string format.
[602]
Let me zoom it.
[605]
You can see that it's in the string format
so we have to convert that into a number.
[609]
so we are going to say parsefloat (d[1] ) ok
and I am going to copy this and paste it here
[620]
d[2] because the next one is high and the
next element is low and the last one is closed.
[628]
That’s it.
[630]
That returns the data in the appropriate format.
[633]
Now we have to do the final step which is
set the data to the chart.
[637]
So, to do we say candlestickSeries.set data
and we pass the candlestick data.
[650]
Finally to catch any errors we can write the
catch module.
[661]
That's it.
[662]
Now, let's open the file in the browser and
let's see if it works or not.
[664]
It’s going to fail because of CORS issue
but let's try to do it first.
[669]
If you press f2, you can see that I made a
small mistake guys.
[674]
It should be ID instead of class.
[676]
so that’s the reason when we get the element
by ID it failed.
[680]
So, let’s refresh now.
[682]
Alright you can see that the request to the
binance API got blocked because of because
[687]
of the CRS policy so I covered about this
several times in the past.
[691]
I’m not going to repeat it, but I am go
to use a proxy to be able to make this call
[696]
and use that proxy in the code and I am going
to share this is the proxy that I have in
[703]
the folder so when we started writing the
code you might have noticed that there were
[707]
two other folders.
[708]
So, one of them is rest API proxy.
[710]
So, let’s start the proxy 1st node restproxy.
[716]
Alright the proxy is activated.
[718]
All you have to do is append your URL to this
URL and it should work.
[724]
:Let’s go back to the code and I’m going
to add it here and let's restart the page,
[732]
refresh the page and see if it works.
[735]
awesome!
[736]
So, you can see that the chart got displayed
and you can see that i can zoom in, I can
[741]
Pan around & i can zoom in zoom out and you
can do everything that I would normally do
[746]
with trading with chart and you can see that
the timestamp is displayed on the bottom.
[750]
So, it works right and I can do this with
any chart.
[753]
so I can change it to let say instead of BTCUSDT,
i can change it to ethereum and I can change
[759]
the interval to one day and ya it should be
small d and let’s refresh the page.
[766]
yup. you can see the ethereum chart now.
[769]
You can see the prices 144 so it works so
this is you display a static chart now let’s
[775]
move on to the real time chart.
[777]
now lot of the code is quite similar to what
we did it with static charts.
[780]
So, i'm going to duplicate this folder and
reuse it for dynamic charts.
[788]
Now in order to create a live chart we need
to have a socket that provides the data feed
[793]
for the live chart.
[794]
This is why I had another folder created initially
which is the socket that provides the data
[799]
feed for the candlesticks.
[800]
So, you can see that if you open the code
you can see that it provides the light chart
[805]
for BTCUSDT 1 minute time frame and it is
a straightforward code so I'm not going to
[811]
go through this code in this video but going
to share this in the repository as well.
[816]
You can check it out and I am going to initiate
this WebSocket from here.
[823]
That’s it.
[827]
The socket is initiated.
[829]
Now let's write the code.
[831]
Let’s close this and go back to our dynamic
chart’s folder and in the index file, so,
[837]
here we’re going to add another step which
is step 5.
[844]
Plug the socket to the chart.
[847]
Ok. that’s it.
[849]
Now I’m going to comment out this code because
we are just going to display the live chart
[854]
and here we’re going to initiate initiate
the socket from the client-side.
[862]
Now to do this to initiate a socket we need
to have the socket related library included
[867]
in our code.
[868]
so go to the, so, go to this website and you
can download this the latest version of the
[875]
library.
[876]
So, I'm going to copy this socket.io cdn and
let's include that.
[885]
Perfect.
[891]
Now let’s write the code.
[892]
1st I’m going to create a variable called
socket and we’re going to connect to the
[901]
socket that we just initiated which is the
local host 127.0.0.1: it’s running on 4000
[908]
port.
[909]
That’s it and then the data is streamed
on Kline message.
[918]
So, we have to listen to that message.
[921]
So, socket.on Kline then we pass a function.
[927]
We’re going to say PL, for now, let’s
just log this data on this console and see
[935]
how the response is.
[936]
so, Let’s save this and let’s open this
page.
[941]
If you go to the console, you can see that,
let’s clear everything, you can see that
[951]
the data is getting streamed like every second.
[953]
So, the data is in the format that we want.
[956]
All we need to do is just plugin it to the
chart.
[958]
So’ let’s do that.
[961]
So I'm going to comment out this code and
in order to connect the data feed to the chart,
[967]
we can do it in one single step which is candlestick
series candle series.
[972]
We need to invoke a function called update.
[975]
Ok. and just pass in the payload as it is.
[979]
That’s it.
[980]
Now, if I restart the page, the chart should
work.
[984]
Just going to close this.
[985]
Ok. it doesn't have any history yet.
[988]
But you can see that the candle works perfectly
fine.
[990]
I’m just going to let it run for a few minutes
and I'm going to fast forward the video.
[994]
So that you can see the new candles popping
up.
[1002]
Alright, you can see that the if we just let
it run for a while, the new candles keep getting
[1018]
plotted.
[1019]
Alright.
[1020]
In the final example, we’re going to combine
the first two examples.
[1022]
So’ We’ll have the historical data as
well as the live data.
[1026]
So, let’s do that.
[1028]
I’m going to clone the dynamics charts folder
because it’s going to be pretty similar’
[1033]
I’ll call it with history.
[1036]
All I need to do is just un-comment this code
and just restart it.
[1040]
That’s it.
[1041]
So, here We’re going to set the historical
data and here We’re going to pass the live
[1046]
data.
[1047]
So, both of them work in tandem.
[1048]
So, let’s open the file in the browser and
see how it looks.
[1057]
It looks good but we have to make a small
correction because you can see that the historical
[1061]
data that we are getting is for Eth USDT.
so, let’s change it to BTC because the live
[1067]
feed is for BTC.
[1068]
So, we have to make sure that the historical
data also belongs to BTC and the candlestick
[1074]
time frame is one minute.
[1075]
So’ let’s refreshthe page now.
[1077]
And ya so it works perfectly fine and if I
just zoom in to the most recent candles and
[1084]
just let’s just let it run.
[1085]
You can see that it’s live, it keeps getting
updated, and I’m going to change the titles
[1096]
to static chart, dynamic chart and dynamic
chart with history before I deploy the code
[1102]
onto the GitHub.
[1103]
That’s it for this video guys.
[1104]
I hope you find it useful and the only downside
is that you cannot have indicators but other
[1110]
than that it’s pretty useful and the charts
are just as good as trading view charts.
[1115]
You don’t have to do any configuration.
[1117]
You can see that we have just a few lines
of code.
[1119]
You can render the charts with your own data.
[1121]
So.
[1122]
ya.
[1123]
Just leave your thoughts in the comment section.
[1124]
Talk to you in the next video.
Most Recent Videos:
You can go back to the homepage right here: Homepage





