Files
Funding_Rate/algo.ipynb

668 lines
79 KiB
Plaintext
Raw Normal View History

2026-04-25 23:43:28 +00:00
{
"cells": [
{
"cell_type": "code",
2026-05-04 18:04:45 +00:00
"execution_count": 2,
2026-04-25 23:47:07 +00:00
"id": "d1eed397",
2026-04-25 23:43:28 +00:00
"metadata": {},
"outputs": [],
"source": [
2026-04-25 23:47:07 +00:00
"import modules.structs as structs\n",
2026-04-25 23:43:28 +00:00
"import json\n",
"from dataclasses import dataclass, asdict\n",
2026-04-25 23:47:07 +00:00
"import valkey\n",
2026-04-30 04:32:49 +00:00
"import modules.utils as utils\n",
"from decimal import Decimal, ROUND_DOWN\n",
2026-04-25 23:43:28 +00:00
"\n",
2026-04-25 23:47:07 +00:00
"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)"
2026-04-25 23:43:28 +00:00
]
},
{
"cell_type": "code",
2026-05-04 18:04:45 +00:00
"execution_count": 3,
2026-04-25 23:47:07 +00:00
"id": "c6151613",
2026-04-25 23:43:28 +00:00
"metadata": {},
"outputs": [],
"source": [
2026-04-25 23:47:07 +00:00
"VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0, decode_responses=True)"
2026-04-25 23:43:28 +00:00
]
},
{
"cell_type": "code",
2026-05-04 18:04:45 +00:00
"execution_count": 47,
2026-04-25 23:47:07 +00:00
"id": "d83c61e5",
2026-04-25 23:43:28 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2026-04-25 23:47:07 +00:00
"1"
2026-04-25 23:43:28 +00:00
]
},
2026-05-04 18:04:45 +00:00
"execution_count": 47,
2026-04-25 23:43:28 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2026-04-26 06:10:18 +00:00
"config_update = {\n",
2026-05-04 18:04:45 +00:00
" 'Config': {\n",
" # 'Price_Worsener_Aster': 1,\n",
" # 'Price_Worsener_Extend': -1\n",
2026-05-04 18:04:45 +00:00
" 'Min_Time_To_Funding_Minutes': 55\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",
2026-05-04 18:04:45 +00:00
" # 'Flatten_Open_Positions_Opportunistic': False,\n",
" # },\n",
2026-04-26 06:10:18 +00:00
"}\n",
2026-04-25 23:47:07 +00:00
"VAL_KEY.publish('fr_orchestrator_input', json.dumps(config_update))"
2026-04-25 23:43:28 +00:00
]
},
2026-04-27 17:57:58 +00:00
{
"cell_type": "code",
2026-04-30 04:32:49 +00:00
"execution_count": 52,
"id": "f5260342",
2026-04-27 17:57:58 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2026-04-30 04:32:49 +00:00
"1"
2026-04-27 17:57:58 +00:00
]
},
2026-04-30 04:32:49 +00:00
"execution_count": 52,
2026-04-27 17:57:58 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2026-04-30 04:32:49 +00:00
"order = {'order_id':'test_1'}\n",
"VAL_KEY.publish('fr_engine_orders_input', json.dumps(order))"
2026-04-27 17:57:58 +00:00
]
},
2026-05-04 18:04:45 +00:00
{
"cell_type": "code",
"execution_count": null,
"id": "34a85b2e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('1.0')"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Decimal('1.0').quantize()"
]
},
2026-04-26 06:10:18 +00:00
{
"cell_type": "code",
"execution_count": 6,
2026-04-30 04:32:49 +00:00
"id": "940586bb",
2026-04-26 06:10:18 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"stream\":\"btcusdt@bookTicker\",\"data\":{\"event_type\":\"bookTicker\",\"update_id\":458028589204,\"symbol\":\"BTCUSDT\",\"best_bid_price\":\"76414.9\",\"best_bid_qty\":\"0.507\",\"best_ask_price\":\"76415.0\",\"best_ask_qty\":\"0.878\",\"transaction_time\":1777565596460,\"event_time\":1777565596488}}'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"VAL_KEY.get('test_key')"
]
2026-04-26 06:10:18 +00:00
},
2026-04-25 23:43:28 +00:00
{
"cell_type": "code",
"execution_count": 13,
2026-04-30 04:32:49 +00:00
"id": "cd600e0e",
2026-04-27 17:57:58 +00:00
"metadata": {},
"outputs": [],
"source": [
"from decimal import Decimal"
]
2026-04-27 17:57:58 +00:00
},
{
"cell_type": "code",
"execution_count": 16,
2026-04-30 04:32:49 +00:00
"id": "db52edf9",
2026-04-27 17:57:58 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(Decimal(0.56))"
]
2026-04-27 17:57:58 +00:00
},
{
"cell_type": "code",
2026-04-30 04:32:49 +00:00
"execution_count": null,
2026-04-27 17:57:58 +00:00
"metadata": {},
"outputs": [],
2026-04-30 04:32:49 +00:00
"source": []
2026-04-27 17:57:58 +00:00
},
{
"cell_type": "code",
2026-04-30 04:32:49 +00:00
"execution_count": 35,
2026-04-25 23:43:28 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2026-04-30 04:32:49 +00:00
"Timestamp('2026-04-27 15:20:58.950000')"
2026-04-25 23:43:28 +00:00
]
},
2026-04-30 04:32:49 +00:00
"execution_count": 35,
2026-04-25 23:43:28 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2026-04-30 04:32:49 +00:00
"import pandas as pd\n",
"pd.to_datetime(1777303258950, unit='ms')"
2026-04-25 23:43:28 +00:00
]
},
{
"cell_type": "code",
2026-05-04 18:04:45 +00:00
"execution_count": 4,
2026-04-30 04:32:49 +00:00
"id": "5f7535df",
2026-04-25 23:43:28 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2026-05-04 18:04:45 +00:00
"'[{\"symbol_ext\":\"BTC-USD\",\"funding_rate_updated_ts_ms_ext\":1777831559598,\"funding_rate_ext\":-0.000009,\"funding_rate_updated_dt_ext\":1777831559598,\"assetName\":\"BTC\",\"status\":\"ACTIVE\",\"next_funding_ts_ext\":1777834800000.0,\"daily_volume_ext\":189468858.5763599873,\"min_order_size_ext\":\"0.0001\",\"min_price_ext\":\"1\",\"USDT_Symbol\":\"BTCUSDT\",\"time_delta_to_next_funding_ext\":2593202,\"symbol_ast\":\"BTCUSDT\",\"funding_rate_updated_ts_ms_ast\":1777832205000,\"funding_rate_ast\":-0.00002882,\"next_funding_ts_ast\":1777852800000,\"funding_rate_updated_dt_ast\":1777832205000,\"time_delta_to_next_funding_ast\":20593213,\"daily_volume_ast\":328819440.9900000095,\"min_order_size_ast\":\"0.001\",\"min_price_ast\":\"1\",\"next_funding_at_same_time\":false,\"net_funding_rate\":-0.000009,\"net_funding_rate_abs\":0.000009},{\"symbol_ext\":\"ETH-USD\",\"funding_rate_updated_ts_ms_ext\":1777831559598,\"funding_rate_ext\":-0.000019,\"funding_rate_updated_dt_ext\":1777831559598,\"assetName\":\"ETH\",\"status\":\"ACTIVE\",\"next_funding_ts_ext\":1777834800000.0,\"daily_volume_ext\":63224882.9324000031,\"min_order_size_ext\":\"0.01\",\"min_price_ext\":\"0.1\",\"USDT_Symbol\":\"ETHUSDT\",\"time_delta_to_next_funding_ext\":2593202,\"symbol_ast\":\"ETHUSDT\",\"funding_rate_updated_ts_ms_ast\":1777832205000,\"funding_rate_ast\":0.00009443,\"next_funding_ts_ast\":1777852800000,\"funding_rate_updated_dt_ast\":1777832205000,\"time_delta_to_next_funding_ast\":20593213,\"daily_volume_ast\":213809320.6999999881,\"min_order_size_ast\":\"0.001\",\"min_price_ast\":\"0.01\",\"next_funding_at_same_time\":false,\"net_funding_rate\":-0.000019,\"net_funding_rate_abs\":0.000019},{\"symbol_ext\":\"SOL-USD\",\"funding_rate_updated_ts_ms_ext\":1777831559598,\"funding_rate_ext\":0.000007,\"funding_rate_updated_dt_ext\":1777831559598,\"assetName\":\"SOL\",\"status\":\"ACTIVE\",\"next_funding_ts_ext\":1777834800000.0,\"daily_volume_ext\":10765070.9333999995,\"min_order_size_ext\":\"0.1\",\"min_price_ext\":\"0.01\",\"USDT_Symbol\":\"SOLUSDT\",\"time_delta_to_next_funding_ext\":2593202,\"symbol_ast\":\"SOLUSDT\",\"funding_rate_updated_ts_ms_ast\":1777832205000,\"funding_rate_ast\":0.0001,\"next_funding_ts_ast\":1777852800000,\"funding_rate_updated_dt_ast\":1777832205000,\"time_delta_to_next_funding_ast\":20593213,\"daily_volume_ast\":27067188.6099999994,\"min_order_size_ast\":\"0.01\",\"min_price_ast\":\"0.4200\",\"next_funding_at_same_time\":false,\"net_funding_rate\":0.000007,\"net_funding_rate_abs\":0.000007},{\"symbol_ext\":\"DOGE-USD\",\"funding_rate_updated_ts_ms_ext\":1777831559598,\"funding_rate_ext\":0.000013,\"funding_rate_updated_dt_ext\":1777831559598,\"assetName\":\"DOGE\",\"status\":\"ACTIVE\",\"next_funding_ts_ext\":1777834800000.0,\"daily_volume_ext\":1930324.1714000001,\"min_order_size_ext\":\"100\",\"min_price_ext\":\"0.00001\",\"USDT_Symbol\":\"DOGEUSDT\",\"time_delta_to_next_funding_ext\":2593202,\"symbol_ast\":\"DOGEUSDT\",\"funding_rate_updated_ts_ms_ast\":1777832205000,\"funding_rate_ast\":0.0001,\"next_funding_ts_ast\":1777852800000,\"funding_rate_updated_dt_ast\":1777832205000,\"time_delta_to_next_funding_ast\":20593213,\"daily_volume_ast\":13296700.8699999992,\"min_order_size_ast\":\"1\",\"min_price_ast\":\"0.002440\",\"next_funding_at_same_time\":false,\"net_funding_rate\":0.000013,\"net_funding_rate_abs\":0.000013},{\"symbol_ext\":\"BNB-USD\",\"funding_rate_updated_ts_ms_ext\":1777831559598,\"funding_rate_ext\":0.000013,\"funding_rate_updated_dt_ext\":1777831559598,\"assetName\":\"BNB\",\"status\":\"ACTIVE\",\"next_funding_ts_ext\":1777834800000.0,\"daily_volume_ext\":3295700.7066000002,\"min_order_size_ext\":\"0.01\",\"min_price_ext\":\"0.01\",\"USDT_Symbol\":\"BNBUSDT\",\"time_delta_to_next_funding_ext\":2593202,\"symbol_ast\":\"BNBUSDT\",\"funding_rate_updated_ts_ms_ast\":1777832205000,\"funding_rate_ast\":0.00010666,\"next_funding_ts_ast\":1777852800000,\"funding_rate_updated_dt_ast\":1777832205000,\"time_delta_to_next_funding_ast\":20593213,\"daily_volume_ast\":7790358.3200000003,\"min_order_size_a
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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"
2026-04-25 23:43:28 +00:00
]
},
2026-05-04 18:04:45 +00:00
"execution_count": 20,
2026-04-25 23:43:28 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2026-05-04 18:04:45 +00:00
"order - ( order % min_lot )"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "7a566db7",
"metadata": {},
"outputs": [],
"source": [
"c = 500\n",
"target = 400\n",
"order = 10"
2026-04-30 04:32:49 +00:00
]
},
2026-05-04 18:04:45 +00:00
{
"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": []
},
2026-04-30 04:32:49 +00:00
{
"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",
")"
2026-04-25 23:43:28 +00:00
]
},
{
"cell_type": "code",
2026-05-04 18:04:45 +00:00
"execution_count": 15,
2026-04-30 04:32:49 +00:00
"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",
2026-05-04 18:04:45 +00:00
"execution_count": 16,
"id": "ba98754e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2026-05-04 18:04:45 +00:00
"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)"
]
},
2026-05-04 18:04:45 +00:00
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"best_symbol_by_exchange_aster"
2026-04-30 04:32:49 +00:00
]
},
{
"cell_type": "code",
2026-05-04 18:04:45 +00:00
"execution_count": 17,
2026-04-30 04:32:49 +00:00
"id": "fa5a8e85",
2026-04-25 23:43:28 +00:00
"metadata": {},
2026-04-27 17:57:58 +00:00
"outputs": [
{
"data": {
"text/plain": [
2026-05-04 18:04:45 +00:00
"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)"
2026-04-27 17:57:58 +00:00
]
},
2026-05-04 18:04:45 +00:00
"execution_count": 17,
2026-04-27 17:57:58 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2026-04-30 04:32:49 +00:00
"best_symbol_by_exchange_extend"
2026-04-27 17:57:58 +00:00
]
},
2026-05-04 18:04:45 +00:00
{
"cell_type": "code",
"execution_count": null,
"id": "d452385f",
"metadata": {},
"outputs": [],
"source": []
},
2026-04-27 17:57:58 +00:00
{
"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,
2026-04-27 17:57:58 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
2026-04-27 17:57:58 +00:00
]
},
"execution_count": 16,
2026-04-27 17:57:58 +00:00
"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",
2026-04-30 04:32:49 +00:00
"\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": 49,
"id": "ad6444a5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(str(price))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "74a227cc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('0.9066')"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"price"
2026-04-27 17:57:58 +00:00
]
2026-04-25 23:43:28 +00:00
},
2026-04-30 04:32:49 +00:00
{
"cell_type": "code",
"execution_count": null,
"id": "57fac02c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "2331e29f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('0.000100000000000000004792173602385929598312941379845142364501953125')"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Decimal(min_price)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c139413",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
2026-04-30 04:32:49 +00:00
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a938b2e0",
2026-04-30 04:32:49 +00:00
"metadata": {},
"outputs": [],
"source": []
},
2026-04-25 23:43:28 +00:00
{
"cell_type": "code",
"execution_count": null,
"id": "39667bd8",
2026-04-25 23:43:28 +00:00
"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",
2026-04-27 17:57:58 +00:00
"version": "3.13.13"
2026-04-25 23:43:28 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}