{ "cells": [ { "cell_type": "code", "execution_count": 4, "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": 5, "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 = \"BTC-USD\"\n", "ORDER_SIDE = OrderSide.BUY\n", "ORDER_QTY = Decimal(\"0.001\")\n", "ORDER_PRICE = Decimal(\"75000\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "fc2c6d2b", "metadata": {}, "outputs": [], "source": [ "client, trading_client = await extend_auth.create_auth_account_and_trading_client()" ] }, { "cell_type": "code", "execution_count": null, "id": "c366706f", "metadata": {}, "outputs": [], "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", "# )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7cd3413d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a7212988", "metadata": {}, "outputs": [], "source": [] }, { "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": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "665377af", "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }