Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Feb 29, 2020
2 parents dc4b631 + e84a70a commit 50f29f8
Show file tree
Hide file tree
Showing 19 changed files with 280 additions and 100,225 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@
### GoTrader
A real-time quantitative trading/backtesting platform in Golang.

### Standard CSV data types formats
* columns delimiter: , (comma)
* new line marker: \n (LF)
* date time format: Unix time (ms)

### Data format
| column name | description |
| ---------------- |--------------------------------- |
| t | Unix time (ms) |
| asks[0-X].price | asks prices in ascending order |
| asks[0-X].amount | asks amounts in ascending order |
| bids[0-X].price | bids prices in descending order |
| bids[0-X].amount | bids amounts in descending order |

### Sample data rows preview
```csv
t,asks[0].price,asks[0].amount,asks[1].price,asks[1].amount,asks[2].price,asks[2].amount,asks[3].price,asks[3].amount,asks[4].price,asks[4].amount,asks[5].price,asks[5].amount,asks[6].price,asks[6].amount,asks[7].price,asks[7].amount,asks[8].price,asks[8].amount,asks[9].price,asks[9].amount,bids[0].price,bids[0].amount,bids[1].price,bids[1].amount,bids[2].price,bids[2].amount,bids[3].price,bids[3].amount,bids[4].price,bids[4].amount,bids[5].price,bids[5].amount,bids[6].price,bids[6].amount,bids[7].price,bids[7].amount,bids[8].price,bids[8].amount,bids[9].price,bids[9].amount
1569888000143,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,1650,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,90,8300,71320,8299.5,310
1569888000285,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,1650,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000307,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,11010,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000309,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000406,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,8960,8307.5,11010,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000500,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000522,8304.5,10270,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000527,8304.5,10270,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185010,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
```

### Backtesting
See [@backtest](https://github.com/coinrust/gotrader/blob/master/examples/backtest/main.go)

Expand Down
27 changes: 27 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@
### GoTrader
GoTrader 是一个用Golang语言开发的量化平台。支持tick级别数字币期货平台的回测和实盘。

### 标准 CSV 数据格式
* 列定界符: , (逗号)
* 换行标记: \n (LF)
* 日期时间格式: Unix 时间戳 (ms)

### 时间格式
| 列名 | 描述 |
| ---------------- |--------------------------------- |
| t | Unix 时间戳 (ms) |
| asks[0-X].price | 卖单价(升序) |
| asks[0-X].amount | 卖单量 |
| bids[0-X].price | 买单价(降序) |
| bids[0-X].amount | 买单量 |

### 样本数据示例
```csv
t,asks[0].price,asks[0].amount,asks[1].price,asks[1].amount,asks[2].price,asks[2].amount,asks[3].price,asks[3].amount,asks[4].price,asks[4].amount,asks[5].price,asks[5].amount,asks[6].price,asks[6].amount,asks[7].price,asks[7].amount,asks[8].price,asks[8].amount,asks[9].price,asks[9].amount,bids[0].price,bids[0].amount,bids[1].price,bids[1].amount,bids[2].price,bids[2].amount,bids[3].price,bids[3].amount,bids[4].price,bids[4].amount,bids[5].price,bids[5].amount,bids[6].price,bids[6].amount,bids[7].price,bids[7].amount,bids[8].price,bids[8].amount,bids[9].price,bids[9].amount
1569888000143,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,1650,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,90,8300,71320,8299.5,310
1569888000285,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,1650,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000307,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,11010,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000309,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000406,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,8960,8307.5,11010,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000500,8304.5,7010,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000522,8304.5,10270,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185750,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
1569888000527,8304.5,10270,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,68260,8308.5,120000,8309,38400,8309.5,8400,8304,185010,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,8301,18000,8300.5,5090,8300,71320,8299.5,310
```

### 回测
示例 [@backtest](https://github.com/coinrust/gotrader/blob/master/examples/backtest/main.go)

Expand Down
2 changes: 1 addition & 1 deletion broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Broker interface {
GetOrderBook(symbol string, depth int) (result OrderBook, err error)

// 下单
PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, size float64,
PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64,
postOnly bool, reduceOnly bool) (result Order, err error)

// 获取活跃委托单列表
Expand Down
8 changes: 6 additions & 2 deletions brokers/bitmex-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (b *BitMEXBroker) GetOrderBook(symbol string, depth int) (result OrderBook,
}

func (b *BitMEXBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var side string
var _orderType string
if direction == Buy {
Expand All @@ -63,6 +63,10 @@ func (b *BitMEXBroker) PlaceOrder(symbol string, direction Direction, orderType
_orderType = bitmex.ORD_TYPE_LIMIT
} else if orderType == OrderTypeMarket {
_orderType = bitmex.ORD_TYPE_MARKET
} else if orderType == OrderTypeStopLimit {
_orderType = bitmex.ORD_TYPE_STOP_LIMIT
} else if orderType == OrderTypeStopMarket {
_orderType = bitmex.ORD_TYPE_STOP
}
var execInst string
if postOnly {
Expand All @@ -75,7 +79,7 @@ func (b *BitMEXBroker) PlaceOrder(symbol string, direction Direction, orderType
execInst += "ReduceOnly"
}
var order swagger.Order
order, err = b.client.PlaceOrder(side, _orderType, 0, price, int32(size), "", execInst, symbol)
order, err = b.client.PlaceOrder(side, _orderType, stopPx, price, int32(size), "", execInst, symbol)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion brokers/bitmex-sim-broker/simbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (b *BitMEXSimBroker) GetOrderBook(symbol string, depth int) (result OrderBo
}

func (b *BitMEXSimBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
_id, _ := util2.NextID()
id := fmt.Sprintf("%v", _id)
order := &Order{
Expand Down
70 changes: 69 additions & 1 deletion brokers/bybit-broker/broker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bybit_broker

import (
"errors"
. "github.com/coinrust/gotrader/models"
"github.com/frankrap/bybit-api/rest"
"strings"
Expand Down Expand Up @@ -54,6 +55,18 @@ func (b *BybitBroker) GetOrderBook(symbol string, depth int) (result OrderBook,
}

func (b *BybitBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
if orderType == OrderTypeLimit || orderType == OrderTypeMarket {
return b.placeOrder(symbol, direction, orderType, price, size, postOnly, reduceOnly)
} else if orderType == OrderTypeStopLimit || orderType == OrderTypeStopMarket {
return b.placeStopOrder(symbol, direction, orderType, price, stopPx, size, postOnly, reduceOnly)
} else {
err = errors.New("error")
return
}
}

func (b *BybitBroker) placeOrder(symbol string, direction Direction, orderType OrderType, price float64,
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var side string
var _orderType string
Expand All @@ -76,7 +89,58 @@ func (b *BybitBroker) PlaceOrder(symbol string, direction Direction, orderType O
timeInForce = "GoodTillCancel"
}
var order rest.Order
order, err = b.client.CreateOrder(side, _orderType, price, int(size), timeInForce, reduceOnly, symbol)
order, err = b.client.CreateOrder(
side,
_orderType,
price,
int(size),
timeInForce,
reduceOnly,
symbol,
)
if err != nil {
return
}
result = b.convertOrder(&order)
return
}

func (b *BybitBroker) placeStopOrder(symbol string, direction Direction, orderType OrderType, price float64,
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var side string
var _orderType string
var timeInForce string

if direction == Buy {
side = "Buy"
} else if direction == Sell {
side = "Sell"
}
if orderType == OrderTypeStopLimit {
_orderType = "Limit"
} else if orderType == OrderTypeStopMarket {
_orderType = "Market"
}

if postOnly {
timeInForce = "PostOnly"
} else {
timeInForce = "GoodTillCancel"
}
basePrice := stopPx // 触发价
var order rest.Order
order, err = b.client.CreateStopOrder(
side,
_orderType,
price,
basePrice,
0,
int(size),
"",
timeInForce,
reduceOnly,
symbol,
)
if err != nil {
return
}
Expand Down Expand Up @@ -198,6 +262,10 @@ func (b *BybitBroker) orderStatus(order *rest.Order) OrderStatus {
return OrderStatusCancelled
case "Rejected":
return OrderStatusRejected
case "Untriggered":
return OrderStatusUntriggered
case "Triggered":
return OrderStatusTriggered
default:
return OrderStatusCreated
}
Expand Down
14 changes: 10 additions & 4 deletions brokers/deribit-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,18 @@ func (b *DiribitBroker) GetOrderBook(symbol string, depth int) (result OrderBook
}

func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var _orderType string
if orderType == OrderTypeLimit {
_orderType = models.OrderTypeLimit
stopPx = 0
} else if orderType == OrderTypeMarket {
_orderType = models.OrderTypeMarket
stopPx = 0
} else if orderType == OrderTypeStopLimit {
_orderType = models.OrderTypeStopLimit
} else if orderType == OrderTypeStopMarket {
_orderType = models.OrderTypeStopMarket
}
if direction == Buy {
var ret models.BuyResponse
Expand All @@ -113,7 +119,7 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType
//MaxShow: nil,
PostOnly: postOnly,
ReduceOnly: reduceOnly,
//StopPrice: 0,
StopPrice: stopPx,
//Trigger: "",
//Advanced: "",
})
Expand All @@ -133,7 +139,7 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType
//MaxShow: nil,
PostOnly: postOnly,
ReduceOnly: reduceOnly,
//StopPrice: 0,
StopPrice: stopPx,
//Trigger: "",
//Advanced: "",
})
Expand Down Expand Up @@ -281,7 +287,7 @@ func (b *DiribitBroker) orderStatus(order *models.Order) OrderStatus {
case models.OrderStateCancelled:
return OrderStatusCancelled
case models.OrderStateUntriggered:
return OrderStatusCreated
return OrderStatusUntriggered
default:
return OrderStatusCreated
}
Expand Down
2 changes: 1 addition & 1 deletion brokers/deribit-sim-broker/simbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (b *DiribitSimBroker) GetOrderBook(symbol string, depth int) (result OrderB
}

func (b *DiribitSimBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
_id, _ := util2.NextID()
id := fmt.Sprintf("%v", _id)
order := &Order{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
t,asks[0].price,asks[0].amount,asks[1].price,asks[1].amount,asks[2].price,asks[2].amount,asks[3].price,asks[3].amount,asks[4].price,asks[4].amount,asks[5].price,asks[5].amount,asks[6].price,asks[6].amount,asks[7].price,asks[7].amount,asks[8].price,asks[8].amount,asks[9].price,asks[9].amount,bids[0].price,bids[0].amount,bids[1].price,bids[1].amount,bids[2].price,bids[2].amount,bids[3].price,bids[3].amount,bids[4].price,bids[4].amount,bids[5].price,bids[5].amount,bids[6].price,bids[6].amount,bids[7].price,bids[7].amount,bids[8].price,bids[8].amount,bids[9].price,bids[9].amount
1569888000178,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362,200,8362.5,3000,8364.5,1150,8365,18860,8356.5,48990,8356,71120,8355.5,400,8355,2220,8354.5,4030,8354,4420,8353.5,3040,8353,12660,8352.5,39340,8352,32000
1569888000423,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,19060,8365.5,700,8356.5,48990,8356,71120,8355.5,400,8355,2220,8354.5,4030,8354,4420,8353.5,3040,8353,12660,8352.5,39340,8352,32000
1569888000439,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,19060,8365.5,700,8356.5,48990,8356,71520,8355,2220,8354.5,4030,8354,4420,8353.5,3040,8353,12660,8352.5,39340,8352,32000,8351.5,1500
1569888001507,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,19060,8365.5,700,8356.5,48990,8356,71520,8355,2220,8354.5,4030,8354,4420,8353.5,5540,8353,12660,8352.5,36840,8352,32000,8351.5,1500
1569888002135,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,19060,8365.5,700,8356.5,48990,8356,71520,8355,2220,8354.5,4030,8354,4420,8353.5,3040,8353,12660,8352.5,36840,8352,32000,8351.5,4000
1569888002527,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8356.5,48990,8356,71520,8355,2220,8354.5,4030,8354,4420,8353.5,3040,8353,12660,8352.5,36840,8352,32000,8351.5,4000
1569888003258,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8356.5,48990,8356,71520,8355,2220,8354.5,4030,8354,4420,8353.5,3040,8353,2660,8352.5,36840,8352,32000,8351.5,4000
1569888003259,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8356.5,48990,8356,71520,8355,2220,8354.5,4030,8354,4420,8353.5,13040,8353,2660,8352.5,36840,8352,32000,8351.5,4000
1569888003778,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8356.5,48990,8356,71520,8355.5,32000,8355,2220,8354.5,4030,8354,4420,8353.5,13040,8353,2660,8352.5,4840,8352,32000
1569888003785,8357,3000,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8356.5,48990,8356,71520,8355.5,32000,8355,2220,8354.5,4030,8354,36420,8353.5,13040,8353,2660,8352.5,4840,8351.5,4000
1569888003787,8357,1590,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8356.5,48990,8356,71520,8355.5,32000,8355,2220,8354.5,4030,8354,36420,8353.5,13040,8353,2660,8352.5,4840,8351.5,4000
1569888003808,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8366,3590,8356.5,48990,8356,71520,8355.5,32000,8355,2220,8354.5,4030,8354,36420,8353.5,13040,8353,2660,8352.5,4840,8351.5,4000
1569888003832,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8366,3590,8357,900,8356.5,48090,8356,71520,8355.5,32000,8355,2220,8354.5,4030,8354,36420,8353.5,13040,8353,2660,8352.5,4840
1569888003844,8357.5,250,8358.5,500,8359,3000,8361,20,8361.5,200,8362.5,3000,8364.5,1150,8365,9060,8365.5,700,8366,3590,8357,1210,8356.5,47780,8356,71520,8355.5,32000,8355,2220,8354.5,4030,8354,36420,8353.5,13040,8353,2660,8352.5,4840
Loading

0 comments on commit 50f29f8

Please sign in to comment.