{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "ab4c0304", "metadata": {}, "outputs": [], "source": [ "from tradingagents.dataflows.binance import get_market_data\n", "\n", "print(get_market_data('BTC/USDT', '2025-12-20', '2025-12-25'))" ] }, { "cell_type": "code", "execution_count": null, "id": "a58bb868", "metadata": {}, "outputs": [], "source": [ "from tradingagents.dataflows.taapi import get_crypto_stats_indicators_window\n", "print(get_crypto_stats_indicators_window('BTC/USDT', 'macd', '2025-12-20', 3))" ] }, { "cell_type": "code", "execution_count": null, "id": "60a3bc38", "metadata": {}, "outputs": [], "source": [ "from tradingagents.dataflows.taapi import get_crypto_stats_indicators\n", "print(get_crypto_stats_indicators('BTC/USDT', ['rsi', 'macd'], '2025-12-25', 30))\n", " # 08:42:55 [Tool Call] get_indicators_bulk(symbol=BTCUSDT, indicators=['sma', 'rsi', 'macd', 'bbands', 'atr'], curr_date=2025-12-26, look_back_days=30))" ] }, { "cell_type": "code", "execution_count": null, "id": "3f341582", "metadata": {}, "outputs": [], "source": [ "from tradingagents.dataflows.openai import get_whitepaper_openai\n", "whitepaper = get_whitepaper_openai('BTC/USDT')" ] }, { "cell_type": "code", "execution_count": null, "id": "6db71e93", "metadata": {}, "outputs": [], "source": [ "print(whitepaper.output[0].content[0].text)" ] }, { "cell_type": "code", "execution_count": null, "id": "c00b90b8", "metadata": {}, "outputs": [], "source": [ "from tradingagents.dataflows.openai import get_fundamentals_openai\n", "fundamentals = get_fundamentals_openai('BTC/USDT', '2025-12-25')" ] }, { "cell_type": "code", "execution_count": null, "id": "01b03899", "metadata": {}, "outputs": [], "source": [ "print(fundamentals)" ] }, { "cell_type": "code", "execution_count": null, "id": "15fd34ce", "metadata": {}, "outputs": [], "source": [ "# Clean test of market_analyst only\n", "print(\"=== CLEAN MARKET ANALYST TEST ===\")\n", "print()\n", "\n", "# Import required modules\n", "from langchain_openai import ChatOpenAI\n", "from langchain_core.messages import HumanMessage\n", "from tradingagents.agents.analysts.market_analyst import create_market_analyst\n", "from datetime import date\n", "import os\n", "\n", "# Create LLM client\n", "llm = ChatOpenAI(\n", " model=\"gpt-4o-mini\", \n", " temperature=0.1,\n", " api_key=os.getenv(\"OPENAI_API_KEY\")\n", ")\n", "\n", "# Create market analyst\n", "market_analyst = create_market_analyst(llm)\n", "\n", "# Correct test state with proper message format\n", "test_state = {\n", " \"trade_date\": \"2025-12-25\",\n", " \"ticker_of_interest\": \"BTC/USDT\",\n", " \"messages\": [HumanMessage(content=\"Analyze BTC/USDT market conditions\")]\n", "}\n", "\n", "print(f\"Testing market analyst for: {test_state['ticker_of_interest']}\")\n", "print(f\"Date: {test_state['trade_date']}\")\n", "print(\"\\nRunning market analysis...\")\n", "print(\"-\" * 40)\n", "\n", "try:\n", " # Run the market analyst\n", " result = market_analyst(test_state)\n", " \n", " print(\"āœ… SUCCESS! Market analysis completed.\")\n", " \n", " # Print the analysis result\n", " if \"messages\" in result and result[\"messages\"]:\n", " latest_message = result[\"messages\"][-1]\n", " \n", " # Check if it made tool calls\n", " if hasattr(latest_message, 'tool_calls') and latest_message.tool_calls:\n", " print(f\"\\nšŸ“Š Tools called: {len(latest_message.tool_calls)}\")\n", " for i, call in enumerate(latest_message.tool_calls):\n", " tool_name = call.get('name', 'unknown')\n", " print(f\" {i+1}. {tool_name}\")\n", " \n", " # Print the content/analysis\n", " if hasattr(latest_message, 'content') and latest_message.content:\n", " print(f\"\\nšŸ“ Market Analysis:\")\n", " print(\"=\" * 50)\n", " print(latest_message.content)\n", " \n", " # Print market report if available\n", " if \"market_report\" in result and result[\"market_report\"]:\n", " print(f\"\\nšŸ“‹ Market Report:\")\n", " print(\"=\" * 50)\n", " print(result[\"market_report\"])\n", " \n", "except Exception as e:\n", " print(f\"āŒ ERROR: {e}\")\n", " import traceback\n", " traceback.print_exc()" ] }, { "cell_type": "code", "execution_count": 1, "id": "fcf9fe21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5000.62453368\n" ] } ], "source": [ "from tradingagents.dataflows.bybit import get_wallet_balance\n", "balance = get_wallet_balance('USDT')\n", "print(balance)" ] }, { "cell_type": "code", "execution_count": 3, "id": "1dfa3165", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "Failed to place order: Bybit API error: An unknown parameter was sent.", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", "\u001b[36mFile \u001b[39m\u001b[32m~/Documents/projects/halalquant/TradingAgents/tradingagents/dataflows/bybit.py:292\u001b[39m, in \u001b[36mplace_order\u001b[39m\u001b[34m(symbol, side, order_type, qty, price, stop_loss, take_profit, time_in_force, account_type, category, order_link_id, reduce_only, close_on_trigger)\u001b[39m\n\u001b[32m 291\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m292\u001b[39m data = \u001b[43mbybit_v5_request\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mPOST\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m/v5/order/create\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbody\u001b[49m\u001b[43m=\u001b[49m\u001b[43mbody\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 293\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m data.get(\u001b[33m\"\u001b[39m\u001b[33mresult\u001b[39m\u001b[33m\"\u001b[39m, {})\n", "\u001b[36mFile \u001b[39m\u001b[32m~/Documents/projects/halalquant/TradingAgents/tradingagents/dataflows/bybit.py:61\u001b[39m, in \u001b[36mbybit_v5_request\u001b[39m\u001b[34m(method, path, params, body)\u001b[39m\n\u001b[32m 60\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m data.get(\u001b[33m\"\u001b[39m\u001b[33mretCode\u001b[39m\u001b[33m\"\u001b[39m) != \u001b[32m0\u001b[39m:\n\u001b[32m---> \u001b[39m\u001b[32m61\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mBybit API error: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdata.get(\u001b[33m'\u001b[39m\u001b[33mretMsg\u001b[39m\u001b[33m'\u001b[39m)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m 63\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m data\n", "\u001b[31mValueError\u001b[39m: Bybit API error: An unknown parameter was sent.", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtradingagents\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mdataflows\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mbybit\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m place_order, place_spot_order_with_sl_tp\n\u001b[32m 2\u001b[39m \u001b[38;5;66;03m# Place a spot limit order with stop loss and take profit\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m result = \u001b[43mplace_spot_order_with_sl_tp\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 4\u001b[39m \u001b[43m \u001b[49m\u001b[43msymbol\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mBTCUSDT\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 5\u001b[39m \u001b[43m \u001b[49m\u001b[43mside\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mBuy\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 6\u001b[39m \u001b[43m \u001b[49m\u001b[43mqty\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m0.001\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 7\u001b[39m \u001b[43m \u001b[49m\u001b[43mprice\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m88000.0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 8\u001b[39m \u001b[43m \u001b[49m\u001b[43mstop_loss_price\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m85000.0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 9\u001b[39m \u001b[43m \u001b[49m\u001b[43mtake_profit_price\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m90000.0\u001b[39;49m\n\u001b[32m 10\u001b[39m \u001b[43m)\u001b[49m\n\u001b[32m 12\u001b[39m \u001b[38;5;66;03m# # Place a market order\u001b[39;00m\n\u001b[32m 13\u001b[39m \u001b[38;5;66;03m# result = place_order(\u001b[39;00m\n\u001b[32m 14\u001b[39m \u001b[38;5;66;03m# symbol=\"BTCUSDT\",\u001b[39;00m\n\u001b[32m (...)\u001b[39m\u001b[32m 18\u001b[39m \u001b[38;5;66;03m# category=\"spot\"\u001b[39;00m\n\u001b[32m 19\u001b[39m \u001b[38;5;66;03m# )\u001b[39;00m\n", "\u001b[36mFile \u001b[39m\u001b[32m~/Documents/projects/halalquant/TradingAgents/tradingagents/dataflows/bybit.py:322\u001b[39m, in \u001b[36mplace_spot_order_with_sl_tp\u001b[39m\u001b[34m(symbol, side, qty, price, stop_loss_price, take_profit_price, order_type)\u001b[39m\n\u001b[32m 298\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mplace_spot_order_with_sl_tp\u001b[39m(\n\u001b[32m 299\u001b[39m symbol: \u001b[38;5;28mstr\u001b[39m,\n\u001b[32m 300\u001b[39m side: \u001b[38;5;28mstr\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 305\u001b[39m order_type: \u001b[38;5;28mstr\u001b[39m = \u001b[33m\"\u001b[39m\u001b[33mLimit\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 306\u001b[39m ) -> Dict:\n\u001b[32m 307\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 308\u001b[39m \u001b[33;03m Convenience function to place a spot order with stop loss and take profit.\u001b[39;00m\n\u001b[32m 309\u001b[39m \u001b[33;03m \u001b[39;00m\n\u001b[32m (...)\u001b[39m\u001b[32m 320\u001b[39m \u001b[33;03m Dict containing order result\u001b[39;00m\n\u001b[32m 321\u001b[39m \u001b[33;03m \"\"\"\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m322\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mplace_order\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 323\u001b[39m \u001b[43m \u001b[49m\u001b[43msymbol\u001b[49m\u001b[43m=\u001b[49m\u001b[43msymbol\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 324\u001b[39m \u001b[43m \u001b[49m\u001b[43mside\u001b[49m\u001b[43m=\u001b[49m\u001b[43mside\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 325\u001b[39m \u001b[43m \u001b[49m\u001b[43morder_type\u001b[49m\u001b[43m=\u001b[49m\u001b[43morder_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 326\u001b[39m \u001b[43m \u001b[49m\u001b[43mqty\u001b[49m\u001b[43m=\u001b[49m\u001b[43mqty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 327\u001b[39m \u001b[43m \u001b[49m\u001b[43mprice\u001b[49m\u001b[43m=\u001b[49m\u001b[43mprice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 328\u001b[39m \u001b[43m \u001b[49m\u001b[43mstop_loss\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop_loss_price\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 329\u001b[39m \u001b[43m \u001b[49m\u001b[43mtake_profit\u001b[49m\u001b[43m=\u001b[49m\u001b[43mtake_profit_price\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 330\u001b[39m \u001b[43m \u001b[49m\u001b[43mcategory\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mspot\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\n\u001b[32m 331\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m~/Documents/projects/halalquant/TradingAgents/tradingagents/dataflows/bybit.py:295\u001b[39m, in \u001b[36mplace_order\u001b[39m\u001b[34m(symbol, side, order_type, qty, price, stop_loss, take_profit, time_in_force, account_type, category, order_link_id, reduce_only, close_on_trigger)\u001b[39m\n\u001b[32m 293\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m data.get(\u001b[33m\"\u001b[39m\u001b[33mresult\u001b[39m\u001b[33m\"\u001b[39m, {})\n\u001b[32m 294\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[32m--> \u001b[39m\u001b[32m295\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mFailed to place order: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mstr\u001b[39m(e)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n", "\u001b[31mValueError\u001b[39m: Failed to place order: Bybit API error: An unknown parameter was sent." ] } ], "source": [ "from tradingagents.dataflows.bybit import place_order, place_spot_order_with_sl_tp\n", "# Place a spot limit order with stop loss and take profit\n", "result = place_spot_order_with_sl_tp(\n", " symbol=\"BTCUSDT\",\n", " side=\"Buy\",\n", " qty=0.001,\n", " price=88000.0,\n", " stop_loss_price=85000.0,\n", " take_profit_price=90000.0\n", ")\n", "\n", "# # Place a market order\n", "# result = place_order(\n", "# symbol=\"BTCUSDT\",\n", "# side=\"Buy\",\n", "# order_type=\"Market\",\n", "# qty=0.001,\n", "# category=\"spot\"\n", "# )" ] }, { "cell_type": "code", "execution_count": 3, "id": "03bdc3c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Order result: {'orderId': '2113891849709292800', 'orderLinkId': '2113891849709292801'}\n", "Order status: {'symbol': 'BTCUSDT', 'orderType': 'Limit', 'orderLinkId': '2113891849709292801', 'slLimitPrice': '27500', 'orderId': '2113891849709292800', 'cancelType': 'UNKNOWN', 'avgPrice': '0.0', 'stopOrderType': '', 'lastPriceOnCreated': '', 'orderStatus': 'New', 'takeProfit': '35000', 'cumExecValue': '0.0000000', 'smpType': 'None', 'triggerDirection': 0, 'blockTradeId': '', 'cumFeeDetail': {}, 'isLeverage': '0', 'rejectReason': 'EC_NoError', 'price': '28000.0', 'orderIv': '', 'createdTime': '1766731545590', 'tpTriggerBy': '', 'positionIdx': 0, 'trailingPercentage': '0', 'timeInForce': 'PostOnly', 'leavesValue': '280.0000000', 'basePrice': '89223.5', 'updatedTime': '1766731545591', 'side': 'Buy', 'smpGroup': 0, 'triggerPrice': '0.0', 'tpLimitPrice': '36000', 'trailingValue': '0', 'cumExecFee': '0', 'leavesQty': '0.01', 'slTriggerBy': '', 'closeOnTrigger': False, 'placeType': '', 'cumExecQty': '0', 'reduceOnly': False, 'activationPrice': '0', 'qty': '0.010000', 'stopLoss': '27000', 'marketUnit': '', 'smpOrderId': '', 'triggerBy': ''}\n", "Open orders: {'nextPageCursor': '2113891849709292800%3A1766731545590%2C2113891849709292800%3A1766731545590', 'category': 'spot', 'list': [{'symbol': 'BTCUSDT', 'orderType': 'Limit', 'orderLinkId': '2113891849709292801', 'slLimitPrice': '27500', 'orderId': '2113891849709292800', 'cancelType': 'UNKNOWN', 'avgPrice': '0.0', 'stopOrderType': '', 'lastPriceOnCreated': '', 'orderStatus': 'New', 'takeProfit': '35000', 'cumExecValue': '0.0000000', 'smpType': 'None', 'triggerDirection': 0, 'blockTradeId': '', 'cumFeeDetail': {}, 'isLeverage': '0', 'rejectReason': 'EC_NoError', 'price': '28000.0', 'orderIv': '', 'createdTime': '1766731545590', 'tpTriggerBy': '', 'positionIdx': 0, 'trailingPercentage': '0', 'timeInForce': 'PostOnly', 'leavesValue': '280.0000000', 'basePrice': '89223.5', 'updatedTime': '1766731545591', 'side': 'Buy', 'smpGroup': 0, 'triggerPrice': '0.0', 'tpLimitPrice': '36000', 'trailingValue': '0', 'cumExecFee': '0', 'leavesQty': '0.01', 'slTriggerBy': '', 'closeOnTrigger': False, 'placeType': '', 'cumExecQty': '0', 'reduceOnly': False, 'activationPrice': '0', 'qty': '0.010000', 'stopLoss': '27000', 'marketUnit': '', 'smpOrderId': '', 'triggerBy': ''}]}\n" ] } ], "source": [ "# Fixed version with correct Bybit parameters\n", "from tradingagents.dataflows.bybit import place_order, place_spot_order_with_sl_tp, get_order_status, get_open_orders\n", "\n", "# Place a spot limit order with stop loss and take profit - like the example\n", "# result = place_spot_order_with_sl_tp(\n", "# symbol=\"BTCUSDT\",\n", "# side=\"Buy\",\n", "# qty=0.01,\n", "# price=28000.0,\n", "# stop_loss_price=27000.0,\n", "# take_profit_price=35000.0,\n", "# sl_limit_price=27500.0, # Stop loss limit price\n", "# tp_limit_price=36000.0, # Take profit limit price\n", "# sl_order_type=\"Limit\", # Stop loss as limit order\n", "# tp_order_type=\"Limit\", # Take profit as limit order\n", "# time_in_force=\"PostOnly\" # Post only for maker orders\n", "# )\n", "\n", "print(\"Order result:\", result)\n", "# Check order status\n", "status = get_order_status(order_id=result.get(\"orderId\", \"2113891849709292800\"), category=\"spot\")\n", "\n", "print(\"Order status:\", status)\n", "\n", "# Get open orders\n", "open_orders = get_open_orders(symbol=\"BTCUSDT\", category=\"spot\")\n", "print(\"Open orders:\", open_orders)" ] }, { "cell_type": "code", "execution_count": null, "id": "330f7261", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dotenv import load_dotenv\n", "\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": null, "id": "153dc726", "metadata": {}, "outputs": [], "source": [ "from tradingagents.dataflows.bybit import *" ] }, { "cell_type": "code", "execution_count": null, "id": "124d7e03", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# Open Order Report for BTCUSDT\n", "- Total Orders: 1\n", "- Capital/Asset Value Locked: $0.00\n", "\n", "\n", "## Conditional / Trigger Orders:\n", "- (Buy): Trigger @ $0.00 | Qty: 0.01 — Created: 3.9d ago\n" ] } ], "source": [ "print(get_open_orders(\"BTC\", \"USDT\"))" ] }, { "cell_type": "code", "execution_count": 3, "id": "d497b56a", "metadata": {}, "outputs": [], "source": [ "a = get_account_balance(\"BTC\", quote_coin=\"USDT\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "83f29137", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# Account Balance Report for BTC/USDT\n", "- Total Equity: $7919.6209963500005\n", "## USDT (Quote) Details:\n", "- Free Margin: 4722.44462368 USDT\n", "- Locked Capital: 280.0 USDT\n", "## BTC (Base) Details:\n", "- Holding Size: 4e-07 BTC\n", "- Locked Asset: 0.0 BTC\n", "\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 11, "id": "6ca5af0f", "metadata": {}, "outputs": [], "source": [ "b = get_open_position(\"BTCUSDT\", category=\"linear\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "b7ea9491", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'status': 'No Open Position',\n", " 'symbol': 'BTCUSDT',\n", " 'size': 0.0,\n", " 'pnl_usd': 0.0}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 10, "id": "6ead1651", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'nextPageCursor': '2113891849709292800%3A1766731545590%2C2113891849709292800%3A1766731545590',\n", " 'category': 'spot',\n", " 'list': [{'symbol': 'BTCUSDT',\n", " 'orderType': 'Limit',\n", " 'orderLinkId': '2113891849709292801',\n", " 'slLimitPrice': '27500',\n", " 'orderId': '2113891849709292800',\n", " 'cancelType': 'UNKNOWN',\n", " 'avgPrice': '0.0',\n", " 'stopOrderType': '',\n", " 'lastPriceOnCreated': '',\n", " 'orderStatus': 'New',\n", " 'takeProfit': '35000',\n", " 'cumExecValue': '0.0000000',\n", " 'smpType': 'None',\n", " 'triggerDirection': 0,\n", " 'blockTradeId': '',\n", " 'cumFeeDetail': {},\n", " 'isLeverage': '0',\n", " 'rejectReason': 'EC_NoError',\n", " 'price': '28000.0',\n", " 'orderIv': '',\n", " 'createdTime': '1766731545590',\n", " 'tpTriggerBy': '',\n", " 'positionIdx': 0,\n", " 'trailingPercentage': '0',\n", " 'timeInForce': 'PostOnly',\n", " 'leavesValue': '280.0000000',\n", " 'basePrice': '89223.5',\n", " 'updatedTime': '1766731545591',\n", " 'side': 'Buy',\n", " 'smpGroup': 0,\n", " 'triggerPrice': '0.0',\n", " 'tpLimitPrice': '36000',\n", " 'trailingValue': '0',\n", " 'cumExecFee': '0',\n", " 'leavesQty': '0.01',\n", " 'slTriggerBy': '',\n", " 'closeOnTrigger': False,\n", " 'placeType': '',\n", " 'cumExecQty': '0',\n", " 'reduceOnly': False,\n", " 'activationPrice': '0',\n", " 'qty': '0.010000',\n", " 'stopLoss': '27000',\n", " 'marketUnit': '',\n", " 'smpOrderId': '',\n", " 'triggerBy': ''}]}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_open_orders(symbol=\"BTCUSDT\", category=\"spot\")" ] }, { "cell_type": "code", "execution_count": null, "id": "50f72edb", "metadata": {}, "outputs": [], "source": [ "def get_symbol(base_coin: str, quote_coin: str, category: str = \"linear\") -> str:\n", " \"\"\"\n", " Safely retrieves the correct Bybit symbol (e.g., \"BTCUSDT\") for a given base/quote pair.\n", " \n", " Args:\n", " base_coin: The asset (e.g., \"BTC\")\n", " quote_coin: The currency (e.g., \"USDT\")\n", " category: \"linear\", \"spot\", or \"inverse\"\n", " \n", " Returns:\n", " The valid symbol string (e.g., \"BTCUSDT\") or None if not found.\n", " \"\"\"\n", " # 1. Query the API specifically for this Base Coin\n", " # This filters the search on the server side, which is much faster.\n", " params = {\n", " \"category\": category,\n", " \"baseCoin\": base_coin.upper(),\n", " \"limit\": 20 # We only expect a few matches (e.g., BTCUSDT, BTC-PERP)\n", " }\n", " \n", " data = bybit_v5_request(\"GET\", \"/v5/market/instruments-info\", params)\n", "\n", " result = data.get(\"result\", {})\n", " instruments = result.get(\"list\", [])\n", "\n", " # 2. Find the exact match for the Quote Coin\n", " # This handles cases where BTC might pair with USDT, USDC, or DAI\n", " for item in instruments:\n", " if item.get(\"quoteCoin\") == quote_coin.upper() and item.get(\"baseCoin\") == base_coin.upper():\n", " return item.get(\"symbol\")\n", "\n", " # 3. Fallback/Error handling\n", " return None" ] }, { "cell_type": "code", "execution_count": 37, "id": "6aa1fd5e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ETHBTC'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_symbol(\"ETH\", \"BTC\", category=\"spot\")" ] }, { "cell_type": "code", "execution_count": null, "id": "61815a34", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "tradingagents", "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.13.11" } }, "nbformat": 4, "nbformat_minor": 5 }