π Get real-time stocks from TradingView
TypeScript backtestingbacktesting-trading-strategiesbitcoinchartscryptocryptocurrencyethereumrealtime-stocksstocksstocks-apistocks-pricestradingtradingviewtradingview-apitradingview-indicatortradingview-replaytradingview-scraper
[2.0.1] Add alerts.js test file
Mathieu Colmon90b1527df716
diff --git a/.gitignore b/.gitignoreindex d5f19d8..7545483 100644--- a/.gitignore+++ b/.gitignore@@ -1,2 +1,3 @@node_modulespackage-lock.json+.envdiff --git a/package.json b/package.jsonindex c26ff75..82ea4a5 100644--- a/package.json+++ b/package.json@@ -1,16 +1,23 @@{"name": "@mathieuc/tradingview",- "version": "2.0.0",+ "version": "2.0.1","description": "Tradingview instant stocks API","main": "main.js","scripts": {"test-prices": "node ./tests/prices.js","test-analysis": "node ./tests/analysis.js","test-indicator": "node ./tests/indicator.js",- "start": "npm test"+ "test-alerts": "node ./tests/alerts/alerts.js",+ "start-prices": "npm test"},"keywords": ["tradingwiew",+ "market",+ "stocks",+ "crypto",+ "forex",+ "indicator",+ "bitcoin","api"],"author": "Mathieu Colmon",diff --git a/tests/alerts/alerts.js b/tests/alerts/alerts.jsnew file mode 100644index 0000000..94fe0a5--- /dev/null+++ b/tests/alerts/alerts.js@@ -0,0 +1,92 @@+require('./envLoader');+const marketAPI = require('../../main');+const discordWH = require('./discordWH')(process.env.DISCORD_TOKEN);++if (!process.env.SYMBOLS) process.env.SYMBOLS = 'BTCEUR';+if (!process.env.PERIOD) process.env.PERIOD = '240';++const market = marketAPI(false);++const sentNotifs = [];++market.on('logged', () => {+ process.env.SYMBOLS.split(',').forEach((symbol) => {+ console.log('Listen', symbol);++ market.initChart({+ symbol,+ period: process.env.PERIOD,+ range: 30,+ timezone: 'Europe/Paris',+ indicators: [+ { name: 'CIPHER_A', id: 'PUB;vrOJcNRPULteowIsuP6iHn3GIxBJdXwT', version: '1.0' },+ {+ name: 'CIPHER_B',+ id: 'PUB;uA35GeckoTA2EfgI63SD2WCSmca4njxp',+ settings: [ // Activate Stoch divergences+ true, true, true, true, true, true, 9, 12,+ 'hlc3', 3, 53, 60, 100, -53, -60, -75, true,+ true, true, 45, -65, true, 15, -40, true, 60,+ 150, 2.5, false, 'close', 14, 30, 60, false,+ false, 60, 30, true, true, false, 'close', 14,+ 14, 3, 3, true, true, false, 'close', 10, 23,+ 50, 0.5, false, false, '720', 0, 0, 0, 0, 0, 0,+ false, '60', '240', 0, 0, false, '240', false,+ ],+ version: '15.0',+ },+ ],+ }, (periods) => {+ if (!periods[0].CIPHER_B) return;++ // periods.forEach((p, i) => {+ // if (p.CIPHER_B.WTWave1 > 0 || p.CIPHER_B.WTWave1 < -35) return;++ // const pBefore = periods[i + 1];+ // if (!pBefore) return;++ // const gapBefore = pBefore.CIPHER_B.VWAP;+ // const gap = p.CIPHER_B.VWAP;+ // if (+ // gapBefore < 0 && gapBefore < gap+ // && pBefore.CIPHER_B.WTWave1 < p.CIPHER_B.WTWave1+ // ) console.log(symbol, 'TRIG', new Date(p.$time * 1000).toLocaleString());+ // });++ const pBefore = periods[1];+ const p = periods[0];++ const gapBefore = pBefore.CIPHER_B.VWAP;+ const gap = p.CIPHER_B.VWAP;++ const notifID = `${symbol}@${p.$time}@BULL_TRIGGER`;++ if (+ !sentNotifs.includes(notifID)+ && p.CIPHER_B.WTWave1 <= 0 && p.CIPHER_B.WTWave1 > -35+ && gapBefore < 0 && gapBefore < gap+ && pBefore.CIPHER_B.WTWave1 < p.CIPHER_B.WTWave1+ ) {+ sentNotifs.push(notifID);+ console.log(symbol, 'TRIG NOW', new Date(p.$time * 1000).toLocaleString());++ discordWH({+ content: process.env.DISCORD_MENTION,+ embeds: [+ {+ title: `Trigger wave on ${symbol} !`,+ color: 720640,+ description: `Cipher B trigger wave detected on ${symbol}.`,+ url: `https://fr.tradingview.com/chart/?symbol=${symbol}`,+ timestamp: new Date(p.$time * 1000),+ footer: {+ text: 'TradingView',+ icon_url: 'https://tradingview.com/favicon.ico',+ },+ },+ ],+ });+ }+ });+ });+});diff --git a/tests/alerts/discordWH.js b/tests/alerts/discordWH.jsnew file mode 100644index 0000000..cb12543--- /dev/null+++ b/tests/alerts/discordWH.js@@ -0,0 +1,76 @@+const https = require('https');++/**+ * @typedef {Object} WebhookMessage Represents a Discord webhook message+ * @property {string} content+ * @property {Embed[]} [embeds]+ */++/**+ * @typedef {Object} Embed Discord Embed+ * @property {string} [title]+ * @property {string} [description]+ * @property {number} [color]+ * @property {string} [url]+ * @property {string} [timestamp]++ * @property {Object} [author]+ * @property {string} [author.name]+ * @property {string} [author.icon_url]+ * @property {string} [author.url]++ * @property {Object} [thumbnail]+ * @property {string} [thumbnail.url]++ * @property {Object} [footer]+ * @property {string} [footer.text]+ * @property {string} [footer.icon_url]+ */++/**+ * @typedef {Object} SendResult+ * @property {boolean} success+ * @property {Object} [result] If error+ * @property {string[]} [result.webhook_id]+ */++/**+ * Create an instance of Discord Webhook API+ * @param {string} token Discord Webhook token (after .../api/webhooks/)+ */++module.exports = function createInstance(token = '') {+ /**+ * Send a message+ * @name MessageSender+ * @param {WebhookMessage} message+ * @returns {Promise<SendResult>}+ */+ return (message) => new Promise((cb, err) => {+ const req = https.request({+ method: 'POST',+ hostname: 'discordapp.com',+ path: `/api/webhooks/${token}`,+ headers: {+ 'Content-Type': 'application/json',+ },+ maxRedirects: 20,+ }, (res) => {+ let data = '';+ res.on('data', (d) => { data += d; });+ res.on('end', () => {+ if (!data) cb({ success: true });+ else {+ cb({+ success: false,+ result: JSON.parse(data),+ });+ }+ });+ });++ req.on('error', err);+ req.write(JSON.stringify(message));+ req.end();+ });+};diff --git a/tests/alerts/envLoader.js b/tests/alerts/envLoader.jsnew file mode 100644index 0000000..8c4d33f--- /dev/null+++ b/tests/alerts/envLoader.js@@ -0,0 +1,11 @@+const fs = require('fs');++if (fs.existsSync('.env')) {+ const envFile = fs.readFileSync('.env', 'utf8');+ envFile.split('\n') // LF+ .forEach((l) => {+ const s = l.split('=');+ // eslint-disable-next-line+ if (s[1]) process.env[s[0]] = s[1];+ });+}