okex

package module
v1.4.51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2024 License: Apache-2.0 Imports: 4 Imported by: 20

README

okex

Go Reference GitHub go.mod Go version of a Go module GoReportCard example GitHub license GitHub release PRs Welcome

NOTICE:

PACKAGE IS CURRENTLY UNDER HEAVY DEVELOPMENT AND THERE IS NO GUARANTY FOR STABILITY UNTIL V1 RELEASE.

Okex V5 Golang API

A complete golang wrapper for Okex V5 API. Pretty simple and easy to use. For more info about Okex V5 API read here.

Installation

go get github.com/aiviaio/okex@v1.0.5

Usage

package main

import (
	"context"
	"github.com/aiviaio/okex"
	"github.com/aiviaio/okex/api"
	"github.com/aiviaio/okex/events"
	"github.com/aiviaio/okex/events/private"
	ws_private_requests "github.com/aiviaio/okex/requests/ws/private"
	ws_public_requests "github.com/aiviaio/okex/requests/ws/public"
	"log"
)

func main() {
	apiKey := "YOUR-API-KEY"
	secretKey := "YOUR-SECRET-KEY"
	passphrase := "YOUR-PASS-PHRASE"
	dest := okex.NormalServer // The main API server
	ctx := context.Background()
	client, err := api.NewClient(ctx, apiKey, secretKey, passphrase, &dest)
	if err != nil {
		log.Fatalln(err)
	}

	response, err := client.Rest.Account.GetConfig()
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("Account Config %+v", response)

	errChan := make(chan *events.Error)
	subChan := make(chan *events.Subscribe)
	uSubChan := make(chan *events.Unsubscribe)
	lCh := make(chan *events.Login)
	oCh := make(chan *private.Order)
	iCh := make(chan *public.Instruments)

	// to receive unique events individually in separated channels
	client.Ws.SetChannels(errChan, subChan, uSubChan, lCh)

	// subscribe into orders private channel
	// it will do the login process and wait until authorization confirmed
	err = client.Ws.Private.Order(ws_private_requests.Order{
		InstType: okex.SwapInstrument,
	}, oCh)
	if err != nil {
		log.Fatalln(err)
	}

	// subscribe into instruments public channel
	// it doesn't need any authorization
	err = client.Ws.Public.Instruments(ws_public_requests.Instruments{
		InstType: okex.SwapInstrument,
	}, iCh)
	if err != nil {
		log.Fatalln("Instruments", err)
	}

	// starting on listening 
	for {
		select {
		case <-lCh:
			log.Print("[Authorized]")
		case sub := <-subChan:
			channel, _ := sub.Arg.Get("channel")
			log.Printf("[Subscribed]\t%s", channel)
		case uSub := <-uSubChan:
			channel, _ := uSub.Arg.Get("channel")
			log.Printf("[Unsubscribed]\t%s", channel)
		case err := <-client.Ws.ErrChan:
			log.Printf("[Error]\t%+v", err)
		case o := <-oCh:
			log.Print("[Event]\tOrder")
			for _, p := range o.Orders {
				log.Printf("\t%+v", p)
			}
		case i := <-iCh:
			log.Print("[Event]\tInstrument")
			for _, p := range i.Instruments {
				log.Printf("\t%+v", p)
			}
		case e := <-client.Ws.StructuredEventChan:
			log.Printf("[Event] STRUCTED:\t%+v", e)
			v := reflect.TypeOf(e)
			switch v {
			case reflect.TypeOf(events.Error{}):
				log.Printf("[Error] STRUCTED:\t%+v", e)
			case reflect.TypeOf(events.Subscribe{}):
				log.Printf("[Subscribed] STRUCTED:\t%+v", e)
			case reflect.TypeOf(events.Unsubscribe{}):
				log.Printf("[Unsubscribed] STRUCTED:\t%+v", e)
			}
		case e := <-client.Ws.RawEventChan:
			log.Printf("[Event] RAW:\t%+v", e)
		case b := <-client.Ws.DoneChan:
			log.Printf("[End]:\t%v", b)
			return
		}
	}
}

Supporting APIs

Features

  • All requests, responses, and events are well typed and will convert into the language built-in types instead of using API's strings. Note that zero values will be replaced with non-existing data.
  • Fully automated authorization steps for both REST and WS
  • To receive websocket events you can choose RawEventChan , StructuredEventChan, or provide your own channels. More info

