Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Add strategyAlert test file + avoid double notifications

Mathieu Colmon1cbc02b0276b
diff --git a/.gitignore b/.gitignore
index 7545483..1e7ccc9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
node_modules
package-lock.json
.env
+sentNotifs.txt
diff --git a/.npmignore b/.npmignore
index 7545483..1e7ccc9 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,3 +1,4 @@
node_modules
package-lock.json
.env
+sentNotifs.txt
diff --git a/tests/alerts/alerts.js b/tests/alerts/alerts.js
index 94fe0a5..7e34371 100644
--- a/tests/alerts/alerts.js
+++ b/tests/alerts/alerts.js
@@ -1,4 +1,5 @@
require('./envLoader');
+const { getSentNotifs, addSentNotifs } = require('./sentNotifs');
const marketAPI = require('../../main');
const discordWH = require('./discordWH')(process.env.DISCORD_TOKEN);
@@ -7,8 +8,6 @@ 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);
@@ -62,12 +61,12 @@ market.on('logged', () => {
const notifID = `${symbol}@${p.$time}@BULL_TRIGGER`;
if (
- !sentNotifs.includes(notifID)
- && p.CIPHER_B.WTWave1 <= 0 && p.CIPHER_B.WTWave1 > -35
+ p.CIPHER_B.WTWave1 <= 0 && p.CIPHER_B.WTWave1 > -35
&& gapBefore < 0 && gapBefore < gap
&& pBefore.CIPHER_B.WTWave1 < p.CIPHER_B.WTWave1
+ && !getSentNotifs().includes(notifID)
) {
- sentNotifs.push(notifID);
+ addSentNotifs(notifID);
console.log(symbol, 'TRIG NOW', new Date(p.$time * 1000).toLocaleString());
discordWH({
diff --git a/tests/alerts/sentNotifs.js b/tests/alerts/sentNotifs.js
new file mode 100644
index 0000000..d91c37a
--- /dev/null
+++ b/tests/alerts/sentNotifs.js
@@ -0,0 +1,16 @@
+const fs = require('fs');
+
+const sentNotifsFile = './sentNotifs.txt';
+
+exports.getSentNotifs = () => {
+ if (!fs.existsSync(sentNotifsFile)) fs.writeFileSync(sentNotifsFile, '');
+ return (fs.readFileSync(sentNotifsFile, 'utf8') || '').split(';').filter((n) => n);
+};
+
+exports.addSentNotifs = (notif = '') => {
+ if (!fs.existsSync(sentNotifsFile)) fs.writeFileSync(sentNotifsFile, '');
+ fs.writeFileSync(
+ sentNotifsFile,
+ `${fs.readFileSync(sentNotifsFile, 'utf8') || ''}${notif};`,
+ );
+};
diff --git a/tests/alerts/strategyAlert.js b/tests/alerts/strategyAlert.js
new file mode 100644
index 0000000..d3f3c1a
--- /dev/null
+++ b/tests/alerts/strategyAlert.js
@@ -0,0 +1,94 @@
+require('./envLoader');
+const { getSentNotifs, addSentNotifs } = require('./sentNotifs');
+const marketAPI = require('../../main');
+const discordWH = require('./discordWH')(process.env.DISCORD_TOKEN);
+
+const markets = [
+ {
+ symbol: 'EURUSD',
+ period: '1D',
+ settings: {
+ 1: 21, // HMA 1 Length
+ 3: 89, // HMA 2 Length
+ 5: 7, // RSI Length
+ 8: 2, // Stop Loss (%)
+ 9: 1, // Take Profit (%)
+ },
+ },
+ {
+ symbol: 'GBPUSD',
+ period: '1D',
+ settings: {
+ 1: 220, // HMA 1 Length
+ 3: 150, // HMA 2 Length
+ 5: 1, // RSI Length
+ 8: 2, // Stop Loss (%)
+ 9: 1, // Take Profit (%)
+ },
+ },
+ {
+ symbol: 'USDCAD',
+ period: '1D',
+ settings: {
+ 1: 89, // HMA 1 Length
+ 3: 21, // HMA 2 Length
+ 5: 10, // RSI Length
+ 8: 3, // Stop Loss (%)
+ 9: 2, // Take Profit (%)
+ },
+ },
+];
+
+const market = marketAPI(false);
+
+market.on('logged', () => {
+ markets.forEach((mrk) => {
+ console.log('Listen', mrk.symbol);
+
+ market.initChart({
+ symbol: mrk.symbol,
+ period: mrk.period || '1D',
+ range: mrk.range || 30,
+ timezone: mrk.timezone || 'Europe/Paris',
+ indicators: [
+ // My Magimix Indicator
+ {
+ name: 'MAGIMIX',
+ id: 'USER;d90a5e852ddc439b994db6beff14d702',
+ version: mrk.version || 'last',
+ settings: mrk.settings || [],
+ },
+ ],
+ }, ({ 0: p }) => {
+ if (!p.MAGIMIX) return;
+
+ const signalType = p.MAGIMIX.Longsignal ? 'LONG' : 'SHORT';
+ const notifID = `${mrk.symbol}@MAGIMIX@${p.$time}@${signalType}`;
+
+ if (
+ (p.MAGIMIX.Longsignal || p.MAGIMIX.Shortsignal)
+ && !getSentNotifs().includes(notifID)
+ ) {
+ addSentNotifs(notifID);
+ console.log(mrk.symbol, 'SIGNAL', new Date(p.$time * 1000).toLocaleString());
+
+ discordWH({
+ content: process.env.DISCORD_MENTION,
+ embeds: [
+ {
+ title: `Magimix ${signalType} signal for ${mrk.symbol} !`,
+ color: p.MAGIMIX.Longsignal ? 3066993 : 15158332,
+ description: `Magimix ${signalType} signal for ${mrk.symbol} at ${p.$prices.close} !`,
+ url: `https://fr.tradingview.com/chart/?symbol=${mrk.symbol}`,
+ timestamp: new Date(p.$time * 1000),
+ footer: {
+ text: 'TradingView',
+ icon_url: 'https://tradingview.com/favicon.ico',
+ },
+ },
+ ],
+ });
+ }
+ });
+ });
+});