github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v1/positions.go (about)

     1  package bitfinex
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  )
     7  
     8  // PositionsService structure
     9  type PositionsService struct {
    10  	client *Client
    11  }
    12  
    13  // Position structure
    14  type Position struct {
    15  	ID        int
    16  	Symbol    string
    17  	Amount    string
    18  	Status    string
    19  	Base      string
    20  	Timestamp string
    21  	Swap      string
    22  	Pl        string
    23  }
    24  
    25  func (p *Position) ParseTime() (*time.Time, error) {
    26  	i, err := strconv.ParseFloat(p.Timestamp, 64)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	t := time.Unix(int64(i), 0)
    31  	return &t, nil
    32  }
    33  
    34  // All - gets all positions
    35  func (b *PositionsService) All() ([]Position, error) {
    36  	req, err := b.client.newAuthenticatedRequest("POST", "positions", nil)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	var positions []Position
    42  	_, err = b.client.do(req, &positions)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return positions, nil
    48  }
    49  
    50  // Claim a position
    51  func (b *PositionsService) Claim(positionId int, amount string) (Position, error) {
    52  
    53  	request := map[string]interface{}{
    54  		"position_id": positionId,
    55  		"amount":      amount,
    56  	}
    57  
    58  	req, err := b.client.newAuthenticatedRequest("POST", "position/claim", request)
    59  
    60  	if err != nil {
    61  		return Position{}, err
    62  	}
    63  
    64  	var position = &Position{}
    65  
    66  	_, err = b.client.do(req, position)
    67  
    68  	if err != nil {
    69  		return Position{}, err
    70  	}
    71  
    72  	return *position, nil
    73  }