github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/order/orderrequests.go (about)

     1  package order
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
     8  )
     9  
    10  // NewRequest represents an order to be posted to the bitfinex websocket
    11  // service.
    12  type NewRequest struct {
    13  	GID           int64                  `json:"gid"`
    14  	CID           int64                  `json:"cid"`
    15  	Type          string                 `json:"type"`
    16  	Symbol        string                 `json:"symbol"`
    17  	Amount        float64                `json:"amount,string"`
    18  	Price         float64                `json:"price,string"`
    19  	Leverage      int64                  `json:"lev,omitempty"`
    20  	PriceTrailing float64                `json:"price_trailing,string,omitempty"`
    21  	PriceAuxLimit float64                `json:"price_aux_limit,string,omitempty"`
    22  	PriceOcoStop  float64                `json:"price_oco_stop,string,omitempty"`
    23  	Hidden        bool                   `json:"hidden,omitempty"`
    24  	PostOnly      bool                   `json:"postonly,omitempty"`
    25  	Close         bool                   `json:"close,omitempty"`
    26  	OcoOrder      bool                   `json:"oco_order,omitempty"`
    27  	TimeInForce   string                 `json:"tif,omitempty"`
    28  	AffiliateCode string                 `json:"-"`
    29  	Meta          map[string]interface{} `json:"meta,omitempty"`
    30  }
    31  
    32  // MarshalJSON converts the order object into the format required by the bitfinex
    33  // websocket service.
    34  func (nr *NewRequest) MarshalJSON() ([]byte, error) {
    35  	jsonOrder, err := nr.ToJSON()
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	return []byte(fmt.Sprintf("[0, \"on\", null, %s]", string(jsonOrder))), nil
    40  }
    41  
    42  // EnrichedPayload returns enriched representation of order struct for submission
    43  func (nr *NewRequest) EnrichedPayload() interface{} {
    44  	pld := struct {
    45  		GID           int64                  `json:"gid"`
    46  		CID           int64                  `json:"cid"`
    47  		Type          string                 `json:"type"`
    48  		Symbol        string                 `json:"symbol"`
    49  		Amount        float64                `json:"amount,string"`
    50  		Price         float64                `json:"price,string"`
    51  		Leverage      int64                  `json:"lev,omitempty"`
    52  		PriceTrailing float64                `json:"price_trailing,string,omitempty"`
    53  		PriceAuxLimit float64                `json:"price_aux_limit,string,omitempty"`
    54  		PriceOcoStop  float64                `json:"price_oco_stop,string,omitempty"`
    55  		TimeInForce   string                 `json:"tif,omitempty"`
    56  		Flags         int                    `json:"flags,omitempty"`
    57  		Meta          map[string]interface{} `json:"meta,omitempty"`
    58  	}{
    59  		GID:           nr.GID,
    60  		CID:           nr.CID,
    61  		Type:          nr.Type,
    62  		Symbol:        nr.Symbol,
    63  		Amount:        nr.Amount,
    64  		Price:         nr.Price,
    65  		Leverage:      nr.Leverage,
    66  		PriceTrailing: nr.PriceTrailing,
    67  		PriceAuxLimit: nr.PriceAuxLimit,
    68  		PriceOcoStop:  nr.PriceOcoStop,
    69  		TimeInForce:   nr.TimeInForce,
    70  	}
    71  
    72  	if nr.Hidden {
    73  		pld.Flags = pld.Flags + common.OrderFlagHidden
    74  	}
    75  
    76  	if nr.PostOnly {
    77  		pld.Flags = pld.Flags + common.OrderFlagPostOnly
    78  	}
    79  
    80  	if nr.OcoOrder {
    81  		pld.Flags = pld.Flags + common.OrderFlagOCO
    82  	}
    83  
    84  	if nr.Close {
    85  		pld.Flags = pld.Flags + common.OrderFlagClose
    86  	}
    87  
    88  	if nr.Meta == nil {
    89  		pld.Meta = make(map[string]interface{})
    90  	}
    91  
    92  	if nr.AffiliateCode != "" {
    93  		pld.Meta["aff_code"] = nr.AffiliateCode
    94  	}
    95  
    96  	return pld
    97  }
    98  
    99  func (nr *NewRequest) ToJSON() ([]byte, error) {
   100  	return json.Marshal(nr.EnrichedPayload())
   101  }
   102  
   103  type UpdateRequest struct {
   104  	ID            int64                  `json:"id"`
   105  	GID           int64                  `json:"gid,omitempty"`
   106  	Price         float64                `json:"price,string,omitempty"`
   107  	Amount        float64                `json:"amount,string,omitempty"`
   108  	Leverage      int64                  `json:"lev,omitempty"`
   109  	Delta         float64                `json:"delta,string,omitempty"`
   110  	PriceTrailing float64                `json:"price_trailing,string,omitempty"`
   111  	PriceAuxLimit float64                `json:"price_aux_limit,string,omitempty"`
   112  	Hidden        bool                   `json:"hidden,omitempty"`
   113  	PostOnly      bool                   `json:"postonly,omitempty"`
   114  	TimeInForce   string                 `json:"tif,omitempty"`
   115  	Meta          map[string]interface{} `json:"meta,omitempty"`
   116  }
   117  
   118  // MarshalJSON converts the order object into the format required by the bitfinex
   119  // websocket service.
   120  func (ur *UpdateRequest) MarshalJSON() ([]byte, error) {
   121  	aux, err := ur.ToJSON()
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	return []byte(fmt.Sprintf("[0, \"ou\", null, %s]", string(aux))), nil
   126  }
   127  
   128  func (ur *UpdateRequest) EnrichedPayload() interface{} {
   129  	pld := struct {
   130  		ID            int64                  `json:"id"`
   131  		GID           int64                  `json:"gid,omitempty"`
   132  		Price         float64                `json:"price,string,omitempty"`
   133  		Amount        float64                `json:"amount,string,omitempty"`
   134  		Leverage      int64                  `json:"lev,omitempty"`
   135  		Delta         float64                `json:"delta,string,omitempty"`
   136  		PriceTrailing float64                `json:"price_trailing,string,omitempty"`
   137  		PriceAuxLimit float64                `json:"price_aux_limit,string,omitempty"`
   138  		Hidden        bool                   `json:"hidden,omitempty"`
   139  		PostOnly      bool                   `json:"postonly,omitempty"`
   140  		TimeInForce   string                 `json:"tif,omitempty"`
   141  		Flags         int                    `json:"flags,omitempty"`
   142  		Meta          map[string]interface{} `json:"meta,omitempty"`
   143  	}{
   144  		ID:            ur.ID,
   145  		GID:           ur.GID,
   146  		Amount:        ur.Amount,
   147  		Leverage:      ur.Leverage,
   148  		Price:         ur.Price,
   149  		PriceTrailing: ur.PriceTrailing,
   150  		PriceAuxLimit: ur.PriceAuxLimit,
   151  		Delta:         ur.Delta,
   152  		TimeInForce:   ur.TimeInForce,
   153  	}
   154  
   155  	if ur.Meta == nil {
   156  		pld.Meta = make(map[string]interface{})
   157  	}
   158  
   159  	if ur.Hidden {
   160  		pld.Flags = pld.Flags + common.OrderFlagHidden
   161  	}
   162  
   163  	if ur.PostOnly {
   164  		pld.Flags = pld.Flags + common.OrderFlagPostOnly
   165  	}
   166  
   167  	return pld
   168  }
   169  
   170  func (ur *UpdateRequest) ToJSON() ([]byte, error) {
   171  	return json.Marshal(ur.EnrichedPayload())
   172  }
   173  
   174  // CancelRequest represents an order cancel request.
   175  // An order can be cancelled using the internal ID or a
   176  // combination of Client ID (CID) and the daten for the given
   177  // CID.
   178  type CancelRequest struct {
   179  	ID      int64  `json:"id,omitempty"`
   180  	CID     int64  `json:"cid,omitempty"`
   181  	CIDDate string `json:"cid_date,omitempty"`
   182  }
   183  
   184  func (cr *CancelRequest) ToJSON() ([]byte, error) {
   185  	resp := struct {
   186  		ID      int64  `json:"id,omitempty"`
   187  		CID     int64  `json:"cid,omitempty"`
   188  		CIDDate string `json:"cid_date,omitempty"`
   189  	}{
   190  		ID:      cr.ID,
   191  		CID:     cr.CID,
   192  		CIDDate: cr.CIDDate,
   193  	}
   194  
   195  	return json.Marshal(resp)
   196  }
   197  
   198  // MarshalJSON converts the order cancel object into the format required by the
   199  // bitfinex websocket service.
   200  func (cr *CancelRequest) MarshalJSON() ([]byte, error) {
   201  	b, err := cr.ToJSON()
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  	return []byte(fmt.Sprintf("[0, \"oc\", null, %s]", string(b))), nil
   206  }