@@ -91,40 +91,39 @@ def apply_grid_fill_to_local_state(
9191 market_type = str (tc .get ("market_type" ) or "swap" ),
9292 fill_source = "grid_poller" ,
9393 )
94- try :
95- profit , _pos , matched_entry = apply_fill_to_local_position (
96- strategy_id = int (strategy_id ),
97- symbol = sym ,
98- signal_type = signal_type ,
99- filled = qty ,
100- avg_price = px ,
101- leg = leg ,
102- )
103- if grid_profit is not None :
104- profit = grid_profit
105- matched_entry = grid_entry_price
106- record_trade (
107- strategy_id = int (strategy_id ),
108- symbol = sym ,
109- trade_type = signal_type ,
110- price = px ,
111- amount = qty ,
112- commission = float (commission or 0.0 ),
113- commission_ccy = str (commission_ccy or "" ),
114- commission_quote = commission_quote ,
115- profit = profit ,
116- close_reason = purpose ,
117- matched_entry_price = matched_entry ,
118- grid_matched_profit = profit if purpose in ("long_exit" , "short_exit" ) and profit is not None else None ,
119- leg = leg ,
120- exchange_fill_id = str (exchange_fill_id or "" ),
121- execution_event_id = int (execution_event_id or 0 ),
122- grid_order_id = int (order .id or 0 ),
123- fee_status = str (fee_status or "pending" ),
124- fee_source = str (fee_source or "" ),
125- )
126- except Exception as e :
127- logger .warning ("apply_grid_fill sid=%s: %s" , strategy_id , e )
94+ profit , _pos , matched_entry = apply_fill_to_local_position (
95+ strategy_id = int (strategy_id ),
96+ symbol = sym ,
97+ signal_type = signal_type ,
98+ filled = qty ,
99+ avg_price = px ,
100+ leg = leg ,
101+ )
102+ if grid_profit is not None :
103+ profit = grid_profit
104+ matched_entry = grid_entry_price
105+ record_trade (
106+ strategy_id = int (strategy_id ),
107+ symbol = sym ,
108+ trade_type = signal_type ,
109+ price = px ,
110+ amount = qty ,
111+ commission = float (commission or 0.0 ),
112+ commission_ccy = str (commission_ccy or "" ),
113+ commission_quote = commission_quote ,
114+ profit = profit ,
115+ close_reason = purpose ,
116+ matched_entry_price = matched_entry ,
117+ grid_matched_profit = (
118+ profit if purpose in ("long_exit" , "short_exit" ) and profit is not None else None
119+ ),
120+ leg = leg ,
121+ exchange_fill_id = str (exchange_fill_id or "" ),
122+ execution_event_id = int (execution_event_id or 0 ),
123+ grid_order_id = int (order .id or 0 ),
124+ fee_status = str (fee_status or "pending" ),
125+ fee_source = str (fee_source or "" ),
126+ )
128127
129128
130129def record_grid_market_fill (
@@ -162,56 +161,54 @@ def record_grid_market_fill(
162161 market_type = str (tc .get ("market_type" ) or "swap" ),
163162 fill_source = "grid_market" ,
164163 )
165- try :
166- profit , _pos , matched_entry = apply_fill_to_local_position (
167- strategy_id = int (strategy_id ),
168- symbol = sym ,
169- signal_type = sig ,
170- filled = qty ,
171- avg_price = px ,
172- leg = leg ,
173- )
174- trade_id = record_trade (
175- strategy_id = int (strategy_id ),
176- symbol = sym ,
177- trade_type = sig ,
178- price = px ,
179- amount = qty ,
180- commission = float (commission or 0.0 ),
181- commission_ccy = str (commission_ccy or "" ),
182- commission_quote = commission_quote ,
183- profit = profit ,
184- close_reason = str (reason or sig ),
185- matched_entry_price = matched_entry ,
186- leg = leg ,
187- fee_status = str (fee_status or "pending" ),
188- fee_source = str (fee_source or "rest" ),
189- )
190- if trade_id and (exchange_order_id or client_order_id ):
191- try :
192- from app .services .execution_streams .repository import ExecutionEventRepository
164+ profit , _pos , matched_entry = apply_fill_to_local_position (
165+ strategy_id = int (strategy_id ),
166+ symbol = sym ,
167+ signal_type = sig ,
168+ filled = qty ,
169+ avg_price = px ,
170+ leg = leg ,
171+ )
172+ trade_id = record_trade (
173+ strategy_id = int (strategy_id ),
174+ symbol = sym ,
175+ trade_type = sig ,
176+ price = px ,
177+ amount = qty ,
178+ commission = float (commission or 0.0 ),
179+ commission_ccy = str (commission_ccy or "" ),
180+ commission_quote = commission_quote ,
181+ profit = profit ,
182+ close_reason = str (reason or sig ),
183+ matched_entry_price = matched_entry ,
184+ leg = leg ,
185+ fee_status = str (fee_status or "pending" ),
186+ fee_source = str (fee_source or "rest" ),
187+ )
188+ if trade_id and (exchange_order_id or client_order_id ):
189+ try :
190+ from app .services .execution_streams .repository import ExecutionEventRepository
193191
194- ExecutionEventRepository ().register_binding (
195- credential_id = int (leg .credential_id or 0 ),
196- exchange_id = str (exchange_id or tc .get ("exchange_id" ) or tc .get ("exchange" ) or "" ),
197- market_type = leg .normalized_market_type (),
198- owner_type = "grid_market" ,
199- owner_id = int (trade_id ),
200- user_id = int (user_id or 1 ),
201- strategy_id = int (strategy_id ),
202- symbol = sym ,
203- signal_type = sig ,
204- client_order_id = str (client_order_id or "" ),
205- exchange_order_id = str (exchange_order_id or "" ),
206- observed_filled = qty ,
207- )
208- except Exception :
209- logger .debug (
210- "grid market execution binding failed trade_id=%s" ,
211- trade_id ,
212- exc_info = True ,
213- )
214- return int (trade_id or 0 )
215- except Exception as e :
216- logger .warning ("record_grid_market_fill sid=%s: %s" , strategy_id , e )
217- return 0
192+ ExecutionEventRepository ().register_binding (
193+ credential_id = int (leg .credential_id or 0 ),
194+ exchange_id = str (
195+ exchange_id or tc .get ("exchange_id" ) or tc .get ("exchange" ) or ""
196+ ),
197+ market_type = leg .normalized_market_type (),
198+ owner_type = "grid_market" ,
199+ owner_id = int (trade_id ),
200+ user_id = int (user_id or 1 ),
201+ strategy_id = int (strategy_id ),
202+ symbol = sym ,
203+ signal_type = sig ,
204+ client_order_id = str (client_order_id or "" ),
205+ exchange_order_id = str (exchange_order_id or "" ),
206+ observed_filled = qty ,
207+ )
208+ except Exception :
209+ logger .debug (
210+ "grid market execution binding failed trade_id=%s" ,
211+ trade_id ,
212+ exc_info = True ,
213+ )
214+ return int (trade_id or 0 )
0 commit comments