You bid ₹102 a share. You paid ₹101 for eight of them.
transcript
A buyer offers 102 rupees a share, for 10 shares. On eight of them, they pay 101. Here's why.
Three sellers are waiting. S1 offered 5 shares at 101. First. S2 offered 3 at 101. Same price, arrived second. S3 offered 10 at 102.
Best price first: 101. Oldest there first: S1. Five shares. Then S2, same price, arrived second. Three shares. 101 is empty. Next best: 102. Two from S3. Filled. S3 keeps 8, still first in its line.
Trade at the buyer's price? All ten at 102. A classic mistake. Every trade is at the resting order's price. The sellers who offered better got filled at what they offered.
Making that fast takes three structures. A dictionary finds a price level. A heap keeps the best price on top. A queue per level keeps arrival order. Oldest at the front.
And prices are integers, in paise. Add 0 point 1 to 101 point 1 in floats. You don't get 101 point 2. A level splits in two.
Best price first. Oldest first within a price. And every trade at the resting order's price.
Full breakdown in the lecture.