# 程式交易實作 - 利用EventBridge定期取得價格資訊並發送通知 ## Previously on ... ![](https://hackmd.io/_uploads/BkdHBZH4n.png) * [20230427 - 初談自動化交易,透過AWS Lambda更新交易訊號](https://hackmd.io/@_ZfaCsoORM2ms6z9wV-aNw/SJdyS2RMh) ![](https://hackmd.io/_uploads/S1yGIZBV3.png) ## Today's Goal: * Create a polling event to periodically check the price. * Upgrade the Discord bot for a serverless architecture. * Set up a function for automatically sending messages to a Discord channel. ## Workshop First !!! ### Step1: Pull the latest code from Github ```bash git remote set-url origin https://github.com/biomotion/aws-binance-bot.git git pull origin main ``` ### Step2: Set environment variables In `serverless.yml` ... ```yaml= environment: DISCORD_BOT_TOKEN: #TODO PUBLIC_KEY: #TODO DISCORD_CHANNEL_ID: #TODO SCHEDULE_RULE_NAME: #TODO! ``` We have to set these variables above. 1. `DISCORD_BOT_TOKEN` and `PUBLIC_KEY`: [HERE](https://discord.com/developers/applications) ![](https://hackmd.io/_uploads/Hk35stzS2.png) ![](https://hackmd.io/_uploads/Sy-niYMSn.png) 2. `DISCORD_CHANNEL_ID`: [How to Find a Discord Channel ID: The Ultimate Guide](https://turbofuture.com/internet/Discord-Channel-ID) 1. In your discord APP: Settings > Advanced > switch on Developer Mode 2. Navigate to the Channel 3. Right-click the channel name > Copy ID ### Step3: Deploy your code to AWS 1. Deploy ``` sls deploy ``` ![](https://hackmd.io/_uploads/ry9CTtzrh.png) Copy the output endpoint. This will be used in the next step. 2. Change the environment variable of `SCHEDULE_RULE_NAME` 1. In your AWS console, find aws-binance-bot-dev-discord_bot 2. Configuration > Environment variables > Edit 3. Change `SCHEDULE_RULE_NAME` ### Step4: Set Interactions Endpoint URL for your bot 1. In your application page > General Information > fill in `INTERACTIONS ENDPOINT URL` 2. Save changes 3. Copy `APPLICATION ID` (for the next step) ![](https://hackmd.io/_uploads/ry056Yzrn.png) ### Step5: Register commands for discord bot Replace `<YOUR_TOKEN>` and `<YOUR_APPLICATION_ID>` ```bash! export DISCORD_BOT_TOKEN=<YOUR_TOKEN> export APPLICATION_ID=<YOUR_APPLICATION_ID> curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bot $DISCORD_BOT_TOKEN" \ -d '{ "name": "start", "description": "Start trade bot" }' \ "https://discord.com/api/v10/applications/$APPLICATION_ID/commands" curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bot $DISCORD_BOT_TOKEN" \ -d '{ "name": "stop", "description": "Stop trade bot" }' \ "https://discord.com/api/v10/applications/$APPLICATION_ID/commands" ``` ![](https://hackmd.io/_uploads/ByP3CYfrn.png) ### Step6: Complete! ![](https://hackmd.io/_uploads/ByvcCtzSh.png) ## Outro. ![](https://hackmd.io/_uploads/SynxNqMHn.png) >「程式交易,就是你把自己想的交易方法叫電腦去做。」 -- 市場先生 ### Disclaimer 本教學僅介紹如何使用AWS Lambda架設自動化交易程式與通知系統。策略與作法僅供參考,投資風險自負。 ### 未來方向 1. 驗證策略: 回測分析 2. 利用ccxt package進行下單 (https://github.com/ccxt/ccxt) 3. 倉位控制 ## Reference - https://aws.amazon.com/tw/eventbridge/ - https://discord.com/developers/docs/reference - https://hackmd.io/@_ZfaCsoORM2ms6z9wV-aNw/SJdyS2RMh - https://github.com/ker0olos/aws-lambda-discord-bot