MCPcopy Create free account
hub / github.com/NoFxAiOS/nofx / CreateOpenPosition

Method CreateOpenPosition

store/position.go:502–546  ·  view source on GitHub ↗

CreateOpenPosition creates an open position

(pos *TraderPosition)

Source from the content-addressed store, hash-verified

500
501// CreateOpenPosition creates an open position
502func (s *PositionStore) CreateOpenPosition(pos *TraderPosition) error {
503 if pos.ExchangePositionID != "" && pos.ExchangeID != "" {
504 existingPos, err := s.GetOpenPositionByExchangePositionID(pos.ExchangeID, pos.ExchangePositionID)
505 if err != nil {
506 return err
507 }
508 if existingPos != nil {
509 return s.UpdatePositionQuantityAndPrice(existingPos.ID, pos.Quantity, pos.EntryPrice, pos.Fee)
510 }
511 exists, err := s.ExistsWithExchangePositionID(pos.ExchangeID, pos.ExchangePositionID)
512 if err != nil {
513 return err
514 }
515 if exists {
516 return nil
517 }
518 }
519
520 if pos.Status == "" {
521 pos.Status = "OPEN"
522 }
523 if pos.Source == "" {
524 pos.Source = "system"
525 }
526 if pos.EntryQuantity == 0 {
527 pos.EntryQuantity = pos.Quantity
528 }
529
530 err := s.db.Create(pos).Error
531 if err != nil {
532 if strings.Contains(err.Error(), "UNIQUE constraint failed") {
533 existingPos, findErr := s.GetOpenPositionByExchangePositionID(pos.ExchangeID, pos.ExchangePositionID)
534 if findErr != nil {
535 return findErr
536 }
537 if existingPos != nil {
538 return s.UpdatePositionQuantityAndPrice(existingPos.ID, pos.Quantity, pos.EntryPrice, pos.Fee)
539 }
540 return nil
541 }
542 return fmt.Errorf("failed to create open position: %w", err)
543 }
544
545 return nil
546}
547
548// ClosePositionWithAccurateData closes a position with accurate data from exchange
549// exitTimeMs is Unix milliseconds UTC

Callers 2

CreatePositionSnapshotFunction · 0.80
handleOpenMethod · 0.80

Calls 6

ErrorfMethod · 0.65
CreateMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected