first commit

This commit is contained in:
lychiyu 2024-06-25 21:52:58 +08:00
commit eef261948d
9 changed files with 328 additions and 0 deletions

19
balance.go Normal file
View File

@ -0,0 +1,19 @@
package trademodel
import "fmt"
type Currency struct {
Coin string
Base string
}
func (currency Currency) String() string {
return fmt.Sprintf("%s_%s", currency.Coin, currency.Base)
}
type Balance struct {
Currency string
Available float64
Frozen float64
Balance float64
}

87
candle.go Normal file
View File

@ -0,0 +1,87 @@
package trademodel
import (
"fmt"
"time"
)
type Candle struct {
ID int64 `xorm:"pk autoincr null 'id'"`
Start int64 `xorm:"unique index 'start'"`
Open float64 `xorm:"notnull 'open'"`
High float64 `xorm:"notnull 'high'"`
Low float64 `xorm:"notnull 'low'"`
Close float64 `xorm:"notnull 'close'"`
Volume float64 `xorm:"notnull 'volume'"`
Turnover float64 `xorm:"turnover 'turnover'"`
Trades int64 `xorm:"notnull 'trades'"`
Table string `xorm:"-"`
}
func (c Candle) TableName() string {
return c.Table
}
func (c Candle) GetTable() string {
return c.Table
}
func (c Candle) GetStart() int64 {
return c.Start
}
func (c *Candle) SetTable(tbl string) {
c.Table = tbl
}
func (c Candle) Time() time.Time {
return time.Unix(c.Start, 0)
}
func (c Candle) String() string {
return fmt.Sprintf("%s open:%f close:%f low:%f high:%f volume:%f trades:%d turnover: %f", c.Time().String(), c.Open, c.Close, c.Low, c.High, c.Volume, c.Trades, c.Turnover)
}
// CandleList candle list
type CandleList []*Candle
// Merge merge multi candle to one
func (l CandleList) Merge() (ret *Candle) {
if len(l) == 0 {
return
}
ret = new(Candle)
ret.Start = l[0].Start
ret.Open = l[0].Open
ret.High = l.High()
ret.Low = l.Low()
ret.Close = l[len(l)-1].Close
for _, v := range l {
ret.Turnover = v.Turnover
ret.Volume += v.Volume
ret.Trades += v.Trades
}
return
}
func (l CandleList) High() (ret float64) {
for _, v := range l {
if ret < v.High {
ret = v.High
}
}
return
}
func (l CandleList) Low() (ret float64) {
for _, v := range l {
if ret == 0 {
ret = v.Low
continue
}
if ret > v.Low {
ret = v.Low
}
}
return
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module trademodel
go 1.22.0

21
order.go Normal file
View File

@ -0,0 +1,21 @@
package trademodel
import "time"
var (
OrderStatusFilled = "FILLED"
OrderStatusCanceled = "CANCELED"
)
type Order struct {
OrderID string
Symbol string
Currency string
Amount float64
Price float64
Status string
Side string
Time time.Time
Remark string
Filled float64
}

16
orderbook.go Normal file
View File

@ -0,0 +1,16 @@
package trademodel
import "time"
type DepthInfo struct {
Price float64
Amount float64
}
type Depth OrderBook
type OrderBook struct {
Sells []DepthInfo
Buys []DepthInfo
UpdateTime time.Time
}

14
pos.go Normal file
View File

@ -0,0 +1,14 @@
package trademodel
const (
Long = 1
Short = 2
)
type Position struct {
Symbol string
Type int // 合约类型Long: 多头Short: 空头
Hold float64 // 持有仓位
Price float64 //开仓价格
ProfitRatio float64 // 盈利比例,正数表示盈利,负数表示亏岁
}

45
symbol.go Normal file
View File

@ -0,0 +1,45 @@
package trademodel
import (
"strings"
"time"
)
const (
SymbolTypeSpot = "spot"
SymbolTypeIndex = "index"
SymbolTypeFutures = "futures"
)
type Symbol struct {
Name string `json:"name"`
Symbol string `json:"code"`
Exchange string `json:"exchange"`
Description string `json:"description"`
Type string `json:"type"`
// 交易时间
Session string `json:"session"`
// price decimal places
Precision int `json:"precision"`
// amount decimal places
AmountPrecision int `json:"volume_precision"`
Expired bool `json:"expired"`
// only use if expired = true
ExpirationDate time.Time `json:"expiration_date"`
Resolutions string `json:"resolutions"`
// mini size between two price
PriceStep float64 `json:"price_step"`
// mini size between two amount
AmountStep float64 `json:"amount"`
}
func (s *Symbol) GetResolutions() []string {
return strings.Split(s.Resolutions, ",")
}
func (s *Symbol) FixPrice(price float64) float64 {
return float64(int(price/s.PriceStep)) * s.PriceStep
}

11
ticker.go Normal file
View File

@ -0,0 +1,11 @@
package trademodel
type Ticker struct {
CurrencyPair string
Last float64
High float64
Low float64
Bid float64
Ask float64
Volume float64
}

112
trade.go Normal file
View File

@ -0,0 +1,112 @@
package trademodel
import (
"fmt"
"time"
)
type TradeType int
const (
CancelOne TradeType = -2
CancelAll TradeType = -1
DirectLong TradeType = 1
DirectShort TradeType = 1 << 1
Limit TradeType = 1 << 3
Market TradeType = 1 << 4
Stop TradeType = 1 << 5
Open TradeType = 1 << 6
Close TradeType = 1 << 7
OpenLong = Open | DirectLong
OpenShort = Open | DirectShort
CloseLong = Close | DirectLong
CloseShort = Close | DirectShort // 130
StopLong = Stop | DirectLong // 33
StopShort = Stop | DirectShort // 34
)
func (t TradeType) String() (ret string) {
if t&Limit == Limit {
ret += "Limit"
} else if t&Market == Market {
ret += "Market"
} else if t&Stop == Stop {
ret += "Stop"
}
if t&Open == Open {
ret += "Open"
} else if t&Close == Close {
ret += "Close"
}
if t&DirectLong == DirectLong {
ret += "Long"
} else if t&DirectShort == DirectShort {
ret += "Short"
}
return
}
func NewTradeType(typ string) (t TradeType, err error) {
switch typ {
case "OpenLong":
t = OpenLong
case "OpenShort":
t = OpenShort
case "CloseLong":
t = CloseLong
case "CloseShort":
t = CloseShort
case "StopLong":
t = StopLong
case "StopShort":
t = StopShort
default:
err = fmt.Errorf("unsupport trade type: %d", t)
}
return
}
type Trade struct {
ID string
Action TradeType
Time time.Time
Price float64
Amount float64
Side string
Remark string
}
// TradeAction trade action
type TradeAction struct {
ID string
Action TradeType
Amount float64
Price float64
Time time.Time
Symbol string
}
func (a TradeType) IsLong() bool {
if a&OpenLong == OpenLong || a&CloseShort == CloseShort || a&StopShort == StopShort {
return true
}
return false
}
func (a TradeType) IsOpen() bool {
if a&Open == Open {
return true
}
return false
}
func (a TradeType) IsStop() bool {
if a&Stop == Stop {
return true
}
return false
}