Documentation

Overview

Package okex is generally a golang Api wrapper of Okex V5 API

https://www.okex.com/docs-v5/en

Index

Constants

View Source
const (
	RestURL       = BaseURL("https://www.okex.com")
	PublicWsURL   = BaseURL("wss://ws.okex.com:8443/ws/v5/public")
	PrivateWsURL  = BaseURL("wss://ws.okex.com:8443/ws/v5/private")
	BusinessWsURL = BaseURL("wss://ws.okx.com:8443/ws/v5/business")

	AwsRestURL      = BaseURL("https://aws.okex.com")
	AwsPublicWsURL  = BaseURL("wss://wsaws.okex.com:8443/ws/v5/public")
	AwsPrivateWsURL = BaseURL("wss://wsaws.okex.com:8443/ws/v5/private")

	DemoRestURL      = BaseURL("https://www.okex.com")
	DemoPublicWsURL  = BaseURL("wss://wspap.okex.com:8443/ws/v5/public?brokerId=9999")
	DemoPrivateWsURL = BaseURL("wss://wspap.okex.com:8443/ws/v5/private?brokerId=9999")

	OmegaRestURL      = BaseURL("https://www.omegaexchange.io")
	OmegaPublicWsURL  = BaseURL("wss://ws.okex.com:8443/ws/v5/public?brokerId=200")
	OmegaPrivateWsURL = BaseURL("wss://ws.okex.com:8443/ws/v5/private?brokerId=200")

	NormalServer   = Destination(iota + 1)
	AwsServer      = NormalServer + 1
	DemoServer     = AwsServer + 1
	OmegaServer    = DemoServer + 1
	BusinessServer = OmegaServer + 1

	SpotInstrument    = InstrumentType("SPOT")
	MarginInstrument  = InstrumentType("MARGIN")
	SwapInstrument    = InstrumentType("SWAP")
	FuturesInstrument = InstrumentType("FUTURES")
	OptionsInstrument = InstrumentType("OPTION")

	MarginCrossMode    = MarginMode("cross")
	MarginIsolatedMode = MarginMode("isolated")

	ContractLinearType  = ContractType("linear")
	ContractInverseType = ContractType("inverse")

	BillTransferType              = BillType(1)
	BillTradeType                 = BillType(2)
	BillDeliveryType              = BillType(3)
	BillAutoTokenConversionType   = BillType(4)
	BillLiquidationType           = BillType(5)
	BillMarginTransferType        = BillType(6)
	BillInterestDeductionType     = BillType(7)
	BillFundingFeeType            = BillType(8)
	BillADLType                   = BillType(9)
	BillClawbackType              = BillType(10)
	BillSystemTokenConversionType = BillType(11)
	BillStrategyTransferType      = BillType(12)
	BillDDHType                   = BillType(13)
	BillBlockTradeType            = BillType(14)
	BillQuickMarginType           = BillType(15)
	BillRepayType                 = BillType(22)
	BillSpreadTradingType         = BillType(24)
	BillProfitSharingExpensesType = BillType(250)
	BillProfitSharingRefundType   = BillType(251)
	BillProfitSharingIncomeType   = BillType(252)

	BillBuySubType                              = BillSubType(1)
	BillSellSubType                             = BillSubType(2)
	BillOpenLongSubType                         = BillSubType(3)
	BillOpenShortSubType                        = BillSubType(4)
	BillCloseLongSubType                        = BillSubType(5)
	BillCloseShortSubType                       = BillSubType(6)
	BillInterestDeductionSubType                = BillSubType(9)
	BillTransferInSubType                       = BillSubType(11)
	BillTransferOutSubType                      = BillSubType(12)
	BillManualMarginIncreaseSubType             = BillSubType(160)
	BillManualMarginDecreaseSubType             = BillSubType(161)
	BillAutoMarginIncreaseSubType               = BillSubType(162)
	BillAutoBuySubType                          = BillSubType(110)
	BillAutoSellSubType                         = BillSubType(111)
	BillSystemTokenConversionTransferInSubType  = BillSubType(118)
	BillSystemTokenConversionTransferOutSubType = BillSubType(119)
	BillPartialLiquidationCloseLongSubType      = BillSubType(100)
	BillPartialLiquidationCloseShortSubType     = BillSubType(101)
	BillPartialLiquidationBuySubType            = BillSubType(102)
	BillPartialLiquidationSellSubType           = BillSubType(103)
	BillLiquidationLongSubType                  = BillSubType(104)
	BillLiquidationShortSubType                 = BillSubType(105)
	BillLiquidationBuySubType                   = BillSubType(106)
	BillLiquidationSellSubType                  = BillSubType(107)
	BillLiquidationTransferInSubType            = BillSubType(110)
	BillLiquidationTransferOutSubType           = BillSubType(111)
	BillADLCloseLongSubType                     = BillSubType(125)
	BillADLCloseShortSubType                    = BillSubType(126)
	BillADLBuySubType                           = BillSubType(127)
	BillADLSellSubType                          = BillSubType(128)
	BillExercisedSubType                        = BillSubType(170)
	BillCounterpartyExercisedSubType            = BillSubType(171)
	BillExpiredOTMSubType                       = BillSubType(172)
	BillDeliveryLongSubType                     = BillSubType(112)
	BillDeliveryShortSubType                    = BillSubType(113)
	BillDeliveryExerciseClawbackSubType         = BillSubType(117)
	BillFundingFeeExpenseSubType                = BillSubType(173)
	BillFundingFeeIncomeSubType                 = BillSubType(174)
	BillSystemTransferInSubType                 = BillSubType(200)
	BillManuallyTransferInSubType               = BillSubType(201)
	BillSystemTransferOutSubType                = BillSubType(202)
	BillManuallyTransferOutSubType              = BillSubType(203)
	BillCryptoDustAutoTransferOutSubType        = BillSubType(290)

	PositionLongShortMode = PositionType("long_short_mode")
	PositionNetMode       = PositionType("net_mode")

	PositionLongSide  = PositionSide("long")
	PositionShortSide = PositionSide("short")
	PositionNetSide   = PositionSide("net")

	TpSide = ActualSide("tp")
	SlSide = ActualSide("sl")

	TradeCrossMode    = TradeMode("cross")
	TradeIsolatedMode = TradeMode("isolated")
	TradeCashMode     = TradeMode("cash")

	CountIncrease = CountAction("add")
	CountDecrease = CountAction("reduce")

	OrderBuy  = OrderSide("buy")
	OrderSell = OrderSide("sell")

	GreekInCoin    = GreekType("PA")
	GreekInDollars = GreekType("PB")

	Bar1m  = BarSize("1m")
	Bar3m  = BarSize("3m")
	Bar5m  = BarSize("5m")
	Bar15m = BarSize("15m")
	Bar30m = BarSize("130")
	Bar1H  = BarSize("1H")
	Bar2H  = BarSize("2H")
	Bar4H  = BarSize("4H")
	Bar6H  = BarSize("6H")
	Bar8H  = BarSize("8H")
	Bar12H = BarSize("12H")
	Bar1D  = BarSize("1D")
	Bar1W  = BarSize("1W")
	Bar1M  = BarSize("1M")
	Bar3M  = BarSize("3M")
	Bar6M  = BarSize("6M")
	Bar1Y  = BarSize("1Y")

	TradeBuySide  = TradeSide("buy")
	TradeSellSide = TradeSide("sell")

	LoginOperation            = Operation("login")
	SubscribeOperation        = Operation("subscribe")
	UnsubscribeOperation      = Operation("unsubscribe")
	OrderOperation            = Operation("order")
	BatchOrderOperation       = Operation("batch-orders")
	CancelOrderOperation      = Operation("cancel-order")
	BatchCancelOrderOperation = Operation("batch-cancel-orders")
	AmendOrderOperation       = Operation("amend-order")
	BatchAmendOrderOperation  = Operation("batch-amend-orders")

	OrderMarket   = OrderType("market")
	OrderLimit    = OrderType("limit")
	OrderPostOnly = OrderType("post_only")
	// OrderFOK is Fill-or-kill order
	OrderFOK = OrderType("fok")
	// OrderIOC is Immediate-or-cancel order
	OrderIOC = OrderType("ioc")
	// OrderOptimalLimitIoc is Market order with immediate-or-cancel order
	OrderOptimalLimitIoc = OrderType("optimal_limit_ioc")
	// OrderMMP is Market Maker Protection (only applicable to Option in Portfolio Margin mode)
	OrderMMP = OrderType("mmp")
	// OrderMMPPostOnly is Market Maker Protection and Post-only order
	// (only applicable to Option in Portfolio Margin mode)
	OrderMMPPostOnly = OrderType("mmp_and_post_only")
	// OrderOPFOK is Simple options (fok)
	OrderOPFOK = OrderType("op_fok")

	// AlgoOrderConditional is One-way stop order
	AlgoOrderConditional = AlgoOrderType("conditional")
	// AlgoOrderOCO is One-cancels-the-other order
	AlgoOrderOCO      = AlgoOrderType("oco")
	AlgoOrderTrigger  = AlgoOrderType("trigger")
	AlgoOrderIceberg  = AlgoOrderType("iceberg")
	AlgoOrderTwap     = AlgoOrderType("twap")
	AlgoOrderTrailing = AlgoOrderType("move_order_stop")

	QuantityBaseCcy  = QuantityType("base_ccy")
	QuantityQuoteCcy = QuantityType("quote_ccy")

	OrderTakerFlow = OrderFlowType("T")
	OrderMakerFlow = OrderFlowType("M")

	ClassA = FeeCategory(1)
	ClassB = FeeCategory(2)
	ClassC = FeeCategory(3)
	ClassD = FeeCategory(4)

	OrderCancel          = OrderState("canceled")
	OrderPause           = OrderState("pause")
	OrderLive            = OrderState("live")
	OrderPartiallyFilled = OrderState("partially_filled")
	OrderFilled          = OrderState("filled")
	OrderUnfilled        = OrderState("unfilled")
	OrderEffective       = OrderState("effective")
	OrderFailed          = OrderState("order_failed")

	TransferWithinAccount     = TransferType(0)
	MasterAccountToSubAccount = TransferType(1)
	MasterSubAccountToAccount = TransferType(2)

	SpotAccount    = AccountType(1)
	FuturesAccount = AccountType(3)
	MarginAccount  = AccountType(5)
	FundingAccount = AccountType(6)
	SwapAccount    = AccountType(9)
	OptionsAccount = AccountType(12)
	UnifiedAccount = AccountType(18)

	WaitingForConfirmation     = DepositState(0)
	DepositCredited            = DepositState(1)
	DepositSuccessful          = DepositState(2)
	DepositTemporarySuspension = DepositState(8)

	WithdrawalOkexDestination           = WithdrawalDestination(3)
	WithdrawalDigitalAddressDestination = WithdrawalDestination(4)

	WithdrawalPendingCancel              = WithdrawalState(-3)
	WithdrawalCanceled                   = WithdrawalState(-2)
	WithdrawalFailed                     = WithdrawalState(-1)
	WithdrawalPending                    = WithdrawalState(0)
	WithdrawalSending                    = WithdrawalState(1)
	WithdrawalSent                       = WithdrawalState(2)
	WithdrawalAwaitingEmailVerification  = WithdrawalState(3)
	WithdrawalAwaitingManualVerification = WithdrawalState(4)
	WithdrawalIdentityManualVerification = WithdrawalState(5)

	ActionPurchase = ActionType("purchase")
	ActionRedempt  = ActionType("redempt")

	APIKeyReadOnly = APIKeyAccess("read_only")
	APIKeyTrade    = APIKeyAccess("trade")
	APIKeyWithdraw = APIKeyAccess("withdraw")

	OptionCall = OptionType("C")
	OptionPut  = OptionType("P")

	AliasThisWeek    = AliasType("this_week")
	AliasNextWeek    = AliasType("next_week")
	AliasQuarter     = AliasType("quarter")
	AliasNextQuarter = AliasType("next_quarter")

	InstrumentLive    = InstrumentState("live")
	InstrumentSuspend = InstrumentState("suspend")
	InstrumentPreOpen = InstrumentState("preopen")

	Delivery   = DeliveryExerciseType("delivery")
	Exercise   = DeliveryExerciseType("exercised")
	ExpiredOtm = DeliveryExerciseType("expired_otm")

	CandleStick1Y  = CandleStickWsBarSize("candle1Y")
	CandleStick6M  = CandleStickWsBarSize("candle6M")
	CandleStick3M  = CandleStickWsBarSize("candle3M")
	CandleStick1M  = CandleStickWsBarSize("candle1M")
	CandleStick5D  = CandleStickWsBarSize("candle5D")
	CandleStick3D  = CandleStickWsBarSize("candle3D")
	CandleStick2D  = CandleStickWsBarSize("candle2D")
	CandleStick1D  = CandleStickWsBarSize("candle1D")
	CandleStick12H = CandleStickWsBarSize("candle12H")
	CandleStick6H  = CandleStickWsBarSize("candle6H")
	CandleStick4H  = CandleStickWsBarSize("candle4H")
	CandleStick2H  = CandleStickWsBarSize("candle2H")
	CandleStick1H  = CandleStickWsBarSize("candle1H")
	CandleStick30m = CandleStickWsBarSize("candle30m")
	CandleStick15m = CandleStickWsBarSize("candle15m")
	CandleStick5m  = CandleStickWsBarSize("candle5m")
	CandleStick3m  = CandleStickWsBarSize("candle3m")
	CandleStick1m  = CandleStickWsBarSize("candle1m")

	ConvertTypeContract = ConvertType(1)
	ConvertTypeCurrency = ConvertType(2)
)

