TradingAgents/playground.ipynb

291 lines
18 KiB
Plaintext

{
"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)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}