{ "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": "8ef06019", "metadata": {}, "source": [ "## The trading bot - code only version" ] }, { "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": "588474f1", "metadata": {}, "source": [ "## Install and import dependencies (all the necessary libraries, packages, modules)" ] }, { "cell_type": "code", "execution_count": 1, "id": "32a7d181", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: alpaca-trade-api in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (1.4.1)\n", "Requirement already satisfied: pandas>=0.18.1 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (1.3.4)\n", "Requirement already satisfied: aiohttp==3.7.4 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (3.7.4)\n", "Requirement already satisfied: websockets<10,>=8.0 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (9.1)\n", "Requirement already satisfied: msgpack==1.0.2 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (1.0.2)\n", "Requirement already satisfied: PyYAML==5.4.1 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (5.4.1)\n", "Requirement already satisfied: numpy>=1.11.1 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (1.21.4)\n", "Requirement already satisfied: requests<3,>2 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (2.26.0)\n", "Requirement already satisfied: urllib3<2,>1.24 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (1.26.7)\n", "Requirement already satisfied: websocket-client<2,>=0.56.0 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from alpaca-trade-api) (1.2.1)\n", "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from aiohttp==3.7.4->alpaca-trade-api) (21.2.0)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from aiohttp==3.7.4->alpaca-trade-api) (1.7.2)\n", "Requirement already satisfied: chardet<4.0,>=2.0 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from aiohttp==3.7.4->alpaca-trade-api) (3.0.4)\n", "Requirement already satisfied: typing-extensions>=3.6.5 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from aiohttp==3.7.4->alpaca-trade-api) (3.10.0.2)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from aiohttp==3.7.4->alpaca-trade-api) (5.2.0)\n", "Requirement already satisfied: async-timeout<4.0,>=3.0 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from aiohttp==3.7.4->alpaca-trade-api) (3.0.1)\n", "Requirement already satisfied: pytz>=2017.3 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from pandas>=0.18.1->alpaca-trade-api) (2021.3)\n", "Requirement already satisfied: python-dateutil>=2.7.3 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from pandas>=0.18.1->alpaca-trade-api) (2.8.2)\n", "Requirement already satisfied: six>=1.5 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from python-dateutil>=2.7.3->pandas>=0.18.1->alpaca-trade-api) (1.16.0)\n", "Requirement already satisfied: charset-normalizer~=2.0.0 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from requests<3,>2->alpaca-trade-api) (2.0.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from requests<3,>2->alpaca-trade-api) (2021.10.8)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from requests<3,>2->alpaca-trade-api) (3.3)\n" ] } ], "source": [ "!pip install alpaca-trade-api" ] }, { "cell_type": "code", "execution_count": 2, "id": "fbd6c36a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: TA-lib in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (0.4.19)\n", "Requirement already satisfied: numpy in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (from TA-lib) (1.21.4)\n" ] } ], "source": [ "!pip install TA-lib" ] }, { "cell_type": "markdown", "id": "f7cd1c87", "metadata": {}, "source": [ "#### EDITED: Added after filming\n", "EDIT START" ] }, { "cell_type": "code", "execution_count": 1, "id": "fc7bfdb0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: websocket-client in c:\\users\\lenovo\\anaconda3\\envs\\alpaca_trader\\lib\\site-packages (1.2.3)\n" ] } ], "source": [ "!pip install websocket-client" ] }, { "cell_type": "markdown", "id": "c987a32a", "metadata": {}, "source": [ "EDIT END\n", "#### EDIT FINISHED." ] }, { "cell_type": "code", "execution_count": 3, "id": "92f0e452", "metadata": {}, "outputs": [], "source": [ "import alpaca_trade_api as tradeapi\n", "import ast\n", "import json\n", "import numpy as np\n", "import talib as ta\n", "import websocket" ] }, { "cell_type": "markdown", "id": "f1de1e69", "metadata": {}, "source": [ "## Prepare your data for authentication and authenticate your bot at Alpaca" ] }, { "cell_type": "code", "execution_count": 17, "id": "1ff39fdd", "metadata": {}, "outputs": [], "source": [ "KEY_ID = \"PK3O8OD82FIXEEPGT8GP\"\n", "SECRET_KEY = \"9tCxXfAtHpeUi6qlhdL9i7dGHt6qgDCNzQreBkcZ\"" ] }, { "cell_type": "code", "execution_count": 18, "id": "23969115", "metadata": {}, "outputs": [], "source": [ "api = tradeapi.REST(KEY_ID, SECRET_KEY, \"https://paper-api.alpaca.markets\", \"v2\")" ] }, { "cell_type": "markdown", "id": "81a8f3b7", "metadata": {}, "source": [ "## Create variables you might want to change in the future to modify your bot's performance" ] }, { "cell_type": "code", "execution_count": 19, "id": "2c362785", "metadata": {}, "outputs": [], "source": [ "rsi_timeframe = 3\n", "oversold_threshold = 49\n", "overbought_threshold = 51\n", "company = \"AAPL\"\n", "shares = 500\n", "data = []" ] }, { "cell_type": "markdown", "id": "f996032f", "metadata": {}, "source": [ "## Create all the necessary functions" ] }, { "cell_type": "code", "execution_count": 20, "id": "f7e5a841", "metadata": {}, "outputs": [], "source": [ "def on_open(ws):\n", " auth_data = {\"action\": \"authenticate\", \"data\": {\"key_id\": KEY_ID, \"secret_key\": SECRET_KEY}}\n", " ws.send(json.dumps(auth_data))\n", " \n", " listen_message = {\"action\": \"listen\", \"data\": {\"streams\": [\"AM.\" + company]}}\n", " ws.send(json.dumps(listen_message))\n", " print(\"I'm connected to Alpaca API and ready to work. I'm starting to watch the prices.\")\n", " print(\"I will start calculating RSIs when I'll collect {} prices.\".format(rsi_timeframe))\n", " print(\"I will buy {} shares of {} when the last RSI is less than {}.\".format(shares, company, oversold_threshold))\n", " print(\"I will sell {} shares of {} when the last RSI is more than {}.\".format(shares, company, overbought_threshold))\n", " print(\"So, here we go. Wish me luck.\")\n", " print()" ] }, { "cell_type": "code", "execution_count": 21, "id": "82b085ee", "metadata": {}, "outputs": [], "source": [ "def on_message(ws, message):\n", " formatted_message = ast.literal_eval(message)\n", " last_close = formatted_message.get(\"data\").get(\"c\")\n", " print(\"Last price after minute closed: {}$\".format(last_close))\n", " \n", " if isinstance(last_close, float):\n", " data.append(last_close)\n", " print('List of last {} collected prices: {}'.format(rsi_timeframe, data[-rsi_timeframe:]))\n", " \n", " try:\n", " if len(data) > rsi_timeframe:\n", " np_data = np.array(data)\n", " rsis = ta.RSI(np_data, rsi_timeframe)\n", " rsi_now = rsis[-1]\n", " \n", " print(\"The list of last 5 RSIs:\", rsis[-5:])\n", " print(\"Last RSI: \", rsi_now)\n", " \n", " if rsi_now < oversold_threshold:\n", " try:\n", " api.get_position(company)\n", " print(\"We hit the threshold to buy, but we already have some shares, so we won't buy more.\")\n", " except:\n", " api.submit_order(symbol=company, qty=shares, side = \"buy\", type='market', time_in_force='gtc')\n", " print('We submitted the order to buy {} {} shares.'.format(shares, company))\n", " \n", " elif rsi_now > overbought_threshold:\n", " try:\n", " api.get_position(company)\n", " api.submit_order(symbol=company,qty=shares,side='sell',type='market',time_in_force='gtc')\n", " print('We submitted an order to sell {} {} shares.'.format(shares, company))\n", " except:\n", " print(\"We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\")\n", " \n", " else:\n", " print(\"The RSI is {} and it's between the given thresholds: {} and {}, so we wait.\".format(rsi_now, oversold_threshold, overbought_threshold))\n", " else:\n", " print(\"Not enough prices to calculate RSI and start trading:\", len(data), \"<=\", rsi_timeframe)\n", " except:\n", " print(\"I tried my best, buy I think something went wrong. I'll try again in a moment.\")\n", " print()" ] }, { "cell_type": "markdown", "id": "7052ad6d", "metadata": {}, "source": [ "## Connect to websocket and ask your bot nicely to start buying and selling stocks for you" ] }, { "cell_type": "code", "execution_count": 22, "id": "2fbed2e9", "metadata": {}, "outputs": [], "source": [ "socket = \"wss://data.alpaca.markets/stream\"\n", "SuperAI_trader = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message)" ] }, { "cell_type": "code", "execution_count": 12, "id": "dc95d02b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm connected to Alpaca API and ready to work. I'm starting to watch the prices.\n", "I will start calculating RSIs when I'll collect 3 prices.\n", "I will buy 10 shares of AAPL when the last RSI is less than 49.\n", "I will sell 10 shares of AAPL when the last RSI is more than 51.\n", "So, here we go. Wish me luck.\n", "\n", "Last price after minute closed: None$\n", "Not enough prices to calculate RSI and start trading: 0 <= 3\n", "\n", "Last price after minute closed: None$\n", "Not enough prices to calculate RSI and start trading: 0 <= 3\n", "\n", "Last price after minute closed: 175.295$\n", "List of last 3 collected prices: [175.295]\n", "Not enough prices to calculate RSI and start trading: 1 <= 3\n", "\n", "Last price after minute closed: 175.38$\n", "List of last 3 collected prices: [175.295, 175.38]\n", "Not enough prices to calculate RSI and start trading: 2 <= 3\n", "\n", "Last price after minute closed: 175.415$\n", "List of last 3 collected prices: [175.295, 175.38, 175.415]\n", "Not enough prices to calculate RSI and start trading: 3 <= 3\n", "\n", "Last price after minute closed: 175.38$\n", "List of last 3 collected prices: [175.38, 175.415, 175.38]\n", "The list of last 5 RSIs: [ nan nan nan 77.41935484]\n", "Last RSI: 77.41935483871205\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.765$\n", "List of last 3 collected prices: [175.415, 175.38, 175.765]\n", "The list of last 5 RSIs: [ nan nan nan 77.41935484 95.221843 ]\n", "Last RSI: 95.22184300341335\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.76$\n", "List of last 3 collected prices: [175.38, 175.765, 175.76]\n", "The list of last 5 RSIs: [ nan nan 77.41935484 95.221843 93.78151261]\n", "Last RSI: 93.78151260504366\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.91$\n", "List of last 3 collected prices: [175.765, 175.76, 175.91]\n", "The list of last 5 RSIs: [ nan 77.41935484 95.221843 93.78151261 96.3 ]\n", "Last RSI: 96.30000000000108\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.825$\n", "List of last 3 collected prices: [175.76, 175.91, 175.825]\n", "The list of last 5 RSIs: [77.41935484 95.221843 93.78151261 96.3 71.63846011]\n", "Last RSI: 71.63846010786588\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.9$\n", "List of last 3 collected prices: [175.91, 175.825, 175.9]\n", "The list of last 5 RSIs: [95.221843 93.78151261 96.3 71.63846011 78.81797347]\n", "Last RSI: 78.81797347038027\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 176$\n", "The list of last 5 RSIs: [95.221843 93.78151261 96.3 71.63846011 78.81797347]\n", "Last RSI: 78.81797347038027\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.95$\n", "List of last 3 collected prices: [175.825, 175.9, 175.95]\n", "The list of last 5 RSIs: [93.78151261 96.3 71.63846011 78.81797347 83.09687431]\n", "Last RSI: 83.09687430724783\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 176.155$\n", "List of last 3 collected prices: [175.9, 175.95, 176.155]\n", "The list of last 5 RSIs: [96.3 71.63846011 78.81797347 83.09687431 92.46183029]\n", "Last RSI: 92.46183029232013\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.9$\n", "List of last 3 collected prices: [175.95, 176.155, 175.9]\n", "The list of last 5 RSIs: [71.63846011 78.81797347 83.09687431 92.46183029 45.46367369]\n", "Last RSI: 45.463673691359155\n", "We submitted the order to buy 10 AAPL shares.\n", "\n", "Last price after minute closed: 175.78$\n", "List of last 3 collected prices: [176.155, 175.9, 175.78]\n", "The list of last 5 RSIs: [78.81797347 83.09687431 92.46183029 45.46367369 33.45873025]\n", "Last RSI: 33.45873025278052\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 175.98$\n", "List of last 3 collected prices: [175.9, 175.78, 175.98]\n", "The list of last 5 RSIs: [83.09687431 92.46183029 45.46367369 33.45873025 59.91826169]\n", "Last RSI: 59.91826169053701\n", "We submitted an order to sell 10 AAPL shares.\n", "\n", "You've interrupted me. That's it then. I hope I did good. Till the next time.\n" ] } ], "source": [ "SuperAI_trader.run_forever()\n", "print(\"You've interrupted me. That's it then. I hope I did good. Till the next time.\")" ] }, { "cell_type": "markdown", "id": "5bc6b573", "metadata": {}, "source": [ "## Modify the bot" ] }, { "cell_type": "code", "execution_count": 13, "id": "201c4e15", "metadata": {}, "outputs": [], "source": [ "rsi_timeframe = 4\n", "oversold_threshold = 48\n", "overbought_threshold = 52\n", "company = \"TSLA\"\n", "shares = 100\n", "data = []" ] }, { "cell_type": "code", "execution_count": 14, "id": "c5815876", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm connected to Alpaca API and ready to work. I'm starting to watch the prices.\n", "I will start calculating RSIs when I'll collect 4 prices.\n", "I will buy 100 shares of TSLA when the last RSI is less than 48.\n", "I will sell 100 shares of TSLA when the last RSI is more than 52.\n", "So, here we go. Wish me luck.\n", "\n", "Last price after minute closed: None$\n", "Not enough prices to calculate RSI and start trading: 0 <= 4\n", "\n", "Last price after minute closed: None$\n", "Not enough prices to calculate RSI and start trading: 0 <= 4\n", "\n", "Last price after minute closed: 958.83$\n", "List of last 4 collected prices: [958.83]\n", "Not enough prices to calculate RSI and start trading: 1 <= 4\n", "\n", "Last price after minute closed: 957.835$\n", "List of last 4 collected prices: [958.83, 957.835]\n", "Not enough prices to calculate RSI and start trading: 2 <= 4\n", "\n", "Last price after minute closed: 958.57$\n", "List of last 4 collected prices: [958.83, 957.835, 958.57]\n", "Not enough prices to calculate RSI and start trading: 3 <= 4\n", "\n", "Last price after minute closed: 954.18$\n", "List of last 4 collected prices: [958.83, 957.835, 958.57, 954.18]\n", "Not enough prices to calculate RSI and start trading: 4 <= 4\n", "\n", "Last price after minute closed: 953.69$\n", "List of last 4 collected prices: [957.835, 958.57, 954.18, 953.69]\n", "The list of last 5 RSIs: [ nan nan nan nan 11.11951589]\n", "Last RSI: 11.119515885022876\n", "We submitted the order to buy 100 TSLA shares.\n", "\n", "Last price after minute closed: 950.56$\n", "List of last 4 collected prices: [958.57, 954.18, 953.69, 950.56]\n", "The list of last 5 RSIs: [ nan nan nan 11.11951589 6.81607419]\n", "Last RSI: 6.816074188562622\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 952.15$\n", "List of last 4 collected prices: [954.18, 953.69, 950.56, 952.15]\n", "The list of last 5 RSIs: [ nan nan 11.11951589 6.81607419 26.16948322]\n", "Last RSI: 26.169483223120356\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 950.42$\n", "List of last 4 collected prices: [953.69, 950.56, 952.15, 950.42]\n", "The list of last 5 RSIs: [ nan 11.11951589 6.81607419 26.16948322 20.11020724]\n", "Last RSI: 20.110207239800182\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 948.42$\n", "List of last 4 collected prices: [950.56, 952.15, 950.42, 948.42]\n", "The list of last 5 RSIs: [11.11951589 6.81607419 26.16948322 20.11020724 14.82068459]\n", "Last RSI: 14.820684588789643\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 949$\n", "The list of last 5 RSIs: [11.11951589 6.81607419 26.16948322 20.11020724 14.82068459]\n", "Last RSI: 14.820684588789643\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 948.89$\n", "List of last 4 collected prices: [952.15, 950.42, 948.42, 948.89]\n", "The list of last 5 RSIs: [ 6.81607419 26.16948322 20.11020724 14.82068459 21.30623526]\n", "Last RSI: 21.306235257452162\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 947.78$\n", "List of last 4 collected prices: [950.42, 948.42, 948.89, 947.78]\n", "The list of last 5 RSIs: [26.16948322 20.11020724 14.82068459 21.30623526 17.1857753 ]\n", "Last RSI: 17.185775300484863\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 947.5$\n", "List of last 4 collected prices: [948.42, 948.89, 947.78, 947.5]\n", "The list of last 5 RSIs: [20.11020724 14.82068459 21.30623526 17.1857753 16.13619901]\n", "Last RSI: 16.13619901243597\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 949.84$\n", "List of last 4 collected prices: [948.89, 947.78, 947.5, 949.84]\n", "The list of last 5 RSIs: [14.82068459 21.30623526 17.1857753 16.13619901 50.09654278]\n", "Last RSI: 50.096542782902546\n", "The RSI is 50.096542782902546 and it's between the given thresholds: 48 and 52, so we wait.\n", "\n", "Last price after minute closed: 947.94$\n", "List of last 4 collected prices: [947.78, 947.5, 949.84, 947.94]\n", "The list of last 5 RSIs: [21.30623526 17.1857753 16.13619901 50.09654278 34.82787874]\n", "Last RSI: 34.82787873764192\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 948.97$\n", "List of last 4 collected prices: [947.5, 949.84, 947.94, 948.97]\n", "The list of last 5 RSIs: [17.1857753 16.13619901 50.09654278 34.82787874 46.59338672]\n", "Last RSI: 46.59338671535519\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 949.33$\n", "List of last 4 collected prices: [949.84, 947.94, 948.97, 949.33]\n", "The list of last 5 RSIs: [16.13619901 50.09654278 34.82787874 46.59338672 50.73783155]\n", "Last RSI: 50.73783155085937\n", "The RSI is 50.73783155085937 and it's between the given thresholds: 48 and 52, so we wait.\n", "\n", "Last price after minute closed: 947.97$\n", "List of last 4 collected prices: [947.94, 948.97, 949.33, 947.97]\n", "The list of last 5 RSIs: [50.09654278 34.82787874 46.59338672 50.73783155 36.47887123]\n", "Last RSI: 36.478871231662936\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 945.05$\n", "List of last 4 collected prices: [948.97, 949.33, 947.97, 945.05]\n", "The list of last 5 RSIs: [34.82787874 46.59338672 50.73783155 36.47887123 20.21523956]\n", "Last RSI: 20.215239559532506\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 946.03$\n", "List of last 4 collected prices: [949.33, 947.97, 945.05, 946.03]\n", "The list of last 5 RSIs: [46.59338672 50.73783155 36.47887123 20.21523956 33.48537222]\n", "Last RSI: 33.485372222938445\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 946.94$\n", "List of last 4 collected prices: [947.97, 945.05, 946.03, 946.94]\n", "The list of last 5 RSIs: [50.73783155 36.47887123 20.21523956 33.48537222 44.8434853 ]\n", "Last RSI: 44.8434853006102\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 944.46$\n", "List of last 4 collected prices: [945.05, 946.03, 946.94, 944.46]\n", "The list of last 5 RSIs: [36.47887123 20.21523956 33.48537222 44.8434853 27.67271403]\n", "Last RSI: 27.67271402670053\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 939.67$\n", "List of last 4 collected prices: [946.03, 946.94, 944.46, 939.67]\n", "The list of last 5 RSIs: [20.21523956 33.48537222 44.8434853 27.67271403 13.93331927]\n", "Last RSI: 13.933319268482036\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 941.56$\n", "List of last 4 collected prices: [946.94, 944.46, 939.67, 941.56]\n", "The list of last 5 RSIs: [33.48537222 44.8434853 27.67271403 13.93331927 31.75835344]\n", "Last RSI: 31.75835344391703\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 940.36$\n", "List of last 4 collected prices: [944.46, 939.67, 941.56, 940.36]\n", "The list of last 5 RSIs: [44.8434853 27.67271403 13.93331927 31.75835344 27.0208225 ]\n", "Last RSI: 27.020822496033226\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 939.59$\n", "List of last 4 collected prices: [939.67, 941.56, 940.36, 939.59]\n", "The list of last 5 RSIs: [27.67271403 13.93331927 31.75835344 27.0208225 23.9625553 ]\n", "Last RSI: 23.962555301372106\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 938.2$\n", "List of last 4 collected prices: [941.56, 940.36, 939.59, 938.2]\n", "The list of last 5 RSIs: [13.93331927 31.75835344 27.0208225 23.9625553 18.83226231]\n", "Last RSI: 18.83226231322911\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 935.95$\n", "List of last 4 collected prices: [940.36, 939.59, 938.2, 935.95]\n", "The list of last 5 RSIs: [31.75835344 27.0208225 23.9625553 18.83226231 12.88047528]\n", "Last RSI: 12.880475279958514\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 937.8$\n", "List of last 4 collected prices: [939.59, 938.2, 935.95, 937.8]\n", "The list of last 5 RSIs: [27.0208225 23.9625553 18.83226231 12.88047528 35.29811052]\n", "Last RSI: 35.298110522462956\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 935.37$\n", "List of last 4 collected prices: [938.2, 935.95, 937.8, 935.37]\n", "The list of last 5 RSIs: [23.9625553 18.83226231 12.88047528 35.29811052 24.33247392]\n", "Last RSI: 24.332473920547187\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 937.985$\n", "List of last 4 collected prices: [935.95, 937.8, 935.37, 937.985]\n", "The list of last 5 RSIs: [18.83226231 12.88047528 35.29811052 24.33247392 47.66191652]\n", "Last RSI: 47.66191652250448\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 937.86$\n", "List of last 4 collected prices: [937.8, 935.37, 937.985, 937.86]\n", "The list of last 5 RSIs: [12.88047528 35.29811052 24.33247392 47.66191652 46.74338919]\n", "Last RSI: 46.743389194037455\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 937.44$\n", "List of last 4 collected prices: [935.37, 937.985, 937.86, 937.44]\n", "The list of last 5 RSIs: [35.29811052 24.33247392 47.66191652 46.74338919 43.02842963]\n", "Last RSI: 43.02842963141346\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 937.8$\n", "List of last 4 collected prices: [937.985, 937.86, 937.44, 937.8]\n", "The list of last 5 RSIs: [24.33247392 47.66191652 46.74338919 43.02842963 47.77223919]\n", "Last RSI: 47.77223919119988\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 937.63$\n", "List of last 4 collected prices: [937.86, 937.44, 937.8, 937.63]\n", "The list of last 5 RSIs: [47.66191652 46.74338919 43.02842963 47.77223919 45.39245339]\n", "Last RSI: 45.392453390419405\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 936.81$\n", "List of last 4 collected prices: [937.44, 937.8, 937.63, 936.81]\n", "The list of last 5 RSIs: [46.74338919 43.02842963 47.77223919 45.39245339 34.37831508]\n", "Last RSI: 34.37831507608808\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 935.38$\n", "List of last 4 collected prices: [937.8, 937.63, 936.81, 935.38]\n", "The list of last 5 RSIs: [43.02842963 47.77223919 45.39245339 34.37831508 21.97830593]\n", "Last RSI: 21.97830592631813\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 935.02$\n", "List of last 4 collected prices: [937.63, 936.81, 935.38, 935.02]\n", "The list of last 5 RSIs: [47.77223919 45.39245339 34.37831508 21.97830593 19.60472827]\n", "Last RSI: 19.604728269782225\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 935.92$\n", "List of last 4 collected prices: [936.81, 935.38, 935.02, 935.92]\n", "The list of last 5 RSIs: [45.39245339 34.37831508 21.97830593 19.60472827 40.88530629]\n", "Last RSI: 40.88530628529858\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 938.41$\n", "List of last 4 collected prices: [935.38, 935.02, 935.92, 938.41]\n", "The list of last 5 RSIs: [34.37831508 21.97830593 19.60472827 40.88530629 70.09041659]\n", "Last RSI: 70.09041658962548\n", "We submitted an order to sell 100 TSLA shares.\n", "\n", "Last price after minute closed: 938.72$\n", "List of last 4 collected prices: [935.02, 935.92, 938.41, 938.72]\n", "The list of last 5 RSIs: [21.97830593 19.60472827 40.88530629 70.09041659 72.35737606]\n", "Last RSI: 72.35737606116118\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 939.68$\n", "List of last 4 collected prices: [935.92, 938.41, 938.72, 939.68]\n", "The list of last 5 RSIs: [19.60472827 40.88530629 70.09041659 72.35737606 78.9462492 ]\n", "Last RSI: 78.94624920021882\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 938.945$\n", "List of last 4 collected prices: [938.41, 938.72, 939.68, 938.945]\n", "The list of last 5 RSIs: [40.88530629 70.09041659 72.35737606 78.9462492 63.49606862]\n", "Last RSI: 63.496068624751146\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 937.57$\n", "List of last 4 collected prices: [938.72, 939.68, 938.945, 937.57]\n", "The list of last 5 RSIs: [70.09041659 72.35737606 78.9462492 63.49606862 42.66769575]\n", "Last RSI: 42.66769575403991\n", "We submitted the order to buy 100 TSLA shares.\n", "\n", "Last price after minute closed: 939.1$\n", "List of last 4 collected prices: [939.68, 938.945, 937.57, 939.1]\n", "The list of last 5 RSIs: [72.35737606 78.9462492 63.49606862 42.66769575 61.43579697]\n", "Last RSI: 61.43579697420961\n", "We submitted an order to sell 100 TSLA shares.\n", "\n", "You've interrupted me. That's it then. I hope I did good. Till the next time.\n" ] } ], "source": [ "SuperAI_trader.run_forever()\n", "print(\"You've interrupted me. That's it then. I hope I did good. Till the next time.\")" ] }, { "cell_type": "markdown", "id": "148fd3ee", "metadata": {}, "source": [ "### Pretty much the same bot in 15 lines of code" ] }, { "cell_type": "code", "execution_count": 16, "id": "c08181ab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "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\": \"PK3O8OD82FIXEEPGT8GP\", \"secret_key\": \"9tCxXfAtHpeUi6qlhdL9i7dGHt6qgDCNzQreBkcZ\"}}))\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(\"PK3O8OD82FIXEEPGT8GP\", \"9tCxXfAtHpeUi6qlhdL9i7dGHt6qgDCNzQreBkcZ\", \"https://paper-api.alpaca.markets\", \"v2\").get_position(\"AAPL\")\n", " except:tradeapi.REST(\"PK3O8OD82FIXEEPGT8GP\", \"9tCxXfAtHpeUi6qlhdL9i7dGHt6qgDCNzQreBkcZ\", \"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(\"PK3O8OD82FIXEEPGT8GP\", \"9tCxXfAtHpeUi6qlhdL9i7dGHt6qgDCNzQreBkcZ\", \"https://paper-api.alpaca.markets\", \"v2\").get_position(\"AAPL\")\n", " tradeapi.REST(\"PK3O8OD82FIXEEPGT8GP\", \"9tCxXfAtHpeUi6qlhdL9i7dGHt6qgDCNzQreBkcZ\", \"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()" ] }, { "cell_type": "markdown", "id": "b470324f", "metadata": {}, "source": [ "## Try a bot for an hour:" ] }, { "cell_type": "code", "execution_count": 23, "id": "d87bfef0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm connected to Alpaca API and ready to work. I'm starting to watch the prices.\n", "I will start calculating RSIs when I'll collect 3 prices.\n", "I will buy 500 shares of AAPL when the last RSI is less than 49.\n", "I will sell 500 shares of AAPL when the last RSI is more than 51.\n", "So, here we go. Wish me luck.\n", "\n", "Last price after minute closed: None$\n", "Not enough prices to calculate RSI and start trading: 0 <= 3\n", "\n", "Last price after minute closed: None$\n", "Not enough prices to calculate RSI and start trading: 0 <= 3\n", "\n", "Last price after minute closed: 174.81$\n", "List of last 3 collected prices: [174.81]\n", "Not enough prices to calculate RSI and start trading: 1 <= 3\n", "\n", "Last price after minute closed: 174.8$\n", "List of last 3 collected prices: [174.81, 174.8]\n", "Not enough prices to calculate RSI and start trading: 2 <= 3\n", "\n", "Last price after minute closed: 174.73$\n", "List of last 3 collected prices: [174.81, 174.8, 174.73]\n", "Not enough prices to calculate RSI and start trading: 3 <= 3\n", "\n", "Last price after minute closed: 174.76$\n", "List of last 3 collected prices: [174.8, 174.73, 174.76]\n", "The list of last 5 RSIs: [ nan nan nan 27.27272727]\n", "Last RSI: 27.27272727272492\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.82$\n", "List of last 3 collected prices: [174.73, 174.76, 174.82]\n", "The list of last 5 RSIs: [ nan nan nan 27.27272727 60. ]\n", "Last RSI: 59.99999999999716\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.96$\n", "List of last 3 collected prices: [174.76, 174.82, 174.96]\n", "The list of last 5 RSIs: [ nan nan 27.27272727 60. 84.46601942]\n", "Last RSI: 84.46601941747481\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.13$\n", "List of last 3 collected prices: [174.82, 174.96, 175.13]\n", "The list of last 5 RSIs: [ nan 27.27272727 60. 84.46601942 92.652124 ]\n", "Last RSI: 92.65212399540648\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.09$\n", "List of last 3 collected prices: [174.96, 175.13, 175.09]\n", "The list of last 5 RSIs: [27.27272727 60. 84.46601942 92.652124 78.12197483]\n", "Last RSI: 78.12197483059212\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.21$\n", "List of last 3 collected prices: [175.13, 175.09, 175.21]\n", "The list of last 5 RSIs: [60. 84.46601942 92.652124 78.12197483 87.17366629]\n", "Last RSI: 87.17366628831002\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.18$\n", "List of last 3 collected prices: [175.09, 175.21, 175.18]\n", "The list of last 5 RSIs: [84.46601942 92.652124 78.12197483 87.17366629 75.46520911]\n", "Last RSI: 75.4652091138004\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.19$\n", "List of last 3 collected prices: [175.21, 175.18, 175.19]\n", "The list of last 5 RSIs: [92.652124 78.12197483 87.17366629 75.46520911 77.00917906]\n", "Last RSI: 77.00917906367731\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.89$\n", "List of last 3 collected prices: [175.18, 175.19, 174.89]\n", "The list of last 5 RSIs: [78.12197483 87.17366629 75.46520911 77.00917906 20.09717049]\n", "Last RSI: 20.097170491407844\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.33$\n", "List of last 3 collected prices: [175.19, 174.89, 175.33]\n", "The list of last 5 RSIs: [87.17366629 75.46520911 77.00917906 20.09717049 69.57083742]\n", "Last RSI: 69.57083742039201\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.33$\n", "List of last 3 collected prices: [174.89, 175.33, 175.33]\n", "The list of last 5 RSIs: [75.46520911 77.00917906 20.09717049 69.57083742 69.57083742]\n", "Last RSI: 69.57083742039201\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.13$\n", "List of last 3 collected prices: [175.33, 175.33, 175.13]\n", "The list of last 5 RSIs: [77.00917906 20.09717049 69.57083742 69.57083742 42.59669357]\n", "Last RSI: 42.59669357165035\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.17$\n", "List of last 3 collected prices: [175.33, 175.13, 175.17]\n", "The list of last 5 RSIs: [20.09717049 69.57083742 69.57083742 42.59669357 48.57793344]\n", "Last RSI: 48.577933435664136\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 175.09$\n", "List of last 3 collected prices: [175.13, 175.17, 175.09]\n", "The list of last 5 RSIs: [69.57083742 69.57083742 42.59669357 48.57793344 37.00921119]\n", "Last RSI: 37.00921119180928\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 175.2$\n", "List of last 3 collected prices: [175.17, 175.09, 175.2]\n", "The list of last 5 RSIs: [69.57083742 42.59669357 48.57793344 37.00921119 57.7577449 ]\n", "Last RSI: 57.75774490428967\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.065$\n", "List of last 3 collected prices: [175.09, 175.2, 175.065]\n", "The list of last 5 RSIs: [42.59669357 48.57793344 37.00921119 57.7577449 35.95528671]\n", "Last RSI: 35.955286707552894\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.145$\n", "List of last 3 collected prices: [175.2, 175.065, 175.145]\n", "The list of last 5 RSIs: [48.57793344 37.00921119 57.7577449 35.95528671 52.04578512]\n", "Last RSI: 52.045785117161145\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.04$\n", "List of last 3 collected prices: [175.065, 175.145, 175.04]\n", "The list of last 5 RSIs: [37.00921119 57.7577449 35.95528671 52.04578512 34.82194973]\n", "Last RSI: 34.82194972611287\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.91$\n", "List of last 3 collected prices: [175.145, 175.04, 174.91]\n", "The list of last 5 RSIs: [57.7577449 35.95528671 52.04578512 34.82194973 21.56697604]\n", "Last RSI: 21.56697603637055\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.655$\n", "List of last 3 collected prices: [175.04, 174.91, 174.655]\n", "The list of last 5 RSIs: [35.95528671 52.04578512 34.82194973 21.56697604 10.17315326]\n", "Last RSI: 10.173153256431895\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.66$\n", "List of last 3 collected prices: [174.91, 174.655, 174.66]\n", "The list of last 5 RSIs: [52.04578512 34.82194973 21.56697604 10.17315326 11.547547 ]\n", "Last RSI: 11.547546997638612\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.59$\n", "List of last 3 collected prices: [174.655, 174.66, 174.59]\n", "The list of last 5 RSIs: [34.82194973 21.56697604 10.17315326 11.547547 8.73946808]\n", "Last RSI: 8.739468077768775\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.79$\n", "List of last 3 collected prices: [174.66, 174.59, 174.79]\n", "The list of last 5 RSIs: [21.56697604 10.17315326 11.547547 8.73946808 55.31220242]\n", "Last RSI: 55.31220242252148\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.94$\n", "List of last 3 collected prices: [174.59, 174.79, 174.94]\n", "The list of last 5 RSIs: [10.17315326 11.547547 8.73946808 55.31220242 71.61089968]\n", "Last RSI: 71.61089967700234\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.92$\n", "List of last 3 collected prices: [174.79, 174.94, 174.92]\n", "The list of last 5 RSIs: [11.547547 8.73946808 55.31220242 71.61089968 66.74239361]\n", "Last RSI: 66.7423936126918\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.88$\n", "List of last 3 collected prices: [174.94, 174.92, 174.88]\n", "The list of last 5 RSIs: [ 8.73946808 55.31220242 71.61089968 66.74239361 55.43587835]\n", "Last RSI: 55.43587835454238\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.86$\n", "List of last 3 collected prices: [174.92, 174.88, 174.86]\n", "The list of last 5 RSIs: [55.31220242 71.61089968 66.74239361 55.43587835 49.18653371]\n", "Last RSI: 49.18653370563978\n", "The RSI is 49.18653370563978 and it's between the given thresholds: 49 and 51, so we wait.\n", "\n", "Last price after minute closed: 174.98$\n", "List of last 3 collected prices: [174.88, 174.86, 174.98]\n", "The list of last 5 RSIs: [71.61089968 66.74239361 55.43587835 49.18653371 74.77713435]\n", "Last RSI: 74.77713434981135\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.84$\n", "List of last 3 collected prices: [174.86, 174.98, 174.84]\n", "The list of last 5 RSIs: [66.74239361 55.43587835 49.18653371 74.77713435 39.74690318]\n", "Last RSI: 39.74690318122695\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.68$\n", "List of last 3 collected prices: [174.98, 174.84, 174.68]\n", "The list of last 5 RSIs: [55.43587835 49.18653371 74.77713435 39.74690318 22.0439254 ]\n", "Last RSI: 22.043925404310933\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.78$\n", "List of last 3 collected prices: [174.84, 174.68, 174.78]\n", "The list of last 5 RSIs: [49.18653371 74.77713435 39.74690318 22.0439254 45.00668964]\n", "Last RSI: 45.00668963794006\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.8$\n", "List of last 3 collected prices: [174.68, 174.78, 174.8]\n", "The list of last 5 RSIs: [74.77713435 39.74690318 22.0439254 45.00668964 49.4717725 ]\n", "Last RSI: 49.47177249878865\n", "The RSI is 49.47177249878865 and it's between the given thresholds: 49 and 51, so we wait.\n", "\n", "Last price after minute closed: 174.825$\n", "List of last 3 collected prices: [174.78, 174.8, 174.825]\n", "The list of last 5 RSIs: [39.74690318 22.0439254 45.00668964 49.4717725 56.14772263]\n", "Last RSI: 56.14772263234076\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.71$\n", "List of last 3 collected prices: [174.8, 174.825, 174.71]\n", "The list of last 5 RSIs: [22.0439254 45.00668964 49.4717725 56.14772263 29.37134109]\n", "Last RSI: 29.371341087620266\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.875$\n", "List of last 3 collected prices: [174.825, 174.71, 174.875]\n", "The list of last 5 RSIs: [45.00668964 49.4717725 56.14772263 29.37134109 65.14495244]\n", "Last RSI: 65.14495244247105\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.93$\n", "List of last 3 collected prices: [174.71, 174.875, 174.93]\n", "The list of last 5 RSIs: [49.4717725 56.14772263 29.37134109 65.14495244 72.18830318]\n", "Last RSI: 72.18830317703674\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.85$\n", "List of last 3 collected prices: [174.875, 174.93, 174.85]\n", "The list of last 5 RSIs: [56.14772263 29.37134109 65.14495244 72.18830318 50.09973303]\n", "Last RSI: 50.099733026305195\n", "The RSI is 50.099733026305195 and it's between the given thresholds: 49 and 51, so we wait.\n", "\n", "Last price after minute closed: 174.835$\n", "List of last 3 collected prices: [174.93, 174.85, 174.835]\n", "The list of last 5 RSIs: [29.37134109 65.14495244 72.18830318 50.09973303 46.12986992]\n", "Last RSI: 46.129869919090716\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.12$\n", "List of last 3 collected prices: [174.85, 174.835, 175.12]\n", "The list of last 5 RSIs: [65.14495244 72.18830318 50.09973303 46.12986992 83.46688688]\n", "Last RSI: 83.46688687511462\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.93$\n", "List of last 3 collected prices: [174.835, 175.12, 174.93]\n", "The list of last 5 RSIs: [72.18830318 50.09973303 46.12986992 83.46688688 49.29846179]\n", "Last RSI: 49.29846179258534\n", "The RSI is 49.29846179258534 and it's between the given thresholds: 49 and 51, so we wait.\n", "\n", "Last price after minute closed: 174.71$\n", "List of last 3 collected prices: [175.12, 174.93, 174.71]\n", "The list of last 5 RSIs: [50.09973303 46.12986992 83.46688688 49.29846179 28.8126194 ]\n", "Last RSI: 28.812619398795807\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.91$\n", "List of last 3 collected prices: [174.93, 174.71, 174.91]\n", "The list of last 5 RSIs: [46.12986992 83.46688688 49.29846179 28.8126194 54.56091967]\n", "Last RSI: 54.56091967447566\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.85$\n", "List of last 3 collected prices: [174.71, 174.91, 174.85]\n", "The list of last 5 RSIs: [83.46688688 49.29846179 28.8126194 54.56091967 46.92347264]\n", "Last RSI: 46.92347264343271\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.015$\n", "List of last 3 collected prices: [174.91, 174.85, 175.015]\n", "The list of last 5 RSIs: [49.29846179 28.8126194 54.56091967 46.92347264 66.35227947]\n", "Last RSI: 66.35227946797147\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 175.055$\n", "List of last 3 collected prices: [174.85, 175.015, 175.055]\n", "The list of last 5 RSIs: [28.8126194 54.56091967 46.92347264 66.35227947 70.30498519]\n", "Last RSI: 70.30498518745712\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.11$\n", "List of last 3 collected prices: [175.015, 175.055, 175.11]\n", "The list of last 5 RSIs: [54.56091967 46.92347264 66.35227947 70.30498519 76.09652271]\n", "Last RSI: 76.09652271006429\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175.175$\n", "List of last 3 collected prices: [175.055, 175.11, 175.175]\n", "The list of last 5 RSIs: [46.92347264 66.35227947 70.30498519 76.09652271 82.23769811]\n", "Last RSI: 82.23769810907035\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 175$\n", "The list of last 5 RSIs: [46.92347264 66.35227947 70.30498519 76.09652271 82.23769811]\n", "Last RSI: 82.23769810907035\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.84$\n", "List of last 3 collected prices: [175.11, 175.175, 174.84]\n", "The list of last 5 RSIs: [66.35227947 70.30498519 76.09652271 82.23769811 27.53966201]\n", "Last RSI: 27.53966200718086\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.71$\n", "List of last 3 collected prices: [175.175, 174.84, 174.71]\n", "The list of last 5 RSIs: [70.30498519 76.09652271 82.23769811 27.53966201 19.85326858]\n", "Last RSI: 19.853268578503858\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.87$\n", "List of last 3 collected prices: [174.84, 174.71, 174.87]\n", "The list of last 5 RSIs: [76.09652271 82.23769811 27.53966201 19.85326858 47.10717071]\n", "Last RSI: 47.107170712718286\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.575$\n", "List of last 3 collected prices: [174.71, 174.87, 174.575]\n", "The list of last 5 RSIs: [82.23769811 27.53966201 19.85326858 47.10717071 24.27640322]\n", "Last RSI: 24.276403218201672\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.61$\n", "List of last 3 collected prices: [174.87, 174.575, 174.61]\n", "The list of last 5 RSIs: [27.53966201 19.85326858 47.10717071 24.27640322 30.28912759]\n", "Last RSI: 30.28912758530808\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.77$\n", "List of last 3 collected prices: [174.575, 174.61, 174.77]\n", "The list of last 5 RSIs: [19.85326858 47.10717071 24.27640322 30.28912759 54.86455161]\n", "Last RSI: 54.864551609889645\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.67$\n", "List of last 3 collected prices: [174.61, 174.77, 174.67]\n", "The list of last 5 RSIs: [47.10717071 24.27640322 30.28912759 54.86455161 41.23603287]\n", "Last RSI: 41.23603287280436\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.685$\n", "List of last 3 collected prices: [174.77, 174.67, 174.685]\n", "The list of last 5 RSIs: [24.27640322 30.28912759 54.86455161 41.23603287 44.34654209]\n", "Last RSI: 44.34654209258958\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.5$\n", "List of last 3 collected prices: [174.67, 174.685, 174.5]\n", "The list of last 5 RSIs: [30.28912759 54.86455161 41.23603287 44.34654209 22.40576792]\n", "Last RSI: 22.40576791940904\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.6$\n", "List of last 3 collected prices: [174.685, 174.5, 174.6]\n", "The list of last 5 RSIs: [54.86455161 41.23603287 44.34654209 22.40576792 44.62121799]\n", "Last RSI: 44.62121799458714\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.82$\n", "List of last 3 collected prices: [174.5, 174.6, 174.82]\n", "The list of last 5 RSIs: [41.23603287 44.34654209 22.40576792 44.62121799 71.52468192]\n", "Last RSI: 71.5246819206448\n", "We submitted an order to sell 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.72$\n", "List of last 3 collected prices: [174.6, 174.82, 174.72]\n", "The list of last 5 RSIs: [44.34654209 22.40576792 44.62121799 71.52468192 53.72815344]\n", "Last RSI: 53.72815343859464\n", "We hit the threshold to sell, but we don't have anything to sell. Next time maybe.\n", "\n", "Last price after minute closed: 174.545$\n", "List of last 3 collected prices: [174.82, 174.72, 174.545]\n", "The list of last 5 RSIs: [22.40576792 44.62121799 71.52468192 53.72815344 32.50059697]\n", "Last RSI: 32.500596972202686\n", "We submitted the order to buy 500 AAPL shares.\n", "\n", "Last price after minute closed: 174.58$\n", "List of last 3 collected prices: [174.72, 174.545, 174.58]\n", "The list of last 5 RSIs: [44.62121799 71.52468192 53.72815344 32.50059697 39.65333942]\n", "Last RSI: 39.65333941607778\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.38$\n", "List of last 3 collected prices: [174.545, 174.58, 174.38]\n", "The list of last 5 RSIs: [71.52468192 53.72815344 32.50059697 39.65333942 20.77948398]\n", "Last RSI: 20.779483982050532\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.45$\n", "List of last 3 collected prices: [174.58, 174.38, 174.45]\n", "The list of last 5 RSIs: [53.72815344 32.50059697 39.65333942 20.77948398 36.61775523]\n", "Last RSI: 36.61775522998833\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.27$\n", "List of last 3 collected prices: [174.38, 174.45, 174.27]\n", "The list of last 5 RSIs: [32.50059697 39.65333942 20.77948398 36.61775523 20.67462747]\n", "Last RSI: 20.674627465384532\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.295$\n", "List of last 3 collected prices: [174.45, 174.27, 174.295]\n", "The list of last 5 RSIs: [39.65333942 20.77948398 36.61775523 20.67462747 27.27159901]\n", "Last RSI: 27.271599012891585\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.175$\n", "List of last 3 collected prices: [174.27, 174.295, 174.175]\n", "The list of last 5 RSIs: [20.77948398 36.61775523 20.67462747 27.27159901 17.05778974]\n", "Last RSI: 17.057789738474003\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.065$\n", "List of last 3 collected prices: [174.295, 174.175, 174.065]\n", "The list of last 5 RSIs: [36.61775523 20.67462747 27.27159901 17.05778974 11.25950853]\n", "Last RSI: 11.259508534572772\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 174.165$\n", "List of last 3 collected prices: [174.175, 174.065, 174.165]\n", "The list of last 5 RSIs: [20.67462747 27.27159901 17.05778974 11.25950853 39.36532231]\n", "Last RSI: 39.36532230784315\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 173.645$\n", "List of last 3 collected prices: [174.065, 174.165, 173.645]\n", "The list of last 5 RSIs: [27.27159901 17.05778974 11.25950853 39.36532231 11.34313488]\n", "Last RSI: 11.343134877573092\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 173.575$\n", "List of last 3 collected prices: [174.165, 173.645, 173.575]\n", "The list of last 5 RSIs: [17.05778974 11.25950853 39.36532231 11.34313488 9.91759162]\n", "Last RSI: 9.917591624937888\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 173.605$\n", "List of last 3 collected prices: [173.645, 173.575, 173.605]\n", "The list of last 5 RSIs: [11.25950853 39.36532231 11.34313488 9.91759162 16.65139042]\n", "Last RSI: 16.65139042255057\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "Last price after minute closed: 173.68$\n", "List of last 3 collected prices: [173.575, 173.605, 173.68]\n", "The list of last 5 RSIs: [39.36532231 11.34313488 9.91759162 16.65139042 34.90008611]\n", "Last RSI: 34.900086108358224\n", "We hit the threshold to buy, but we already have some shares, so we won't buy more.\n", "\n", "You've interrupted me. That's it then. I hope I did good. Till the next time.\n" ] } ], "source": [ "SuperAI_trader.run_forever()\n", "print(\"You've interrupted me. That's it then. I hope I did good. Till the next time.\")" ] }, { "cell_type": "markdown", "id": "917e45fd", "metadata": {}, "source": [ "After an hour or so...\n", "\n", "In general, after almost 3 hours of using this bot in different configurations we lost around 1% of our 'paper' money...\n", "\n", "Thanks a lot bot..." ] }, { "cell_type": "code", "execution_count": null, "id": "8ecf91f6", "metadata": {}, "outputs": [], "source": [] } ], "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 }