Skip to content

Commit 573e88d

Browse files
0xhhh321321saniales
authored andcommitted
SellMarket should use bids and BuyMarket should use asks
1 parent 8385d73 commit 573e88d

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

exchanges/exchange_mock.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ func (wrapper *ExchangeWrapperSimulator) BuyMarket(market *environment.Market, a
7272
remainingAmount := decimal.NewFromFloat(amount)
7373
expense := decimal.Zero
7474

75-
for _, bid := range orderbook.Bids {
76-
if remainingAmount.LessThanOrEqual(bid.Quantity) {
75+
for _, ask := range orderbook.Asks {
76+
if remainingAmount.LessThanOrEqual(ask.Quantity) {
7777
totalQuote = totalQuote.Add(remainingAmount)
78-
expense = expense.Add(remainingAmount.Mul(bid.Value))
78+
expense = expense.Add(remainingAmount.Mul(ask.Value))
7979
if expense.GreaterThan(*baseBalance) {
8080
return "", fmt.Errorf("cannot Buy not enough %s balance", market.BaseCurrency)
8181
}
8282
break
8383
}
84-
totalQuote = totalQuote.Add(bid.Quantity)
85-
expense = expense.Add(bid.Quantity.Mul(bid.Value))
84+
totalQuote = totalQuote.Add(ask.Quantity)
85+
expense = expense.Add(bid.Quantity.Mul(ask.Value))
8686
if expense.GreaterThan(*baseBalance) {
8787
return "", fmt.Errorf("cannot Buy not enough %s balance", market.BaseCurrency)
8888
}
@@ -116,14 +116,14 @@ func (wrapper *ExchangeWrapperSimulator) SellMarket(market *environment.Market,
116116
return "", fmt.Errorf("Cannot Sell: not enough %s balance", market.MarketCurrency)
117117
}
118118

119-
for _, ask := range orderbook.Asks {
120-
if remainingAmount.LessThanOrEqual(ask.Quantity) {
119+
for _, bid := range orderbook.Bids {
120+
if remainingAmount.LessThanOrEqual(bid.Quantity) {
121121
totalQuote = totalQuote.Add(remainingAmount)
122-
gain = gain.Add(remainingAmount.Mul(ask.Value))
122+
gain = gain.Add(remainingAmount.Mul(bid.Value))
123123
break
124124
}
125-
totalQuote = totalQuote.Add(ask.Quantity)
126-
gain = gain.Add(ask.Quantity.Mul(ask.Value))
125+
totalQuote = totalQuote.Add(bid.Quantity)
126+
gain = gain.Add(ask.Quantity.Mul(bid.Value))
127127
}
128128

129129
wrapper.balances[market.BaseCurrency] = baseBalance.Add(gain)

0 commit comments

Comments
 (0)