Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Apr 12, 2020
1 parent 447961d commit e76d4ac
Show file tree
Hide file tree
Showing 34 changed files with 342 additions and 364 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
## 主要特性
* 使用简单
* Tick级别回测
* 支持实盘
* 支持 WebSocket

## 支持交易所
| name | id | ver | doc |
Expand Down
2 changes: 1 addition & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ See [@live trading](https://github.com/coinrust/crex/blob/master/examples/live/m
### Main Features
* Ease of use.
* Tick-level backtesting.
* Backtesting and live-trading functionality.
* WebSocket Supported

### Supported Exchanges
| name | id | ver | doc |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bitmex_sim_broker
package bitmex_sim

import (
"errors"
Expand All @@ -22,8 +22,8 @@ type MarginInfo struct {
LiquidationPriceShort float64
}

// BitMEXSimBroker the bitmex broker for backtest
type BitMEXSimBroker struct {
// BitMEXSim the bitmex broker for backtest
type BitMEXSim struct {
data *data.Data
makerFeeRate float64 // -0.00025 // Maker fee rate
takerFeeRate float64 // 0.00075 // Taker fee rate
Expand All @@ -36,11 +36,11 @@ type BitMEXSimBroker struct {
symbol string
}

func (b *BitMEXSimBroker) GetName() (name string) {
func (b *BitMEXSim) GetName() (name string) {
return "BitMEX"
}

func (b *BitMEXSimBroker) GetAccountSummary(currency string) (result AccountSummary, err error) {
func (b *BitMEXSim) GetAccountSummary(currency string) (result AccountSummary, err error) {
result.Balance = b.balance
var symbol string
if currency == "XBT" || currency == "BTC" {
Expand All @@ -61,29 +61,29 @@ func (b *BitMEXSimBroker) GetAccountSummary(currency string) (result AccountSumm
return
}

func (b *BitMEXSimBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) {
func (b *BitMEXSim) GetOrderBook(symbol string, depth int) (result OrderBook, err error) {
result = *b.data.GetOrderBook()
return
}

func (b *BitMEXSimBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) {
func (b *BitMEXSim) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) {
return
}

func (b *BitMEXSimBroker) SetContractType(currencyPair string, contractType string) (err error) {
func (b *BitMEXSim) SetContractType(currencyPair string, contractType string) (err error) {
b.symbol = currencyPair
return
}

func (b *BitMEXSimBroker) GetContractID() (symbol string, err error) {
func (b *BitMEXSim) GetContractID() (symbol string, err error) {
return b.symbol, nil
}

func (b *BitMEXSimBroker) SetLeverRate(value float64) (err error) {
func (b *BitMEXSim) SetLeverRate(value float64) (err error) {
return
}

func (b *BitMEXSimBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
func (b *BitMEXSim) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) {
_id, _ := util.NextID()
id := fmt.Sprintf("%v", _id)
Expand Down Expand Up @@ -117,7 +117,7 @@ func (b *BitMEXSimBroker) PlaceOrder(symbol string, direction Direction, orderTy
}

// 撮合成交
func (b *BitMEXSimBroker) matchOrder(order *Order, immediate bool) (err error) {
func (b *BitMEXSim) matchOrder(order *Order, immediate bool) (err error) {
switch order.Type {
case OrderTypeMarket:
err = b.matchMarketOrder(order)
Expand All @@ -127,7 +127,7 @@ func (b *BitMEXSimBroker) matchOrder(order *Order, immediate bool) (err error) {
return
}

func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {
func (b *BitMEXSim) matchMarketOrder(order *Order) (err error) {
if !order.IsOpen() {
return
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {
return
}

func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err error) {
func (b *BitMEXSim) matchLimitOrder(order *Order, immediate bool) (err error) {
if !order.IsOpen() {
return
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err err
}

// 更新持仓
func (b *BitMEXSimBroker) updatePosition(symbol string, size float64, price float64) {
func (b *BitMEXSim) updatePosition(symbol string, size float64, price float64) {
position := b.getPosition(symbol)
if position == nil {
log.Fatalf("position error symbol=%v", symbol)
Expand All @@ -269,7 +269,7 @@ func (b *BitMEXSimBroker) updatePosition(symbol string, size float64, price floa
}

// 增加持仓
func (b *BitMEXSimBroker) addPosition(position *Position, size float64, price float64) (err error) {
func (b *BitMEXSim) addPosition(position *Position, size float64, price float64) (err error) {
if position.Size < 0 && size > 0 || position.Size > 0 && size < 0 {
err = errors.New("方向错误")
return
Expand All @@ -294,7 +294,7 @@ func (b *BitMEXSimBroker) addPosition(position *Position, size float64, price fl
}

// 平仓,超过数量,则开立新仓
func (b *BitMEXSimBroker) closePosition(position *Position, size float64, price float64) (err error) {
func (b *BitMEXSim) closePosition(position *Position, size float64, price float64) (err error) {
if position.Size == 0 {
err = errors.New("当前无持仓")
return
Expand Down Expand Up @@ -328,17 +328,17 @@ func (b *BitMEXSimBroker) closePosition(position *Position, size float64, price
}

// 增加Balance
func (b *BitMEXSimBroker) addBalance(value float64) {
func (b *BitMEXSim) addBalance(value float64) {
b.balance += value
}

// 增加P/L
func (b *BitMEXSimBroker) addPnl(pnl float64) {
func (b *BitMEXSim) addPnl(pnl float64) {
b.balance += pnl
}

// 获取持仓
func (b *BitMEXSimBroker) getPosition(symbol string) *Position {
func (b *BitMEXSim) getPosition(symbol string) *Position {
if position, ok := b.positions[symbol]; ok {
return position
} else {
Expand All @@ -354,7 +354,7 @@ func (b *BitMEXSimBroker) getPosition(symbol string) *Position {
}
}

func (b *BitMEXSimBroker) GetOpenOrders(symbol string) (result []Order, err error) {
func (b *BitMEXSim) GetOpenOrders(symbol string) (result []Order, err error) {
for _, v := range b.openOrders {
if v.Symbol == symbol {
result = append(result, *v)
Expand All @@ -363,7 +363,7 @@ func (b *BitMEXSimBroker) GetOpenOrders(symbol string) (result []Order, err erro
return
}

func (b *BitMEXSimBroker) GetOrder(symbol string, id string) (result Order, err error) {
func (b *BitMEXSim) GetOrder(symbol string, id string) (result Order, err error) {
order, ok := b.orders[id]
if !ok {
err = errors.New("not found")
Expand All @@ -373,7 +373,7 @@ func (b *BitMEXSimBroker) GetOrder(symbol string, id string) (result Order, err
return
}

func (b *BitMEXSimBroker) CancelOrder(symbol string, id string) (result Order, err error) {
func (b *BitMEXSim) CancelOrder(symbol string, id string) (result Order, err error) {
if order, ok := b.orders[id]; ok {
if !order.IsOpen() {
err = errors.New("status error")
Expand All @@ -393,7 +393,7 @@ func (b *BitMEXSimBroker) CancelOrder(symbol string, id string) (result Order, e
return
}

func (b *BitMEXSimBroker) CancelAllOrders(symbol string) (err error) {
func (b *BitMEXSim) CancelAllOrders(symbol string) (err error) {
var idsToBeRemoved []string

for _, order := range b.openOrders {
Expand All @@ -416,11 +416,11 @@ func (b *BitMEXSimBroker) CancelAllOrders(symbol string) (err error) {
return
}

func (b *BitMEXSimBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) {
func (b *BitMEXSim) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) {
return
}

func (b *BitMEXSimBroker) GetPosition(symbol string) (result Position, err error) {
func (b *BitMEXSim) GetPosition(symbol string) (result Position, err error) {
position, ok := b.positions[symbol]
if !ok {
err = errors.New("not found")
Expand All @@ -430,15 +430,15 @@ func (b *BitMEXSimBroker) GetPosition(symbol string) (result Position, err error
return
}

func (b *BitMEXSimBroker) RunEventLoopOnce() (err error) {
func (b *BitMEXSim) RunEventLoopOnce() (err error) {
for _, order := range b.openOrders {
b.matchOrder(order, false)
}
return
}

func NewBroker(data *data.Data, cash float64, makerFeeRate float64, takerFeeRate float64) *BitMEXSimBroker {
return &BitMEXSimBroker{
func New(data *data.Data, cash float64, makerFeeRate float64, takerFeeRate float64) *BitMEXSim {
return &BitMEXSim{
data: data,
balance: cash,
makerFeeRate: makerFeeRate, // -0.00025 // Maker 费率
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bitmex_sim_broker
package bitmex_sim

import (
"github.com/coinrust/crex"
Expand Down
Loading

0 comments on commit e76d4ac

Please sign in to comment.