-
Notifications
You must be signed in to change notification settings - Fork 11
/
position.go
89 lines (78 loc) · 2.38 KB
/
position.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package swap
import (
"context"
"encoding/json"
"github.com/NadiaSama/ccexgo/exchange"
ccexgo "github.com/NadiaSama/ccexgo/exchange"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type (
Position struct {
Symbol string `json:"symbol"`
ContractCode string `json:"contract_code"`
Volume decimal.Decimal `json:"volume"`
Available decimal.Decimal `json:"available"`
Frozen decimal.Decimal `json:"frozen"`
CostOpen decimal.Decimal `json:"cost_open"`
CostHold decimal.Decimal `json:"cost_hold"`
ProfitUnreal decimal.Decimal `json:"profit_unreal"`
ProfitRate decimal.Decimal `json:"profit_rate"`
LeverRate int `json:"lever_rate"`
PositionMargin decimal.Decimal `json:"position_margin"`
Direction string `json:"direction"`
Profit decimal.Decimal `json:"profit"`
LastPrice decimal.Decimal `json:"last_price"`
}
PositionInfoRequest struct {
ContractCode string `json:"contract_code"`
}
)
const (
PositionInfoEndPoint = "/swap-api/v1/swap_position_info"
)
func NewPositionInfoRequest(code string) *PositionInfoRequest {
return &PositionInfoRequest{
ContractCode: code,
}
}
func (pir *PositionInfoRequest) Serialize() ([]byte, error) {
if pir.ContractCode != "" {
r, err := json.Marshal(pir)
return r, err
}
return nil, nil
}
func (rc *RestClient) PositionInfo(ctx context.Context, req *PositionInfoRequest) ([]Position, error) {
var ret []Position
if err := rc.PrivatePostReq(ctx, PositionInfoEndPoint, req, &ret); err != nil {
return nil, errors.WithMessage(err, "get position info fail")
}
return ret, nil
}
func (p *Position) Transfer() (*ccexgo.Position, error) {
sym, err := ParseSymbol(p.ContractCode)
if err != nil {
return nil, err
}
var side ccexgo.PositionSide
if p.Direction == OrderDirectionBuy {
side = ccexgo.PositionSideLong
} else if p.Direction == OrderDirectionSell {
side = ccexgo.PositionSideShort
} else {
return nil, errors.Errorf("unkown direction '%s'", p.Direction)
}
return &exchange.Position{
Symbol: sym,
Side: side,
Mode: ccexgo.PositionModeCross,
Position: p.Volume,
AvailPosition: p.Available,
AvgOpenPrice: p.CostHold,
UNRealizedPNL: p.Profit,
Margin: p.PositionMargin,
Leverage: decimal.NewFromInt(int64(p.LeverRate)),
Raw: p,
}, nil
}