Files
Funding_Rate/aster.ipynb

529 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "3a269644",
"metadata": {},
"outputs": [],
"source": [
"import modules.aster_auth as aster_auth"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4395fabb",
"metadata": {},
"outputs": [],
"source": [
"listen_key_request = {\n",
" \"url\": \"/fapi/v3/listenKey\",\n",
" \"method\": \"POST\",\n",
" \"params\": {}\n",
"}\n",
"cancel_all_open_orders = {\n",
" \"url\": \"/fapi/v3/allOpenOrders\",\n",
" \"method\": \"DELETE\",\n",
" \"params\": {\n",
" 'symbol': 'ETHUSDT',\n",
" }\n",
"}\n",
"cancel_order = {\n",
" \"url\": \"/fapi/v3/order\",\n",
" \"method\": \"DELETE\",\n",
" \"params\": {\n",
" 'symbol': 'ETHUSDT',\n",
" 'orderId': 17349955824,\n",
" }\n",
"}\n",
"fut_acct_balances = {\n",
" \"url\": \"/fapi/v3/balance\",\n",
" \"method\": \"GET\",\n",
" \"params\": {}\n",
"}\n",
"fut_acct_openOrders = {\n",
" \"url\": \"/fapi/v3/openOrders\",\n",
" \"method\": \"GET\",\n",
" \"params\": {}\n",
"}\n",
"fut_acct_positionRisk = {\n",
" \"url\": \"/fapi/v3/positionRisk\",\n",
" \"method\": \"GET\",\n",
" \"params\": {\n",
" 'symbol': 'ETHUSDT',\n",
" }\n",
"}\n",
"fut_acct_exchangeInfo = {\n",
" \"url\": \"/fapi/v3/exchangeInfo\",\n",
" \"method\": \"GET\",\n",
" \"params\": {}\n",
"}\n",
"commission_rate = {\n",
" \"url\": \"/fapi/v3/commissionRate\",\n",
" \"method\": \"GET\",\n",
" \"params\": {\n",
" 'symbol': 'ETHUSDT',\n",
" }\n",
"}\n",
"post_order = {\n",
" \"url\": \"/fapi/v3/order\",\n",
" \"method\": \"POST\",\n",
" \"params\": {\n",
" 'symbol': 'ETHUSDT',\n",
" 'side': 'SELL',\n",
" 'type': 'LIMIT',\n",
" 'timeInForce': 'GTC',\n",
" 'quantity': '0.01',\n",
" 'price': '2500',\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2122885a",
"metadata": {},
"outputs": [],
"source": [
"j = await aster_auth.post_authenticated_url(fut_acct_positionRisk)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e895ac52",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'symbol': 'ETHUSDT',\n",
" 'positionAmt': '-0.215',\n",
" 'entryPrice': '2316.616543779',\n",
" 'markPrice': '2318.47005039',\n",
" 'unRealizedProfit': '-0.39850392',\n",
" 'liquidationPrice': '2422.12954566',\n",
" 'leverage': '150',\n",
" 'maxNotionalValue': '300000',\n",
" 'marginType': 'cross',\n",
" 'isolatedMargin': '0.00000000',\n",
" 'isAutoAddMargin': 'false',\n",
" 'positionSide': 'BOTH',\n",
" 'notional': '-498.47106083',\n",
" 'isolatedWallet': '0',\n",
" 'updateTime': 1777000243527}]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"j"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e61dca73",
"metadata": {},
"outputs": [],
"source": [
"d = [d for d in j if d.get('symbol', None) == 'ETHUSDT'][0]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f0538cde",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'symbol': 'ETHUSDT',\n",
" 'positionAmt': '0.000',\n",
" 'entryPrice': '0.0',\n",
" 'markPrice': '2317.47321317',\n",
" 'unRealizedProfit': '0.00000000',\n",
" 'liquidationPrice': '0',\n",
" 'leverage': '150',\n",
" 'maxNotionalValue': '300000',\n",
" 'marginType': 'cross',\n",
" 'isolatedMargin': '0.00000000',\n",
" 'isAutoAddMargin': 'false',\n",
" 'positionSide': 'BOTH',\n",
" 'notional': '0',\n",
" 'isolatedWallet': '0',\n",
" 'updateTime': 1776996575970}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "80b9c0e5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-499.15475000000004"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float(d['notional'])-float(d['unRealizedProfit'])"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"t = round(datetime.now().timestamp()*1000)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "ebded6ad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2026-04-23 23:17:32.516000')"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"pd.to_datetime(t, unit='ms')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "67a3bbb2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 23,
"id": "e958e7da",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"### User Data Stream ###\n",
"# After posting limit order to rest\n",
"after_order_post = json.loads('{\"e\":\"ORDER_TRADE_UPDATE\",\"T\":1776836992350,\"E\":1776836992388,\"o\":{\"s\":\"ETHUSDT\",\"c\":\"KFi375tZh5kzHsQJWKMJHe\",\"S\":\"BUY\",\"o\":\"LIMIT\",\"f\":\"GTC\",\"q\":\"0.010\",\"p\":\"2100\",\"ap\":\"0\",\"sp\":\"0\",\"x\":\"NEW\",\"X\":\"NEW\",\"i\":17341121450,\"l\":\"0\",\"z\":\"0\",\"L\":\"0\",\"T\":1776836992350,\"t\":0,\"b\":\"21\",\"a\":\"0\",\"m\":false,\"R\":false,\"wt\":\"CONTRACT_PRICE\",\"ot\":\"LIMIT\",\"ps\":\"BOTH\",\"cp\":false,\"rp\":\"0\",\"pP\":false,\"si\":0,\"ss\":0,\"h\":\"0xeec41a368072d5a47c13b3ad83703eded2e2b8cf9ff427095beb8b7684968db0\"}}')\n",
"\n",
"# After order cancellation in GUI\n",
"after_order_cxl = json.loads('{\"e\":\"ORDER_TRADE_UPDATE\",\"T\":1776836998500,\"E\":1776836998533,\"o\":{\"s\":\"ETHUSDT\",\"c\":\"KFi375tZh5kzHsQJWKMJHe\",\"S\":\"BUY\",\"o\":\"LIMIT\",\"f\":\"GTC\",\"q\":\"0.010\",\"p\":\"2100\",\"ap\":\"0\",\"sp\":\"0\",\"x\":\"CANCELED\",\"X\":\"CANCELED\",\"i\":17341121450,\"l\":\"0\",\"z\":\"0\",\"L\":\"0\",\"T\":1776836998500,\"t\":0,\"b\":\"0\",\"a\":\"0\",\"m\":false,\"R\":false,\"wt\":\"CONTRACT_PRICE\",\"ot\":\"LIMIT\",\"ps\":\"BOTH\",\"cp\":false,\"rp\":\"0\",\"pP\":false,\"si\":0,\"ss\":0,\"h\":\"0x116bb48a4b41420aebbc76343d6f55f22d1ccbc36b04462e6172571fda836599\"}}')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'e': 'ORDER_TRADE_UPDATE',\n",
" 'T': 1776836992350,\n",
" 'E': 1776836992388,\n",
" 'o': {'s': 'ETHUSDT',\n",
" 'c': 'KFi375tZh5kzHsQJWKMJHe',\n",
" 'S': 'BUY',\n",
" 'o': 'LIMIT',\n",
" 'f': 'GTC',\n",
" 'q': '0.010',\n",
" 'p': '2100',\n",
" 'ap': '0',\n",
" 'sp': '0',\n",
" 'x': 'NEW',\n",
" 'X': 'NEW',\n",
" 'i': 17341121450,\n",
" 'l': '0',\n",
" 'z': '0',\n",
" 'L': '0',\n",
" 'T': 1776836992350,\n",
" 't': 0,\n",
" 'b': '21',\n",
" 'a': '0',\n",
" 'm': False,\n",
" 'R': False,\n",
" 'wt': 'CONTRACT_PRICE',\n",
" 'ot': 'LIMIT',\n",
" 'ps': 'BOTH',\n",
" 'cp': False,\n",
" 'rp': '0',\n",
" 'pP': False,\n",
" 'si': 0,\n",
" 'ss': 0,\n",
" 'h': '0xeec41a368072d5a47c13b3ad83703eded2e2b8cf9ff427095beb8b7684968db0'}}"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"after_order_post"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "1ea320f2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'e': 'ORDER_TRADE_UPDATE',\n",
" 'T': 1776836998500,\n",
" 'E': 1776836998533,\n",
" 'o': {'s': 'ETHUSDT',\n",
" 'c': 'KFi375tZh5kzHsQJWKMJHe',\n",
" 'S': 'BUY',\n",
" 'o': 'LIMIT',\n",
" 'f': 'GTC',\n",
" 'q': '0.010',\n",
" 'p': '2100',\n",
" 'ap': '0',\n",
" 'sp': '0',\n",
" 'x': 'CANCELED',\n",
" 'X': 'CANCELED',\n",
" 'i': 17341121450,\n",
" 'l': '0',\n",
" 'z': '0',\n",
" 'L': '0',\n",
" 'T': 1776836998500,\n",
" 't': 0,\n",
" 'b': '0',\n",
" 'a': '0',\n",
" 'm': False,\n",
" 'R': False,\n",
" 'wt': 'CONTRACT_PRICE',\n",
" 'ot': 'LIMIT',\n",
" 'ps': 'BOTH',\n",
" 'cp': False,\n",
" 'rp': '0',\n",
" 'pP': False,\n",
" 'si': 0,\n",
" 'ss': 0,\n",
" 'h': '0x116bb48a4b41420aebbc76343d6f55f22d1ccbc36b04462e6172571fda836599'}}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"after_order_cxl"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "07ef4360",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2019-09-19 07:51:05.651000')"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"pd.to_datetime(1568879465651, unit='ms')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ok': True,\n",
" 'result': {'message_id': 24,\n",
" 'sender_chat': {'id': -1003864341457,\n",
" 'title': 'Atwater_Alert',\n",
" 'type': 'channel'},\n",
" 'chat': {'id': -1003864341457, 'title': 'Atwater_Alert', 'type': 'channel'},\n",
" 'date': 1777009128,\n",
" 'text': 'alert!'}}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import modules.utils as utils\n",
"\n",
"utils.send_tg_alert('alert!')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "284b7266",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'sd'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"sdfsdfsfdsdfs\"[:2]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "98ed3a27",
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"\n",
"@dataclass(kw_only=True)\n",
"class Flags:\n",
" LIQUIDATE_POS_AND_KILL_ALGO_FLAG: bool = False\n",
" NET_FUNDING_IS_ZERO: bool = False\n",
" # list = field(init=False) \n",
"Flags = Flags()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "73a525c2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Flags(LIQUIDATE_POS_AND_KILL_ALGO_FLAG=False, NET_FUNDING_IS_ZERO=False)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Flags"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "094e374c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "15c3bcef",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "940ceba7",
"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
}