ChatGPT automated trading tutorial, helping you achieve automated investment and financial management

March 6, 2023

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee

ExplainThis currently offers ChatGPT integration consulting services. Whether you want to integrate ChatGPT API into an existing platform or use ChatGPT API to develop customized applications for your business, please fill out this form. We are happy to assist your company and products. 🙂。 👉 《ChatGPT API Integration Service Request》form

ChatGPT Trading Bot

💡 Disclosures: the Content is for informational purposes only, and should not be relied upon when making any investment decision

Impressive Results

ChatGPT Trading Bot Demo 1
ChatGPT Trading Bot Demo 2

Take a quick look at the effectiveness of the strategies I asked ChatGPT to write for me. The above two are completely different strategies, and with some parameter adjustments, both have achieved good results in Bitcoin trading. How was this achieved? Let us break it down step by step

If you encounter any problems during implementation, please feel free to create an issue on Github. We will regularly respond to questions in the issue section in TheExplainthis/ChatGPT-Trading-Bot

How to train ChatGPT

  1. First, I had ChatGPT 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
  2. Now input the following to Give me pipescript code with version 4 running on TradingView for {trading strategy} to ChatGPT

    Give me pipescript code with version 4 running on TradingView for A momentum trading strategy based on price and volume data.
  3. Copy the following codes from ChatGPT:

  • For the second line of the code, sometimes ChatGPT will give you study and you need to change it to strategy

  • Buy/Sell Signal Adjustment.

    • Sometimes ChatGPT will give you the code for the entry and exit
      if buy_signal
      	alert("Buy Signal")
      if sell_signal
      	alert("Sell Signal")
    • You have to change the code to the following in order to trigger it on backtest
      if buy_signal
      	strategy.entry("Buy", strategy.long)
      if sell_signal
      	strategy.entry("Sell", strategy.short)
  • After Buy, Sell, Buy Exit, Sell Exit, you need to add the parameter alert_message so that there will be no error when setting up Notification later.

       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")

    ⚠️ Sometimes ChatGPT gives you code that doesn't work, so ask him a few more times or throw him an error message

  1. Adjust the parameters to get the best results, as shown below.

    ChatGPT Trading Bot Demo 1

Automated Process

Token acquisition

  1. Login to Binance

  2. After login, there is an API Management on the left side, enter it and press Create on the top right corner.

    Binance Create Token 1
    Binance Create Token 2
  3. then you will get API Key and Secret Key

    Binance Create Token 3

Project Settings

  1. Fork Github project: 1.

    1. Register/Login to GitHub
    2. Go to ChatGPT-Trading-Bot
    3. click Star Support Developer
    4. click Fork to copy all the code to your own repository
  2. Deploy space registration (free space): 1.

    1. Register/login to Heroku

    2. There is a New -> Create new app at the top right. 3.

    3. App Name: Enter this App Name, Region: Europe 4.

    4. press Create app

      Heroku Create app

    ⚠️ There are two limitations when choosing a deployment platform: Coinan requires an IP location if you need to trade contracts, and Coinan's API has regional restrictions, such as the IP is not available in the US.

Project Implementation

  1. Environment variable setting

    Click Settings -> Reveal Config Vars. 2. 2. To add environment variables, add.

    • API Key. key: API_KEY value: [obtained from step 1 above]
    • API SECRET KEY. key: API_SECRET_KEY value: [obtained from step 1 above]
    • PASSPHRASE -> The purpose is that TradingView can be used as a Token when playing Request to Server, to avoid allowing everyone to play API key: PASSPHRASE value: `User generated, will be used again in step 4
    Heroku Add Environment Variables
  2. Deployment steps

    1. Use Terminal to enter the folder where ChatGPT-Trading-Bot is located 2. ls to see if the folder is the same as the following

    Procfile; Procfile; ``demo; src;; runtime.txt;;; requirements.txt;

    Heroku Add Environment Variables

    3. install Heroku cli 4. Deploy, see the process at the bottom of the Deploy page - First login to Heroku, in Terminal type in jsx $ heroku login

    Heroku Add Environment Variables

    After pressing Enter, it will open the browser and ask for a login, which will be successful. - To add a new location, in Terminal enter. jsx $ heroku git:remote -a [your App Name] 5. Push the repo to Heroku and enter in Terminal

    jsx $ git push heroku main 6.

    6. After successful deployment, your URL column will be in Settings -> Domains

    Heroku Get URL

    7. Click on the link and you will see Hello, World!. 8. 8. Terminal enter heroku logs --tail and find the place where "My IP" is and copy the IP down. For example

    jsx 2023-03-05T13:38:36.171417+00:00 app[web.1]: My IP:

    Heroku Get IP

    9. Go back to Binance, click Edit restrictions -> under IP access restrictions and check Restrict access to trusted IPs (Recommended) -> and add the IPs from the previous step to it. 10. Check the Enable Futures box above. 11. 11. Click Save

    Heroku Get IP

⚠️ The free version of Heroku will hibernate if no one sends a request within 30 minutes, so the next step is required.

  1. CronJob Scheduled Send Request

    1. Register/login to

    2. Go to the top right of the back panel and select CREATE CRONJOB.

      CronJob Step 1
    3. Title enter ChatGPT-Trading-Bot, the URL of the previous step

    4. The next step will be typed every 5 minutes. 5.

    5. Press CREATE

      CronJob Step 2

Trading View Alert Settings

1. Under TradingView Strategy Tester, select your strategy and press the icon of the alarm clock 2. Under Settings the Message format is as follows.

{ "passphrase": "PASSPHRASE for environment setting", "symbol": "Currency to be traded", "leverage": the number of leverage, "quantity": the quantity to be traded, "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: The contract trade is set to BTCUSDT with a leverage of 10 times and a quantity of 0.002 bitcoins.

  1. Notifications Settings

    1. Webhook URL setting: URL in Heroku (Settings -> Domains) + /webhook

    For example.

    Notifications setting

Q & A

  1. How to test if the automated stringing is on?

    You can open a real-time Alert on TradingView, for example, if the current currency price is 25000, set an Alert to cross 25000 and then let it trigger quickly, but because it is triggered instantly, not by strategy, so "message": {{strategy.order.alert_message }} can be changed to "message": "Buy", otherwise the message will be empty. 2.

  2. must set the quantity of each transaction can not be a ratio?

    For example, if max_quantity_ratio is set to 0.01, it means 1% of assets.

  3. How do I debug the code if there is an error?

    You can input heroku logs --tail in Terminal to confirm the error first, if you can't solve it by yourself, you can send Issue

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee