中文 | English
This repository teaches you how to use ChatGPT to help you write trading programs and deploy the code to Heroku to achieve automated trading. The exchange used for this article is Binance, but if you have other trading platforms, you can connect them yourself.
- First, I asked NotionAI to come up with ten possible trading strategies for me, as follows:
- A momentum trading strategy based on price and volume data
- A mean reversion strategy using Bollinger Bands and RSI indicators
- A trend following strategy using moving averages and MACD indicator
- A breakout trading strategy based on support and resistance levels
- A pairs trading strategy using cointegration analysis
- A news-based trading strategy using sentiment analysis on financial news
- An arbitrage trading strategy using cross-market analysis
- A swing trading strategy using candlestick patterns and chart analysis
- A quantitative trading strategy based on statistical models and machine learning algorithms
- A position trading strategy using fundamental analysis and value investing principles
- Tell ChatGPT like this:
Give me pipescript code with version 4 running on TradingView for {trading strategy}
, so an example is as follows:Give me pipescript code with version 4 running on TradingView for A momentum trading strategy based on price and volume data.
- Copy the ChatGPT code, some parts need to be adjusted
- For the second line of the code, sometimes ChatGPT will give you
study
and you need to change it tostrategy
. - Sometimes for the entry/exit code, he will give you the following code, which needs to be modified to trigger during backtesting:
if buy_signal alert("Buy Signal") if sell_signal alert("Sell Signal")
- Modify it to the following code:
if buy_signal strategy.entry("Buy", strategy.long) if sell_signal strategy.entry("Sell", strategy.short)
- Add the parameter
alert_message
after Buy, Sell, Buy Exit, and Sell Exit, so that the subsequent Notification setup won't go wrong.if long_bb and long_ma and macd_above_signal and time >= start_time strategy.entry("Buy", strategy.long, alert_message="Buy") if short_bb and short_ma and macd_below_signal and time >= start_time strategy.entry("Sell", strategy.short, alert_message="Sell") if exit_bb or exit_ma strategy.exit('Buy Exit', 'Buy', alert_message="Buy_Exit") strategy.exit('Sell Exit', 'Sell', alert_message="Sell_Exit")
Note: Sometimes the code provided by ChatGPT may not work, so you can ask him multiple times or provide him with the error message.
- For the second line of the code, sometimes ChatGPT will give you
- Adjust the parameters to get the best results, as shown in the figure below:
- Log in to Binance
- On the left side after logging in, there is an
API Management
option. Click on it and then clickCreate
in the upper right corner. - You will then receive an
API Key
andSecret Key
.
- Fork Github repository:
- Register/Login to GitHub.
- Go to ChatGPT-Trading-Bot.
- Click
Star
to support the developer. - Click
Fork
to copy all the code to your own repository.
- Space deployment registration (free space):
- Register/Login to Heroku.
- In the upper right corner, click
New
->Create new app
. - App Name:
Enter the App name
,Region
:Europe
. - Click
Create app
.
- Environment Variable Configuration
- Click on
Settings
->Reveal Config Vars
- Add environment variables, that include:
- API Key:
- key:
API_KEY
- value:
[obtained from step one above]
- key:
- API SECRET KEY:
- key:
API_SECRET_KEY
- value:
[obtained from step one above]
- key:
- PASSPHRASE -> Used as a Token when TradingView sends a Request to the Server to avoid making the API available to everyone.
- key:
PASSPHRASE
- value:
User-generated, will be used in step four
- key:
- API Key:
- Click on
- Deployment Steps
- Enter the folder where
ChatGPT-Trading-Bot
is located using the Terminal - Check if the folder is the same as the following with
ls
Procfile demo src main.py runtime.txt README.md README.en.md requirements.txt
- Install Heroku cli
- Deploy, refer to the process in the Deploy page below
- Log in to Heroku, enter in the Terminal:
$ heroku login
- Add location, enter in the Terminal:
$ heroku git:remote -a [Your App Name]
- Log in to Heroku, enter in the Terminal:
- Push the repo to Heroku, enter in the Terminal:
$ git push heroku main
- After successful deployment, your URL will be in
Settings
->Domains
- After clicking the link, you will see
Hello, World!
- Enter
heroku logs --tail
in the Terminal and find the location of "My IP". Copy the IP address. For example:2023-03-05T13:38:36.171417+00:00 app[web.1]: My IP: 54.78.178.135
- Go back to Binance, click
Edit restrictions
for the Token you just created -> checkRestrict access to trusted IPs only (Recommended)
underIP access restrictions
-> add the IP from the previous step. - Check the box for
Enable Futures
at the top. - Click
Save
- Enter the folder where
- CronJob Scheduled Request Sending
- Register/Login to cron-job.org
- Choose
CREATE CRONJOB
in the right upper corner of the backend - Enter
ChatGPT-Trading-Bot
inTitle
, and enter the URL from the previous step. - Below, send every
5 minutes
- Click
CREATE
- Trading View Alert Configuration
- In TradingView's
Strategy Tester
, select your strategy, and click on the bell icon - In
Settings
, enter the message below:For example:{ "passphrase": "PASSPHRASE used during setup", "symbol": "Cryptocurrency to trade", "leverage": Leverage amount, "quantity": Quantity to trade, "time": "{{time}}", "close": {{close}}, "message": {{strategy.order.alert_message}} }
{ "passphrase": "Zw'4Tx^5/]f/pN>}fx*9m6<X,fxLx;x(", "symbol": "BTCUSDT", "leverage": 10, "quantity": 0.002, "time": "{{time}}", "close": {{close}}, "message": {{strategy.order.alert_message}} }
Explanation: Contract trading set
BTCUSDT
trading pair leverage to10
times, quantity0.002
BTC. - Notifications Configuration
- Webhook URL setting: The URL in Heroku (
Settings
->Domains
) +/webhook
- Example
https://chatgpt-trading-bot.herokuapp.com/webhook
- Webhook URL setting: The URL in Heroku (
- In TradingView's
Like this free project? Please consider supporting us to keep it running.