Variables

This section is empty.

Functions

func S2M

func S2M(i interface{}) map[string]string

Types

type APIKeyAccess

type APIKeyAccess string

type AccountType

type AccountType uint16

func (*AccountType) UnmarshalJSON

func (t *AccountType) UnmarshalJSON(s []byte) (err error)

type ActionType

type ActionType string

type ActualSide

type ActualSide string

type AlgoOrderType

type AlgoOrderType string

type AliasType

type AliasType string

type BarSize

type BarSize string

func (BarSize) Duration

func (t BarSize) Duration() time.Duration

type BaseURL

type BaseURL string

type BillSubType

type BillSubType uint16

func (*BillSubType) UnmarshalJSON

func (t *BillSubType) UnmarshalJSON(s []byte) (err error)

type BillType

type BillType uint16

func (*BillType) UnmarshalJSON

func (t *BillType) UnmarshalJSON(s []byte) (err error)

type CandleStickWsBarSize

type CandleStickWsBarSize string

type ChannelName

type ChannelName string

type ClientError

type ClientError error

type ContractType

type ContractType string

type ConvertType added in v1.4.26

type ConvertType uint16

type CountAction

type CountAction string

type DeliveryExerciseType

type DeliveryExerciseType string

type DepositState

type DepositState uint16

