mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-21 15:10:12 +00:00
Binance,Okx: Add Leverage, MarginType, Positions and CollateralMode support (#1220)
* init * surprise train commit * basic distinctions * the terms of binance are confusing * renames and introduction of allocatedMargin * add new margin funcs * pulling out wires * implement proper getposition stuff * bad coding day * investigate order manager next * a broken mess, but a progressing one * finally completes some usdtmargined stuff * coinMfutures eludes me * expand to okx * imports fix * completes okx wrapper implementations * cleans and polishes before rpc implementations * rpc setup, order manager features, exch features * more rpc, collateral and margin things * mini test * looking at rpc response, expansion of features * reorganising before the storm * changing how futures requests work * cleanup and tests of cli usage * remove silly client side logic * cleanup * collateral package, typo fix, margin err, rpc derive * uses convert.StringToFloat ONLY ON STRUCTS FROM THIS PR * fix binance order history bug * niteroos * adds new funcs to exchange standards testing * more post merge fixes * fix binance * replace simepletimeformat * fix for merge * merge fixes * micro fixes * order side now required for leverage * fix up the rest * global -> portfolio collateral * Update exchanges/collateral/collateral_test.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * adds fields and todos * rm field redundancy * lint fix oopsie daisy * fixes panic, expands error and cli explanations (sorry shaz) * ensures casing is appropriate for underlying * Adds a shiny TODO --------- Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
67
exchanges/margin/margin.go
Normal file
67
exchanges/margin/margin.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package margin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Valid returns whether the margin type is valid
|
||||
func (t Type) Valid() bool {
|
||||
return t != Unset && supported&t == t
|
||||
}
|
||||
|
||||
// UnmarshalJSON converts json into margin type
|
||||
func (t *Type) UnmarshalJSON(d []byte) error {
|
||||
var marginType string
|
||||
err := json.Unmarshal(d, &marginType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t, err = StringToMarginType(marginType)
|
||||
return err
|
||||
}
|
||||
|
||||
// String returns the string representation of the margin type in lowercase
|
||||
// the absence of a lower func should hopefully highlight that String is lower
|
||||
func (t Type) String() string {
|
||||
switch t {
|
||||
case Unset:
|
||||
return unsetStr
|
||||
case Isolated:
|
||||
return isolatedStr
|
||||
case Multi:
|
||||
return multiStr
|
||||
case Unknown:
|
||||
return unknownStr
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Upper returns the upper case string representation of the margin type
|
||||
func (t Type) Upper() string {
|
||||
return strings.ToUpper(t.String())
|
||||
}
|
||||
|
||||
// IsValidString checks to see if the supplied string is a valid margin type
|
||||
func IsValidString(m string) bool {
|
||||
switch strings.ToLower(m) {
|
||||
case isolatedStr, multiStr, unsetStr, crossedStr, crossStr:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StringToMarginType converts a string to a margin type
|
||||
// doesn't error, just returns unknown if the string is not recognised
|
||||
func StringToMarginType(m string) (Type, error) {
|
||||
switch strings.ToLower(m) {
|
||||
case isolatedStr:
|
||||
return Isolated, nil
|
||||
case multiStr, crossedStr, crossStr:
|
||||
return Multi, nil
|
||||
case "":
|
||||
return Unset, nil
|
||||
}
|
||||
return Unknown, fmt.Errorf("%w %v", ErrInvalidMarginType, m)
|
||||
}
|
||||
162
exchanges/margin/margin_test.go
Normal file
162
exchanges/margin/margin_test.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package margin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValid(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !Isolated.Valid() {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !Multi.Valid() {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if Unset.Valid() {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if Unknown.Valid() {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if Type(137).Valid() {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
type martian struct {
|
||||
M Type `json:"margin"`
|
||||
}
|
||||
|
||||
var alien martian
|
||||
jason := []byte(`{"margin":"isolated"}`)
|
||||
err := json.Unmarshal(jason, &alien)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if alien.M != Isolated {
|
||||
t.Errorf("received '%v' expected 'isolated'", alien.M)
|
||||
}
|
||||
|
||||
jason = []byte(`{"margin":"cross"}`)
|
||||
err = json.Unmarshal(jason, &alien)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if alien.M != Multi {
|
||||
t.Errorf("received '%v' expected 'Multi'", alien.M)
|
||||
}
|
||||
|
||||
jason = []byte(`{"margin":"hello moto"}`)
|
||||
err = json.Unmarshal(jason, &alien)
|
||||
if !errors.Is(err, ErrInvalidMarginType) {
|
||||
t.Error(err)
|
||||
}
|
||||
if alien.M != Unknown {
|
||||
t.Errorf("received '%v' expected 'isolated'", alien.M)
|
||||
}
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
t.Parallel()
|
||||
if Unknown.String() != unknownStr {
|
||||
t.Errorf("received '%v' expected '%v'", Unknown.String(), unknownStr)
|
||||
}
|
||||
if Isolated.String() != isolatedStr {
|
||||
t.Errorf("received '%v' expected '%v'", Isolated.String(), isolatedStr)
|
||||
}
|
||||
if Multi.String() != multiStr {
|
||||
t.Errorf("received '%v' expected '%v'", Multi.String(), multiStr)
|
||||
}
|
||||
if Unset.String() != unsetStr {
|
||||
t.Errorf("received '%v' expected '%v'", Unset.String(), unsetStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpper(t *testing.T) {
|
||||
t.Parallel()
|
||||
if Unknown.Upper() != strings.ToUpper(unknownStr) {
|
||||
t.Errorf("received '%v' expected '%v'", Unknown.String(), strings.ToUpper(unknownStr))
|
||||
}
|
||||
if Isolated.Upper() != strings.ToUpper(isolatedStr) {
|
||||
t.Errorf("received '%v' expected '%v'", Isolated.String(), strings.ToUpper(isolatedStr))
|
||||
}
|
||||
if Multi.Upper() != strings.ToUpper(multiStr) {
|
||||
t.Errorf("received '%v' expected '%v'", Multi.String(), strings.ToUpper(multiStr))
|
||||
}
|
||||
if Unset.Upper() != strings.ToUpper(unsetStr) {
|
||||
t.Errorf("received '%v' expected '%v'", Unset.String(), strings.ToUpper(unsetStr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidString(t *testing.T) {
|
||||
t.Parallel()
|
||||
if IsValidString("lol") {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if !IsValidString("isolated") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !IsValidString("cross") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !IsValidString("multi") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !IsValidString("unset") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if IsValidString("") {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if IsValidString("unknown") {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToMarginType(t *testing.T) {
|
||||
t.Parallel()
|
||||
resp, err := StringToMarginType("lol")
|
||||
if !errors.Is(err, ErrInvalidMarginType) {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != Unknown {
|
||||
t.Errorf("received '%v' expected '%v'", resp, Unknown)
|
||||
}
|
||||
|
||||
resp, err = StringToMarginType("")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != Unset {
|
||||
t.Errorf("received '%v' expected '%v'", resp, Unset)
|
||||
}
|
||||
|
||||
resp, err = StringToMarginType("cross")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != Multi {
|
||||
t.Errorf("received '%v' expected '%v'", resp, Multi)
|
||||
}
|
||||
|
||||
resp, err = StringToMarginType("multi")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != Multi {
|
||||
t.Errorf("received '%v' expected '%v'", resp, Multi)
|
||||
}
|
||||
|
||||
resp, err = StringToMarginType("isolated")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != Isolated {
|
||||
t.Errorf("received '%v' expected '%v'", resp, Isolated)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package margin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
@@ -8,6 +9,17 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidMarginType returned when the margin type is invalid
|
||||
ErrInvalidMarginType = errors.New("invalid margin type")
|
||||
// ErrMarginTypeUnsupported returned when the margin type is unsupported
|
||||
ErrMarginTypeUnsupported = errors.New("unsupported margin type")
|
||||
// ErrNewAllocatedMarginRequired returned when the new allocated margin is missing and is required
|
||||
ErrNewAllocatedMarginRequired = errors.New("new allocated margin required")
|
||||
// ErrOriginalPositionMarginRequired is returned when original position margin is empty and is required
|
||||
ErrOriginalPositionMarginRequired = errors.New("original allocated margin required")
|
||||
)
|
||||
|
||||
// RateHistoryRequest is used to request a funding rate
|
||||
type RateHistoryRequest struct {
|
||||
Exchange string
|
||||
@@ -32,6 +44,55 @@ type RateHistoryRequest struct {
|
||||
Rates []Rate
|
||||
}
|
||||
|
||||
// PositionChangeRequest used for wrapper functions to change margin fields for a position
|
||||
type PositionChangeRequest struct {
|
||||
// Required fields
|
||||
Exchange string
|
||||
Pair currency.Pair
|
||||
Asset asset.Item
|
||||
// Optional fields depending on desired outcome/exchange requirements
|
||||
MarginType Type
|
||||
OriginalAllocatedMargin float64
|
||||
NewAllocatedMargin float64
|
||||
MarginSide string
|
||||
}
|
||||
|
||||
// PositionChangeResponse holds response data for margin change requests
|
||||
type PositionChangeResponse struct {
|
||||
Exchange string
|
||||
Pair currency.Pair
|
||||
Asset asset.Item
|
||||
AllocatedMargin float64
|
||||
MarginType Type
|
||||
}
|
||||
|
||||
// Type defines the different margin types supported by exchanges
|
||||
type Type uint8
|
||||
|
||||
// Margin types
|
||||
const (
|
||||
// Unset is the default value
|
||||
Unset = Type(0)
|
||||
// Isolated means a margin trade is isolated from other margin trades
|
||||
Isolated Type = 1 << (iota - 1)
|
||||
// Multi means a margin trade is not isolated from other margin trades
|
||||
// it can sometimes be referred to as "cross"
|
||||
Multi
|
||||
// Unknown is an unknown margin type but is not unset
|
||||
Unknown
|
||||
)
|
||||
|
||||
var supported = Isolated | Multi
|
||||
|
||||
const (
|
||||
unsetStr = "unset"
|
||||
isolatedStr = "isolated"
|
||||
multiStr = "multi"
|
||||
crossedStr = "crossed"
|
||||
crossStr = "cross"
|
||||
unknownStr = "unknown"
|
||||
)
|
||||
|
||||
// RateHistoryResponse has the funding rate details
|
||||
type RateHistoryResponse struct {
|
||||
Rates []Rate
|
||||
|
||||
Reference in New Issue
Block a user