@@ -423,6 +423,101 @@ public virtual void LongFromShort(OrderTestParameters parameters)
423423 }
424424 }
425425
426+ /// <summary>
427+ /// Places a long order, updates it, and then cancels it. Verifies that each operation completes successfully.
428+ /// </summary>
429+ /// <param name="parameters">The parameters for creating and managing the order.</param>
430+ /// <param name="quantityIncrement">The increment to add to the order quantity during the update.</param>
431+ /// <param name="limitPriceIncrement">The increment to add to the order's limit price during the update.</param>
432+ /// <param name="stopPriceIncrement">The increment to add to the order's stop price during the update.</param>
433+ /// <exception cref="AssertFailedException">Thrown if the order fails to update or cancel as expected.</exception>
434+ public virtual void LongFromZeroUpdateAndCancel ( OrderTestParameters parameters , decimal quantityIncrement = 1 , decimal limitPriceIncrement = 0.01m , decimal stopPriceIncrement = 0.01m )
435+ {
436+ Log . Trace ( "" ) ;
437+ Log . Trace ( "LONG FROM ZERO THEN UPDATE AND CANCEL" ) ;
438+ Log . Trace ( "" ) ;
439+
440+ var order = PlaceOrderWaitForStatus ( parameters . CreateLongOrder ( GetDefaultQuantity ( ) ) , parameters . ExpectedStatus ) ;
441+
442+ using var updatedOrderStatusEvent = new AutoResetEvent ( false ) ;
443+ using var canceledOrderStatusEvent = new AutoResetEvent ( false ) ;
444+
445+ EventHandler < List < OrderEvent > > brokerageOnOrdersStatusChanged = ( _ , orderEvents ) =>
446+ {
447+ var eventOrderStatus = orderEvents [ 0 ] . Status ;
448+
449+ order . Status = eventOrderStatus ;
450+
451+ switch ( eventOrderStatus )
452+ {
453+ case OrderStatus . UpdateSubmitted :
454+ updatedOrderStatusEvent . Set ( ) ;
455+ break ;
456+ case OrderStatus . Canceled :
457+ canceledOrderStatusEvent . Set ( ) ;
458+ break ;
459+ }
460+ } ;
461+
462+ EventHandler < BrokerageOrderIdChangedEvent > brokerageOrderIdChanged = ( _ , args ) =>
463+ {
464+ order . BrokerId = args . BrokerId ;
465+ Log . Trace ( $ "ORDER ID CHANGED EVENT: Id = { args . OrderId } , BrokerageId = [{ string . Join ( ',' , args . BrokerId ) } ]") ;
466+ } ;
467+
468+ Brokerage . OrderIdChanged += brokerageOrderIdChanged ;
469+ Brokerage . OrdersStatusChanged += brokerageOnOrdersStatusChanged ;
470+
471+ var newQuantity = order . Quantity + quantityIncrement ;
472+
473+ decimal ? limitPrice = order switch
474+ {
475+ LimitOrder lo => lo . LimitPrice ,
476+ StopLimitOrder slo => slo . LimitPrice ,
477+ LimitIfTouchedOrder lito => lito . LimitPrice ,
478+ _ => null
479+ } ;
480+
481+ decimal ? stopPrice = order switch
482+ {
483+ StopMarketOrder smo => smo . StopPrice ,
484+ StopLimitOrder slo => slo . StopPrice ,
485+ _ => null
486+ } ;
487+
488+ decimal ? newLimitPrice = limitPrice . HasValue ? limitPrice . Value + limitPriceIncrement : null ;
489+ decimal ? newStopPrice = stopPrice . HasValue ? stopPrice . Value + stopPriceIncrement : null ;
490+
491+ Log . Trace ( "" ) ;
492+ Log . Trace ( $ "UPDATE ORDER FIELDS: \n " +
493+ $ " oldQuantity = { order . Quantity } , newQuantity = { newQuantity } \n " +
494+ $ " oldLimitPrice = { limitPrice } , newLimitPrice = { newLimitPrice } \n " +
495+ $ " oldStopPrice = { stopPrice } , newStopPrice = { newStopPrice } ") ;
496+ Log . Trace ( "" ) ;
497+
498+ var updateOrderFields = new UpdateOrderFields ( )
499+ {
500+ Quantity = newQuantity ,
501+ LimitPrice = newLimitPrice ,
502+ StopPrice = newStopPrice
503+ } ;
504+
505+ order . ApplyUpdateOrderRequest ( new UpdateOrderRequest ( DateTime . UtcNow , order . Id , updateOrderFields ) ) ;
506+
507+ if ( ! Brokerage . UpdateOrder ( order ) || ! updatedOrderStatusEvent . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) )
508+ {
509+ Assert . Fail ( "Order is not updated well." ) ;
510+ }
511+
512+ if ( ! Brokerage . CancelOrder ( order ) || ! canceledOrderStatusEvent . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) )
513+ {
514+ Assert . Fail ( "Order is not canceled well." ) ;
515+ }
516+
517+ Brokerage . OrderIdChanged -= brokerageOrderIdChanged ;
518+ Brokerage . OrdersStatusChanged -= brokerageOnOrdersStatusChanged ;
519+ }
520+
426521 [ Test ]
427522 public virtual void GetCashBalanceContainsSomething ( )
428523 {
0 commit comments