func (*DepositState) UnmarshalJSON

func (t *DepositState) UnmarshalJSON(s []byte) (err error)

type Destination

type Destination int

type EventType

type EventType string

type FeeCategory

type FeeCategory uint16

func (*FeeCategory) UnmarshalJSON

func (t *FeeCategory) UnmarshalJSON(s []byte) (err error)

type GreekType

type GreekType string

type InstrumentState

type InstrumentState string

type InstrumentType

type InstrumentType string

type JSONFloat64

type JSONFloat64 float64

func (*JSONFloat64) UnmarshalJSON

func (t *JSONFloat64) UnmarshalJSON(s []byte) (err error)

type JSONInt64

type JSONInt64 int64

func (*JSONInt64) UnmarshalJSON

func (t *JSONInt64) UnmarshalJSON(s []byte) (err error)

type JSONTime

type JSONTime time.Time

func (JSONTime) String

func (t JSONTime) String() string

func (*JSONTime) UnmarshalJSON

func (t *JSONTime) UnmarshalJSON(s []byte) (err error)

type MarginMode

type MarginMode string

type Operation

type Operation string

type OptionType

type OptionType string

type OrderFlowType

type OrderFlowType string

type OrderSide

type OrderSide string

type OrderState

type OrderState string

type OrderType

type OrderType string

type PositionSide

type PositionSide string

type PositionType

type PositionType string

type QuantityType

type QuantityType string

type TradeMode

type TradeMode string

type TradeSide

type TradeSide string

type TransferType

type TransferType uint16

type WithdrawalDestination

type WithdrawalDestination uint16

type WithdrawalState

type WithdrawalState int8

func (*WithdrawalState) UnmarshalJSON

func (t *WithdrawalState) UnmarshalJSON(s []byte) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL