{ "cells": [ { "cell_type": "markdown", "id": "44bb7c0b", "metadata": {}, "source": [ "# Alpaca Paper Trading Tutorial for Beginners (2025) | Stock & Crypto Orders in Python" ] }, { "cell_type": "markdown", "id": "01469e69-3a2f-4725-b05f-2ffc444a3e10", "metadata": {}, "source": [ "I. PREPARING ENVIRONMENT\n", "\n", "1. SETTING UP THE ACCOUNT\n", "2. INSTALLING PYTHON/CREATING ENVIRONMENT\n", "3. INSTALLING NECESSARY LIBRARIES\n", "\n", "II. BASIC TASKS:\n", "\n", "1. How to check the account in the browser\n", "2. How to submit an order to get all positions\n", "3. How to submit an order to buy stocks\n", "4. How to submit an order to sell stocks\n", "5. How to submit an order to buy cryptocurrency\n", "6. How to submit an order to sell cryptocurrency\n", "7. How to submit an order to close all positions\n", "\n", "EXTRA. How to get historical data\n", "\n", "You can find more info on alpaca.markets:\n", "\n", "https://alpaca.markets/sdks/python/getting_started.html" ] }, { "cell_type": "markdown", "id": "a85728f4-29c9-489c-80d3-f8b7fc33cd67", "metadata": {}, "source": [ "# I. PREPARING ENVIRONMENT " ] }, { "cell_type": "markdown", "id": "728c9d0c-f558-4f55-b975-9345c16f9c75", "metadata": {}, "source": [ "## 1. SETTING UP THE ACCOUNT" ] }, { "cell_type": "markdown", "id": "1dd005a6-5d72-4bad-9c19-289ee3661d21", "metadata": {}, "source": [ "https://alpaca.markets/" ] }, { "cell_type": "markdown", "id": "7e8399bb-23d1-4464-baf2-a6d6ebb5b334", "metadata": {}, "source": [ "## 2. INSTALLING PYTHON/CREATING ENVIRONMENT" ] }, { "cell_type": "markdown", "id": "df4e990a-e86a-44bd-92d6-9ddb700050bd", "metadata": {}, "source": [ "https://anaconda.org/" ] }, { "cell_type": "markdown", "id": "568b841f-ef07-4a52-8cba-9f63af0cfde5", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "

Environment: alpaca-env

\n", "\n", "-\tAnaconda Navigator: 2.6.4 \n", "-\tjupyter Notebook: 7.2.2\n", "-\tPython version: 3.11.11\n", "-\tPip version: 24.2\n", "- alpaca-py version: 0.35.0" ] }, { "cell_type": "markdown", "id": "0ebb53c3-4008-4bdd-9b50-a3716a745aac", "metadata": {}, "source": [ "## 3. INSTALLING NECESSARY LIBRARIES" ] }, { "cell_type": "code", "execution_count": null, "id": "0750002f", "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": null, "id": "daa291e7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "!pip install alpaca-py" ] }, { "cell_type": "markdown", "id": "fb002556-0b24-42b3-9b61-51a3f45df0ac", "metadata": {}, "source": [ "# II. BASIC TASKS" ] }, { "cell_type": "markdown", "id": "4caa520f-c5eb-4e4a-b564-f84b814027bf", "metadata": {}, "source": [ "## 1. How to check the account in the browser" ] }, { "cell_type": "markdown", "id": "2f27fea1-5183-4fa8-b0c6-7b9f75cb8e7d", "metadata": {}, "source": [ "https://alpaca.markets/" ] }, { "cell_type": "markdown", "id": "377529ad-81e1-4658-aa5e-336bbc8b4c1a", "metadata": {}, "source": [ "## 2. How to submit an order to get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "19e7a318-0c8c-41b1-aa0e-f8f5af1505f2", "metadata": {}, "outputs": [], "source": [ "api_key = \"your API KEY\" #replace it with your own KEY_ID from Alpaca: https://alpaca.markets/\n", "secret_key = \"your SECRET KEY\" #replace it with your own SECRET_KEY from Alpaca" ] }, { "cell_type": "markdown", "id": "225deb48-9923-42b9-a8c6-eb1304fb7ed3", "metadata": {}, "source": [ "Submitting order to get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "9388d211-3cea-4826-ba3e-4e08794c4ead", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce" ] }, { "cell_type": "code", "execution_count": null, "id": "c65d04fd-278d-480c-afcb-8e6cedf2ff45", "metadata": {}, "outputs": [], "source": [ "trading_client = TradingClient(api_key, secret_key, paper=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "622107d1-2dad-41ec-8953-249c8371759b", "metadata": {}, "outputs": [], "source": [ "getting_all_positions = trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "fbc55be7-e330-4d4c-969c-5c5ef771ddfa", "metadata": { "scrolled": true }, "outputs": [], "source": [ "getting_all_positions" ] }, { "cell_type": "markdown", "id": "933de74b-a064-4003-baf6-7f26ae1489e5", "metadata": {}, "source": [ "## 3. How to submit an order to buy stocks" ] }, { "cell_type": "markdown", "id": "aa85fa63-1799-44a7-8d8d-6c2831565a89", "metadata": {}, "source": [ "BUY GOOG" ] }, { "cell_type": "code", "execution_count": null, "id": "931aeb8e-4198-4d80-895c-ed883d6c9952", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"GOOG\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "c634bffc-e27c-4583-9786-0b87a681bfd4", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "a32cc385-547e-469a-ab47-78898d8908c5", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "be4339dd-406a-4bde-98df-3e4ab730a15c", "metadata": {}, "source": [ "BUY APPL" ] }, { "cell_type": "code", "execution_count": null, "id": "fbe88c97-faa8-4c64-bfc4-188037ac937b", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "41660bac-f03f-40ef-912c-947a9bf7caf4", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "60a4687e-de6d-47bc-adc7-24a433b02699", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "8b8fd4aa-7eb0-4c37-90bf-4dcdec2ef9c4", "metadata": {}, "source": [ "## 4. How to submit an order to sell stocks" ] }, { "cell_type": "markdown", "id": "edfe4c3b-9944-417c-95b1-a26a29e86318", "metadata": {}, "source": [ "SELL APPL" ] }, { "cell_type": "code", "execution_count": null, "id": "c934ec21-515d-4144-a519-ac0288191511", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "5f222e84-487b-45ee-a2ee-a5aea6d2a102", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "51a42d0f-1f8f-49c1-8a19-9c179e337b11", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "9d4d1462-f06b-4307-95a6-59137bb7cc48", "metadata": {}, "source": [ "SELL GOOG" ] }, { "cell_type": "code", "execution_count": null, "id": "4de84606-f635-4cc1-b1fa-e6ee81580bb8", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"GOOG\",\n", " qty=1,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "dd6724ef-0eb5-45f8-9a57-c64d20daf709", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "35366fe5-ea6e-4edb-93cb-591d1f84038f", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "71c349f1-664b-41bf-b32f-1dcf5a3eb790", "metadata": {}, "source": [ "## 5. How to submit an order to buy cryptocurrency" ] }, { "cell_type": "markdown", "id": "23cc729d-32d6-4d3d-a1ea-84c320295efd", "metadata": {}, "source": [ "BUY BTC" ] }, { "cell_type": "code", "execution_count": null, "id": "429f4b31-bd37-4a2c-bb51-44ffd77bc04d", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"BTC/USD\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.GTC\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "05a6da38-395b-41c5-b152-b6e1e31fb03e", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "c016b4a1-f843-4397-a085-5a2caf5fe5ad", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "34737749-57fc-4106-819b-d66a19712ad7", "metadata": {}, "source": [ "## 6. How to submit an order to sell cryptocurrency" ] }, { "cell_type": "markdown", "id": "e2d36f23-0a3e-4079-bc94-0b5aecb40586", "metadata": {}, "source": [ "SELL BTC" ] }, { "cell_type": "code", "execution_count": null, "id": "5b9f8929-35d9-4af6-99c6-5837387c52e0", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"BTC/USD\",\n", " qty=1,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.GTC\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "1c748b7a-d9e9-47fd-a8c4-7a2b844de864", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "1e7aa479-eaeb-4711-9e15-82ba31f9545c", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "ff76fa85-6d89-40f6-9cc5-81068d1421b5", "metadata": { "scrolled": true }, "outputs": [], "source": [ "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"BTC/USD\",\n", " qty=0.1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.GTC\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "9c802cb1-5213-4be0-a758-f6222f1b1d60", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "9004f170-3bef-464f-b243-909e8aaf360c", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"BTC/USD\",\n", " qty=0.1,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.GTC\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "3e9cf3db-909e-4887-b16a-bbdfb6fac58c", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "be1a1eea-f81f-41d2-b164-919c8d095176", "metadata": {}, "source": [ "## 7. How to submit an order to close all positions" ] }, { "cell_type": "markdown", "id": "8fd8290a-2af8-40bf-a3b4-13f04016904a", "metadata": {}, "source": [ "Closing all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "bd03d26f-f02b-4150-91fd-43fc64dae6db", "metadata": {}, "outputs": [], "source": [ "market_order = trading_client.close_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "eb8a36a7-ba12-4760-b266-c1df6b6420ce", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "22159d94-acd8-411f-80aa-c065712b6593", "metadata": {}, "source": [ "GETTING HISTORICAL DATA" ] }, { "cell_type": "code", "execution_count": null, "id": "9b1c5c58-36b2-4da2-bc7e-372562881ce2", "metadata": {}, "outputs": [], "source": [ "from alpaca.data.historical import CryptoHistoricalDataClient\n", "from alpaca.data.requests import CryptoBarsRequest\n", "from alpaca.data.timeframe import TimeFrame\n", "\n", "# no keys required for crypto data\n", "client = CryptoHistoricalDataClient()\n", "\n", "request_params = CryptoBarsRequest(\n", " symbol_or_symbols=[\"BTC/USD\", \"ETH/USD\"],\n", " timeframe=TimeFrame.Day,\n", " start=\"2024-08-01\"\n", " )\n", "\n", "bars = client.get_crypto_bars(request_params)" ] }, { "cell_type": "code", "execution_count": null, "id": "d5bbdf81-01be-48d7-9a6f-406ff2a31d78", "metadata": { "scrolled": true }, "outputs": [], "source": [ "bars" ] }, { "cell_type": "code", "execution_count": null, "id": "159bcb78-7992-4cfe-9744-915e6b27b7fd", "metadata": { "scrolled": true }, "outputs": [], "source": [ "bars['BTC/USD']" ] }, { "cell_type": "code", "execution_count": null, "id": "4b4ab016-2eb9-4bf7-b284-394d471ba6f3", "metadata": {}, "outputs": [], "source": [ "from alpaca.data.historical import CryptoHistoricalDataClient\n", "from alpaca.data.requests import CryptoBarsRequest\n", "from alpaca.data.timeframe import TimeFrame\n", "\n", "# no keys required for crypto data\n", "client = CryptoHistoricalDataClient()\n", "\n", "request_params = CryptoBarsRequest(\n", " symbol_or_symbols=\"BTC/USD\",\n", " timeframe=TimeFrame.Day,\n", " start=\"2024-08-01\"\n", " )\n", "\n", "bars = client.get_crypto_bars(request_params)" ] }, { "cell_type": "code", "execution_count": null, "id": "1f486073-c324-4893-8e93-0254bf74856f", "metadata": { "scrolled": true }, "outputs": [], "source": [ "bars" ] }, { "cell_type": "code", "execution_count": null, "id": "ba711d1f-0818-4f6f-be52-ad3d4fce0cd7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "bars['BTC/USD']" ] }, { "cell_type": "markdown", "id": "3d48f947-a6cf-42f5-8e8a-d3eac96503de", "metadata": {}, "source": [ "## 3. How to submit an order to buy stocks" ] }, { "cell_type": "markdown", "id": "aa0d6a31-d14e-4c39-bcdb-2b36a6ea3d6e", "metadata": {}, "source": [ "BUY GOOG" ] }, { "cell_type": "code", "execution_count": null, "id": "fd400b19-a98b-4211-82f6-f9e09db49f3b", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"GOOG\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "8744e42f-eb1e-4ae8-af65-383f5d33cb07", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "05774649-5c6a-4d85-92c0-e7c9e8d3a172", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "d78f5dd1-5823-414f-84c0-9daf3d57dc94", "metadata": {}, "source": [ "BUY APPL" ] }, { "cell_type": "code", "execution_count": null, "id": "3d9db2bc-60a1-4006-b675-4b573707e9a9", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "9f30c5af-c0a5-4945-b256-671d6aed9e67", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "c0433e4a-ed2a-4483-9af1-aa3a788ed8cf", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "d26e9253-1b30-4442-984b-72c3858b79f8", "metadata": {}, "source": [ "## 4. How to submit an order to sell stocks" ] }, { "cell_type": "markdown", "id": "3422fe2d-12ed-4963-a7cf-e0da3ec25d31", "metadata": {}, "source": [ "SELL APPL" ] }, { "cell_type": "code", "execution_count": null, "id": "0373e2bc-83fa-49f7-ae95-749f6e65d58f", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "625c62cd-745c-4c7d-89ab-439e92b543e8", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "9f5fb0a1-4b78-49b4-b879-43b3684be23f", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "9bd83af4-f5d1-428c-a492-6f83d7d85b20", "metadata": {}, "source": [ "SELL GOOG" ] }, { "cell_type": "code", "execution_count": null, "id": "20cbadfe-f416-49fe-afd4-2c8fc24bce30", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"GOOG\",\n", " qty=1,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "b19213f7-1b2f-4a69-a260-087edcb6df7e", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "c1b83e31-4198-413d-ba4d-a5c751442cff", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "50802fff-d82c-4f0b-8e0d-d28a0caee2e7", "metadata": {}, "source": [ "# EXTRA: ADVANCED ORDERS - A STEP TOWARDS A TRADING BOT" ] }, { "cell_type": "markdown", "id": "802d6650-0ed3-45cf-932d-516af0c459f5", "metadata": {}, "source": [ "1. BASIC ORDERS\n", "2. BRACKET ORDERS: TAKE PROFIT / STOP LOSS ORDER\n", "- A. BRACKET ORDER: BUY NOW + SELL IF PRICE >= LIMIT PRICE (take profit) OR SELL IF PRICE <= STOP PRICE (stop loss) WITH FIXED PRICES\n", "- B. BRACKET ORDER: BUY NOW + SELL IF PRICE >= LIMIT PRICE (take profit) OR SELL IF PRICE <= STOP PRICE (stop loss) WITH PRICES SET BASED ON AVERAGE ENTRY PRICE\n", "3. TRAILING STOP ORDERS\n", "- A TRAILING STOP ORDER WITH trail_price\n", "- B. TRAILING STOP ORDER WITH trail_percent\t" ] }, { "cell_type": "markdown", "id": "42ee1f11-a77f-4ccb-8b25-38019db75d2a", "metadata": {}, "source": [ "More info:\n", "- https://docs.alpaca.markets/docs/orders-at-alpaca\n", "- https://docs.alpaca.markets/docs/working-with-orders" ] }, { "cell_type": "code", "execution_count": null, "id": "c419f0ef-acc7-44a3-a1f2-6aa6a3a9acad", "metadata": {}, "outputs": [], "source": [ "api_key = \"your API KEY\" #replace it with your own KEY_ID from Alpaca: https://alpaca.markets/\n", "secret_key = \"your SECRET KEY\" #replace it with your own SECRET_KEY from Alpaca" ] }, { "cell_type": "markdown", "id": "a09ad172-5635-425c-9622-92f1e99c269d", "metadata": {}, "source": [ "## 1. BASIC ORDERS" ] }, { "cell_type": "code", "execution_count": null, "id": "b7b7dea7-ab43-4241-9dba-9ba444864761", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "260cca0c-f2cd-4dbd-b52b-f551a88fe39c", "metadata": {}, "source": [ "BUY BTC" ] }, { "cell_type": "code", "execution_count": null, "id": "7688b737-c465-483b-bc7a-4cac1c407c26", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"BTC/USD\",\n", " qty=0.1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.GTC\n", " )\n", ")" ] }, { "cell_type": "markdown", "id": "07073105-ba4e-4ba8-8b68-c7a9a01a8538", "metadata": {}, "source": [ "Get all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "b0a82603-1036-436d-a34b-32902cbb6853", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "ec2d1843-fd3c-411e-a389-1419b8cbd5b8", "metadata": {}, "source": [ "SELL BTC" ] }, { "cell_type": "code", "execution_count": null, "id": "9f2cb941-91b0-43b2-a06a-c4539bd7f9db", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"BTC/USD\",\n", " qty=trading_client.get_open_position('BTCUSD').qty, #sell all the BTCUSD we have\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.GTC\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "6ca0dd39-cc68-4824-8565-e87563abb5b9", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "857b496f-ffcb-4b33-9369-b08509491845", "metadata": {}, "source": [ "## 2. BRACKET ORDERS: TAKE PROFIT / STOP LOSS ORDER" ] }, { "cell_type": "code", "execution_count": null, "id": "4ac4e97f-4798-400c-9fa7-7a45fe3bc3e4", "metadata": {}, "outputs": [], "source": [ "api_key = \"your API KEY\" #replace it with your own KEY_ID from Alpaca: https://alpaca.markets/\n", "secret_key = \"your SECRET KEY\" #replace it with your own SECRET_KEY from Alpaca" ] }, { "cell_type": "code", "execution_count": null, "id": "8251de2b-5bec-421c-af67-c024cb91f980", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "92a1e59b-2f05-4b23-9350-4bdf717821e3", "metadata": {}, "source": [ "### 2.A. BRACKET ORDER: BUY NOW + SELL IF PRICE >= LIMIT PRICE (take profit) OR SELL IF PRICE <= STOP PRICE (stop loss) WITH FIXED PRICES" ] }, { "cell_type": "code", "execution_count": null, "id": "e6aedb6f-6ea1-4b98-9420-78ea066bd2cb", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest, LimitOrderRequest, TakeProfitRequest, StopLossRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce, OrderClass\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "# preparing bracket order with both stop loss and take profit\n", "bracket__order_data = MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY,\n", " order_class=OrderClass.BRACKET,\n", " take_profit=TakeProfitRequest(limit_price=250),\n", " stop_loss=StopLossRequest(stop_price=228)\n", " )\n", "\n", "bracket_order = trading_client.submit_order(\n", " order_data=bracket__order_data\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "2c241f8d-62eb-48af-ac1d-fca552adae01", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "c44e7046-a233-4a1f-a70b-439c5912e890", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "65191bcc-a4bd-48c7-9d18-8fa6da6ac027", "metadata": {}, "source": [ "Canceling order" ] }, { "cell_type": "code", "execution_count": null, "id": "d26fc6dc-5fc9-47df-9c81-7dee5698e1e5", "metadata": {}, "outputs": [], "source": [ "trading_client.cancel_orders()" ] }, { "cell_type": "code", "execution_count": null, "id": "fa97d61d-97b6-4af4-9352-cda954be0b6c", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "cea9e5ad-9cf5-4771-9f7b-90840fdc4946", "metadata": {}, "source": [ "Closing all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "5f68b2ca-e43a-4799-8f88-6eb9081ec93c", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "39964bed-3c51-45ba-b992-33a1e4cbbf3e", "metadata": {}, "outputs": [], "source": [ "trading_client.close_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "1d522c68-6754-4609-87b0-0bcc23c04105", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "be6d1335-4eda-4aa6-a16e-00875c129015", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "6d3b49a9-8461-485b-8637-018e3fb9fcf2", "metadata": {}, "source": [ "### 2.B. BRACKET ORDER: BUY NOW + SELL IF PRICE >= LIMIT PRICE (take profit) OR SELL IF PRICE <= STOP PRICE (stop loss) WITH PRICES SET BASED ON AVERAGE ENTRY PRICE" ] }, { "cell_type": "code", "execution_count": null, "id": "6dd6430c-344a-4700-9e75-95e7bff6de70", "metadata": {}, "outputs": [], "source": [ "api_key = \"your API KEY\" #replace it with your own KEY_ID from Alpaca: https://alpaca.markets/\n", "secret_key = \"your SECRET KEY\" #replace it with your own SECRET_KEY from Alpaca" ] }, { "cell_type": "code", "execution_count": null, "id": "b93621f9-026f-482b-9bdc-f0c199f88ff1", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "3924a496-5469-4c8b-9bce-9f0330dc3b07", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest, LimitOrderRequest, TakeProfitRequest, StopLossRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce, OrderClass\n", "import time\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "#basic order just to have some AAPL in portfolio\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")\n", "\n", "#wait 5 seconds for the order to be filled (or change to the apropriate number of seconds)\n", "time.sleep(5)\n", "\n", "limiting_price=(float(\"{:.2f}\".format(float(trading_client.get_open_position('AAPL').avg_entry_price)))+1)\n", "stopping_price=(float(\"{:.2f}\".format(float(trading_client.get_open_position('AAPL').avg_entry_price)))-1)\n", "\n", "print(\"I will take profit at: {}\".format(limiting_price))\n", "print(\"I will stop loss at: {}\".format(stopping_price))\n", "\n", "# preparing bracket order with both stop loss and take profit\n", "bracket__order_data = MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY,\n", " order_class=OrderClass.BRACKET,\n", " take_profit=TakeProfitRequest(limit_price=limiting_price),\n", " stop_loss=StopLossRequest(stop_price=stopping_price)\n", " )\n", "\n", "bracket_order = trading_client.submit_order(\n", " order_data=bracket__order_data\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "3c4d6e95-617d-459e-b793-2ef29a8794f6", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "fd8ea55f-2d2f-454e-9c24-5ae592e52c83", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "3d613633-e747-4b33-9c6a-2af7cb4a7ca0", "metadata": {}, "source": [ "Canceling order" ] }, { "cell_type": "code", "execution_count": null, "id": "da2613a3-0a0b-4889-84b1-8d8b98c3c6c9", "metadata": {}, "outputs": [], "source": [ "trading_client.cancel_orders()" ] }, { "cell_type": "code", "execution_count": null, "id": "e56837ff-2787-4529-803e-91c4fd1263de", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "e78021a8-a12c-4599-88e0-5d6a7b9ea116", "metadata": {}, "source": [ "Closing all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "f5c3e87a-8aa2-4c0a-a0c6-12c64e315243", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "04d1b1bb-4998-41bf-8465-34e260bf5d86", "metadata": {}, "outputs": [], "source": [ "trading_client.close_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "95223a0f-a6bb-419c-827f-716fc88a0b63", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "467f572d-6466-4ad5-b7b3-3d6a4e6d10f5", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "b7315726-7b17-480e-b823-42c6164e58cc", "metadata": {}, "source": [ "## 3. TRAILING STOP ORDERS" ] }, { "cell_type": "code", "execution_count": null, "id": "9c81a244-a695-4365-8cc7-c5c3f28e9724", "metadata": {}, "outputs": [], "source": [ "api_key = \"your API KEY\" #replace it with your own KEY_ID from Alpaca: https://alpaca.markets/\n", "secret_key = \"your SECRET KEY\" #replace it with your own SECRET_KEY from Alpaca" ] }, { "cell_type": "code", "execution_count": null, "id": "77c4da91-cab3-4eed-9d5b-22cf73de84b3", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "befdc656-7bc4-46d2-b970-be22670e567b", "metadata": {}, "source": [ "### 3.A. TRAILING STOP ORDER WITH trail_price" ] }, { "cell_type": "markdown", "id": "e3afed47-e7df-437b-b757-fee1333e3cae", "metadata": {}, "source": [ "#### SELL IF PRICE < (HIGHEST TRACKED PRICE - 1$)\n", "\n", "SELL if the price falls $1 below its highest tracked value since the order was placed." ] }, { "cell_type": "code", "execution_count": null, "id": "b0f7c941-686d-4a47-a95a-022a8fff52b9", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest, TrailingStopOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "#basic order just to have some AAPL in portfolio\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")\n", "\n", "trailing_price_data = TrailingStopOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=trading_client.get_open_position('AAPL').qty,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.DAY,\n", " trail_price=1.00 # hwm - $1.00\n", " )\n", "\n", "trailing_price_order = trading_client.submit_order(\n", " order_data=trailing_price_data\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "07302670-dcad-42df-a2e1-fc916d4b07e7", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "d568ccc2-84c7-4a20-aff7-0753e7f90773", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "99de8781-0b04-4677-aa42-b95962a68848", "metadata": {}, "source": [ "Wait a minute or so..." ] }, { "cell_type": "code", "execution_count": null, "id": "ae6f9478-8696-4c54-b052-289d8c177735", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "2fa81ce5-8d52-44d2-96bb-d553eaea20ab", "metadata": {}, "source": [ "Wait another minute or so..." ] }, { "cell_type": "code", "execution_count": null, "id": "6290cedd-b635-4285-b8dc-9104a7e917fe", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "26e9c876-26d4-4929-ae5a-9924f621de09", "metadata": {}, "source": [ "Canceling order" ] }, { "cell_type": "code", "execution_count": null, "id": "4c757ee5-3e4f-46d3-a1d3-3da2e7545144", "metadata": {}, "outputs": [], "source": [ "trading_client.cancel_orders()" ] }, { "cell_type": "code", "execution_count": null, "id": "26f16c12-d05b-4a42-914a-5ccc35ec9dcc", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "8c02a1b4-1600-4a40-be90-99d4bb491a74", "metadata": {}, "source": [ "Closing all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "34631427-6d82-4e67-951c-48280b6c9321", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "fa11244f-f66e-4c8d-8d62-e48457a0d2e0", "metadata": {}, "outputs": [], "source": [ "trading_client.close_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "df9fed36-9ab3-4b82-8cac-926825d312f5", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "3fc8cd89-39d5-4af4-abeb-f35d8aff042f", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "dc145061-b010-4c17-bf82-fd61ddcfc0dc", "metadata": {}, "source": [ "### 3.B. TRAILING STOP ORDER WITH trail_percent" ] }, { "cell_type": "code", "execution_count": null, "id": "a339b25a-745a-41e4-87a7-bc14246bb9cc", "metadata": {}, "outputs": [], "source": [ "api_key = \"your API KEY\" #replace it with your own KEY_ID from Alpaca: https://alpaca.markets/\n", "secret_key = \"your SECRET KEY\" #replace it with your own SECRET_KEY from Alpaca" ] }, { "cell_type": "code", "execution_count": null, "id": "54576062-fa11-4fb7-888e-4390a7104b77", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "trading_client.get_all_positions()" ] }, { "cell_type": "markdown", "id": "b04fcdc3-31b4-40c2-92b0-5f53f2e8cbf5", "metadata": {}, "source": [ "#### SELL BTC IF PRICE < (HIGHEST TRACKED PRICE - 1% OF HIGHEST TRACKED PRICE)\n", "\n", "SELL if the price falls 1% below its highest tracked value since the order was placed." ] }, { "cell_type": "code", "execution_count": null, "id": "7dd89e3e-043f-4be7-a031-a44578ef5998", "metadata": {}, "outputs": [], "source": [ "from alpaca.trading.client import TradingClient\n", "from alpaca.trading.requests import MarketOrderRequest, TrailingStopOrderRequest\n", "from alpaca.trading.enums import OrderSide, TimeInForce\n", "import time\n", "\n", "trading_client = TradingClient(api_key, secret_key, paper=True)\n", "\n", "#basic order just to have some AAPL in portfolio\n", "market_order = trading_client.submit_order(\n", " MarketOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=1,\n", " side=OrderSide.BUY,\n", " time_in_force=TimeInForce.DAY\n", " )\n", ")\n", "\n", "time.sleep(5)\n", "\n", "trailing_percent_data = TrailingStopOrderRequest(\n", " symbol=\"AAPL\",\n", " qty=trading_client.get_open_position('AAPL').qty,\n", " side=OrderSide.SELL,\n", " time_in_force=TimeInForce.DAY,\n", " trail_percent=1.00 # hwm * 0.99\n", " )\n", "\n", "trailing_percent_order = trading_client.submit_order(\n", " order_data=trailing_percent_data\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "1d6f99ea-bd8a-4651-824c-68f60228bb12", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "7bb0fd0e-acd7-45cb-b658-17effcf533b7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "d86acd65-2a46-44d5-b346-438859cc692a", "metadata": {}, "source": [ "Wait a minute or so..." ] }, { "cell_type": "code", "execution_count": null, "id": "5888cf7d-3f7e-4e68-9f2c-572a5c9d4693", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "0f269d99-7f5d-40ed-986a-4439d0e87967", "metadata": {}, "source": [ "Wait another minute or so..." ] }, { "cell_type": "code", "execution_count": null, "id": "685076af-10c1-4836-86e9-48323670d2c6", "metadata": { "scrolled": true }, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "5170c9df-d348-4977-8197-060ba883374d", "metadata": {}, "source": [ "Canceling order" ] }, { "cell_type": "code", "execution_count": null, "id": "b1ff6008-f0dc-4cd9-9fec-315df1e4f826", "metadata": {}, "outputs": [], "source": [ "trading_client.cancel_orders()" ] }, { "cell_type": "code", "execution_count": null, "id": "a204de68-b9cd-44eb-8a26-c409adc755f5", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] }, { "cell_type": "markdown", "id": "ee1e4bb6-027d-4930-9104-e0a526e5496a", "metadata": {}, "source": [ "Closing all positions" ] }, { "cell_type": "code", "execution_count": null, "id": "96cc512f-9c1f-4ce2-8319-51fd1dc97f6f", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "362ed26f-40b9-4f4b-94cc-2e3ce1d95f01", "metadata": {}, "outputs": [], "source": [ "trading_client.close_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "ec6fa2d4-ac69-447b-afee-723441a7d4c5", "metadata": {}, "outputs": [], "source": [ "trading_client.get_all_positions()" ] }, { "cell_type": "code", "execution_count": null, "id": "d5fa48b4-e6cf-4fd8-86c5-932e55e56658", "metadata": {}, "outputs": [], "source": [ "trading_client.get_orders()" ] } ], "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }