diff --git a/algo.ipynb b/algo.ipynb index 118f65b..023d012 100644 --- a/algo.ipynb +++ b/algo.ipynb @@ -2,152 +2,101 @@ "cells": [ { "cell_type": "code", - "execution_count": 28, - "id": "ac27075d", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "from dataclasses import dataclass, asdict\n", - "\n", - "@dataclass(kw_only=True)\n", - "class Algo_Config:\n", - " Allow_Ordering_Aster: bool\n", - " Allow_Ordering_Extend: bool\n", - " Loop_Sleep_Sec: int\n", - " Min_Time_To_Funding_Minutes: int\n", - " Price_Worsener_Aster: float\n", - " Price_Worsener_Extend: float\n", - " Target_Open_Cash_Position: int" - ] - }, - { - "cell_type": "code", - "execution_count": 29, + "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", + "\n", "with open('algo_config.json', 'r', encoding='utf-8') as file:\n", - " ALGO_CONFIG = json.load(file, object_hook=lambda d: Algo_Config(**d))" + " ALGO_CONFIG = json.load(file, object_hook=lambda d: structs.Algo_Config(**d))" ] }, { "cell_type": "code", - "execution_count": 30, - "id": "286bf2d2", + "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": 3, + "id": "d83c61e5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "Algo_Config(Allow_Ordering_Aster=True, Allow_Ordering_Extend=True, Loop_Sleep_Sec=1, Min_Time_To_Funding_Minutes=7, Price_Worsener_Aster=0.0, Price_Worsener_Extend=0.0, Target_Open_Cash_Position=10)" + "1" ] }, - "execution_count": 30, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "ALGO_CONFIG" + "config_update = {'Min_Time_To_Funding_Minutes': 7}\n", + "VAL_KEY.publish('fr_orchestrator_input', json.dumps(config_update))" ] }, { "cell_type": "code", - "execution_count": 35, - "id": "0cbed6d4", - "metadata": {}, - "outputs": [], - "source": [ - "d = asdict(ALGO_CONFIG)" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "id": "daa47980", - "metadata": {}, - "outputs": [], - "source": [ - "d_update = {'Allow_Ordering_Aster': False}" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "id": "aba7797b", + "execution_count": 11, + "id": "45fae761", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'Allow_Ordering_Aster': True,\n", + "Algo_Config(Config_Updated_Timestamp=1777151524162, Allow_Ordering_Aster=True, Allow_Ordering_Extend=True, Loop_Sleep_Sec=1, Max_Target_Notional=0.0, Min_Time_To_Funding_Minutes=60, Price_Worsener_Aster=0.0, Price_Worsener_Extend=0.0, Target_Open_Cash_Position=10)" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "json.loads(VAL_KEY.get('fr_orchestrator_output'), object_hook=lambda d: structs.Algo_Config(**d))" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Config_Updated_Timestamp': 1777098091913,\n", + " 'Allow_Ordering_Aster': True,\n", " 'Allow_Ordering_Extend': True,\n", " 'Loop_Sleep_Sec': 1,\n", - " 'Min_Time_To_Funding_Minutes': 7,\n", + " 'Max_Target_Notional': 0.0,\n", + " 'Min_Time_To_Funding_Minutes': 60,\n", " 'Price_Worsener_Aster': 0.0,\n", " 'Price_Worsener_Extend': 0.0,\n", " 'Target_Open_Cash_Position': 10}" ] }, - "execution_count": 42, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "d" + "asdict(ALGO_CONFIG)" ] }, - { - "cell_type": "code", - "execution_count": 43, - "id": "3250cb84", - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "expected ':' (3324129842.py, line 1)", - "output_type": "error", - "traceback": [ - " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[43]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mif d.get(d_update)\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m expected ':'\n" - ] - } - ], - "source": [ - "if d.get(d_update)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e283b819", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_keys(['Allow_Ordering_Aster'])" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "list(d_update.keys())" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6f067c02", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null, diff --git a/algo_config.json b/algo_config.json index e0d945f..acee321 100644 --- a/algo_config.json +++ b/algo_config.json @@ -7,5 +7,6 @@ "Min_Time_To_Funding_Minutes": 60, "Price_Worsener_Aster": 0.0, "Price_Worsener_Extend": 0.0, - "Target_Open_Cash_Position": 10 + "Target_Open_Cash_Position": 10, + "Print_Summary_Each_Loop" : false } \ No newline at end of file diff --git a/extended.ipynb b/extended.ipynb index ad23069..f6e77b6 100644 --- a/extended.ipynb +++ b/extended.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "6c70a8c3", "metadata": {}, "outputs": [], @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "ff971ca9", "metadata": {}, "outputs": [], @@ -49,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "fc2c6d2b", "metadata": {}, "outputs": [], @@ -97,78 +97,207 @@ }, { "cell_type": "code", - "execution_count": 59, - "id": "1e0cc529", + "execution_count": null, + "id": "03913674", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "8dd8aa73", "metadata": {}, "outputs": [], "source": [ - "d = dict(placed_order)\n", - "d = d['data']" + "d = await trading_client.account.get_positions_history()" ] }, { "cell_type": "code", - "execution_count": 61, - "id": "c6e2570b", + "execution_count": 12, + "id": "5f74f7cc", "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
012345678910111213
0(id, 2047947640758337536)(account_id, 270571)(market, ETH-USD)(side, SHORT)(size, 0.2160000000000000)(max_position_size, 0.2160000000000000)(leverage, 50.0000000000000000)(open_price, 2315.5000000000000000)(exit_price, 2308.6000000000000000)(realised_pnl, 1.4249250000000000)(realised_pnl_breakdown, trade_pnl=Decimal('1....(created_time, 1777103741241)(exit_type, TRADE)(closed_time, 1777133049026)
1(id, 2047512381621272576)(account_id, 270571)(market, ETH-USD)(side, LONG)(size, 0.2150000000000000)(max_position_size, 0.2150000000000000)(leverage, 50.0000000000000000)(open_price, 2316.6000000000000000)(exit_price, 2315.5000000000000000)(realised_pnl, 0.0720660000000000)(realised_pnl_breakdown, trade_pnl=Decimal('-0...(created_time, 1776999967376)(exit_type, TRADE)(closed_time, 1777103741241)
2(id, 2047419314696355840)(account_id, 270571)(market, ETH-USD)(side, LONG)(size, 0.2150000000000000)(max_position_size, 0.2150000000000000)(leverage, 50.0000000000000000)(open_price, 2321.7000000000000000)(exit_price, 2327.3000000000000000)(realised_pnl, 1.3196460000000000)(realised_pnl_breakdown, trade_pnl=Decimal('1....(created_time, 1776977778492)(exit_type, TRADE)(closed_time, 1776996621824)
\n", + "
" + ], "text/plain": [ - "{'id': 2047411567531950080,\n", - " 'external_id': '970778360519119766032805910949664642915982435081116578514332451865869879614'}" + " 0 1 2 \\\n", + "0 (id, 2047947640758337536) (account_id, 270571) (market, ETH-USD) \n", + "1 (id, 2047512381621272576) (account_id, 270571) (market, ETH-USD) \n", + "2 (id, 2047419314696355840) (account_id, 270571) (market, ETH-USD) \n", + "\n", + " 3 4 \\\n", + "0 (side, SHORT) (size, 0.2160000000000000) \n", + "1 (side, LONG) (size, 0.2150000000000000) \n", + "2 (side, LONG) (size, 0.2150000000000000) \n", + "\n", + " 5 6 \\\n", + "0 (max_position_size, 0.2160000000000000) (leverage, 50.0000000000000000) \n", + "1 (max_position_size, 0.2150000000000000) (leverage, 50.0000000000000000) \n", + "2 (max_position_size, 0.2150000000000000) (leverage, 50.0000000000000000) \n", + "\n", + " 7 8 \\\n", + "0 (open_price, 2315.5000000000000000) (exit_price, 2308.6000000000000000) \n", + "1 (open_price, 2316.6000000000000000) (exit_price, 2315.5000000000000000) \n", + "2 (open_price, 2321.7000000000000000) (exit_price, 2327.3000000000000000) \n", + "\n", + " 9 \\\n", + "0 (realised_pnl, 1.4249250000000000) \n", + "1 (realised_pnl, 0.0720660000000000) \n", + "2 (realised_pnl, 1.3196460000000000) \n", + "\n", + " 10 \\\n", + "0 (realised_pnl_breakdown, trade_pnl=Decimal('1.... \n", + "1 (realised_pnl_breakdown, trade_pnl=Decimal('-0... \n", + "2 (realised_pnl_breakdown, trade_pnl=Decimal('1.... \n", + "\n", + " 11 12 \\\n", + "0 (created_time, 1777103741241) (exit_type, TRADE) \n", + "1 (created_time, 1776999967376) (exit_type, TRADE) \n", + "2 (created_time, 1776977778492) (exit_type, TRADE) \n", + "\n", + " 13 \n", + "0 (closed_time, 1777133049026) \n", + "1 (closed_time, 1777103741241) \n", + "2 (closed_time, 1776996621824) " ] }, - "execution_count": 61, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dict(d)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3e4cedd1", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'OK'" - ] - }, - "execution_count": 58, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dict(placed_order).get('status' \\\n", - "'')" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Timestamp('2026-04-23 23:55:13.704000')" - ] - }, - "execution_count": 4, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", - "pd.to_datetime(1776988513704, unit='ms')" + "pd.DataFrame(list(dict(d)['data']))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e119aaac", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Timestamp('2026-04-25 16:04:09.026000')" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "pd.to_datetime(1777133049026, unit='ms')" ] }, { diff --git a/main.py b/main.py index 9b55f9f..e00d1d7 100644 --- a/main.py +++ b/main.py @@ -442,7 +442,7 @@ async def run_algo(): --- EXTEND OPEN ORDERS --- {EXTEND_OPEN_ORDERS} ''') - if ALGO_CONFIG.print_summary_each_loop: + if ALGO_CONFIG.Print_Summary_Each_Loop: print_summary() # print_summary() diff --git a/modules/__pycache__/structs.cpython-313.pyc b/modules/__pycache__/structs.cpython-313.pyc index 5b7fd84..248e47f 100644 Binary files a/modules/__pycache__/structs.cpython-313.pyc and b/modules/__pycache__/structs.cpython-313.pyc differ diff --git a/modules/structs.py b/modules/structs.py index a286ec0..dd16a42 100644 --- a/modules/structs.py +++ b/modules/structs.py @@ -16,6 +16,8 @@ class Algo_Config: Price_Worsener_Aster: float Price_Worsener_Extend: float Target_Open_Cash_Position: int + + Print_Summary_Each_Loop: bool = False @dataclass(kw_only=True) class Flags: