Skip to content
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

FIX: [exchange] fix bugs and add integration tests for Coinbase Exchange #1914

Merged
merged 6 commits into from
Feb 26, 2025

Conversation

dboyliao
Copy link
Collaborator

@dboyliao dboyliao commented Feb 26, 2025

截圖 2025-02-26 下午2 12 55

@dboyliao dboyliao requested review from c9s and bailantaotao February 26, 2025 01:47
default:
return nil, fmt.Errorf("unsupported time in force: %v", order.TimeInForce)
// set time in force, using default if not set
if len(order.TimeInForce) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you set a default timeInForce beforehand?

Copy link
Collaborator Author

@dboyliao dboyliao Feb 26, 2025

Choose a reason for hiding this comment

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

I want to leave it unset if it's not given by to user.
So it will fallback to Coinbase API defaults.
I want it to be consistent with Coinbase API.

@@ -308,25 +310,36 @@ func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval type
log.Warnf("limit %d is greater than the maximum limit 300, set to 300", options.Limit)
options.Limit = DefaultKLineLimit
}
granity := interval.String()
granity := fmt.Sprintf("%d", interval.Seconds())
Copy link
Collaborator

Choose a reason for hiding this comment

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

use string(interval) ?

Copy link
Collaborator Author

@dboyliao dboyliao Feb 26, 2025

Choose a reason for hiding this comment

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

I believe it's not the right way doing it.
string(interval) will be "1m" but the Coinbase API is expecting "60".

Copy link
Collaborator

Choose a reason for hiding this comment

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

how about renaming the varialbe "granity" to "granularity"?

Copy link
Collaborator Author

@dboyliao dboyliao Feb 26, 2025

Choose a reason for hiding this comment

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

Opps, my bad.
Misspelling.
Will fix it.

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.

Comment on lines 325 to 328
candles := make([]api.Candle, 0, len(res))
for _, c := range res {
candles = append(candles, *c.Candle())
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Most of us are accustomed to using pointers, for example make([]*api.Candle, 0, len(res)), so you don't have to dereference.

candles := make([]*api.Candle, 0, len(res))
	for _, c := range res {
		candles = append(candles, c.Candle())
	}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As your review for PR #1909, I will apply the error handling here for invalid raw candles.
In that case, I think we don't need to use pointers.

Comment on lines 338 to 339
if idx > 0 {
klines[idx-1].StartTime = kline.EndTime
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

如果你都帶入 granity,那這個 start time和 end time的計算應該可以維持原邏輯?如果coinbase回傳非granity的間隔,那應該很有問題...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

有道理
重看了一下 doc: https://docs.cdp.coinbase.com/exchange/reference/exchangerestapi_getproductcandles
API 回傳的應該是 start time
我應該用下一根的 start time 當 end time 才對~
當時寫的時候應該不小心搞反了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

喔 不對
我應該用 granity 換算才對

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/coinbase-test-fix branch from 29175d8 to 910e1c9 Compare February 26, 2025 03:11
Base automatically changed from dboy/coinbase-test to main February 26, 2025 05:58

klines := make([]types.KLine, 0, len(candles))
for _, candle := range candles {
kline := toGlobalKline(symbol, interval, &candle)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bbgo uses go 1.21.6. Before go 1.22, the variable between each for loop iteration is the same variable. That means the &candle always points to the same address unless you use go 1.22. Please ensure this behavior is expected.

https://go.dev/blog/loopvar-preview

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

截圖 2025-02-26 下午2 23 28
Since I do not keep the pointer (&candle) in the body of toGlobalKline(), I think it will do what is expected in go 1.21.6.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

截圖 2025-02-26 下午3 06 05
截圖 2025-02-26 下午3 06 14

I compile and run it with go1.21.6
As shown in the screenshot, there should be no issue where pointers outlive the loop.

@dboyliao dboyliao requested a review from ycdesu February 26, 2025 06:25
@dboyliao dboyliao requested review from gx578007 and removed request for c9s February 26, 2025 07:09
@dboyliao dboyliao merged commit 9432a58 into main Feb 26, 2025
3 checks passed
@dboyliao dboyliao deleted the dboy/coinbase-test-fix branch February 26, 2025 07:24
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.

4 participants