821 lines
28 KiB
Plaintext
821 lines
28 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "6c70a8c3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\n",
|
|
"import asyncio\n",
|
|
"import requests\n",
|
|
"from x10.config import MAINNET_CONFIG, TESTNET_CONFIG\n",
|
|
"from x10.core.stark_account import StarkPerpetualAccount\n",
|
|
"from x10.perpetual.trading_client import PerpetualTradingClient\n",
|
|
"from x10.models.order import OrderSide, OrderType\n",
|
|
"import time\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"import os\n",
|
|
"import uuid\n",
|
|
"import asyncio\n",
|
|
"import logging\n",
|
|
"from decimal import Decimal\n",
|
|
"import modules.extended_auth as extend_auth\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ff971ca9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"load_dotenv()\n",
|
|
"\n",
|
|
"API_KEY = os.getenv('EXTENDED_API_KEY')\n",
|
|
"PUBLIC_KEY = os.getenv('EXTENDED_STARK_KEY_PUBLIC') # public Stark key (l2Key from account info)\n",
|
|
"PRIVATE_KEY = os.getenv('EXTENDED_STARK_KEY_PRIVATE') # private Stark key (hex)\n",
|
|
"VAULT = int(os.getenv('EXTENDED_VAULT_NUMBER')) # l2Vault from account info (integer)\n",
|
|
"\n",
|
|
"CONFIG = MAINNET_CONFIG\n",
|
|
"\n",
|
|
"ORDER_MARKET = \"ETH-USD\"\n",
|
|
"ORDER_SIDE = OrderSide.BUY\n",
|
|
"ORDER_QTY = Decimal(\"0.01\")\n",
|
|
"ORDER_PRICE = Decimal(\"2200.1\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "fc2c6d2b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"client, trading_client = await extend_auth.create_auth_account_and_trading_client()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "c366706f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Error response from https://api.starknet.extended.exchange/api/v1/user/order: {\"status\":\"ERROR\",\"error\":{\"code\":1125,\"message\":\"Invalid price precision\"}}\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "ValueError",
|
|
"evalue": "Error response from https://api.starknet.extended.exchange/api/v1/user/order: code 400 - {\"status\":\"ERROR\",\"error\":{\"code\":1125,\"message\":\"Invalid price precision\"}}",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
|
"\u001b[31mValueError\u001b[39m Traceback (most recent call last)",
|
|
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m placed_order = await trading_client.place_order(\n\u001b[32m 2\u001b[39m market_name=ORDER_MARKET,\n\u001b[32m 3\u001b[39m amount_of_synthetic=ORDER_QTY,\n\u001b[32m 4\u001b[39m price=ORDER_PRICE,\n",
|
|
"\u001b[36mFile \u001b[39m\u001b[32m~/miniconda3/envs/py_313/lib/python3.13/site-packages/x10/perpetual/trading_client/trading_client.py:101\u001b[39m, in \u001b[36mPerpetualTradingClient.place_order\u001b[39m\u001b[34m(self, market_name, amount_of_synthetic, price, side, taker_fee, post_only, previous_order_id, expire_time, time_in_force, self_trade_protection_level, external_id, builder_fee, builder_id, reduce_only, tp_sl_type, take_profit, stop_loss)\u001b[39m\n\u001b[32m 78\u001b[39m expire_time = utc_now() + timedelta(hours=\u001b[32m1\u001b[39m)\n\u001b[32m 80\u001b[39m order = create_order_object(\n\u001b[32m 81\u001b[39m account=\u001b[38;5;28mself\u001b[39m.__stark_account,\n\u001b[32m 82\u001b[39m market=market,\n\u001b[32m (...)\u001b[39m\u001b[32m 99\u001b[39m stop_loss=stop_loss,\n\u001b[32m 100\u001b[39m )\n\u001b[32m--> \u001b[39m\u001b[32m101\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mawait\u001b[39;00m \u001b[38;5;28mself\u001b[39m.__order_management_module.place_order(order)\n",
|
|
"\u001b[36mFile \u001b[39m\u001b[32m~/miniconda3/envs/py_313/lib/python3.13/site-packages/x10/perpetual/trading_client/order_management_module.py:31\u001b[39m, in \u001b[36mOrderManagementModule.place_order\u001b[39m\u001b[34m(self, order)\u001b[39m\n\u001b[32m 28\u001b[39m LOGGER.debug(\u001b[33m\"\u001b[39m\u001b[33mPlacing an order: id=\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[33m\"\u001b[39m, order.id)\n\u001b[32m 30\u001b[39m url = \u001b[38;5;28mself\u001b[39m._get_url(\u001b[33m\"\u001b[39m\u001b[33m/user/order\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m31\u001b[39m response = \u001b[38;5;28;01mawait\u001b[39;00m send_post_request(\n\u001b[32m 32\u001b[39m \u001b[38;5;28;01mawait\u001b[39;00m \u001b[38;5;28mself\u001b[39m.get_session(),\n\u001b[32m 33\u001b[39m url,\n\u001b[32m 34\u001b[39m PlacedOrderModel,\n\u001b[32m 35\u001b[39m json=order.to_api_request_json(exclude_none=\u001b[38;5;28;01mTrue\u001b[39;00m),\n\u001b[32m 36\u001b[39m api_key=\u001b[38;5;28mself\u001b[39m._get_api_key(),\n\u001b[32m 37\u001b[39m )\n\u001b[32m 38\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m response\n",
|
|
"\u001b[36mFile \u001b[39m\u001b[32m~/miniconda3/envs/py_313/lib/python3.13/site-packages/x10/utils/http.py:173\u001b[39m, in \u001b[36msend_post_request\u001b[39m\u001b[34m(session, url, model_class, json, api_key, request_headers, response_code_to_exception)\u001b[39m\n\u001b[32m 171\u001b[39m \u001b[38;5;28;01masync\u001b[39;00m \u001b[38;5;28;01mwith\u001b[39;00m session.post(url, json=json, headers=headers) \u001b[38;5;28;01mas\u001b[39;00m response:\n\u001b[32m 172\u001b[39m response_text = \u001b[38;5;28;01mawait\u001b[39;00m response.text()\n\u001b[32m--> \u001b[39m\u001b[32m173\u001b[39m \u001b[30;43mhandle_known_errors\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43murl\u001b[39;49m\u001b[30;43m,\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mresponse_code_to_exception\u001b[39;49m\u001b[30;43m,\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mresponse\u001b[39;49m\u001b[30;43m,\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mresponse_text\u001b[39;49m\u001b[30;43m)\u001b[39;49m\n\u001b[32m 174\u001b[39m response_model = parse_response_to_model(response_text, model_class)\n\u001b[32m 176\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m (response_model.status != ResponseStatus.OK) \u001b[38;5;129;01mor\u001b[39;00m (response_model.error \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m):\n",
|
|
"\u001b[36mFile \u001b[39m\u001b[32m~/miniconda3/envs/py_313/lib/python3.13/site-packages/x10/utils/http.py:243\u001b[39m, in \u001b[36mhandle_known_errors\u001b[39m\u001b[34m(url, response_code_handler, response, response_text)\u001b[39m\n\u001b[32m 241\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m response.status > \u001b[32m299\u001b[39m:\n\u001b[32m 242\u001b[39m LOGGER.error(\u001b[33m\"\u001b[39m\u001b[33mError response from \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[33m: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[33m\"\u001b[39m, url, response_text)\n\u001b[32m--> \u001b[39m\u001b[32m243\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[33mError response from \u001b[39m\u001b[38;5;132;01m{\u001b[39;00murl\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m: code \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mresponse.status\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m - \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mresponse_text\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n",
|
|
"\u001b[31mValueError\u001b[39m: Error response from https://api.starknet.extended.exchange/api/v1/user/order: code 400 - {\"status\":\"ERROR\",\"error\":{\"code\":1125,\"message\":\"Invalid price precision\"}}"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"placed_order = await trading_client.place_order(\n",
|
|
" market_name=ORDER_MARKET,\n",
|
|
" amount_of_synthetic=ORDER_QTY,\n",
|
|
" price=ORDER_PRICE,\n",
|
|
" side=ORDER_SIDE,\n",
|
|
" taker_fee=Decimal(\"0.00025\"),\n",
|
|
" # previous_order_id='1295034892466447624365619416628580523728221205816494340545831832663414858661'\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"WrappedApiResponse[EmptyModel](status='OK', data=EmptyModel(), error=None, pagination=None)"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"c = await trading_client.orders.mass_cancel(markets=['ETH-USD'])\n",
|
|
"c"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "03913674",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "8dd8aa73",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"d = await trading_client.account.get_positions_history()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "5f74f7cc",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" <th>3</th>\n",
|
|
" <th>4</th>\n",
|
|
" <th>5</th>\n",
|
|
" <th>6</th>\n",
|
|
" <th>7</th>\n",
|
|
" <th>8</th>\n",
|
|
" <th>9</th>\n",
|
|
" <th>10</th>\n",
|
|
" <th>11</th>\n",
|
|
" <th>12</th>\n",
|
|
" <th>13</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>(id, 2047947640758337536)</td>\n",
|
|
" <td>(account_id, 270571)</td>\n",
|
|
" <td>(market, ETH-USD)</td>\n",
|
|
" <td>(side, SHORT)</td>\n",
|
|
" <td>(size, 0.2160000000000000)</td>\n",
|
|
" <td>(max_position_size, 0.2160000000000000)</td>\n",
|
|
" <td>(leverage, 50.0000000000000000)</td>\n",
|
|
" <td>(open_price, 2315.5000000000000000)</td>\n",
|
|
" <td>(exit_price, 2308.6000000000000000)</td>\n",
|
|
" <td>(realised_pnl, 1.4249250000000000)</td>\n",
|
|
" <td>(realised_pnl_breakdown, trade_pnl=Decimal('1....</td>\n",
|
|
" <td>(created_time, 1777103741241)</td>\n",
|
|
" <td>(exit_type, TRADE)</td>\n",
|
|
" <td>(closed_time, 1777133049026)</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>(id, 2047512381621272576)</td>\n",
|
|
" <td>(account_id, 270571)</td>\n",
|
|
" <td>(market, ETH-USD)</td>\n",
|
|
" <td>(side, LONG)</td>\n",
|
|
" <td>(size, 0.2150000000000000)</td>\n",
|
|
" <td>(max_position_size, 0.2150000000000000)</td>\n",
|
|
" <td>(leverage, 50.0000000000000000)</td>\n",
|
|
" <td>(open_price, 2316.6000000000000000)</td>\n",
|
|
" <td>(exit_price, 2315.5000000000000000)</td>\n",
|
|
" <td>(realised_pnl, 0.0720660000000000)</td>\n",
|
|
" <td>(realised_pnl_breakdown, trade_pnl=Decimal('-0...</td>\n",
|
|
" <td>(created_time, 1776999967376)</td>\n",
|
|
" <td>(exit_type, TRADE)</td>\n",
|
|
" <td>(closed_time, 1777103741241)</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>(id, 2047419314696355840)</td>\n",
|
|
" <td>(account_id, 270571)</td>\n",
|
|
" <td>(market, ETH-USD)</td>\n",
|
|
" <td>(side, LONG)</td>\n",
|
|
" <td>(size, 0.2150000000000000)</td>\n",
|
|
" <td>(max_position_size, 0.2150000000000000)</td>\n",
|
|
" <td>(leverage, 50.0000000000000000)</td>\n",
|
|
" <td>(open_price, 2321.7000000000000000)</td>\n",
|
|
" <td>(exit_price, 2327.3000000000000000)</td>\n",
|
|
" <td>(realised_pnl, 1.3196460000000000)</td>\n",
|
|
" <td>(realised_pnl_breakdown, trade_pnl=Decimal('1....</td>\n",
|
|
" <td>(created_time, 1776977778492)</td>\n",
|
|
" <td>(exit_type, TRADE)</td>\n",
|
|
" <td>(closed_time, 1776996621824)</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2 \\\n",
|
|
"0 (id, 2047947640758337536) (account_id, 270571) (market, ETH-USD) \n",
|
|
"1 (id, 2047512381621272576) (account_id, 270571) (market, ETH-USD) \n",
|
|
"2 (id, 2047419314696355840) (account_id, 270571) (market, ETH-USD) \n",
|
|
"\n",
|
|
" 3 4 \\\n",
|
|
"0 (side, SHORT) (size, 0.2160000000000000) \n",
|
|
"1 (side, LONG) (size, 0.2150000000000000) \n",
|
|
"2 (side, LONG) (size, 0.2150000000000000) \n",
|
|
"\n",
|
|
" 5 6 \\\n",
|
|
"0 (max_position_size, 0.2160000000000000) (leverage, 50.0000000000000000) \n",
|
|
"1 (max_position_size, 0.2150000000000000) (leverage, 50.0000000000000000) \n",
|
|
"2 (max_position_size, 0.2150000000000000) (leverage, 50.0000000000000000) \n",
|
|
"\n",
|
|
" 7 8 \\\n",
|
|
"0 (open_price, 2315.5000000000000000) (exit_price, 2308.6000000000000000) \n",
|
|
"1 (open_price, 2316.6000000000000000) (exit_price, 2315.5000000000000000) \n",
|
|
"2 (open_price, 2321.7000000000000000) (exit_price, 2327.3000000000000000) \n",
|
|
"\n",
|
|
" 9 \\\n",
|
|
"0 (realised_pnl, 1.4249250000000000) \n",
|
|
"1 (realised_pnl, 0.0720660000000000) \n",
|
|
"2 (realised_pnl, 1.3196460000000000) \n",
|
|
"\n",
|
|
" 10 \\\n",
|
|
"0 (realised_pnl_breakdown, trade_pnl=Decimal('1.... \n",
|
|
"1 (realised_pnl_breakdown, trade_pnl=Decimal('-0... \n",
|
|
"2 (realised_pnl_breakdown, trade_pnl=Decimal('1.... \n",
|
|
"\n",
|
|
" 11 12 \\\n",
|
|
"0 (created_time, 1777103741241) (exit_type, TRADE) \n",
|
|
"1 (created_time, 1776999967376) (exit_type, TRADE) \n",
|
|
"2 (created_time, 1776977778492) (exit_type, TRADE) \n",
|
|
"\n",
|
|
" 13 \n",
|
|
"0 (closed_time, 1777133049026) \n",
|
|
"1 (closed_time, 1777103741241) \n",
|
|
"2 (closed_time, 1776996621824) "
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"pd.DataFrame(list(dict(d)['data']))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e119aaac",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Timestamp('2026-04-25 16:04:09.026000')"
|
|
]
|
|
},
|
|
"execution_count": 17,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"pd.to_datetime(1777133049026, unit='ms')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[PositionModel(id=2047419314696355840, account_id=270571, market='ETH-USD', status='OPENED', side='LONG', leverage=Decimal('50'), size=Decimal('0.215'), value=Decimal('500.247305'), open_price=Decimal('2321.7'), mark_price=Decimal('2326.731653474999'), liquidation_price=Decimal('2222.8'), unrealised_pnl=Decimal('1.081805'), realised_pnl=Decimal('0.065534'), tp_price=None, sl_price=None, adl=2, created_at=1776977778498, updated_at=1776986778819)]"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"d = dict(await trading_client.account.get_positions()).get('data')\n",
|
|
"d"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "b9ac87f2",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'id': 2047419314696355840,\n",
|
|
" 'account_id': 270571,\n",
|
|
" 'market': 'ETH-USD',\n",
|
|
" 'status': 'OPENED',\n",
|
|
" 'side': 'LONG',\n",
|
|
" 'leverage': Decimal('50'),\n",
|
|
" 'size': Decimal('0.215'),\n",
|
|
" 'value': Decimal('500.247305'),\n",
|
|
" 'open_price': Decimal('2321.7'),\n",
|
|
" 'mark_price': Decimal('2326.731653474999'),\n",
|
|
" 'liquidation_price': Decimal('2222.8'),\n",
|
|
" 'unrealised_pnl': Decimal('1.081805'),\n",
|
|
" 'realised_pnl': Decimal('0.065534'),\n",
|
|
" 'tp_price': None,\n",
|
|
" 'sl_price': None,\n",
|
|
" 'adl': 2,\n",
|
|
" 'created_at': 1776977778498,\n",
|
|
" 'updated_at': 1776986778819}"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"dict(d[0])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "62299776",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "IndexError",
|
|
"evalue": "list index out of range",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
|
"\u001b[31mIndexError\u001b[39m Traceback (most recent call last)",
|
|
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[40]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m [j \u001b[38;5;28;01mfor\u001b[39;00m j \u001b[38;5;28;01min\u001b[39;00m d \u001b[38;5;28;01mif\u001b[39;00m j.get(\u001b[33m'market'\u001b[39m) == \u001b[33m'ETH-USD'\u001b[39m][\u001b[32m0\u001b[39m]\n",
|
|
"\u001b[31mIndexError\u001b[39m: list index out of range"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"[j for j in d if j.get('market') == 'ETH-USD']"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 31,
|
|
"id": "7cd3413d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"balance = dict(dict(await trading_client.account.get_balance()).get('data', {}))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "62632db3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"r = await trading_client.markets_info.get_pos"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 55,
|
|
"id": "e9c3fdc4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"d = r['ETH-USD'].trading_config"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 58,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"0.01"
|
|
]
|
|
},
|
|
"execution_count": 58,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"float(d.min_order_size)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "2b5d7d21",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'type': 'SNAPSHOT',\n",
|
|
" 'data': {'t': 'SNAPSHOT',\n",
|
|
" 'm': 'ETH-USD',\n",
|
|
" 'b': [{'q': '362.381', 'p': '2318.1'}],\n",
|
|
" 'a': [{'q': '70.572', 'p': '2318.2'}],\n",
|
|
" 'd': '1'},\n",
|
|
" 'ts': 1776822518279,\n",
|
|
" 'seq': 14}"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"{\"type\":\"SNAPSHOT\",\"data\":{\"t\":\"SNAPSHOT\",\"m\":\"ETH-USD\",\"b\":[{\"q\":\"362.381\",\"p\":\"2318.1\"}],\"a\":[{\"q\":\"70.572\",\"p\":\"2318.2\"}],\"d\":\"1\"},\"ts\":1776822518279,\"seq\":14}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "19f51572",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'data': {'m': 'ETH-USD', 'f': '-0.000001', 'T': 1776823319595},\n",
|
|
" 'ts': 1776823319595,\n",
|
|
" 'seq': 2}"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"{'data': {'m': 'ETH-USD', 'f': '-0.000001', 'T': 1776823319595}, 'ts': 1776823319595, 'seq': 2}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 41,
|
|
"id": "68006231",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Timestamp('2026-04-22 03:00:00')"
|
|
]
|
|
},
|
|
"execution_count": 41,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"pd.to_datetime(1776826800000, unit='ms')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "5561153b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def time_round_down(dt, interval_mins=5) -> int: # returns timestamp in seconds\n",
|
|
" interval_secs = interval_mins * 60\n",
|
|
" seconds = dt.timestamp()\n",
|
|
" rounded_seconds = math.floor(seconds / interval_secs) * interval_secs\n",
|
|
" \n",
|
|
" return rounded_seconds"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 40,
|
|
"id": "bd83b59f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"1776826800000"
|
|
]
|
|
},
|
|
"execution_count": 40,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"(time_round_down(dt=datetime.now(timezone.utc), interval_mins=60)+(60*60))*1000"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "086dbfb3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2d734f79",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "e6b59c51",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"\n",
|
|
"### USER DATA STREAM ###\n",
|
|
"order = json.loads('{\"type\":\"ORDER\",\"data\":{\"isSnapshot\":true,\"orders\":[]},\"ts\":1776839215443,\"seq\":1}')\n",
|
|
"position = json.loads('{\"type\":\"POSITION\",\"data\":{\"isSnapshot\":true,\"positions\":[]},\"ts\":1776839215443,\"seq\":2}')\n",
|
|
"balance = json.loads('{\"type\":\"BALANCE\",\"data\":{\"isSnapshot\":true,\"balance\":{\"collateralName\":\"USD\",\"balance\":\"25.979998\",\"status\":\"ACTIVE\",\"equity\":\"25.979998\",\"spotEquity\":\"0\",\"spotEquityForAvailableForTrade\":\"0\",\"availableForTrade\":\"25.979998\",\"availableForWithdrawal\":\"25.979998\",\"unrealisedPnl\":\"0\",\"initialMargin\":\"0.000000\",\"marginRatio\":\"0.0000\",\"updatedTime\":1776822250184,\"exposure\":\"0\",\"leverage\":\"0.0000\"}},\"ts\":1776839215443,\"seq\":3}')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "ee8420c4",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'type': 'ORDER',\n",
|
|
" 'data': {'isSnapshot': True, 'orders': []},\n",
|
|
" 'ts': 1776839215443,\n",
|
|
" 'seq': 1}"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"order"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "e8d20d9f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'type': 'POSITION',\n",
|
|
" 'data': {'isSnapshot': True, 'positions': []},\n",
|
|
" 'ts': 1776839215443,\n",
|
|
" 'seq': 2}"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"position"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'type': 'BALANCE',\n",
|
|
" 'data': {'isSnapshot': True,\n",
|
|
" 'balance': {'collateralName': 'USD',\n",
|
|
" 'balance': '25.979998',\n",
|
|
" 'status': 'ACTIVE',\n",
|
|
" 'equity': '25.979998',\n",
|
|
" 'spotEquity': '0',\n",
|
|
" 'spotEquityForAvailableForTrade': '0',\n",
|
|
" 'availableForTrade': '25.979998',\n",
|
|
" 'availableForWithdrawal': '25.979998',\n",
|
|
" 'unrealisedPnl': '0',\n",
|
|
" 'initialMargin': '0.000000',\n",
|
|
" 'marginRatio': '0.0000',\n",
|
|
" 'updatedTime': 1776822250184,\n",
|
|
" 'exposure': '0',\n",
|
|
" 'leverage': '0.0000'}},\n",
|
|
" 'ts': 1776839215443,\n",
|
|
" 'seq': 3}"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"balance"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "fb16dfb9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "b0388dc7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import valkey\n",
|
|
"import json\n",
|
|
"VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0, decode_responses=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"b = json.loads(VAL_KEY.get('fund_rate_aster'))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"a = json.loads(VAL_KEY.get('fund_rate_aster'))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"id": "665377af",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'timestamp_arrival': 1776970338127,\n",
|
|
" 'timestamp_msg': 1776970338000,\n",
|
|
" 'symbol': 'ETHUSDT',\n",
|
|
" 'mark_price': '2310.59000000',\n",
|
|
" 'index_price': '2311.29372093',\n",
|
|
" 'estimated_settle_price': '2312.11380907',\n",
|
|
" 'funding_rate': '-0.00001108',\n",
|
|
" 'next_funding_time_ts_ms': 1776988800000}"
|
|
]
|
|
},
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"json.loads(VAL_KEY.get('fund_rate_aster'))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"id": "f15dd3c7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"VAL_KEY.get('fr_aster_user_positions')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "23f88a3e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "af5c751b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "py_313",
|
|
"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.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|