Introduction
Cryptocurrency trading is all about timing and staying informed about market movements. To help traders keep track of significant price movements, we've built an automated alert system using Node.js, Google Cloud Pub/Sub, and Slack. This system fetches data from CoinMarketCap and GeckoTerminal, filters cryptocurrency pairs based on specific criteria, and sends alerts to Slack when certain conditions are met. In this blog post, we'll walk you through how this project works and how you can set up your own cryptocurrency alert system.
Project Overview
Our alert system performs the following tasks:
- Trigger: It is activated by a message on a Google Cloud Pub/Sub topic.
- Fetch Data: It retrieves cryptocurrency pair data from CoinMarketCap.
- Filter: It filters the pairs based on liquidity, valuation, and transaction volume criteria.
- Analyze: It compares current prices with historical highs using GeckoTerminal data.
- Notify: It sends a Slack message if the current price exceeds the highest historical price.
Prerequisites
Before you get started, ensure you have the following:
- A Google Cloud account.
- A Pub/Sub topic set up in Google Cloud.
- A Slack workspace and an incoming webhook URL.
- Node.js installed on your local machine.
Setting Up the Project
Step 1: Initialize the Node.js Project
Create a new directory for your project and navigate into it:
shellmkdir crypto-alert-system cd crypto-alert-system
Initialize a new Node.js project:
shellnpm init -y
Install the required dependencies:
shellnpm install @slack/webhook node-fetch
Step 2: Write the Code
Create a file named
index.js and add the following code:javascriptconst { IncomingWebhook } = require('@slack/webhook'); const fetch = require('node-fetch'); const slackUrl = 'https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK'; const coinMarketCapUrl = 'https://api.coinmarketcap.com/dexer/v3/platformpage/pair-pages?platform-id=1&dexer-id=1069&sort-field=volumeUsd24h&verify=1&category=spot&page=1'; const geckoTerminalBaseUrl = 'https://api.geckoterminal.com/api/v2/networks/eth/pools'; const liquidityThreshold = 30000; const fdvThreshold = 3000000; const txns24hThreshold = 500; exports.entryPoint = async (event, context) => { const postSlackMessage = async () => { const webhook = new IncomingWebhook(slackUrl); try { const response = await fetch(coinMarketCapUrl); const data = await response.json(); const filteredCoins = data.data.pageList.filter(coin => coin.liquidity > liquidityThreshold && coin.fdv < fdvThreshold && coin.txns24h > txns24hThreshold ).map(coin => coin.pairContractAddress); console.log('Number of coins passed filter:', filteredCoins.length); for (const pairAddress of filteredCoins) { try { const req = await fetch(`${geckoTerminalBaseUrl}/${pairAddress}/ohlcv/hour?aggregate=1&limit=1000`); const geckoData = await req.json(); const ohlcvList = geckoData.data.attributes.ohlcv_list; if (ohlcvList.length < 20) continue; const highest = ohlcvList.reduce((max, candle) => Math.max(max, candle[2]), 0); const currentPrice = ohlcvList[0][4]; if (currentPrice > highest) { await webhook.send({ text: `Current Candle Higher than Previous ATH: ${pairAddress}`, }); } } catch (error) { console.error(`Error fetching data for pair ${pairAddress}:`, error); } } } catch (error) { console.error('Error fetching initial data:', error); } }; await postSlackMessage(); };
Replace
YOUR/SLACK/WEBHOOK with your actual Slack webhook URL.Step 3: Deploy to Google Cloud Functions
Deploy your function to Google Cloud Functions:
shellgcloud functions deploy cryptoAlertSystem --runtime nodejs16 --trigger-topic YOUR_PUBSUB_TOPIC --entry-point entryPoint
Replace
YOUR_PUBSUB_TOPIC with your actual Pub/Sub topic name.Conclusion
By following these steps, you've set up a cryptocurrency alert system that fetches market data, filters it based on specific criteria, and sends alerts to Slack when significant price movements occur. This system helps traders stay informed and make timely decisions. Feel free to customize the filtering criteria and extend the functionality to suit your needs. Happy trading!