@@ -195,7 +195,6 @@ impl Market {
195195 #[ must_use]
196196 pub fn merge_trade < H , TS , C , T , P , V , R > (
197197 & self ,
198- ts : TS ,
199198 half_days : H ,
200199 period : Period ,
201200 input : Option < C > ,
@@ -204,17 +203,22 @@ impl Market {
204203 ) -> UpdateAction < C >
205204 where
206205 H : Days ,
207- TS : TradeSessionType ,
206+ TS : TradeSessionType + Eq ,
208207 C : CandlestickType < PriceType = P , VolumeType = V , TurnoverType = R , TradeSessionType = TS > ,
209- T : TradeType < PriceType = P , VolumeType = V , TurnoverType = R > ,
208+ T : TradeType < PriceType = P , VolumeType = V , TurnoverType = R , TradeSessionType = TS > ,
210209 P : PartialOrd + Add < Output = P > ,
211210 V : Add < Output = V > + Zero ,
212211 R : Add < Output = R > + Zero ,
213212 {
213+ let trade_session = trade. trade_session ( ) ;
214+
214215 debug_assert ! ( period != Period :: Day ) ;
216+ if let Some ( input_trade_session) = input. as_ref ( ) . map ( |c| c. trade_session ( ) ) {
217+ debug_assert ! ( input_trade_session == trade_session) ;
218+ }
215219
216220 let Some ( time) = self . candlestick_time (
217- ts ,
221+ trade_session ,
218222 half_days,
219223 period,
220224 trade. time ( ) . to_timezone ( self . timezone ) ,
@@ -260,7 +264,7 @@ impl Market {
260264 close : trade. price ( ) ,
261265 volume : trade. volume ( ) ,
262266 turnover : trade. turnover ( self . lot_size ) ,
263- trade_session : ts ,
267+ trade_session,
264268 } ) ;
265269 UpdateAction :: AppendNew {
266270 confirmed : None ,
@@ -279,7 +283,7 @@ impl Market {
279283 close : prev. close ( ) ,
280284 volume : V :: zero ( ) ,
281285 turnover : R :: zero ( ) ,
282- trade_session : ts ,
286+ trade_session,
283287 } ) ;
284288
285289 if update_fields. contains ( UpdateFields :: PRICE ) {
@@ -306,23 +310,31 @@ impl Market {
306310 #[ must_use]
307311 pub fn merge_quote < H , TS , C , Q , P , V , R > (
308312 & self ,
309- ts : TS ,
310313 half_days : H ,
311314 period : Period ,
312315 input : Option < C > ,
313316 quote : & Q ,
314317 ) -> UpdateAction < C >
315318 where
316319 H : Days ,
317- TS : TradeSessionType ,
320+ TS : TradeSessionType + Eq ,
318321 C : CandlestickType < PriceType = P , VolumeType = V , TurnoverType = R , TradeSessionType = TS > ,
319- Q : QuoteType < PriceType = P , VolumeType = V , TurnoverType = R > ,
322+ Q : QuoteType < PriceType = P , VolumeType = V , TurnoverType = R , TradeSessionType = TS > ,
320323 {
324+ let trade_session = quote. trade_session ( ) ;
325+
321326 debug_assert ! ( period == Period :: Day ) ;
327+ if let Some ( input_trade_session) = input. as_ref ( ) . map ( |c| c. trade_session ( ) ) {
328+ debug_assert ! ( input_trade_session == trade_session) ;
329+ }
322330
323331 let tz = self . timezone ;
324- let Some ( time) = self . candlestick_time ( ts, half_days, period, quote. time ( ) . to_timezone ( tz) )
325- else {
332+ let Some ( time) = self . candlestick_time (
333+ trade_session,
334+ half_days,
335+ period,
336+ quote. time ( ) . to_timezone ( tz) ,
337+ ) else {
326338 return UpdateAction :: None ;
327339 } ;
328340
@@ -336,7 +348,7 @@ impl Market {
336348 close : quote. last_done ( ) ,
337349 volume : quote. volume ( ) ,
338350 turnover : quote. turnover ( ) ,
339- trade_session : ts ,
351+ trade_session,
340352 } ) )
341353 }
342354 None => UpdateAction :: AppendNew {
@@ -349,7 +361,7 @@ impl Market {
349361 close : quote. last_done ( ) ,
350362 volume : quote. volume ( ) ,
351363 turnover : quote. turnover ( ) ,
352- trade_session : ts ,
364+ trade_session,
353365 } ) ,
354366 } ,
355367 Some ( prev) if time > prev. time ( ) => UpdateAction :: AppendNew {
@@ -362,93 +374,14 @@ impl Market {
362374 close : quote. last_done ( ) ,
363375 volume : quote. volume ( ) ,
364376 turnover : quote. turnover ( ) ,
365- trade_session : ts ,
377+ trade_session,
366378 } ) ,
367379 } ,
368380 _ => UpdateAction :: None ,
369381 }
370382 }
371383
372- #[ must_use]
373- pub fn merge_candlestick < H , C > (
374- & self ,
375- period : Period ,
376- input : Option < C > ,
377- candlestick : & C ,
378- ) -> UpdateAction < C >
379- where
380- H : Days ,
381- C : CandlestickType ,
382- C :: PriceType : PartialOrd ,
383- C :: VolumeType : Add < Output = C :: VolumeType > ,
384- C :: TurnoverType : Add < Output = C :: TurnoverType > ,
385- {
386- let Some ( time) = self . candlestick_time (
387- candlestick. trade_session ( ) ,
388- false ,
389- period,
390- candlestick. time ( ) ,
391- ) else {
392- return UpdateAction :: None ;
393- } ;
394-
395- match input {
396- Some ( prev) if time == prev. time ( ) => {
397- let mut res_candlestick = prev;
398-
399- res_candlestick. set_high ( if candlestick. high ( ) > res_candlestick. high ( ) {
400- candlestick. high ( )
401- } else {
402- res_candlestick. high ( )
403- } ) ;
404-
405- res_candlestick. set_low ( if candlestick. low ( ) < res_candlestick. low ( ) {
406- candlestick. low ( )
407- } else {
408- res_candlestick. low ( )
409- } ) ;
410-
411- res_candlestick. set_close ( candlestick. close ( ) ) ;
412-
413- res_candlestick. set_volume ( candlestick. volume ( ) + res_candlestick. volume ( ) ) ;
414- res_candlestick. set_turnover ( candlestick. turnover ( ) + res_candlestick. turnover ( ) ) ;
415-
416- UpdateAction :: UpdateLast ( res_candlestick)
417- }
418- None => UpdateAction :: AppendNew {
419- confirmed : None ,
420- new : C :: new ( CandlestickComponents {
421- time : time. to_timezone ( time_tz:: timezones:: db:: UTC ) ,
422- open : candlestick. open ( ) ,
423- high : candlestick. high ( ) ,
424- low : candlestick. low ( ) ,
425- close : candlestick. close ( ) ,
426- volume : candlestick. volume ( ) ,
427- turnover : candlestick. turnover ( ) ,
428- trade_session : candlestick. trade_session ( ) ,
429- } ) ,
430- } ,
431- Some ( prev) if time > prev. time ( ) => UpdateAction :: AppendNew {
432- confirmed : Some ( prev) ,
433- new : C :: new ( CandlestickComponents {
434- time : time. to_timezone ( time_tz:: timezones:: db:: UTC ) ,
435- open : candlestick. open ( ) ,
436- high : candlestick. high ( ) ,
437- low : candlestick. low ( ) ,
438- close : candlestick. close ( ) ,
439- volume : candlestick. volume ( ) ,
440- turnover : candlestick. turnover ( ) ,
441- trade_session : candlestick. trade_session ( ) ,
442- } ) ,
443- } ,
444- _ => UpdateAction :: None ,
445- }
446- }
447-
448- pub fn candlestick_trade_session (
449- & self ,
450- candlestick_time : OffsetDateTime ,
451- ) -> Option < TradeSessionKind > {
384+ pub fn trade_session ( & self , candlestick_time : OffsetDateTime ) -> Option < TradeSessionKind > {
452385 let candlestick_time = candlestick_time. to_timezone ( self . timezone ) ;
453386 for ( idx, trade_sessions) in self . trade_sessions . iter ( ) . enumerate ( ) {
454387 for TradeSession {
0 commit comments