165 lines
4.0 KiB
Plaintext
165 lines
4.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "c8a08105",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"from alpaca.trading.client import TradingClient\n",
|
|
"\n",
|
|
"load_dotenv()\n",
|
|
"\n",
|
|
"# Set paper=True for paper trading (sandbox), paper=False for live trading\n",
|
|
"trading_client = TradingClient(\n",
|
|
" api_key=os.getenv(\"ALPACA_API_KEY\"),\n",
|
|
" secret_key=os.getenv(\"ALPACA_SECRET_KEY\"),\n",
|
|
" paper=True,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"id": "49b14380",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"No open positions.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"positions = trading_client.get_all_positions()\n",
|
|
"\n",
|
|
"if not positions:\n",
|
|
" print(\"No open positions.\")\n",
|
|
"else:\n",
|
|
" print(\"Current Portfolio Positions:\")\n",
|
|
" for pos in positions:\n",
|
|
" print(\n",
|
|
" f\"Symbol: {pos.symbol:<5} | \"\n",
|
|
" f\"Qty: {pos.qty:<5} | \"\n",
|
|
" f\"Avg Entry Price: ${float(pos.avg_entry_price):.2f} | \"\n",
|
|
" f\"Current Price: ${float(pos.current_price):.2f} | \"\n",
|
|
" f\"Unrealized P/L: ${float(pos.unrealized_pl):.2f}\"\n",
|
|
" )"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"id": "c032239c",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"99997.95 99997.95\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"account = trading_client.get_account()\n",
|
|
"print(\n",
|
|
" account.cash,\n",
|
|
" account.equity\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"id": "c759e40f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"No open orders found.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from alpaca.trading.requests import GetOrdersRequest\n",
|
|
"from alpaca.trading.enums import QueryOrderStatus\n",
|
|
"\n",
|
|
"# Request only open orders\n",
|
|
"request_params = GetOrdersRequest(status=QueryOrderStatus.OPEN)\n",
|
|
"open_orders = trading_client.get_orders(filter=request_params)\n",
|
|
"\n",
|
|
"if not open_orders:\n",
|
|
" print(\"No open orders found.\")\n",
|
|
"else:\n",
|
|
" print(f\"Found {len(open_orders)} open order(s):\")\n",
|
|
" for order in open_orders:\n",
|
|
" print(\n",
|
|
" f\"ID: {order.id} | Symbol: {order.symbol} | \"\n",
|
|
" f\"Side: {order.side} | Qty: {order.qty} | Status: {order.status}\"\n",
|
|
" )"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"id": "091cf061",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Submitted Order ID: 4838c16d-7a12-4dc3-80cb-7c5d1dbecda3 | Status: OrderStatus.ACCEPTED\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from alpaca.trading.requests import MarketOrderRequest\n",
|
|
"from alpaca.trading.enums import OrderSide, TimeInForce\n",
|
|
"\n",
|
|
"# Define a market buy order for 10 shares of SPY\n",
|
|
"market_order_data = MarketOrderRequest(\n",
|
|
" symbol=\"SPY\",\n",
|
|
" notional=100.0,\n",
|
|
" side=OrderSide.BUY,\n",
|
|
" time_in_force=TimeInForce.DAY,\n",
|
|
")\n",
|
|
"\n",
|
|
"# Submit the order\n",
|
|
"order = trading_client.submit_order(order_data=market_order_data)\n",
|
|
"\n",
|
|
"print(f\"Submitted Order ID: {order.id} | Status: {order.status}\")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"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.11.15"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|