Files
Funding_Rate/algo.ipynb
2026-05-05 16:38:45 +00:00

1469 lines
40 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "d1eed397",
"metadata": {},
"outputs": [],
"source": [
"import modules.structs as structs\n",
"import json\n",
"from dataclasses import dataclass, asdict\n",
"import valkey\n",
"import modules.utils as utils\n",
"from decimal import Decimal, ROUND_DOWN\n",
"from typing import Any\n",
"\n",
"with open('algo_config.json', 'r', encoding='utf-8') as file:\n",
" ALGO_CONFIG = json.load(file)\n",
" ALGO_CONFIG = structs.Algo_Config(**ALGO_CONFIG)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c6151613",
"metadata": {},
"outputs": [],
"source": [
"VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0, decode_responses=True)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d83c61e5",
"metadata": {},
"outputs": [],
"source": [
"config_update = {\n",
" 'Config': {\n",
" # 'Price_Worsener_Aster': 1,\n",
" # 'Price_Worsener_Extend': -1\n",
" 'Min_Time_To_Funding_Minutes': 60\n",
" },\n",
" 'Logging': {\n",
" 'Log_Summary_Each_Loop': False,\n",
" 'Print_Summary_Each_Loop': True,\n",
" },\n",
" # 'Overrides': {\n",
" # 'Allow_Ordering_Aster': True,\n",
" # 'Allow_Ordering_Extend': True,\n",
" # 'Allow_Symbol_Change': True,\n",
" # 'Flatten_Open_Positions_Opportunistic': False,\n",
" # },\n",
"}\n",
"# VAL_KEY.publish('fr_orchestrator_input', json.dumps(config_update))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7e448690",
"metadata": {},
"outputs": [],
"source": [
"### ALGO FLOW ###\n",
"best_symbol_by_exchange: dict = json.loads(s=VAL_KEY.get(name='fr_engine_best_fund_rate_output')) # ty:ignore[invalid-argument-type]\n",
"Aster = structs.Perpetual_Exchange(**best_symbol_by_exchange['ASTER'])\n",
"Extend = structs.Perpetual_Exchange(**best_symbol_by_exchange['EXTEND'])\n",
" \n",
" # await get_aster_exch_info(symbol_override=Open_Symbols[0])\n",
" # await get_extend_exch_info(symbol_override=Open_Symbols[0])\n",
"\n",
"with open('algo_config.json', mode='r', encoding='utf-8') as file:\n",
" Config = json.load(file)\n",
" Config = structs.Algo_Config(**Config)\n",
"\n",
"Config.Config.Max_Target_Notional = float(min([Aster.mult, Extend.mult]) * Config.Config.Target_Open_Cash_Position)\n",
"# logging.info(f'Initial Algo Config: {ALGO_CONFIG}')\n",
"\n",
"VAL_KEY.set(name='fr_orchestrator_output', value=json.dumps(obj=Config.model_dump()))\n",
"VAL_KEY.set(name='fr_algo_working_symbol', value=json.dumps(obj={'ASTER': asdict(obj=Aster), 'EXTEND': asdict(obj=Extend)}))\n",
"\n",
"aster_ticker_dict: Any = VAL_KEY.get('fut_ticker_aster')\n",
"aster_ticker_dict: dict = json.loads(s=aster_ticker_dict) if aster_ticker_dict is not None else {}\n",
"if ( aster_ticker_dict.get('symbol', None) != Aster.symbol ) and not(Config.Overrides.Flatten_Open_Positions):\n",
" VAL_KEY.set(name='fr_algo_working_symbol', value=json.dumps(obj={'ASTER': asdict(obj=Aster), 'EXTEND': asdict(obj=Extend)}))\n",
"\n",
" # raise ValueError(f'ASTER Symbol mismatch: {ASTER_TICKER_DICT}; expected symbol: {ASTER.symbol}')\n",
"\n",
"extend_ticker_dict: Any = VAL_KEY.get('fut_ticker_extended')\n",
"extend_ticker_dict: dict = json.loads(s=extend_ticker_dict) if extend_ticker_dict is not None else {}\n",
"if ( extend_ticker_dict.get('symbol', None) != Extend.symbol) and not(Config.Overrides.Flatten_Open_Positions):\n",
" VAL_KEY.set(name='fr_algo_working_symbol', value=json.dumps(obj={'ASTER': asdict(obj=Aster), 'EXTEND': asdict(obj=Extend)}))\n",
" # raise ValueError(f'EXTEND Symbol mismatch: {EXTENDED_TICKER_DICT}; expected symbol: {EXTEND.symbol}')\n",
"\n",
"### Load Local Notional Updates from WS ###\n",
"aster_ws_pos_updates: Any = VAL_KEY.get(name='fr_aster_user_positions')\n",
"aster_ws_pos_updates: list = json.loads(s=aster_ws_pos_updates) if aster_ws_pos_updates is not None else []\n",
"extend_ws_pos_updates: Any = VAL_KEY.get('fr_extended_user_positions')\n",
"extend_ws_pos_updates: list = json.loads(extend_ws_pos_updates) if extend_ws_pos_updates is not None else [] \n",
"\n",
"### Load Local Order Updates from WS ###\n",
"aster_ws_order_updates: Any = VAL_KEY.get('fr_aster_user_orders')\n",
"aster_ws_order_updates: list = json.loads(aster_ws_order_updates) if aster_ws_order_updates is not None else [] \n",
"extend_ws_order_updates: Any = VAL_KEY.get('fr_extended_user_orders')\n",
"extend_ws_order_updates: list = json.loads(extend_ws_order_updates) if extend_ws_order_updates is not None else [] "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1d72da04",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'timestamp_arrival': 1777965341650,\n",
" 'timestamp_msg': 1777965341629,\n",
" 'timestamp_transaction': 1777965341600,\n",
" 'orderbook_update_id': 459659132993,\n",
" 'symbol': '4USDT',\n",
" 'best_bid_px': '0.0176320',\n",
" 'best_bid_qty': '8160',\n",
" 'best_ask_px': '0.0177000',\n",
" 'best_ask_qty': '388'}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"aster_ticker_dict"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "0bcf9b05",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Aster.notional_position\n",
"Extend.notional_position"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5934a164",
"metadata": {},
"outputs": [],
"source": [
"@dataclass(kw_only=True)\n",
"class Signal:\n",
" signal: bool\n",
" exchange: str # ASTER | EXTEND\n",
" side: str # BUY | SELL\n",
" symbol: str # e.g. BTC-USD"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27c193de",
"metadata": {},
"outputs": [],
"source": [
"def signal_alpha_over_taker(aster_ticker_dict, extend_ticker_dict) -> Signal:\n",
" aster_best_ask = Decimal(aster_ticker_dict['best_ask_px'])\n",
" aster_best_bid = Decimal(aster_ticker_dict['best_bid_px'])\n",
" \n",
" aster_current\n",
" \n",
" extend_best_ask = Decimal(extend_ticker_dict['best_ask_px'])\n",
" extend_best_bid = Decimal(extend_ticker_dict['best_bid_px'])\n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "407919b8",
"metadata": {},
"outputs": [],
"source": [
"aster_mid_px = ( Decimal(str(aster_ticker_dict['best_ask_px'])) + Decimal(str(aster_ticker_dict['best_bid_px'])) ) / 2\n",
"extend_mid_px = ( Decimal(str(extend_ticker_dict['best_ask_px'])) + Decimal(str(extend_ticker_dict['best_bid_px'])) ) / 2\n",
"\n",
"aster_buy_ratio = (extend_mid_px / aster_mid_px) - 1\n",
"extend_buy_ratio = aster_buy_ratio*-1\n",
"\n",
"extend_taker_fee = Decimal(str(0.00025))\n",
"\n",
"aster_buy_ratio_min_taker = Decimal(str(aster_buy_ratio)) - extend_taker_fee\n",
"extend_buy_ratio_min_taker = Decimal(str(extend_buy_ratio)) - extend_taker_fee\n",
"\n",
"aster_buy_expected_alpha = aster_buy_ratio_min_taker - Decimal(str(Aster.buy_ratio))\n",
"extend_buy_expected_alpha = extend_buy_ratio_min_taker - Decimal(str(Extend.buy_ratio))"
]
},
{
"cell_type": "code",
"execution_count": 161,
"id": "5253967d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('0.01364809170643964895111000236')"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(aster_buy_expected_alpha * aster_mid_px)*500"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"extend_buy_expected_alpha"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"extend_buy_expected_alpha"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "cee994fa",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5f57c5e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 13,
"id": "cd600e0e",
"metadata": {},
"outputs": [],
"source": [
"from decimal import Decimal"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "db52edf9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(Decimal(0.56))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2026-04-27 15:20:58.950000')"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"pd.to_datetime(1777303258950, unit='ms')"
]
},
{
"cell_type": "code",
"execution_count": 170,
"id": "5f7535df",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'symbol_ast': 'BTCUSDT',\n",
" 'max_leverage_ast': 150,\n",
" 'lh_asset_ast': 'BTC',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': -7.975e-05,\n",
" 'min_price_ast': '1',\n",
" 'min_order_size_ast': '0.001',\n",
" 'min_lot_size_ast': '0.001',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'BTC-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'BTC',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': -3e-06,\n",
" 'min_price_ext': '1',\n",
" 'min_order_size_ext': '0.0001',\n",
" 'min_lot_size_ext': '0.00001',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'ETHUSDT',\n",
" 'max_leverage_ast': 150,\n",
" 'lh_asset_ast': 'ETH',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': -9.77e-06,\n",
" 'min_price_ast': '0.01',\n",
" 'min_order_size_ast': '0.001',\n",
" 'min_lot_size_ast': '0.001',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'ETH-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'ETH',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': -1.3e-05,\n",
" 'min_price_ext': '0.1',\n",
" 'min_order_size_ext': '0.01',\n",
" 'min_lot_size_ext': '0.001',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'SOLUSDT',\n",
" 'max_leverage_ast': 100,\n",
" 'lh_asset_ast': 'SOL',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 2.399e-05,\n",
" 'min_price_ast': '0.4200',\n",
" 'min_order_size_ast': '0.01',\n",
" 'min_lot_size_ast': '0.01',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'SOL-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'SOL',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': -1.2e-05,\n",
" 'min_price_ext': '0.01',\n",
" 'min_order_size_ext': '0.1',\n",
" 'min_lot_size_ext': '0.01',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'DOGEUSDT',\n",
" 'max_leverage_ast': 75,\n",
" 'lh_asset_ast': 'DOGE',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 9.699e-05,\n",
" 'min_price_ast': '0.002440',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'DOGE-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'DOGE',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.00001',\n",
" 'min_order_size_ext': '100',\n",
" 'min_lot_size_ext': '10',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'BNBUSDT',\n",
" 'max_leverage_ast': 100,\n",
" 'lh_asset_ast': 'BNB',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 0.00011803,\n",
" 'min_price_ast': '0.010',\n",
" 'min_order_size_ast': '0.01',\n",
" 'min_lot_size_ast': '0.01',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'BNB-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'BNB',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.01',\n",
" 'min_order_size_ext': '0.01',\n",
" 'min_lot_size_ext': '0.001',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'XRPUSDT',\n",
" 'max_leverage_ast': 100,\n",
" 'lh_asset_ast': 'XRP',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 5.373e-05,\n",
" 'min_price_ast': '0.0143',\n",
" 'min_order_size_ast': '0.1',\n",
" 'min_lot_size_ast': '0.1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'XRP-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'XRP',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 6e-06,\n",
" 'min_price_ext': '0.0001',\n",
" 'min_order_size_ext': '10',\n",
" 'min_lot_size_ext': '1',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'SUIUSDT',\n",
" 'max_leverage_ast': 75,\n",
" 'lh_asset_ast': 'SUI',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 0.0001,\n",
" 'min_price_ast': '0.000100',\n",
" 'min_order_size_ast': '0.1',\n",
" 'min_lot_size_ast': '0.1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'SUI-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'SUI',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.0001',\n",
" 'min_order_size_ext': '10',\n",
" 'min_lot_size_ext': '1',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'HYPEUSDT',\n",
" 'max_leverage_ast': 300,\n",
" 'lh_asset_ast': 'HYPE',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 5e-05,\n",
" 'min_price_ast': '0.00100',\n",
" 'min_order_size_ast': '0.01',\n",
" 'min_lot_size_ast': '0.01',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'HYPE-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'HYPE',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.001',\n",
" 'min_order_size_ext': '0.1',\n",
" 'min_lot_size_ext': '0.01',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'ENAUSDT',\n",
" 'max_leverage_ast': 25,\n",
" 'lh_asset_ast': 'ENA',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 5e-05,\n",
" 'min_price_ast': '0.0000100',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'ENA-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'ENA',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.00001',\n",
" 'min_order_size_ext': '100',\n",
" 'min_lot_size_ext': '10',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'AAVEUSDT',\n",
" 'max_leverage_ast': 10,\n",
" 'lh_asset_ast': 'AAVE',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 0.0001,\n",
" 'min_price_ast': '4.400',\n",
" 'min_order_size_ast': '0.1',\n",
" 'min_lot_size_ast': '0.1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'AAVE-USD',\n",
" 'max_leverage_ext': 50,\n",
" 'lh_asset_ext': 'AAVE',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.01',\n",
" 'min_order_size_ext': '0.1',\n",
" 'min_lot_size_ext': '0.01',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'TRUMPUSDT',\n",
" 'max_leverage_ast': 10,\n",
" 'lh_asset_ast': 'TRUMP',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 5e-05,\n",
" 'min_price_ast': '0.00100',\n",
" 'min_order_size_ast': '0.01',\n",
" 'min_lot_size_ast': '0.01',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'TRUMP-USD',\n",
" 'max_leverage_ext': 25,\n",
" 'lh_asset_ext': 'TRUMP',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.001',\n",
" 'min_order_size_ext': '1',\n",
" 'min_lot_size_ext': '1',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'INITUSDT',\n",
" 'max_leverage_ast': 50,\n",
" 'lh_asset_ast': 'INIT',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 1.25e-05,\n",
" 'min_price_ast': '0.0000100',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'INIT-USD',\n",
" 'max_leverage_ext': 5,\n",
" 'lh_asset_ext': 'INIT',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': -1e-06,\n",
" 'min_price_ext': '0.00001',\n",
" 'min_order_size_ext': '10',\n",
" 'min_lot_size_ext': '1',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'ZORAUSDT',\n",
" 'max_leverage_ast': 5,\n",
" 'lh_asset_ast': 'ZORA',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 5e-05,\n",
" 'min_price_ast': '0.0000100',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'ZORA-USD',\n",
" 'max_leverage_ext': 5,\n",
" 'lh_asset_ext': 'ZORA',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.000001',\n",
" 'min_order_size_ext': '1000',\n",
" 'min_lot_size_ext': '100',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'WLFIUSDT',\n",
" 'max_leverage_ast': 25,\n",
" 'lh_asset_ast': 'WLFI',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 5e-05,\n",
" 'min_price_ast': '0.0001000',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'WLFI-USD',\n",
" 'max_leverage_ext': 10,\n",
" 'lh_asset_ext': 'WLFI',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': -1.3e-05,\n",
" 'min_price_ext': '0.00001',\n",
" 'min_order_size_ext': '100',\n",
" 'min_lot_size_ext': '10',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'ASTERUSDT',\n",
" 'max_leverage_ast': 75,\n",
" 'lh_asset_ast': 'ASTER',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 5e-05,\n",
" 'min_price_ast': '0.00010',\n",
" 'min_order_size_ast': '0.01',\n",
" 'min_lot_size_ast': '0.01',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'ASTER-USD',\n",
" 'max_leverage_ext': 25,\n",
" 'lh_asset_ext': 'ASTER',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.00001',\n",
" 'min_order_size_ext': '10',\n",
" 'min_lot_size_ext': '1',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'ZECUSDT',\n",
" 'max_leverage_ast': 75,\n",
" 'lh_asset_ast': 'ZEC',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 1.25e-05,\n",
" 'min_price_ast': '0.0100',\n",
" 'min_order_size_ast': '0.001',\n",
" 'min_lot_size_ast': '0.001',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'ZEC-USD',\n",
" 'max_leverage_ext': 10,\n",
" 'lh_asset_ext': 'ZEC',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.001',\n",
" 'min_order_size_ext': '0.1',\n",
" 'min_lot_size_ext': '0.1',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': '4USDT',\n",
" 'max_leverage_ast': 50,\n",
" 'lh_asset_ast': '4',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 1.25e-05,\n",
" 'min_price_ast': '0.0000010',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': '4-USD',\n",
" 'max_leverage_ext': 5,\n",
" 'lh_asset_ext': '4',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 0.000179,\n",
" 'min_price_ext': '0.00001',\n",
" 'min_order_size_ext': '100',\n",
" 'min_lot_size_ext': '10',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'LITUSDT',\n",
" 'max_leverage_ast': 50,\n",
" 'lh_asset_ast': 'LIT',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 2.475e-05,\n",
" 'min_price_ast': '0.0001000',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'LIT-USD',\n",
" 'max_leverage_ext': 25,\n",
" 'lh_asset_ext': 'LIT',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.0001',\n",
" 'min_order_size_ext': '10',\n",
" 'min_lot_size_ext': '1',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'XMRUSDT',\n",
" 'max_leverage_ast': 50,\n",
" 'lh_asset_ast': 'XMR',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 1.25e-05,\n",
" 'min_price_ast': '0.01',\n",
" 'min_order_size_ast': '0.001',\n",
" 'min_lot_size_ast': '0.001',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'XMR-USD',\n",
" 'max_leverage_ext': 25,\n",
" 'lh_asset_ext': 'XMR',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.01',\n",
" 'min_order_size_ext': '0.1',\n",
" 'min_lot_size_ext': '0.01',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'XPTUSDT',\n",
" 'max_leverage_ast': 3,\n",
" 'lh_asset_ast': 'XPT',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': 0.0,\n",
" 'min_price_ast': '0.0100',\n",
" 'min_order_size_ast': '0.001',\n",
" 'min_lot_size_ast': '0.001',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'XPT-USD',\n",
" 'max_leverage_ext': 5,\n",
" 'lh_asset_ext': 'XPT',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': -3e-06,\n",
" 'min_price_ext': '0.1',\n",
" 'min_order_size_ext': '0.01',\n",
" 'min_lot_size_ext': '0.001',\n",
" 'min_notional_ext': 0.0},\n",
" {'symbol_ast': 'CHIPUSDT',\n",
" 'max_leverage_ast': 50,\n",
" 'lh_asset_ast': 'CHIP',\n",
" 'rh_asset_ast': 'USDT',\n",
" 'funding_rate_ast': -2.45e-06,\n",
" 'min_price_ast': '0.0000100',\n",
" 'min_order_size_ast': '1',\n",
" 'min_lot_size_ast': '1',\n",
" 'min_notional_ast': '5',\n",
" 'symbol_ext': 'CHIP-USD',\n",
" 'max_leverage_ext': 5,\n",
" 'lh_asset_ext': 'CHIP',\n",
" 'rh_asset_ext': 'USD',\n",
" 'funding_rate_ext': 1.3e-05,\n",
" 'min_price_ext': '0.000001',\n",
" 'min_order_size_ext': '100',\n",
" 'min_lot_size_ext': '10',\n",
" 'min_notional_ext': 0.0}]"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json.loads(VAL_KEY.get('fr_engine_best_fund_rate_master'))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "b71bd50c",
"metadata": {},
"outputs": [],
"source": [
"min_lot = 10\n",
"order = 601"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "56f77b83",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"600"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"order - ( order % min_lot )"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "7a566db7",
"metadata": {},
"outputs": [],
"source": [
"c = 500\n",
"target = 400\n",
"order = 10"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "c9d67074",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Should be selling, but its not - skipping\n"
]
}
],
"source": [
"if (target < c) and ((c + order) > c):\n",
" print('Should be selling, but its not - skipping')\n",
"else:\n",
" print('good')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 41,
"id": "9eb7e9b5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2026-05-03 15:00:00')"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"pd.to_datetime(1777820400000, unit='ms')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "101dbef1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 51,
"id": "3acaa8cd",
"metadata": {},
"outputs": [],
"source": [
"ASTER = structs.Perpetual_Exchange(\n",
" mult = 150,\n",
" lh_asset = 'ETH',\n",
" rh_asset = 'USD',\n",
" symbol_asset_separator = '',\n",
")\n",
"EXTEND = structs.Perpetual_Exchange(\n",
" mult = 50,\n",
" lh_asset = 'ETH',\n",
" rh_asset = 'USD',\n",
" symbol_asset_separator = '-',\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b417adad",
"metadata": {},
"outputs": [],
"source": [
"best_symbol_by_exchange: dict = json.loads(s=VAL_KEY.get(name='fr_engine_best_fund_rate_output')) # ty:ignore[invalid-argument-type]\n",
"best_symbol_by_exchange_aster = structs.Perpetual_Exchange(**best_symbol_by_exchange['ASTER'])\n",
"best_symbol_by_exchange_extend = structs.Perpetual_Exchange(**best_symbol_by_exchange['EXTEND'])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "ba98754e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Perpetual_Exchange(lh_asset='WLFI', rh_asset='USDT', symbol='WLFIUSDT', symbol_asset_separator='', mult=25, initial_funding_rate=0.00087322, min_price=0.0001, min_order_size=1.0)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"best_symbol_by_exchange_aster"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "fa5a8e85",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Perpetual_Exchange(lh_asset='WLFI', rh_asset='USD', symbol='WLFI-USD', symbol_asset_separator='-', mult=10, initial_funding_rate=4e-06, min_price=1e-05, min_order_size=100.0)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"best_symbol_by_exchange_extend"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d452385f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "09571e38",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('1.0')"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Decimal('1.0').quantize()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1.0\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "70b57870",
"metadata": {},
"outputs": [],
"source": [
"f = ['b','a','z','e']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['b', 'a', 'z', 'e']"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "9fd60c6e",
"metadata": {},
"outputs": [],
"source": [
"f.sort()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "cd8b41de",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 'b', 'e', 'z']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "4c566e14",
"metadata": {},
"outputs": [],
"source": [
"price = float(0.9066)\n",
"min_price = float(0.0001)\n",
"\n",
"min_price = int(min_price) if min_price == int(min_price) else min_price\n",
"price: Decimal = Decimal(str(price)).quantize(Decimal(str(min_price)), rounding=ROUND_DOWN)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57fac02c",
"metadata": {},
"outputs": [],
"source": [
"### Locked Values 🔒🔑 ###"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "8c2d003f",
"metadata": {},
"outputs": [],
"source": [
"from sqlalchemy.util.typing import Self\n",
"from typing import Any\n",
"from collections.abc import Sequence, Callable\n",
"\n",
"class Locked_Value(Sequence):\n",
" def __init__(self, initial_value: Any, unlock_func: Callable):\n",
" self._value: Any = initial_value\n",
" self._unlock_func: Callable = unlock_func\n",
" self._is_locked: bool = True\n",
"\n",
" def __repr__(self):\n",
" return str((self._value, self._is_locked, self._unlock_func))\n",
"\n",
" def __len__(self):\n",
" return len((self._value, self._is_locked, self._unlock_func))\n",
"\n",
" def __getitem__(self, index):\n",
" return (self._value, self._is_locked, self._unlock_func)[index]\n",
"\n",
" def __str__(self):\n",
" return str((self._value))\n",
"\n",
" def unlock(self) -> Self:\n",
" if self._unlock_func():\n",
" self._is_locked = False\n",
" return self\n",
"\n",
" @property\n",
" def is_locked(self):\n",
" return self._is_locked\n",
"\n",
" @property\n",
" def value(self):\n",
" return self._value\n",
"\n",
" @value.setter\n",
" def value(self, v):\n",
" if not(self._is_locked):\n",
" self._value = v\n",
" else:\n",
" raise ValueError(f'Failed to set value, item is locked: {str(self.__repr__)}')\n"
]
},
{
"cell_type": "code",
"execution_count": 155,
"id": "6fd6a46c",
"metadata": {},
"outputs": [],
"source": [
"class Current_Previous_Value:\n",
" def __init__(self, value: Any = None, previous_value: Any = None):\n",
" self._value: Any = value\n",
" self._previous_value: Any = previous_value\n",
"\n",
" def __repr__(self):\n",
" return str((self._value, self._previous_value))\n",
"\n",
" def __len__(self):\n",
" return len((self._value, self._previous_value))\n",
"\n",
" def __getitem__(self, index):\n",
" return (self._value, self._previous_value)[index]\n",
"\n",
" def __str__(self):\n",
" return str(self._value)\n",
"\n",
" @property\n",
" def value(self):\n",
" return self._value\n",
"\n",
" @property\n",
" def previous_value(self):\n",
" return self._previous_value\n",
"\n",
" @value.setter\n",
" def value(self, v):\n",
" self._previous_value = self._value\n",
" self._value = v\n"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [],
"source": [
"vo = Current_Previous_Value()"
]
},
{
"cell_type": "code",
"execution_count": 163,
"id": "b557d932",
"metadata": {},
"outputs": [],
"source": [
"vo.value = 1\n",
"vo.value = 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef1d4ce0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 131,
"id": "04efe6cc",
"metadata": {},
"outputs": [],
"source": [
"v = 1"
]
},
{
"cell_type": "code",
"execution_count": 132,
"id": "36b56800",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v"
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "e24ff466",
"metadata": {},
"outputs": [],
"source": [
"v = 2"
]
},
{
"cell_type": "code",
"execution_count": 134,
"id": "b5ee5afa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74051e1d",
"metadata": {},
"outputs": [],
"source": [
"def test():\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 128,
"id": "38ee912d",
"metadata": {},
"outputs": [],
"source": [
"v = Locked_Value('locked', unlock_func=test)"
]
},
{
"cell_type": "code",
"execution_count": 129,
"id": "bb703d87",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Failed to set value, item is locked: <bound method Locked_Value.__repr__ of ('locked', True, <function test at 0x76725ff485e0>)>",
"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[129]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m v.unlock().value = \u001b[33m'unlocked'\u001b[39m\n",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[124]\u001b[39m\u001b[32m, line 41\u001b[39m, in \u001b[36mLocked_Value.value\u001b[39m\u001b[34m(self, v)\u001b[39m\n\u001b[32m 37\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m value(self, v):\n\u001b[32m 38\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28;01mnot\u001b[39;00m(self._is_locked):\n\u001b[32m 39\u001b[39m self._value = v\n\u001b[32m 40\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m---> \u001b[39m\u001b[32m41\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m ValueError(f'Failed to set value, item is locked: {str(self.__repr__)}')\n",
"\u001b[31mValueError\u001b[39m: Failed to set value, item is locked: <bound method Locked_Value.__repr__ of ('locked', True, <function test at 0x76725ff485e0>)>"
]
}
],
"source": [
"v.unlock().value = 'unlocked'"
]
},
{
"cell_type": "code",
"execution_count": 123,
"id": "76c44d97",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('locked', True, <function test at 0x767268132520>)"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"lv = Locked_Value(initial_value=999)\n",
"# lv.unlock()"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "76e21865",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(lv)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "10258f5b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(999, False)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9270dd8d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a938b2e0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "39667bd8",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"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
}