diff --git a/algo.ipynb b/algo.ipynb index 023d012..4486d98 100644 --- a/algo.ipynb +++ b/algo.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "id": "d1eed397", "metadata": {}, "outputs": [], @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "id": "c6151613", "metadata": {}, "outputs": [], @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 31, "id": "d83c61e5", "metadata": {}, "outputs": [ @@ -38,16 +38,32 @@ "1" ] }, - "execution_count": 3, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "config_update = {'Min_Time_To_Funding_Minutes': 7}\n", + "config_update = {\n", + " 'Min_Time_To_Funding_Minutes': 60,\n", + " 'Print_Summary_Each_Loop': True,\n", + " 'Allow_Ordering_Aster': True,\n", + " 'Allow_Ordering_Extend': True,\n", + " 'Loop_Sleep_Sec': 0.0,\n", + " 'Flip_Side_For_Testing': False,\n", + " 'Price_Worsener_Extend': 0.0,\n", + "}\n", "VAL_KEY.publish('fr_orchestrator_input', json.dumps(config_update))" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d2fdd7d2", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": 11, @@ -71,24 +87,26 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Config_Updated_Timestamp': 1777098091913,\n", - " 'Allow_Ordering_Aster': True,\n", - " 'Allow_Ordering_Extend': True,\n", + " 'Allow_Ordering_Aster': False,\n", + " 'Allow_Ordering_Extend': False,\n", " 'Loop_Sleep_Sec': 1,\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}" + " 'Target_Open_Cash_Position': 10,\n", + " 'Print_Summary_Each_Loop': False,\n", + " 'Flip_Side_For_Testing': False}" ] }, - "execution_count": 16, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } diff --git a/algo_config.json b/algo_config.json index acee321..e2a9366 100644 --- a/algo_config.json +++ b/algo_config.json @@ -2,11 +2,12 @@ "Config_Updated_Timestamp": 1777098091913, "Allow_Ordering_Aster": true, "Allow_Ordering_Extend": true, - "Loop_Sleep_Sec": 1, + "Loop_Sleep_Sec": 0.00, "Max_Target_Notional": 0.00, - "Min_Time_To_Funding_Minutes": 60, + "Min_Time_To_Funding_Minutes": 10, "Price_Worsener_Aster": 0.0, "Price_Worsener_Extend": 0.0, "Target_Open_Cash_Position": 10, - "Print_Summary_Each_Loop" : false + "Print_Summary_Each_Loop" : false, + "Flip_Side_For_Testing": false } \ No newline at end of file diff --git a/aster.ipynb b/aster.ipynb index 266323c..1a60523 100644 --- a/aster.ipynb +++ b/aster.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "3a269644", "metadata": {}, "outputs": [], @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "id": "4395fabb", "metadata": {}, "outputs": [], @@ -82,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "id": "2122885a", "metadata": {}, "outputs": [], @@ -92,7 +92,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "id": "e895ac52", "metadata": {}, "outputs": [ @@ -100,23 +100,23 @@ "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", + " 'positionAmt': '-0.214',\n", + " 'entryPrice': '2327.21',\n", + " 'markPrice': '2326.46000000',\n", + " 'unRealizedProfit': '0.16050000',\n", + " 'liquidationPrice': '2444.20070305',\n", " 'leverage': '150',\n", " 'maxNotionalValue': '300000',\n", " 'marginType': 'cross',\n", " 'isolatedMargin': '0.00000000',\n", " 'isAutoAddMargin': 'false',\n", " 'positionSide': 'BOTH',\n", - " 'notional': '-498.47106083',\n", + " 'notional': '-497.86244000',\n", " 'isolatedWallet': '0',\n", - " 'updateTime': 1777000243527}]" + " 'updateTime': 1777182273777}]" ] }, - "execution_count": 10, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -125,6 +125,80 @@ "j" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5de2d25", + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "6de7227b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "216.842" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r = requests.get('https://fapi.asterdex.com/fapi/v3/ping')\n", + "r.elapsed.total_seconds()*1000" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "d70b81ab", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "818.9119999999999" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r = requests.get('https://api.starknet.extended.exchange/api/v1/info/markets/ETH-USD/trades')\n", + "r.elapsed.total_seconds()*1000" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'{\"status\":\"OK\",\"data\":[{\"i\":2048281060801777676,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183234774,\"p\":\"2329.5\",\"q\":\"0.011\"},{\"i\":2048281060801777677,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183234774,\"p\":\"2329.6\",\"q\":\"0.500\"},{\"i\":2048281062441750529,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183235165,\"p\":\"2329.7\",\"q\":\"0.010\"},{\"i\":2048281062441750530,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183235165,\"p\":\"2329.7\",\"q\":\"0.146\"},{\"i\":2048281069555290113,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183236861,\"p\":\"2329.6\",\"q\":\"0.147\"},{\"i\":2048281092531687424,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183242339,\"p\":\"2329.7\",\"q\":\"0.012\"},{\"i\":2048281428902285317,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183322536,\"p\":\"2329.7\",\"q\":\"0.266\"},{\"i\":2048281428927451138,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183322542,\"p\":\"2329.7\",\"q\":\"0.092\"},{\"i\":2048281434937888768,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183323975,\"p\":\"2329.8\",\"q\":\"0.250\"},{\"i\":2048281434942083072,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183323976,\"p\":\"2329.8\",\"q\":\"0.250\"},{\"i\":2048281434942083073,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183323976,\"p\":\"2329.8\",\"q\":\"0.084\"},{\"i\":2048281434942083074,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183323976,\"p\":\"2329.8\",\"q\":\"0.058\"},{\"i\":2048281448279969795,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183327156,\"p\":\"2330.0\",\"q\":\"0.110\"},{\"i\":2048281448477102080,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183327203,\"p\":\"2330.0\",\"q\":\"0.390\"},{\"i\":2048281462658043904,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183330584,\"p\":\"2330.2\",\"q\":\"0.500\"},{\"i\":2048281462658043905,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183330584,\"p\":\"2330.2\",\"q\":\"0.868\"},{\"i\":2048281616119238656,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183367172,\"p\":\"2330.0\",\"q\":\"0.500\"},{\"i\":2048281710952452097,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183389782,\"p\":\"2329.8\",\"q\":\"0.169\"},{\"i\":2048281718711914496,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183391632,\"p\":\"2329.8\",\"q\":\"0.102\"},{\"i\":2048281768477331457,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183403497,\"p\":\"2329.8\",\"q\":\"0.407\"},{\"i\":2048281768519274496,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183403507,\"p\":\"2329.8\",\"q\":\"1.672\"},{\"i\":2048281773514690563,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183404698,\"p\":\"2329.6\",\"q\":\"0.500\"},{\"i\":2048281853550399489,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183423780,\"p\":\"2329.6\",\"q\":\"0.090\"},{\"i\":2048281858856194048,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183425045,\"p\":\"2329.6\",\"q\":\"0.010\"},{\"i\":2048281867026698240,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183426993,\"p\":\"2329.5\",\"q\":\"0.011\"},{\"i\":2048281904020459520,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183435813,\"p\":\"2329.4\",\"q\":\"0.500\"},{\"i\":2048281904074985480,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183435826,\"p\":\"2329.3\",\"q\":\"0.043\"},{\"i\":2048281904074985481,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183435826,\"p\":\"2329.3\",\"q\":\"0.043\"},{\"i\":2048281946777194496,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183446007,\"p\":\"2329.2\",\"q\":\"0.259\"},{\"i\":2048281958982619136,\"m\":\"ETH-USD\",\"S\":\"SELL\",\"tT\":\"TRADE\",\"T\":1777183448917,\"p\":\"2329.2\",\"q\":\"0.241\"},{\"i\":2048282097155575814,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183481860,\"p\":\"2329.4\",\"q\":\"0.500\"},{\"i\":2048282097155575815,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183481860,\"p\":\"2329.4\",\"q\":\"0.056\"},{\"i\":2048282097575006208,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183481960,\"p\":\"2329.6\",\"q\":\"0.500\"},{\"i\":2048282097575006209,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183481960,\"p\":\"2329.7\",\"q\":\"0.046\"},{\"i\":2048282097575006210,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183481960,\"p\":\"2329.8\",\"q\":\"0.500\"},{\"i\":2048282097575006211,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183481960,\"p\":\"2329.8\",\"q\":\"0.223\"},{\"i\":2048282097575006212,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183481960,\"p\":\"2329.8\",\"q\":\"0.062\"},{\"i\":2048282099558912002,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183482433,\"p\":\"2329.9\",\"q\":\"0.078\"},{\"i\":2048282099558912003,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183482433,\"p\":\"2329.9\",\"q\":\"0.373\"},{\"i\":2048282132433866756,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183490271,\"p\":\"2329.9\",\"q\":\"0.368\"},{\"i\":2048282132719079424,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183490339,\"p\":\"2329.9\",\"q\":\"1.200\"},{\"i\":2048282175693918208,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183500585,\"p\":\"2329.9\",\"q\":\"0.906\"},{\"i\":2048282176276926464,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183500724,\"p\":\"2329.9\",\"q\":\"0.150\"},{\"i\":2048282177048678400,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183500908,\"p\":\"2329.9\",\"q\":\"1.295\"},{\"i\":2048282177048678401,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183500908,\"p\":\"2329.9\",\"q\":\"3.775\"},{\"i\":2048282177048678402,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183500908,\"p\":\"2329.9\",\"q\":\"0.030\"},{\"i\":2048282177048678403,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183500908,\"p\":\"2329.9\",\"q\":\"0.032\"},{\"i\":2048282190487228416,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183504112,\"p\":\"2330.0\",\"q\":\"0.113\"},{\"i\":2048282212599599106,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183509384,\"p\":\"2330.0\",\"q\":\"0.387\"},{\"i\":2048282212872228864,\"m\":\"ETH-USD\",\"S\":\"BUY\",\"tT\":\"TRADE\",\"T\":1777183509449,\"p\":\"2330.1\",\"q\":\"0.493\"}]}'" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r.text" + ] + }, { "cell_type": "code", "execution_count": 7, diff --git a/main.py b/main.py index e00d1d7..82b09dc 100644 --- a/main.py +++ b/main.py @@ -229,6 +229,8 @@ async def kill_algo(): ### ALGO LOOP ### async def run_algo(): global ALGO_CONFIG + global ASTER_OPEN_ORDERS + global EXTEND_OPEN_ORDERS try: while True: @@ -246,8 +248,14 @@ async def run_algo(): ASTER_FUND_RATE = float(ASTER_FUND_RATE_DICT.get('funding_rate', 0)) EXTEND_FUND_RATE = float(EXTENDED_FUND_RATE_DICT.get('funding_rate', 0)) + + if ALGO_CONFIG.Flip_Side_For_Testing: + ASTER_FUND_RATE = ASTER_FUND_RATE * -1 + EXTEND_FUND_RATE = EXTEND_FUND_RATE * -1 + ASTER_FUND_RATE_TIME = float(ASTER_FUND_RATE_DICT.get('next_funding_time_ts_ms', 0)) EXTEND_FUND_RATE_TIME = float(EXTENDED_FUND_RATE_DICT.get('next_funding_time_ts_ms', 0)) + EXTEND_FUND_RATE_TIME = max([EXTEND_FUND_RATE_TIME, 0]) ASTER_TICKER_DICT = json.loads(VAL_KEY.get('fut_ticker_aster')) EXTENDED_TICKER_DICT = json.loads(VAL_KEY.get('fut_ticker_extended')) @@ -290,11 +298,13 @@ async def run_algo(): if len(EXTEND_WS_POS_UPDATES) > 0: await get_extend_notional(resp=EXTEND_WS_POS_UPDATES) + # await get_extend_notional() # ************** NOT USING WEBSOCKET FEED DUE TO ISSUES WITH IT OVERWRITING API DATA ie the WS just statically shows last update and doesnt pull new when you start the algo. + ### Also WS was just stale and caused issues where it sees a fill then gets new API Collateral (correct) and then the next loop would be the old incorrect collateral in the WS, causing bad orders. Do not have issue on ASTER. if ASTER_WS_ORDER_UPDATES is not None: for idx, o in enumerate(ASTER_OPEN_ORDERS): - order_id = o.get('order_id') if o.get('order_id') is not None else o.get('orderId') - order_orig_status = o['status'] + order_id = o.get('order_id') if o.get('order_id') is not None else o['orderId'] + order_orig_status = o['status'] ### Got a keyerror on this order_update = [ou for ou in ASTER_WS_ORDER_UPDATES if ou.get('order_id', None) == order_id] if len(order_update) > 0: @@ -349,9 +359,6 @@ async def run_algo(): else: logging.critical(f'EXTEND ORDER STATUS CHG TO UNEXPECTED VALUE, KILLING... ({order_id}): {order_orig_status} -> {order_update_status}') - - ASTER_PAYOUT_DIRECTION_STR = 'LONG PAYS SHORT' if ASTER_FUND_RATE > 0 else 'SHORT PAYS LONG' - EXTEND_PAYOUT_DIRECTION_STR = 'LONG PAYS SHORT' if EXTEND_FUND_RATE > 0 else 'SHORT PAYS LONG' min_between_fundings = round((abs(ASTER_FUND_RATE_TIME - EXTEND_FUND_RATE_TIME) / 1000 / 60)) FUNDINGS_AT_SAME_TIME_NEXT_HR = min_between_fundings < 5 @@ -382,7 +389,6 @@ async def run_algo(): Flags.NET_FUNDING_IS_ZERO = NEXT_NET_FUNDING_RATE == 0.00 if Flags.NET_FUNDING_IS_ZERO: logging.info('NET FUNDING = 0.00; Cancelling Open Orders; Wait Until Non-Zero.') - ALPHA_TGT_NOTIONAL = 0.00 if ALPHA_EXCH == 'EXTEND': @@ -420,9 +426,10 @@ async def run_algo(): OUT: print | logging.info = logging.info if use_logging else print OUT(f''' + FLIP SIDES FOR TESTING?: {ALGO_CONFIG.Flip_Side_For_Testing} {pd.to_datetime(ASTER_FUND_RATE_TIME, unit='ms')} ({(pd.to_datetime(ASTER_FUND_RATE_TIME, unit='ms')-datetime.now()):}) | {pd.to_datetime(EXTEND_FUND_RATE_TIME, unit='ms')} ({(pd.to_datetime(EXTEND_FUND_RATE_TIME, unit='ms')-datetime.now()):}) ASTER: {ASTER_FUND_RATE:.6%} [{ASTER_FUND_RATE*10_000:.2f}bps] [{ASTER_FUND_RATE*1_000_000:.0f}pips] | EXTEND: {EXTEND_FUND_RATE:.6%} [{EXTEND_FUND_RATE*10_000:.2f}bps] [{EXTEND_FUND_RATE*1_000_000:.0f}pips] - ASTER: {ASTER_PAYOUT_DIRECTION_STR} | EXTEND: {EXTEND_PAYOUT_DIRECTION_STR} + ASTER: {'LONG PAYS SHORT' if ASTER_FUND_RATE > 0 else 'SHORT PAYS LONG'} | EXTEND: {'LONG PAYS SHORT' if EXTEND_FUND_RATE > 0 else 'SHORT PAYS LONG'} ASTER: [ Available Collateral: {ASTER_AVAIL_COLLATERAL:.4f} ] | EXTEND: [ Available Collateral: {EXTEND_AVAIL_COLLATERAL:.4f} ] ASTER: [ Notional Position $ : {ASTER_NOTIONAL_POSITION:.4f} ] | EXTEND: [ Notional Position $ : {EXTEND_NOTIONAL_POSITION:.4f} ] @@ -508,7 +515,8 @@ async def run_algo(): logging.info(f'ASTER ORDER PLACED SUCCESS: {order_resp}') print_summary(use_logging=True) else: - logging.warning('ASTER PLACE ORDER CHECKS FAILED, SKIPPING') + pass + # logging.warning('ASTER PLACE ORDER CHECKS FAILED, SKIPPING') elif not(ASTER_TGT_TAIL_ORDERABLE) and ASTER_OPEN_ORDERS: logging.info('ASTER HAS NO TAIL BUT OPEN ORDERS - CANCELLING OPEN ORDERS') @@ -528,7 +536,16 @@ async def run_algo(): open_order_dict = dict(EXTEND_OPEN_ORDERS[0]) open_order_id = open_order_dict['external_id'] open_order_px = float(open_order_dict['price']) - place_order = True + open_order_filled_qty = float(open_order_dict['filled_qty']) + + # qty = abs(float(qty)) - abs(float(open_order_filled_qty)) # Was trying to account for partial fills but thats not necessary, handled by position change so qty is correct w/o further adj. + # qty = Decimal(str(qty)) + + if qty >= MAX_MIN_ORDER_QTY: + place_order = True + else: + place_order = False + logging.info(f'EXTEND NOT ORDERING DUE TO FILLED QTY RESIDUAL < MIN ORDER; Filled: {float(open_order_filled_qty):.4f}; Residual: {qty:.4f}') else: open_order_id = None open_order_px = 0 @@ -538,15 +555,22 @@ async def run_algo(): if round(open_order_px - float(price), 2) == 0.00: logging.info('EXTEND OPEN ORDER NO PX CHG; SKIPPING') else: - order_resp = await EXTEND_CLIENT.place_order( - market_name=symbol, - amount_of_synthetic=qty, - price=price, - side=side, - taker_fee=Decimal("0.00025"), - previous_order_id=open_order_id, - ) + try: + order_resp = await EXTEND_CLIENT.place_order( + market_name=symbol, + amount_of_synthetic=qty, + price=price, + side=side, + taker_fee=Decimal("0.00025"), + previous_order_id=open_order_id, + ) + except Exception as e: + logging.error(f'EXTEND ORDER PLACEMENT FAILED - RESP: {order_resp}') + logging.error(f'EXTEND ORDER PLACEMENT FAILED: {e}') + logging.error(traceback.format_exc()) + order_resp_dict = dict(order_resp) + if order_resp_dict.get('status', None) == 'OK': if EXTEND_OPEN_ORDERS: EXTEND_OPEN_ORDERS.pop(0) @@ -554,11 +578,15 @@ async def run_algo(): order_dict = dict(order_resp_dict['data']) order_dict['status'] = 'NEW' order_dict['price'] = str(price) + order_dict['qty'] = str(qty) + order_dict['filled_qty'] = str(0) EXTEND_OPEN_ORDERS.append(order_dict) utils.send_tg_alert(f'FR_ALGO - EXTEND Order. Start_$: {EXTEND_NOTIONAL_POSITION:.2f}; Value: {float(EXTEND_TGT_TAIL_BASE_QTY)*float(price):.2f}; Price: {float(price):.2f}') logging.info(f'EXTEND ORDER PLACED SUCCESS: {order_dict}') print_summary(use_logging=True) + else: + order_resp_dict.get else: logging.warning('EXTEND PLACE ORDER CHECKS FAILED, SKIPPING') @@ -595,6 +623,8 @@ async def main(): with open('algo_config.json', 'r', encoding='utf-8') as file: ALGO_CONFIG = json.load(file, object_hook=lambda d: structs.Algo_Config(**d)) ALGO_CONFIG.Max_Target_Notional = float(min([ASTER_MULT, EXTEND_MULT]) * ALGO_CONFIG.Target_Open_Cash_Position) + + VAL_KEY.set('fr_orchestrator_output', json.dumps(asdict(ALGO_CONFIG))) async with engine.connect() as CON: ### ASTER SETUP ### diff --git a/modules/__pycache__/structs.cpython-313.pyc b/modules/__pycache__/structs.cpython-313.pyc index 248e47f..52b646d 100644 Binary files a/modules/__pycache__/structs.cpython-313.pyc and b/modules/__pycache__/structs.cpython-313.pyc differ diff --git a/modules/db.py b/modules/db.py index 83080d6..1755510 100644 --- a/modules/db.py +++ b/modules/db.py @@ -20,7 +20,6 @@ async def insert_df_to_mysql( df = pd.DataFrame(params) else: df = params - print(f'DB INSERT: table: {table_name}; CON: {CON}; params: {params}') await CON.run_sync( lambda sync_conn: df.to_sql(name=table_name, con=sync_conn, if_exists='append', index=False) ) diff --git a/modules/structs.py b/modules/structs.py index dd16a42..0d55047 100644 --- a/modules/structs.py +++ b/modules/structs.py @@ -18,6 +18,7 @@ class Algo_Config: Target_Open_Cash_Position: int Print_Summary_Each_Loop: bool = False + Flip_Side_For_Testing: bool = False @dataclass(kw_only=True) class Flags: diff --git a/ws_extended_user.py b/ws_extended_user.py index 69154ac..2ce5a54 100644 --- a/ws_extended_user.py +++ b/ws_extended_user.py @@ -193,7 +193,7 @@ async def ws_stream(): 'updated_at_ts': p['updatedAt'], } list_for_df.append(position_update) - LOCAL_RECENT_POSITIONS = utils.upsert_list_of_dicts_by_id(LOCAL_RECENT_POSITIONS, position_update, id='position_id', seq_check_field='sequence_id') + LOCAL_RECENT_POSITIONS = utils.upsert_list_of_dicts_by_id(LOCAL_RECENT_POSITIONS, position_update, id='market', seq_check_field='sequence_id') LOCAL_RECENT_POSITIONS = [t for t in LOCAL_RECENT_POSITIONS if t.get('timestamp_arrival', 0) >= LOOKBACK_MIN_TS_MS] VAL_KEY_OBJ = json.dumps(LOCAL_RECENT_POSITIONS)