exch

package module
v0.1.32 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2020 License: MIT Imports: 4 Imported by: 0

README

:men_wrestling: exchanges

Release Go Report Card Go Doc
Change Log Go Version MIT License

对交易所 API 的封装

安装与更新

在命令行中输入以下内容,可以获取到最新版

go get -u github.com/jujili/exch

Documentation

Index

Constants

View Source
const (
	// Special Function
	LOCAL Name = "LOCAL" // For local generated data

	// CryptoCurrency
	BITMEX   = "BITMEX"
	OKEX     = "OKEX"
	HUOBI    = "HUOBI"
	BITFINEX = "BITFINEX"
	BINANCE  = "BINANCE"
	BYBIT    = "BYBIT" // bybit.com
	COINBASE = "COINBASE"
	DERIBIT  = "DERIBIT"
	GATEIO   = "GATEIO"
	BITSTAMP = "BITSTAMP"

	// China
	CFFEX = "CFFEX" // China Financial Futures Exchange
	SHFE  = "SHFE"  // Shanghai Futures Exchange
	CZCE  = "CZCE"  // Zhengzhou Commodity Exchange
	DCE   = "DCE"   // Dalian Commodity Exchange
	INE   = "INE"   // Shanghai International Energy Exchange
	SSE   = "SSE"   // Shanghai Stock Exchange
	SZSE  = "SZSE"  // Shenzhen Stock Exchange
	SGE   = "SGE"   // Shanghai Gold Exchange
	WXE   = "WXE"   // Wuxi Steel Exchange
	CFETS = "CFETS" // China Foreign Exchange Trade System

	// Global
	SMART    = "SMART"    // Smart Router for US stocks
	NYSE     = "NYSE"     // New York Stock Exchange
	NASDAQ   = "NASDAQ"   // Nasdaq Exchange
	NYMEX    = "NYMEX"    // New York Mercantile Exchange
	COMEX    = "COMEX"    // a division of theNew York Mercantile Exchange
	GLOBEX   = "GLOBEX"   // Globex of CME
	IDEALPRO = "IDEALPRO" // Forex ECN of Interactive Brokers
	CME      = "CME"      // Chicago Mercantile Exchange
	ICE      = "ICE"      // Intercontinental Exchange
	SEHK     = "SEHK"     // Stock Exchange of Hong Kong
	HKFE     = "HKFE"     // Hong Kong Futures Exchange
	HKSE     = "HKSE"     // Hong Kong Stock Exchange
	SGX      = "SGX"      // Singapore Global Exchange
	CBOT     = "CBT"      // Chicago Board of Trade
	CBOE     = "CBOE"     // Chicago Board Options Exchange
	CFE      = "CFE"      // CBOE Futures Exchange
	DME      = "DME"      // Dubai Mercantile Exchange
	EUREX    = "EUX"      // Eurex Exchange
	APEX     = "APEX"     // Asia Pacific Exchange
	LME      = "LME"      // London Metal Exchange
	BMD      = "BMD"      // Bursa Malaysia Derivatives
	TOCOM    = "TOCOM"    // Tokyo Commodity Exchange
	EUNX     = "EUNX"     // Euronext Exchange
	KRX      = "KRX"      // Korean Exchange

	OANDA = "OANDA" // oanda.com
)

交易所的代号

Variables

View Source
var NilTick = Tick{}

NilTick 只是一个标志

Functions

func Begin

func Begin(date time.Time, interval time.Duration) time.Time

Begin 会根据 time 和 interval 计算 time 所在周期的开始时间 当 interval 的单位

为分钟或秒时,推荐值为 1,2,3,4,5,6,10,12,15,20,30,60
为小时时,   推荐值为 1,2,3,4,6,8,12,24

NOTICE: 由于每个月的时间长度不一致,无法计算月线的起始日期。年线同理。

func DecBalanceFunc

func DecBalanceFunc() func(bs []byte) *Balance

DecBalanceFunc 返回的函数会把序列化成 []byte 的 Balances 值转换回来

func DecBarFunc

func DecBarFunc() func(bs []byte) Bar

DecBarFunc 返回的函数会把序列化成 []byte 的 Bar 值转换回来

func DecOrderFunc

func DecOrderFunc() func(bs []byte) *Order

DecOrderFunc 返回的函数会把序列化成 []byte 的 Order 值转换回来

func DecTickFunc

func DecTickFunc() func(bs []byte) Tick

DecTickFunc 返回的函数会把序列化成 []byte 的 Balances 值转换回来

func EncFunc

func EncFunc() func(interface{}) []byte

EncFunc 返回的函数能够将输入转换成 []byte 这些写成闭包的形式,而不是 enc 是为了提高速度 运行压力测试即可知道,提速了 6 倍

func GenTickBarFunc

func GenTickBarFunc(begin BeginFunc, interval time.Duration) func(Tick) []Bar

GenTickBarFunc 会返回一个接收 tick 并生成 bar 的闭包函数 有以下情况需要处理

  1. 接收第一个 tick, 不返回 bar
  2. 接收到当前的 interval 的 tick 不返回 bar
  3. 接收到下一个 interval 的 tick 返回上一个 bar
  4. 接收到下一个 interval 后面的 interval 的 tick,市场冷清,长时间没有交易 返回多个 bar

func Limit

func Limit(side OrderSide, quantity, price float64) func(*Order)

Limit 会按照限价单的方式设置订单

func Market

func Market(side OrderSide, quantity float64) func(*Order)

Market 会按照市价单的方式设置订单

Types

type Asset

type Asset struct {
	Name         string
	Free, Locked float64
}

Asset 代表了交易所中,某一项资产的状态和数目 Asset 是一个值对象 value object

func NewAsset

func NewAsset(name string, free, locked float64) Asset

NewAsset return new asset

func (Asset) Add

func (a Asset) Add(delta Asset) Asset

Add return new asset by a change with delta

func (Asset) String added in v0.1.23

func (a Asset) String() string

func (Asset) Total

func (a Asset) Total() float64

Total returns total asset of this asset

type Balance

type Balance map[string]Asset

Balance 记录了交易所中的资产

func NewBalances

func NewBalances(assets ...Asset) Balance

NewBalances returns a new Balances

func (Balance) Add

func (b Balance) Add(as ...Asset) Balance

Add change *Balance with a slice of Asset

func (*Balance) Total

func (b *Balance) Total(prices map[string]float64) float64

Total count the total value of balance

type Bar

type Bar struct {
	Begin                  time.Time
	Interval               time.Duration
	Open, High, Low, Close float64 // Price
	Volume                 float64
}

Bar 实现了 k 线的相关方法

type BeginFunc

type BeginFunc func(time.Time, time.Duration) time.Time

BeginFunc 会根据 time 和 interval 计算 time 所在周期的开始时间

type Name

type Name string

Name 枚举了交易所的名称

type Order

type Order struct {
	Symbol      string
	AssetName   string
	CapitalName string
	// if ID is negative value, means unset
	// ID is time.Now().Unix()
	ID   int64
	Side OrderSide
	Type OrderType
	// 根据 Type 的不同,以下 3 个属性不是全都必须的
	AssetQuantity   float64
	AssetPrice      float64
	CapitalQuantity float64
}

Order 是下单的格式

func NewOrder

func NewOrder(symbol, asset, capital string) Order

NewOrder returns a order with default Symbol, Asset, Capital. return value is INCOMPLETE. It need run 'With' method to make a complete copy.

func (Order) IsEmpty added in v0.1.27

func (o Order) IsEmpty() bool

IsEmpty 用于判断 Order 是否是空订单

func (*Order) IsLessThan

func (o *Order) IsLessThan(a *Order) bool

IsLessThan return true if o < a

func (Order) String added in v0.1.25

func (o Order) String() string

func (Order) With

func (o Order) With(apply func(*Order)) *Order

With 可以生成一个根据 apply 实施的新订单

type OrderSide

type OrderSide int8

OrderSide is side of order

const (
	// 在 LIMIT 类型的订单排序中,因为
	// SELL 方是 低 价优先成交
	// BUY  方是 高 价优先成交
	// 让 BUY 成为 -1 的话,可以让
	// order.Side * order.Price 都是从低到高的排序
	SELL OrderSide = 1
	BUY  OrderSide = -1
)

OrderType is side of order

func (OrderSide) String

func (t OrderSide) String() string

type OrderType

type OrderType uint8

OrderType is type of order

const (
	MARKET OrderType = iota + 1
	LIMIT

	// 以下类型是从 binance 抄过来的
	// https://binance-docs.github.io/apidocs/spot/cn/#trade
	STOPloss
	STOPlossLIMIT
	TAKEprofit
	TAKEprofitLIMIT
	LIMITmaker
)

OrderType is type of order 类型值从 iota+1 也就是 1 开始 是为了避开默认的 0 值 OrderType 也代表了 match 时的优先顺序

func (OrderType) String

func (t OrderType) String() string

type Tick

type Tick struct {
	// Exchange Name
	// Symbol    string // like "BTCUSDT"
	// Asset  string // like "BTC"
	ID     int64
	Date   time.Time
	Price  float64
	Volume float64
}

Tick 记录了单笔的交易记录 各个具体交易所的交易记录 要么直接使用 Tick, 要么提供转换到 Tick 函数,

func NewTick

func NewTick(id int64, date time.Time, price, volume float64) Tick

NewTick returns a new tick

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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