How to create node.js app with TradingView integration
Here is a step-by-step guide on how to create a simple Node.js application that uses the "TradingView-API" GitHub repository as a dependency:
- Make sure you have Node.js and npm (Node Package Manager) installed on your system. You can check if you have them installed by running the commands
node -v
andnpm -v
in the terminal.
2. Create a new directory for your project and navigate to it in the terminal.
3. Initialize a new Node.js project by running the command npm init
in the terminal. This will create a package.json file in your project directory.
4. Install the "TradingView-API" package from GitHub by running the following command in the terminal:
npm install Mathieu2301/TradingView-API
This will install the package as a dependency in your project.
5. Create a new file called SimpleChart.js
in your project directory. This file will contain the main logic of your application.
6. Open the SimpleChart.js
file in a text editor and import the "TradingView-API" package at the top of the file by using the following line of code:
const TradingView = require('@mathieuc/tradingview');
7. Use the functions and classes of the package in your code as you need. Here is example of use to get the most recent Bitcoin price from Binance:
const TradingView = require('@mathieuc/tradingview');
const client = new TradingView.Client();
const chart = new client.Session.Chart(); // Init a Chart session
chart.setMarket('BINANCE:BTCBUSD', { // Set the market
timeframe: '15',
range: 20000
});
chart.onError((...err) => { // Listen for errors (can avoid crash)
console.error('Chart error:', ...err);
// Do something...
});
chart.onSymbolLoaded(() => { // When the symbol is successfully loaded
console.log(`Market "${chart.infos.description}" loaded !`);
});
chart.onUpdate(() => { // When price changes
if (!chart.periods[0]) return;
console.log(`[${chart.infos.description}]: ${chart.periods[0].close} ${chart.infos.currency_id}`);
});
9. Once you have finished writing your code, you can run it by using the command. node SimpleChart.js
10. If you want to share your project with others or deploy it to a production environment, you can use the npm pack
command to create a tarball of your project, which can then be distributed and installed using npm install <tarball-file>
Please note that the package you are trying to install is not officially supported by TradingView, it could be that it's not working properly or is no longer maintained. Make sure to check the Github repository and read the README file to get more information about how to use the package.