{ "cells": [ { "cell_type": "markdown", "id": "c9dc167c", "metadata": {}, "source": [ "# How to create a trading bot in Python (2) - for beginners, using RSI, TA-lib, Alpaca, Anaconda." ] }, { "cell_type": "markdown", "id": "68dea90f", "metadata": {}, "source": [ "If you want, you can find the previous tutorial about starting with Anaconda, Alpaca, Jupyter Notebook, and creating a simple trading bot in 10 lines of code at: \n", "\n", "https://superai.pl/the_simple_trading_bot.html" ] }, { "cell_type": "markdown", "id": "e831d962", "metadata": {}, "source": [ "### The trading bot using Relative Strength Index to decide when to buy or sell shares in less than 15 lines of code" ] }, { "cell_type": "markdown", "id": "442500b8", "metadata": {}, "source": [ "Specification of the bot below (things you might modify are bolded):\n", "- buys **10** shares of **AAPL** when the RSI (from last **3** close prices measured in 1 minute intervals) is less than **49** and you have no shares in your protfolio\n", "- sells **10** shares of **AAPL** when the RSI (from last **3** close prices measured in 1 minute intervals) is more than **51** and you have some shares in your portfolio" ] }, { "cell_type": "code", "execution_count": null, "id": "b64f2324", "metadata": {}, "outputs": [], "source": [ "import alpaca_trade_api as tradeapi, ast, json, numpy as np, talib as ta, websocket\n", "data = []\n", "def on_open(ws):\n", " ws.send(json.dumps({\"action\": \"authenticate\", \"data\": {\"key_id\": \"paste your KEY_ID here\", \"secret_key\": \"paste your SECRET_KEY here\"}}))\n", " ws.send(json.dumps({\"action\": \"listen\", \"data\": {\"streams\": [\"AM.AAPL\"]}}))\n", "def on_message(ws, message):\n", " if isinstance(ast.literal_eval(message).get(\"data\").get(\"c\"), float): data.append(ast.literal_eval(message).get(\"data\").get(\"c\")) \n", " if len(data) > 3 and ta.RSI(np.array(data), 3)[-1] < 49:\n", " try: tradeapi.REST(\"paste your KEY_ID here\", \"paste your SECRET_KEY here\", \"https://paper-api.alpaca.markets\", \"v2\").get_position(\"AAPL\")\n", " except:tradeapi.REST(\"paste your KEY_ID here\", \"paste your SECRET_KEY here\", \"https://paper-api.alpaca.markets\", \"v2\").submit_order(symbol=\"AAPL\", qty=10, side = \"buy\", type='market', time_in_force='gtc') \n", " elif len(data) > 3 and ta.RSI(np.array(data), 3)[-1] > 51:\n", " tradeapi.REST(\"paste your KEY_ID here\", \"paste your SECRET_KEY here\", \"https://paper-api.alpaca.markets\", \"v2\").get_position(\"AAPL\")\n", " tradeapi.REST(\"paste your KEY_ID here\", \"paste your SECRET_KEY here\", \"https://paper-api.alpaca.markets\", \"v2\").submit_order(symbol=\"AAPL\",qty=10,side='sell',type='market',time_in_force='gtc')\n", "websocket.WebSocketApp(\"wss://data.alpaca.markets/stream\", on_open=on_open, on_message=on_message).run_forever()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }