Skip to content

Conversation

dboyliao
Copy link
Collaborator

No description provided.

@dboyliao dboyliao requested a review from c9s as a code owner April 11, 2025 13:50
@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch 5 times, most recently from b0b1b01 to 6b8b815 Compare April 11, 2025 14:57
@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch from 6b8b815 to 008ae56 Compare April 11, 2025 15:05
@dboyliao dboyliao requested a review from c9s April 11, 2025 15:05
price, quantity fixedpoint.Value,
tradingMarket types.Market,
balances types.BalanceMap,
) (bool, string) {
Copy link
Owner

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

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

@dboyliao dboyliao Apr 15, 2025

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)

switch side {
case types.SideTypeSell:
balance := balances[baseCurrency]
if balance.Available.Compare(quantity) < 0 {
Copy link
Owner

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.

Copy link
Collaborator Author

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.

}

// GetStatus locks the mutex and return the position status
func (p *Position) GetStatus() PositionStatus {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

_, okBase := balances[baseCurrency]
_, okQuote := balances[quoteCurrency]
if !okBase || !okQuote {
return false, fmt.Sprintf("invalid currency paire: %s/%s", baseCurrency, quoteCurrency)
Copy link
Owner

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch 3 times, most recently from a2a204c to 1eef8dd Compare April 14, 2025 03:11
@dboyliao dboyliao requested a review from c9s April 14, 2025 03:11
@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch from 1eef8dd to b1e1e96 Compare April 14, 2025 03:32
@dboyliao dboyliao changed the title REFACTOR: [xgap] balance checks on orders and skip large quantity for making spread orders REFACTOR: [xgap] balance checks on order forms Apr 14, 2025
Market: s.tradingMarket,
TimeInForce: types.TimeInForceIOC,
}
if err := tradingutil.HasSufficientBalance(
Copy link
Owner

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

// 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(
Copy link
Owner

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch 3 times, most recently from 3331dea to 4079b7f Compare April 14, 2025 08:19
@dboyliao dboyliao requested a review from c9s April 14, 2025 08:20
@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch from 4079b7f to f33bc21 Compare April 15, 2025 02:16
Market: tradingMarket,
TimeInForce: types.TimeInForceIOC,
})
}
if ok, err := tradingutil.HasSufficientBalance(
Copy link
Owner

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

pv = bestBid
side = types.SideTypeSell
}
if tryPlaceOrder {
Copy link
Owner

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

Copy link
Collaborator Author

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.

@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch from f33bc21 to fdc210e Compare April 15, 2025 06:36
@dboyliao dboyliao requested a review from c9s April 15, 2025 06:36
@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch from fdc210e to e9e629a Compare April 15, 2025 07:39
@dboyliao dboyliao force-pushed the dboy/fix-xgap-adj-order branch from e9e629a to 044f676 Compare April 15, 2025 08:09
@dboyliao dboyliao enabled auto-merge April 15, 2025 08:15
@dboyliao dboyliao merged commit c3ccaad into main Apr 15, 2025
3 checks passed
@dboyliao dboyliao deleted the dboy/fix-xgap-adj-order branch April 15, 2025 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants