Skip to content

Commit

Permalink
DEX-849 fixed orderbook bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniya Tsybrenko committed Apr 21, 2022
1 parent 4d66837 commit 9d053ab
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,21 @@ case class WsOrderBookState(

def updatePrevTickState(asks: Map[Price, Amount], bids: Map[Price, Amount], lt: Option[LastTrade], lc: Option[Double]): WsState =
prevTickState.copy(
asks = applyLevelChanges(prevTickState.asks, asks),
bids = applyLevelChanges(prevTickState.bids, bids),
asks = (prevTickState.asks ++ asks).filter(_._2 != 0),
bids = (prevTickState.bids ++ bids).filter(_._2 != 0),
lastTrade = if (lt.isEmpty) prevTickState.lastTrade else lt,
tickSize = if (lc.isEmpty) prevTickState.tickSize else lc
)

def applyLevelChanges(m1: TreeMap[Price, Amount], m2: Map[Price, Amount]): TreeMap[Price, Amount] = (m1 ++ m2.map {
def updatePrevTickStateFromDiff(asks: Map[Price, Amount], bids: Map[Price, Amount], lt: Option[LastTrade], lc: Option[Double]): WsState =
prevTickState.copy(
asks = applyLevelDiff(prevTickState.asks, asks),
bids = applyLevelDiff(prevTickState.bids, bids),
lastTrade = if (lt.isEmpty) prevTickState.lastTrade else lt,
tickSize = if (lc.isEmpty) prevTickState.tickSize else lc
)

def applyLevelDiff(m1: TreeMap[Price, Amount], m2: Map[Price, Amount]): TreeMap[Price, Amount] = (m1 ++ m2.map {
case (pr, am) =>
(pr, m1.get(pr).fold(am)(_ + am))
}).filter(_._2 != 0)
Expand Down Expand Up @@ -123,7 +131,7 @@ case class WsOrderBookState(
)(this)
else
copy(
prevTickState = updatePrevTickState(lc.asks, lc.bids, lt, ts)
prevTickState = updatePrevTickStateFromDiff(lc.asks, lc.bids, lt, ts)
)

}
Expand Down

0 comments on commit 9d053ab

Please sign in to comment.