@@ -362,9 +362,8 @@ pub fn load_state() -> BybitResult<FuturesPaperState> {
362362 ) ) ;
363363 }
364364 let contents = fs:: read_to_string ( & path) ?;
365- let state: FuturesPaperState = serde_json:: from_str ( & contents) . map_err ( |e| {
366- BybitError :: Parse ( format ! ( "Failed to parse futures paper state: {e}" ) )
367- } ) ?;
365+ let state: FuturesPaperState = serde_json:: from_str ( & contents)
366+ . map_err ( |e| BybitError :: Parse ( format ! ( "Failed to parse futures paper state: {e}" ) ) ) ?;
368367 Ok ( state)
369368}
370369
@@ -469,11 +468,7 @@ impl FuturesPaperState {
469468 ( self . collateral + self . unrealized_pnl ( mark_prices) - self . used_margin ( ) ) . max ( 0.0 )
470469 }
471470
472- pub fn resolve_leverage (
473- & self ,
474- order_leverage : Option < f64 > ,
475- symbol : & str ,
476- ) -> BybitResult < f64 > {
471+ pub fn resolve_leverage ( & self , order_leverage : Option < f64 > , symbol : & str ) -> BybitResult < f64 > {
477472 let lev = order_leverage
478473 . or_else ( || self . leverage_preferences . get ( symbol) . copied ( ) )
479474 . unwrap_or ( DEFAULT_LEVERAGE ) ;
@@ -575,9 +570,9 @@ impl FuturesPaperState {
575570 market : & MarketSnapshot ,
576571 mark_prices : & HashMap < String , f64 > ,
577572 ) -> BybitResult < OrderPlacementResult > {
578- let price = params. price . ok_or_else ( || {
579- BybitError :: Paper ( "Limit orders require --price" . to_string ( ) )
580- } ) ?;
573+ let price = params
574+ . price
575+ . ok_or_else ( || BybitError :: Paper ( "Limit orders require --price" . to_string ( ) ) ) ?;
581576 validate_finite_positive ( price, "price" ) ?;
582577
583578 // Check if immediately fillable
@@ -682,9 +677,9 @@ impl FuturesPaperState {
682677 leverage : f64 ,
683678 market : & MarketSnapshot ,
684679 ) -> BybitResult < OrderPlacementResult > {
685- let price = params. price . ok_or_else ( || {
686- BybitError :: Paper ( "Post orders require --price" . to_string ( ) )
687- } ) ?;
680+ let price = params
681+ . price
682+ . ok_or_else ( || BybitError :: Paper ( "Post orders require --price" . to_string ( ) ) ) ?;
688683 validate_finite_positive ( price, "price" ) ?;
689684
690685 // Post-only: cancel if would immediately cross
@@ -699,13 +694,16 @@ impl FuturesPaperState {
699694 order_id,
700695 status : OrderStatus :: Cancelled ,
701696 fills : vec ! [ ] ,
702- message : Some ( "Post-only order would cross the spread and was cancelled." . to_string ( ) ) ,
697+ message : Some (
698+ "Post-only order would cross the spread and was cancelled." . to_string ( ) ,
699+ ) ,
703700 } ) ;
704701 }
705702
706703 let reserved_margin = params. size * price / leverage;
707- let mark_prices: HashMap < String , f64 > =
708- [ ( params. symbol . to_uppercase ( ) , market. mark ) ] . into_iter ( ) . collect ( ) ;
704+ let mark_prices: HashMap < String , f64 > = [ ( params. symbol . to_uppercase ( ) , market. mark ) ]
705+ . into_iter ( )
706+ . collect ( ) ;
709707 let available = self . available_margin ( & mark_prices) ;
710708 if !params. reduce_only && available < reserved_margin {
711709 return Err ( BybitError :: Paper ( format ! (
@@ -753,9 +751,9 @@ impl FuturesPaperState {
753751 leverage : f64 ,
754752 market : & MarketSnapshot ,
755753 ) -> BybitResult < OrderPlacementResult > {
756- let price = params. price . ok_or_else ( || {
757- BybitError :: Paper ( "IOC orders require --price" . to_string ( ) )
758- } ) ?;
754+ let price = params
755+ . price
756+ . ok_or_else ( || BybitError :: Paper ( "IOC orders require --price" . to_string ( ) ) ) ?;
759757 validate_finite_positive ( price, "price" ) ?;
760758
761759 let fillable = match params. side {
@@ -769,7 +767,9 @@ impl FuturesPaperState {
769767 order_id,
770768 status : OrderStatus :: Cancelled ,
771769 fills : vec ! [ ] ,
772- message : Some ( "IOC order could not fill immediately and was cancelled." . to_string ( ) ) ,
770+ message : Some (
771+ "IOC order could not fill immediately and was cancelled." . to_string ( ) ,
772+ ) ,
773773 } ) ;
774774 }
775775
@@ -778,8 +778,9 @@ impl FuturesPaperState {
778778 Side :: Short => market. bid . max ( price) ,
779779 } ;
780780
781- let mark_prices: HashMap < String , f64 > =
782- [ ( params. symbol . to_uppercase ( ) , market. mark ) ] . into_iter ( ) . collect ( ) ;
781+ let mark_prices: HashMap < String , f64 > = [ ( params. symbol . to_uppercase ( ) , market. mark ) ]
782+ . into_iter ( )
783+ . collect ( ) ;
783784 let available = self . available_margin ( & mark_prices) ;
784785 let required_margin = params. size * fill_price / leverage;
785786 if !params. reduce_only && available < required_margin {
@@ -820,9 +821,9 @@ impl FuturesPaperState {
820821 leverage : f64 ,
821822 market : & MarketSnapshot ,
822823 ) -> BybitResult < OrderPlacementResult > {
823- let price = params. price . ok_or_else ( || {
824- BybitError :: Paper ( "FOK orders require --price" . to_string ( ) )
825- } ) ?;
824+ let price = params
825+ . price
826+ . ok_or_else ( || BybitError :: Paper ( "FOK orders require --price" . to_string ( ) ) ) ?;
826827 validate_finite_positive ( price, "price" ) ?;
827828
828829 let fillable = match params. side {
@@ -836,7 +837,9 @@ impl FuturesPaperState {
836837 order_id,
837838 status : OrderStatus :: Cancelled ,
838839 fills : vec ! [ ] ,
839- message : Some ( "FOK order could not fill in full immediately and was cancelled." . to_string ( ) ) ,
840+ message : Some (
841+ "FOK order could not fill in full immediately and was cancelled." . to_string ( ) ,
842+ ) ,
840843 } ) ;
841844 }
842845
@@ -869,8 +872,9 @@ impl FuturesPaperState {
869872 Side :: Short => market. bid . max ( price) ,
870873 } ;
871874
872- let mark_prices: HashMap < String , f64 > =
873- [ ( params. symbol . to_uppercase ( ) , market. mark ) ] . into_iter ( ) . collect ( ) ;
875+ let mark_prices: HashMap < String , f64 > = [ ( params. symbol . to_uppercase ( ) , market. mark ) ]
876+ . into_iter ( )
877+ . collect ( ) ;
874878 let available = self . available_margin ( & mark_prices) ;
875879 let required_margin = params. size * fill_price / leverage;
876880 if !params. reduce_only && available < required_margin {
@@ -912,10 +916,7 @@ impl FuturesPaperState {
912916 mark_prices : & HashMap < String , f64 > ,
913917 ) -> BybitResult < OrderPlacementResult > {
914918 let stop_price = params. stop_price . ok_or_else ( || {
915- BybitError :: Paper ( format ! (
916- "{} orders require --stop-price" ,
917- params. order_type
918- ) )
919+ BybitError :: Paper ( format ! ( "{} orders require --stop-price" , params. order_type) )
919920 } ) ?;
920921 validate_finite_positive ( stop_price, "stop_price" ) ?;
921922
@@ -933,7 +934,8 @@ impl FuturesPaperState {
933934 . unwrap_or ( "percent" ) ;
934935 if unit != "percent" && unit != "quote_currency" {
935936 return Err ( BybitError :: Paper (
936- "trailing_stop_deviation_unit must be 'percent' or 'quote_currency'" . to_string ( ) ,
937+ "trailing_stop_deviation_unit must be 'percent' or 'quote_currency'"
938+ . to_string ( ) ,
937939 ) ) ;
938940 }
939941 }
@@ -1080,8 +1082,7 @@ impl FuturesPaperState {
10801082 . iter ( )
10811083 . position ( |o| {
10821084 order_id. is_some_and ( |id| o. id == id)
1083- || client_order_id
1084- . is_some_and ( |cid| o. client_order_id . as_deref ( ) == Some ( cid) )
1085+ || client_order_id. is_some_and ( |cid| o. client_order_id . as_deref ( ) == Some ( cid) )
10851086 } )
10861087 . ok_or_else ( || {
10871088 BybitError :: Paper (
@@ -1203,9 +1204,7 @@ impl FuturesPaperState {
12031204 event_type : "realized_pnl" . to_string ( ) ,
12041205 symbol : Some ( symbol. to_string ( ) ) ,
12051206 amount : pnl,
1206- details : format ! (
1207- "Closed {side} position: {size} @ {price:.4}, PnL = {pnl:.4}"
1208- ) ,
1207+ details : format ! ( "Closed {side} position: {size} @ {price:.4}, PnL = {pnl:.4}" ) ,
12091208 timestamp : now_rfc3339 ( ) ,
12101209 } ;
12111210 self . history . push ( event) ;
@@ -1299,12 +1298,7 @@ impl FuturesPaperState {
12991298 } ) ;
13001299 }
13011300
1302- fn validate_reduce_only (
1303- & self ,
1304- symbol : & str ,
1305- side : Side ,
1306- size : f64 ,
1307- ) -> BybitResult < ( ) > {
1301+ fn validate_reduce_only ( & self , symbol : & str , side : Side , size : f64 ) -> BybitResult < ( ) > {
13081302 let pos = self
13091303 . positions
13101304 . iter ( )
@@ -1339,17 +1333,8 @@ impl FuturesPaperState {
13391333 let mut result = ReconcileResult :: default ( ) ;
13401334
13411335 self . reconcile_limit_orders ( last_prices, mark_prices, & mut result. fills ) ;
1342- self . reconcile_triggered_orders (
1343- mark_prices,
1344- last_prices,
1345- index_prices,
1346- & mut result. fills ,
1347- ) ;
1348- self . reconcile_liquidations (
1349- mark_prices,
1350- maintenance_rates,
1351- & mut result. liquidations ,
1352- ) ;
1336+ self . reconcile_triggered_orders ( mark_prices, last_prices, index_prices, & mut result. fills ) ;
1337+ self . reconcile_liquidations ( mark_prices, maintenance_rates, & mut result. liquidations ) ;
13531338 self . reconcile_funding ( funding_rates, mark_prices, & mut result. funding_events ) ;
13541339
13551340 self . last_reconciled_at = Some ( now_rfc3339 ( ) ) ;
@@ -1711,10 +1696,7 @@ pub fn compute_unrealized_pnl(pos: &FuturesPaperPosition, mark_price: f64) -> f6
17111696 compute_realized_pnl ( pos. side , pos. entry_price , mark_price, pos. size )
17121697}
17131698
1714- pub fn compute_liquidation_price (
1715- pos : & FuturesPaperPosition ,
1716- maintenance_margin_rate : f64 ,
1717- ) -> f64 {
1699+ pub fn compute_liquidation_price ( pos : & FuturesPaperPosition , maintenance_margin_rate : f64 ) -> f64 {
17181700 if pos. entry_price <= 0.0 || pos. leverage <= 0.0 {
17191701 return f64:: INFINITY ;
17201702 }
@@ -1835,9 +1817,7 @@ pub async fn fetch_market_snapshot(
18351817 . as_array ( )
18361818 . and_then ( |a| a. first ( ) )
18371819 . cloned ( )
1838- . ok_or_else ( || {
1839- BybitError :: Paper ( format ! ( "No ticker data for symbol {symbol_upper}" ) )
1840- } ) ?;
1820+ . ok_or_else ( || BybitError :: Paper ( format ! ( "No ticker data for symbol {symbol_upper}" ) ) ) ?;
18411821
18421822 let parse_f = |v : & Value | -> f64 {
18431823 v. as_str ( )
0 commit comments