├── requirements.txt ├── assets └── logo.png ├── docker-compose.yml ├── Dockerfile ├── LICENSE ├── main.py ├── config.py ├── .gitignore ├── handler.py └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | flask 2 | waitress 3 | python-telegram-bot 4 | discord-webhook 5 | tweepy -------------------------------------------------------------------------------- /assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TreborNamor/TradingView-Webhook-Bot/HEAD/assets/logo.png -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: '3.5' 3 | services: 4 | tradingview-webhook-bot: 5 | build: . 6 | container_name: tradingview-webhook-bot 7 | ports: 8 | - '80:80' 9 | restart: unless-stopped -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9-alpine 2 | LABEL Auther="jon4hz" 3 | WORKDIR /usr/src/app 4 | COPY requirements.txt ./ 5 | RUN apk add gcc python3-dev openssl-dev musl-dev libffi-dev &&\ 6 | pip install --no-cache-dir -r requirements.txt 7 | 8 | COPY main.py handler.py config.py ./ 9 | EXPOSE 80 10 | 11 | ENTRYPOINT [ "python", "main.py" ] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 vsnz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------- # 2 | # Plugin Name : TradingView-Webhook-Bot # 3 | # Author Name : vsnz # 4 | # File Name : main.py # 5 | # ----------------------------------------------- # 6 | 7 | import config 8 | import time 9 | from flask import Flask, request 10 | import json 11 | from handler import * 12 | 13 | app = Flask(__name__) 14 | 15 | def get_timestamp(): 16 | timestamp = time.strftime("%Y-%m-%d %X") 17 | return timestamp 18 | 19 | @app.route('/webhook', methods=['POST']) 20 | def webhook(): 21 | try: 22 | if request.method == 'POST': 23 | data = request.get_json() 24 | key = data['key'] 25 | if key == config.sec_key: 26 | print(get_timestamp(), 'Alert Received & Sent!') 27 | send_alert(data) 28 | return 'Sent alert', 200 29 | 30 | else: 31 | print('[X]', get_timestamp(), 'Alert Received & Refused! (Wrong Key)') 32 | return 'Refused alert', 400 33 | 34 | except Exception as e: 35 | print('[X]', get_timestamp(), 'Error:\n>', e) 36 | return 'Error', 400 37 | 38 | if __name__ == '__main__': 39 | from waitress import serve 40 | serve(app, host='0.0.0.0', port=80) -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------- # 2 | # Plugin Name : TradingView-Webhook-Bot # 3 | # Author Name : vsnz # 4 | # File Name : config.py # 5 | # ----------------------------------------------- # 6 | 7 | # TradingView Example Alert Message: 8 | # {"key": "9T2q394M92", "telegram": "-1001298977502", "discord": "789842349670960670/BFeBBrCt-w2Z9RJ2wlH6TWUjM5bJuC29aJaJ5OQv9sE6zCKY_AlOxxFwRURkgEl852s3","msg": "Long #{{ticker}} at `{{close}}`"} 9 | 10 | sec_key = '' # Can be anything. Has to match with "key" in your TradingView alert message 11 | 12 | # Telegram Settings 13 | send_telegram_alerts = False 14 | tg_token = '' # Bot token. Get it from @Botfather 15 | channel = 0 # Channel ID (ex. -1001487568087) 16 | 17 | # Discord Settings 18 | send_discord_alerts = False 19 | discord_webhook = '' # Discord Webhook URL (https://support.discordapp.com/hc/de/articles/228383668-Webhooks-verwenden) 20 | 21 | #Twitter Settings 22 | send_twitter_alerts = False 23 | tw_ckey = '' 24 | tw_csecret = '' 25 | tw_atoken = '' 26 | tw_asecret = '' 27 | 28 | # Email Settings 29 | send_email_alerts = False 30 | email_sender = '' # Your email address 31 | email_receivers = ['', ''] # Receivers, can be multiple 32 | email_subject = 'Trade Alert!' 33 | 34 | email_port = 465 # SMTP SSL Port (ex. 465) 35 | email_host = '' # SMTP host (ex. smtp.gmail.com) 36 | email_user = '' # SMTP Login credentials 37 | email_password = '' # SMTP Login credentials -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /handler.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------- # 2 | # Plugin Name : TradingView-Webhook-Bot # 3 | # Author Name : vsnz # 4 | # File Name : handler.py # 5 | # ----------------------------------------------- # 6 | 7 | import config 8 | from telegram import Bot 9 | from discord_webhook import DiscordWebhook, DiscordEmbed 10 | import tweepy 11 | import smtplib, ssl 12 | from email.mime.text import MIMEText 13 | 14 | def send_alert(data): 15 | if config.send_telegram_alerts: 16 | tg_bot = Bot(token=config.tg_token) 17 | try: 18 | tg_bot.sendMessage(data['telegram'], data['msg'].encode('latin-1','backslashreplace').decode('unicode_escape'), parse_mode='MARKDOWN') 19 | except KeyError: 20 | tg_bot.sendMessage(config.channel, data['msg'].encode('latin-1','backslashreplace').decode('unicode_escape'), parse_mode='MARKDOWN') 21 | except Exception as e: 22 | print('[X] Telegram Error:\n>', e) 23 | 24 | if config.send_discord_alerts: 25 | try: 26 | webhook = DiscordWebhook(url="https://discord.com/api/webhooks/" + data['discord']) 27 | embed = DiscordEmbed(title=data['msg']) 28 | webhook.add_embed(embed) 29 | response = webhook.execute() 30 | except KeyError: 31 | webhook = DiscordWebhook(url="https://discord.com/api/webhooks/" + config.discord_webhook) 32 | embed = DiscordEmbed(title=data['msg']) 33 | webhook.add_embed(embed) 34 | response = webhook.execute() 35 | except Exception as e: 36 | print('[X] Discord Error:\n>', e) 37 | 38 | if config.send_twitter_alerts: 39 | tw_auth = tweepy.OAuthHandler(config.tw_ckey, config.tw_csecret) 40 | tw_auth.set_access_token(config.tw_atoken, config.tw_asecret) 41 | tw_api = tweepy.API(tw_auth) 42 | try: 43 | tw_api.update_status(status=data) 44 | except Exception as e: 45 | print('[X] Twitter Error:\n>', e) 46 | 47 | if config.send_email_alerts: 48 | try: 49 | email_msg = MIMEText(data) 50 | email_msg['Subject'] = config.email_subject 51 | email_msg['From'] = config.email_sender 52 | email_msg['To'] = config.email_sender 53 | context = ssl.create_default_context() 54 | with smtplib.SMTP_SSL(config.email_host, config.email_port, context=context) as server: 55 | server.login(config.email_user, config.email_password) 56 | server.sendmail(config.email_sender, config.email_receivers, email_msg.as_string()) 57 | server.quit() 58 | except Exception as e: 59 | print('[X] Email Error:\n>', e) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
14 | About 15 | • 16 | Features 17 | • 18 | Installation 19 | • 20 | Images 21 | • 22 | Help 23 |
24 | 25 | ## About 26 | The **TradingView Webhook Bot** ⚙️ listens to [TradingView](https://tradingview.com) alerts via [webhooks](https://www.tradingview.com/support/solutions/43000529348-i-want-to-know-more-about-webhooks/) using [flask](https://flask.palletsprojects.com/en/1.1.x/). 27 | All alerts can be instantly sent to Telegram, Discord, Twitter and/or Email. 28 | 29 | ### TV-Alerts.com 🔥 30 | I am running my own TradingView Webhook Service. No setup and hosting required. Send all your alerts to Telegram, Discord, Slack & Twitter along with a **full screenshot of the chart** complete with your indicators. More at [TV-Alerts.com](https://tv-alerts.com). Got a question? Let me know! 31 | 32 | ## Features 33 | - Telegram Support using the [Python Telegram](https://github.com/python-telegram-bot/python-telegram-bot) libary 34 | - Discord Support using [webhooks](https://support.discord.com/hc/de/articles/228383668-Webhooks-verwenden) 35 | - Twitter Support using the [tweepy](https://github.com/tweepy/tweepy) libary 36 | - Email Support using [smtplib](https://docs.python.org/3/library/smtplib.html) 37 | - Alert channels can be enabled or disabled in [`config.py`](https://github.com/vsnz/TradingView-Webhook-Bot/blob/master/config.py) 38 | - Dynamically send alerts to different Telegram and/or Discord channels 39 | - TradingView `{{close}}`, `{{exchange}}` etc. variables support. Read more [here](https://www.tradingview.com/blog/en/introducing-variables-in-alerts-14880/) 40 | 41 | > 💡 Got a feature idea? Open an [issue](https://github.com/vsnz/TradingView-Webhook-Bot/issues/new) and I might implement it. 42 | 43 | ## Installation 44 | > ⚠️ Best to run the bot on a VPS. I can recommend Hetzner's CX11 VPS for 2.89€/month. 45 | 1. Clone this repository `git clone https://github.com/vsnz/TradingView-Webhook-Bot.git` 46 | 1. Create your virtual environment `python3 -m venv TradingView-Webhook-Bot` 47 | 1. Activate it `source TradingView-Webhook-Bot/bin/activate && cd TradingView-Webhook-Bot` 48 | 1. Install all requirements `pip install -r requirements.txt` 49 | 1. Edit and update [`config.py`](https://github.com/vsnz/TradingView-Webhook-Bot/blob/master/config.py) 50 | 1. Setup TradingView alerts. An example alert message would be: 51 | ```json 52 | {"key": "9T2q394M92", "telegram": "-1001277977502", "discord": "789842341870960670/BFeBBrCt-w2Z9RJ2wlH6TWUjM5bJuC29aJaJ5OQv9sE6zCKY_AlOxxFwRURkgEl852s3", "msg": "Long #{{ticker}} at `{{close}}`"} 53 | ``` 54 | - `key` is mandatory! It has to match with `sec_key` in [`config.py`](https://github.com/vsnz/TradingView-Webhook-Bot/blob/master/config.py). It's an extra security measurement to ensure nobody else is executing your alerts 55 | - `telegram` and `discord` is optional. If it is not set it will fall back to the config.py settings 56 | - `msg` can be anything. Markdown for [Telegram](https://core.telegram.org/api/entities) and [Discord](https://support.discord.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-) is supported as well 57 | - TradingViews variables like `{{close}}`, `{{exchange}}` etc. work too. More can be found [here](https://www.tradingview.com/blog/en/introducing-variables-in-alerts-14880/) 58 | - Your webhook url would be `http://