Files
Funding_Rate/algo.ipynb

522 lines
11 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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",
"\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": 2,
"id": "c6151613",
"metadata": {},
"outputs": [],
"source": [
"VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0, decode_responses=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d83c61e5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config_update = {\n",
" # 'Config': {\n",
" # 'Price_Worsener_Aster': 0,\n",
" # 'Price_Worsener_Extend': -1\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",
" # },\n",
"}\n",
"VAL_KEY.publish('fr_orchestrator_input', json.dumps(config_update))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "f5260342",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"order = {'order_id':'test_1'}\n",
"VAL_KEY.publish('fr_engine_orders_input', json.dumps(order))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "940586bb",
"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')"
]
},
{
"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": 18,
"id": "5f7535df",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"ASTER\": {\"lh_asset\": \"ZEC\", \"rh_asset\": \"USDT\", \"symbol_asset_separator\": \"\", \"mult\": 75, \"initial_funding_rate\": -8.836e-05, \"min_price\": 0.01, \"min_order_size\": 0.01}, \"EXTEND\": {\"lh_asset\": \"ZEC\", \"rh_asset\": \"USD\", \"symbol_asset_separator\": \"-\", \"mult\": 10, \"initial_funding_rate\": 1.3e-05, \"min_price\": 0.01, \"min_order_size\": 0.01}}'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"VAL_KEY.get('fr_algo_working_symbol')"
]
},
{
"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": 3,
"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": 4,
"id": "ba98754e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Perpetual_Exchange(lh_asset='ZEC', rh_asset='USDT', symbol_asset_separator='', mult=75, initial_funding_rate=-8.836e-05, min_price=0.01, min_order_size=0.001)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"best_symbol_by_exchange_aster"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fa5a8e85",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Perpetual_Exchange(lh_asset='ZEC', rh_asset='USD', symbol_asset_separator='-', mult=10, initial_funding_rate=1.3e-05, min_price=0.001, min_order_size=0.1)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"best_symbol_by_exchange_extend"
]
},
{
"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": 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"
]
},
{
"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,
"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
}