FEATURE: [sync] Sync futures positions in DB#2506
Conversation
| `open_order_initial_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `adl` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `margin_asset` VARCHAR(20) NOT NULL DEFAULT '', | ||
| `update_time` DATETIME(3) NOT NULL, |
| `maint_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `position_initial_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `open_order_initial_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `adl` DECIMAL(16, 8) NOT NULL DEFAULT 0, |
There was a problem hiding this comment.
do we need 8 precision for just ADL?
There was a problem hiding this comment.
Fixed.
It's now just 2 precision for ADL.
| `initial_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `maint_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `position_initial_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `open_order_initial_margin` DECIMAL(16, 8) NOT NULL DEFAULT 0, |
There was a problem hiding this comment.
for usd based unit, do we really need 8 fraction numbers?
There was a problem hiding this comment.
Fixed. It's 2 now.
| `mark_price` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `break_even_price` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `position_amount` DECIMAL(16, 8) NOT NULL DEFAULT 0, | ||
| `unrealized_pnl` DECIMAL(16, 8) NOT NULL DEFAULT 0, |
… support database interactions
d4481c7 to
23eefa2
Compare
| `unrealized_pnl` DECIMAL(16, 2) NOT NULL DEFAULT 0, | ||
| `notional` DECIMAL(16, 2) NOT NULL DEFAULT 0, | ||
| `initial_margin` DECIMAL(16, 2) NOT NULL DEFAULT 0, | ||
| `maint_margin` DECIMAL(16, 2) NOT NULL DEFAULT 0, |
There was a problem hiding this comment.
sqlite doesn't support DECIMAL(16, 2), just DECIMAL i think, you can use AI to fix your query
There was a problem hiding this comment.
Opps, I didn't notice that.
This query is generated by AI.
Will fix it.
There was a problem hiding this comment.
Can you add the process as a Claude skill, like add-migration? so that we can reuse it
There was a problem hiding this comment.
done.
Please refer to .claude/skills/add-migration.
| } | ||
| } | ||
|
|
||
| futuresPositionWroterCreator := func(session *ExchangeSession) func(types.Trade) { |
| if environ.SyncService == nil || environ.SyncService.FuturesPositionRiskService == nil { | ||
| return | ||
| } | ||
|
|
||
| service, ok := session.Exchange.(types.ExchangeRiskService) | ||
| if !ok { | ||
| return | ||
| } | ||
| if !(trade.IsFutures || trade.IsIsolated) { | ||
| return | ||
| } |
There was a problem hiding this comment.
these checks can be done before setting this closure i think
you only need this callback for futures session
| return | ||
| } | ||
|
|
||
| lastTradeTime = trade.Time.Time() |
There was a problem hiding this comment.
since you need to store the lastTradeTime variable, I would prefer you pull out this code, and add it in the FuturesService as a method like "QueryAndInsert(exchange, ...)", pass the exchange instance to the sync service to query and insert
| orderWriter := orderWriterCreator(s2) | ||
| session.UserDataStream.OnOrderUpdate(orderWriter) | ||
| } | ||
| if config.UserDataStream.FuturesPosition { |
There was a problem hiding this comment.
move these lines of code ( https://github.com/c9s/bbgo/pull/2506/changes#diff-906fed76a73bf1656df5b830d57f22714231b84461c634668336b13a54eb6552R434-R444 ) to this condition.
| MarginService: environ.MarginService, | ||
| WithdrawService: &service.WithdrawService{DB: db}, | ||
| DepositService: &service.DepositService{DB: db}, | ||
| FuturesPositionRiskService: &service.FuturesPositionRiskService{DB: db}, |
There was a problem hiding this comment.
maybe just name it FuturesService to include all futures related sync methods, or you have to add more service for other futures api
|
|
||
| func (s *FuturesPositionRiskService) Sync( | ||
| ctx context.Context, | ||
| exchange types.Exchange, symbol string, |
There was a problem hiding this comment.
use type types.ExchangeRiskService for the parameter, force the api caller to convert the object into this interface to call this method.
| func (s *FuturesPositionRiskService) Sync( | ||
| ctx context.Context, | ||
| exchange types.Exchange, symbol string, | ||
| startTime, endTime time.Time, |
There was a problem hiding this comment.
If they are not being used in the current moment, please remove them.
5ed5c13 to
fd60860
Compare
9ccb98e to
44eac99
Compare
44eac99 to
9d38c04
Compare
driver.ValuerandScannerinterface fortypes.MillisecondTimestamp