TutulDevs / trading-view

TradingView Chart Implementation in Nextjs
12 stars 4 forks source link
cryptocurrency exchange nextjs tradingview-charting-library tradingview-widget typescript

Trading View Chart in NextJS

Let's implement TradingView's Advanced Real-Time Chart Widget in NextJS.

Required Files:

Folder Structure

Lets Start Chartin' 🚀

We start our charting functions from /src/hooks/useTradingView.ts file. Let's open the file and read through.

The Arguments:

First we create a function named chartinit, which gets an argument which will be the widgetOptions we took as an argument for this custom hook. In the function we store the widget constructor in a variable and later we add the variable in the global window object as tvWidget. This new object has many methods attached to it. Here's the list of the methods in the interface ChartingLibraryWidgetOptions. We worked on the onChartReady and applyOverrides inside this.

Inside the useEffect we first made the tvWidget null so it doesn't keep anything from the past. Then we call out chartInit function. At last we write our cleaner function.

Finally we return the depsArr which we got from the argument. It's optional.

Notes 📝

The Chart

In the Chart Component file, we only have a div that has a ref, id and some styling. We get the id and ref from the useChart hook file. The useChart hook is the place where we customize the chart.

👉 Make sure the file is default exported. Named export will give build error.

useChart

From the line 25-40, we mainly have some variables and helper functions. Then we see some objects.

applyOverrides

Apply overrides to the colors of the main chart background and bars. This doesn't override the colors of the top, bottom or side toolbars. See the applyOverrides object in useChart hook which is commented there.

studyOverrides

This override object is to set in widgetOptions object for the key studies_overrides. It overrides the colors of the newly created indicators. Example can be found in the studyOverrides object in useChart hook which is commented there for the volume indicator. Others indicators are not properly tested yet.

widgetOptions

It's the constructor of the tvWidget that is needed to inititate the chart. Let's talk about the required parameters.

Symbol, interval

Symbol is the symbol for the data fetching. Resolution or interval is a time period of one bar.

Container

The container can either be a reference to an attribute of a DOM element inside which the iframe with the chart will be placed or the HTMLElement itself. It can be useRef or document.getElementById('id') or just the id of the element as an reference.

library_path

The static path of the charting library. Provide the path to the folder, not any file.

datafeed

JS object that implements the JS API. The required methods are:

To read more about datafeed, go to the datafeed wiki or the datafeed file.

timeFrames

You can see the toolbar at the bottom of the chart. Each of those buttons on the left side switches the chart time frame. Switching time frame means:

You can customize default time frames using the time_frames argument of the widget constructor.