@@ -702,13 +702,6 @@ impl OrderParams {
702702 }
703703 . cast :: < i64 > ( ) ?;
704704
705- let baseline_start_price_offset_slow = mark_twap_slow. safe_sub (
706- perp_market
707- . amm
708- . historical_oracle_data
709- . last_oracle_price_twap ,
710- ) ?;
711-
712705 let baseline_start_price_offset_fast = perp_market
713706 . amm
714707 . last_mark_price_twap_5min
@@ -720,27 +713,42 @@ impl OrderParams {
720713 . last_oracle_price_twap_5min ,
721714 ) ?;
722715
723- let frac_of_long_spread_in_price : i64 = perp_market
724- . amm
725- . long_spread
726- . cast :: < i64 > ( ) ?
727- . safe_mul ( mark_twap_slow ) ?
728- . safe_div ( PRICE_PRECISION_I64 * 10 ) ?;
716+ let baseline_start_price_offset_slow = mark_twap_slow . safe_sub (
717+ perp_market
718+ . amm
719+ . historical_oracle_data
720+ . last_oracle_price_twap ,
721+ ) ?;
729722
730- let frac_of_short_spread_in_price: i64 = perp_market
731- . amm
732- . short_spread
733- . cast :: < i64 > ( ) ?
734- . safe_mul ( mark_twap_slow) ?
735- . safe_div ( PRICE_PRECISION_I64 * 10 ) ?;
736-
737- let baseline_start_price_offset = match direction {
738- PositionDirection :: Long => baseline_start_price_offset_slow
739- . safe_add ( frac_of_long_spread_in_price) ?
740- . min ( baseline_start_price_offset_fast. safe_sub ( frac_of_short_spread_in_price) ?) ,
741- PositionDirection :: Short => baseline_start_price_offset_slow
742- . safe_sub ( frac_of_short_spread_in_price) ?
743- . max ( baseline_start_price_offset_fast. safe_add ( frac_of_long_spread_in_price) ?) ,
723+ let baseline_start_price_offset = if baseline_start_price_offset_slow
724+ . abs_diff ( baseline_start_price_offset_fast)
725+ < perp_market. amm . last_mark_price_twap_5min / 200
726+ {
727+ let frac_of_long_spread_in_price: i64 = perp_market
728+ . amm
729+ . long_spread
730+ . cast :: < i64 > ( ) ?
731+ . safe_mul ( mark_twap_slow) ?
732+ . safe_div ( PRICE_PRECISION_I64 * 10 ) ?;
733+
734+ let frac_of_short_spread_in_price: i64 = perp_market
735+ . amm
736+ . short_spread
737+ . cast :: < i64 > ( ) ?
738+ . safe_mul ( mark_twap_slow) ?
739+ . safe_div ( PRICE_PRECISION_I64 * 10 ) ?;
740+
741+ match direction {
742+ PositionDirection :: Long => baseline_start_price_offset_slow
743+ . safe_add ( frac_of_long_spread_in_price) ?
744+ . min ( baseline_start_price_offset_fast. safe_sub ( frac_of_short_spread_in_price) ?) ,
745+ PositionDirection :: Short => baseline_start_price_offset_slow
746+ . safe_sub ( frac_of_short_spread_in_price) ?
747+ . max ( baseline_start_price_offset_fast. safe_add ( frac_of_long_spread_in_price) ?) ,
748+ }
749+ } else {
750+ // more than 50bps different of fast/slow twap, use fast only
751+ baseline_start_price_offset_fast
744752 } ;
745753
746754 Ok ( baseline_start_price_offset)
0 commit comments