-
-
Notifications
You must be signed in to change notification settings - Fork 344
REFACTOR: [xgap] balance checks on order forms #1977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
b0b1b01
to
6b8b815
Compare
6b8b815
to
008ae56
Compare
pkg/util/tradingutil/orders.go
Outdated
price, quantity fixedpoint.Value, | ||
tradingMarket types.Market, | ||
balances types.BalanceMap, | ||
) (bool, string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the problem of this function you got too many arguments, better to avoid it
For side buy, you don't need Base asset balance, you only need Quote asset balance and the buy price to calculate the actual quantity.
For side sell, you only need Base asset balance, quote balance and price are not used for this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to have a unified way to check the balance when submitting an order with SubmitOrder
.
Maybe it will be better if I pass SubmitOrder
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated function signature:
func HasSufficientBalance(
tradingMarket types.Market,
submitOrder types.SubmitOrder,
balances types.BalanceMap,
) (bool, error)
pkg/util/tradingutil/orders.go
Outdated
switch side { | ||
case types.SideTypeSell: | ||
balance := balances[baseCurrency] | ||
if balance.Available.Compare(quantity) < 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If our goal is to place an order no matter what—or at least as reliably as possible—we shouldn’t just return an error.
I think this is actually by design, so we need to consider the real-world scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not thinking about a guarantee on placing order.
Instead, I'd like to leave it to the caller to decide what to do next if there is an error concerning the balances.
That's why I return an error here.
pkg/types/position.go
Outdated
} | ||
|
||
// GetStatus locks the mutex and return the position status | ||
func (p *Position) GetStatus() PositionStatus { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the issue lies in the need for a thread-safe way to access position-related information,
which is used to determine trading conditions.
Perhaps, just like we discussed offline,
renaming it to "snapshot" would make it easier to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I leave the core API unchanged and move the locking util function to a private module in xgap
folder instead.
pkg/util/tradingutil/orders.go
Outdated
_, okBase := balances[baseCurrency] | ||
_, okQuote := balances[quoteCurrency] | ||
if !okBase || !okQuote { | ||
return false, fmt.Sprintf("invalid currency paire: %s/%s", baseCurrency, quoteCurrency) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe return type error? and use fmt.Errorf() for the error message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
a2a204c
to
1eef8dd
Compare
1eef8dd
to
b1e1e96
Compare
pkg/strategy/xgap/strategy.go
Outdated
Market: s.tradingMarket, | ||
TimeInForce: types.TimeInForceIOC, | ||
} | ||
if err := tradingutil.HasSufficientBalance( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HasSufficientBalance sounds you will return bool, but it returns error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
pkg/strategy/xgap/strategy.go
Outdated
// When in a short position, it will try to place a buy order. | ||
// When in a long position, it will try to place a sell order. | ||
// It's based on the spread and the condition of the current position. | ||
func getAdjustPositionOrders( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are generating an new order, not returning an existing order, so using prefix "get" could be confusing
you may use "build" or "new" as the prefix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
3331dea
to
4079b7f
Compare
4079b7f
to
f33bc21
Compare
pkg/strategy/xgap/strategy.go
Outdated
Market: tradingMarket, | ||
TimeInForce: types.TimeInForceIOC, | ||
}) | ||
} | ||
if ok, err := tradingutil.HasSufficientBalance( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pull out this check to the caller
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
pkg/strategy/xgap/strategy.go
Outdated
pv = bestBid | ||
side = types.SideTypeSell | ||
} | ||
if tryPlaceOrder { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can check if the pv is assigned instead of introducing a new var
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
I add a .IsZero()
method instead.
f33bc21
to
fdc210e
Compare
fdc210e
to
e9e629a
Compare
e9e629a
to
044f676
Compare
No description provided.