Jelajahi Sumber

fix(策略): 限制账户信息更新频率并清理调试代码

添加上次账户更新时间戳字段,确保至少间隔1秒才更新账户信息
移除main函数中的调试代码和冗余的订单参数打印
skyfffire 1 Minggu lalu
induk
melakukan
63ba622572
1 mengubah file dengan 21 tambahan dan 36 penghapusan
  1. 21 36
      src/record/strategy.py

+ 21 - 36
src/record/strategy.py

@@ -42,6 +42,7 @@ class TradingStrategy:
         self.entry_price_bps = 5        # 入场时的价差
         self.target_symbol = "DOGE"     # 目标交易对
         self.account_info = None        # 存储账户信息
+        self.last_account_update_time = 0  # 上次更新账户信息的时间戳
 
         self.account_index = 318163
         self.api_key_index = 0
@@ -82,17 +83,20 @@ class TradingStrategy:
         if not market_data:
             return
         
-        # 每次执行时更新账户信息
-        try:
-            account_response = await self.account_api.account(by="index", value=f"{self.account_index}")
-            if account_response.code == 200:
-                self.account_info = account_response
-                # logger.info(f"账户信息更新成功: 可用余额={account_response.accounts[0].available_balance}, 总资产={account_response.accounts[0].total_asset_value}")
-            else:
-                logger.warning(f"账户信息查询失败: code={account_response.code}, message={account_response.message}")
-        except Exception as e:
-            logger.error(f"查询账户信息时出错: {str(e)}")
-            return
+        # 更新账户信息,但至少间隔1秒
+        current_time = time.time()
+        if current_time - self.last_account_update_time >= 1.0:  # 确保至少间隔1秒
+            try:
+                account_response = await self.account_api.account(by="index", value=f"{self.account_index}")
+                if account_response.code == 200:
+                    self.account_info = account_response
+                    self.last_account_update_time = current_time  # 更新时间戳
+                    # logger.info(f"账户信息更新成功: 可用余额={account_response.accounts[0].available_balance}, 总资产={account_response.accounts[0].total_asset_value}")
+                else:
+                    logger.warning(f"账户信息查询失败: code={account_response.code}, message={account_response.message}")
+            except Exception as e:
+                logger.error(f"查询账户信息时出错: {str(e)}")
+                return
         
         symbol = market_data.get('symbol')
         
@@ -172,10 +176,10 @@ class TradingStrategy:
 
 async def main():    
     strategy = TradingStrategy()
-    account = await strategy.account_api.account(by="index", value=f"{strategy.account_index}")
+    # account = await strategy.account_api.account(by="index", value=f"{strategy.account_index}")
 
     # [AccountPosition(market_id=3, symbol='DOGE', initial_margin_fraction='10.00', open_order_count=0, pending_order_count=0, position_tied_order_count=0, sign=1, position='1', avg_entry_price='0.194368', position_value='0.194360', unrealized_pnl='-0.000008', realized_pnl='0.000000', liquidation_price='0', total_funding_paid_out=None, margin_mode=0, allocated_margin='0.000000', additional_properties={})]
-    print(account.accounts[0].positions)
+    # print(account.accounts[0].positions)
 
     doge_market = {
         "symbol": "DOGE",
@@ -192,28 +196,10 @@ async def main():
         "supported_quote_decimals": 6
     }
 
-    next_nonce = await strategy.transaction_api.next_nonce(account_index=strategy.account_index, api_key_index=strategy.api_key_index)
-    nonce_value = next_nonce.nonce
-
     base_amount = int(1 * (10 ** doge_market['supported_size_decimals']))
-    avg_execution_price = int(0.190000 * (10 ** doge_market['supported_price_decimals']))
-    
-    # 打印所有参数
-    client_order_index = int(time.time() * 1000)
-    print("=== 创建订单参数 ===")
-    print(f"market_index: {doge_market['market_id']}")
-    print(f"client_order_index: {client_order_index}")
-    print(f"base_amount: {base_amount}")
-    print(f"price: {avg_execution_price}")
-    print(f"is_ask: True")
-    print(f"order_type: {strategy.signer_client.ORDER_TYPE_MARKET}")
-    print(f"time_in_force: {strategy.signer_client.ORDER_TIME_IN_FORCE_IMMEDIATE_OR_CANCEL}")
-    print(f"reduce_only: True")
-    print(f"trigger_price: 0")
-    print(f"nonce: {nonce_value}")
-    print(f"order_expiry: 0")
-    print("==================")
+    avg_execution_price = int(0.180000 * (10 ** doge_market['supported_price_decimals']))
     
+    client_order_index = int(time.time() * 1000)    
     tx_info, error = strategy.signer_client.sign_create_order(
         market_index=doge_market["market_id"],
         client_order_index=client_order_index,
@@ -224,7 +210,6 @@ async def main():
         time_in_force=strategy.signer_client.ORDER_TIME_IN_FORCE_IMMEDIATE_OR_CANCEL,
         reduce_only=True,
         trigger_price=0,
-        nonce=nonce_value,
         order_expiry=0, # 所有市价单(包括减仓市价单)都必须使用NilOrderExpiry (0)
     )
     if error is not None:
@@ -232,8 +217,8 @@ async def main():
         return
     
     print(tx_info)
-    tx_hash = await strategy.transaction_api.send_tx(strategy.signer_client.TX_TYPE_CREATE_ORDER, tx_info)
-    print(tx_hash)
+    # tx_responnse = await strategy.transaction_api.send_tx(strategy.signer_client.TX_TYPE_CREATE_ORDER, tx_info)
+    # print(tx_responnse)
 
 if __name__ == '__main__':
     import asyncio