Skip to content

Commit

Permalink
Update interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Feb 29, 2020
1 parent 50f29f8 commit 67a1d7e
Show file tree
Hide file tree
Showing 26 changed files with 242 additions and 164 deletions.
2 changes: 1 addition & 1 deletion models/accountsummary.go → accountsummary.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package gotrader

type AccountSummary struct {
Balance float64
Expand Down
16 changes: 8 additions & 8 deletions backtest/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (b *Backtest) runEventLoopOnce() {
}

func (b *Backtest) addItemStats() {
tick := b.data.GetTick()
tm := tick.Timestamp
ob := b.data.GetOrderBook()
tm := ob.Time
update := false
timestamp := time.Date(tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute()+1, 0, 0, time.UTC)
var lastItem *LogItem
Expand All @@ -73,17 +73,17 @@ func (b *Backtest) addItemStats() {
var item *LogItem
if update {
item = lastItem
item.RawTime = tick.Timestamp
item.Ask = tick.Ask
item.Bid = tick.Bid
item.RawTime = ob.Time
item.Ask = ob.AskPrice()
item.Bid = ob.BidPrice()
item.Stats = nil
b.fetchItemStats(item)
} else {
item = &LogItem{
Time: timestamp,
RawTime: tick.Timestamp,
Ask: tick.Ask,
Bid: tick.Bid,
RawTime: ob.Time,
Ask: ob.AskPrice(),
Bid: ob.BidPrice(),
Stats: nil,
}
b.fetchItemStats(item)
Expand Down
2 changes: 1 addition & 1 deletion models/bar.go → bar.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package gotrader

type Bar struct {
Event
Expand Down
4 changes: 0 additions & 4 deletions broker.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package gotrader

import (
. "github.com/coinrust/gotrader/models"
)

type Broker interface {
// 订阅事件
Subscribe(event string, param string, listener interface{})
Expand Down
2 changes: 1 addition & 1 deletion brokers/bitmex-broker/broker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bitmex_broker

import (
. "github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"github.com/frankrap/bitmex-api"
"github.com/frankrap/bitmex-api/swagger"
"strings"
Expand Down
4 changes: 2 additions & 2 deletions brokers/bitmex-broker/broker_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bitmex_broker

import (
"github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"github.com/frankrap/bitmex-api"
"testing"
)
Expand All @@ -26,7 +26,7 @@ func TestBitMEXBroker_GetOrderBook(t *testing.T) {

func TestBitMEXBroker_PlaceOrder(t *testing.T) {
b := newBrokerForTest()
order, err := b.PlaceOrder("XBTUSD", models.Buy, models.OrderTypeLimit, 8000, 10, true, false)
order, err := b.PlaceOrder("XBTUSD", Buy, OrderTypeLimit, 8000, 0, 10, true, false)
if err != nil {
t.Error(err)
return
Expand Down
10 changes: 6 additions & 4 deletions brokers/bitmex-sim-broker/helper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package bitmex_sim_broker

import "github.com/coinrust/gotrader/models"
import (
"github.com/coinrust/gotrader"
)

// 计算收益
// pnl: 收益(BTC/ETH)
// pnlUsd: 收益(USD)
func CalcPnl(side models.Direction, positionSize float64, entryPrice float64, exitPrice float64) (pnl float64, pnlUsd float64) {
func CalcPnl(side gotrader.Direction, positionSize float64, entryPrice float64, exitPrice float64) (pnl float64, pnlUsd float64) {
//side := "Short" // "Short"
//positionSize := 3850.0
//entryPrice := 3850.0
Expand All @@ -15,10 +17,10 @@ func CalcPnl(side models.Direction, positionSize float64, entryPrice float64, ex
if positionSize == 0 {
return
}
if side == models.Buy {
if side == gotrader.Buy {
pnl = (((entryPrice - exitPrice) / exitPrice) * (positionSize / entryPrice)) * -1
pnlUsd = ((entryPrice - exitPrice) * (positionSize / entryPrice)) * -1
} else if side == models.Sell {
} else if side == gotrader.Sell {
pnl = ((entryPrice - exitPrice) / exitPrice) * (positionSize / entryPrice)
pnlUsd = (entryPrice - exitPrice) * (positionSize / entryPrice)
}
Expand Down
35 changes: 13 additions & 22 deletions brokers/bitmex-sim-broker/simbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package bitmex_sim_broker
import (
"errors"
"fmt"
. "github.com/coinrust/gotrader"
"github.com/coinrust/gotrader/data"
. "github.com/coinrust/gotrader/models"
"github.com/coinrust/gotrader/util2"
"log"
"math"
Expand Down Expand Up @@ -46,12 +46,12 @@ func (b *BitMEXSimBroker) GetAccountSummary(currency string) (result AccountSumm
}
position := b.getPosition(symbol)
var price float64
tick := b.data.GetTick()
ob := b.data.GetOrderBook()
side := position.Side()
if side == Buy {
price = tick.Ask
price = ob.AskPrice()
} else if side == Sell {
price = tick.Bid
price = ob.BidPrice()
}
pnl, _ := CalcPnl(side, math.Abs(position.Size), position.AvgPrice, price)
result.Pnl = pnl
Expand All @@ -60,16 +60,7 @@ func (b *BitMEXSimBroker) GetAccountSummary(currency string) (result AccountSumm
}

func (b *BitMEXSimBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) {
tick := b.data.GetTick()
result.Time = tick.Timestamp
result.Asks = []Item{{
Price: tick.Ask,
Amount: float64(tick.AskVolume),
}}
result.Bids = []Item{{
Price: tick.Bid,
Amount: float64(tick.BidVolume),
}}
result = *b.data.GetOrderBook()
return
}

Expand Down Expand Up @@ -135,7 +126,7 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {
return
}

tick := b.data.GetTick()
ob := b.data.GetOrderBook()

// 判断开仓数量
margin := b.balance
Expand All @@ -147,13 +138,13 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {

// 市价成交
if order.Direction == Buy {
maxSize = margin * 100 * tick.Ask
maxSize = margin * 100 * ob.AskPrice()
if order.Size > maxSize {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxSize))
return
}

price := tick.Ask
price := ob.AskPrice()
size := order.Size

// trade fee
Expand All @@ -165,13 +156,13 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {
// Update position
b.updatePosition(order.Symbol, size, price)
} else if order.Direction == Sell {
maxSize = margin * 100 * tick.Bid
maxSize = margin * 100 * ob.BidPrice()
if order.Size > maxSize {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxSize))
return
}

price := tick.Bid
price := ob.BidPrice()
size := order.Size

// trade fee
Expand All @@ -191,9 +182,9 @@ func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err err
return
}

tick := b.data.GetTick()
ob := b.data.GetOrderBook()
if order.Direction == Buy { // Bid order
if order.Price >= tick.Ask {
if order.Price >= ob.AskPrice() {
if immediate && order.PostOnly {
order.Status = OrderStatusRejected
return
Expand All @@ -217,7 +208,7 @@ func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err err
b.updatePosition(order.Symbol, size, order.Price)
}
} else { // Ask order
if order.Price <= tick.Bid {
if order.Price <= ob.BidPrice() {
if immediate && order.PostOnly {
order.Status = OrderStatusRejected
return
Expand Down
2 changes: 1 addition & 1 deletion brokers/bybit-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bybit_broker

import (
"errors"
. "github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"github.com/frankrap/bybit-api/rest"
"strings"
)
Expand Down
9 changes: 6 additions & 3 deletions brokers/deribit-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package deribit_broker

import (
"github.com/chuckpreslar/emission"
. "github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"github.com/frankrap/deribit-api"
"github.com/frankrap/deribit-api/models"
"time"
Expand Down Expand Up @@ -96,6 +96,7 @@ func (b *DiribitBroker) GetOrderBook(symbol string, depth int) (result OrderBook
func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var _orderType string
var trigger string
if orderType == OrderTypeLimit {
_orderType = models.OrderTypeLimit
stopPx = 0
Expand All @@ -104,8 +105,10 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType
stopPx = 0
} else if orderType == OrderTypeStopLimit {
_orderType = models.OrderTypeStopLimit
trigger = models.TriggerTypeLastPrice
} else if orderType == OrderTypeStopMarket {
_orderType = models.OrderTypeStopMarket
trigger = models.TriggerTypeLastPrice
}
if direction == Buy {
var ret models.BuyResponse
Expand All @@ -120,7 +123,7 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType
PostOnly: postOnly,
ReduceOnly: reduceOnly,
StopPrice: stopPx,
//Trigger: "",
Trigger: trigger,
//Advanced: "",
})
if err != nil {
Expand All @@ -140,7 +143,7 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType
PostOnly: postOnly,
ReduceOnly: reduceOnly,
StopPrice: stopPx,
//Trigger: "",
Trigger: trigger,
//Advanced: "",
})
if err != nil {
Expand Down
33 changes: 28 additions & 5 deletions brokers/deribit-broker/broker_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package deribit_broker

import (
. "github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"github.com/frankrap/deribit-api"
"log"
"testing"
"time"
)

func TestDiribitBroker_GetOrderBook(t *testing.T) {
func newBroker() Broker {
apiKey := "AsJTU16U"
secretKey := "mM5_K8LVxztN6TjjYpv_cJVGQBvk4jglrEpqkw1b87U"
b := NewBroker(deribit.TestBaseURL, apiKey, secretKey)
return b
}

func TestDiribitBroker_GetOrderBook(t *testing.T) {
b := newBroker()
b.GetOrderBook("BTC-PERPETUAL", 10)
}

func TestDiribitBroker_Subscribe(t *testing.T) {
apiKey := "AsJTU16U"
secretKey := "mM5_K8LVxztN6TjjYpv_cJVGQBvk4jglrEpqkw1b87U"
b := NewBroker(deribit.TestBaseURL, apiKey, secretKey)
b := newBroker()
//event := "book.ETH-PERPETUAL.100.1.100ms"
param := "book.BTC-PERPETUAL.100ms"
b.Subscribe("orderbook", param, func(e *OrderBook) {
Expand All @@ -29,3 +32,23 @@ func TestDiribitBroker_Subscribe(t *testing.T) {
time.Sleep(1 * time.Second)
}
}

func TestDiribitBroker_PlaceStopOrder(t *testing.T) {
b := newBroker()
order, err := b.PlaceOrder(
"BTC-PERPETUAL",
Buy,
OrderTypeStopMarket,
0,
8900,
10,
false,
false,
)
if err != nil {
t.Error(err)
return
}
t.Logf("%#v", order)
t.Logf("Status: %v", order.Status.String())
}
2 changes: 1 addition & 1 deletion brokers/deribit-broker/orderbook_local.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package deribit_broker

import (
. "github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"github.com/frankrap/deribit-api/models"
"sort"
"strconv"
Expand Down
2 changes: 1 addition & 1 deletion brokers/deribit-broker/orderbook_manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package deribit_broker

import (
. "github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"github.com/frankrap/deribit-api/models"
"sync"
)
Expand Down
10 changes: 6 additions & 4 deletions brokers/deribit-sim-broker/helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package deribit_sim_broker

import "github.com/coinrust/gotrader/models"
import (
. "github.com/coinrust/gotrader"
)

//im := (0.01 + sizeCurrency*0.00005) * sizeCurrency
//t.Logf("IM: %v/%v", 0, im) // 保留9位小数,四舍五入
Expand All @@ -12,7 +14,7 @@ import "github.com/coinrust/gotrader/models"
// 计算收益
// pnl: 收益(BTC/ETH)
// pnlUsd: 收益(USD)
func CalcPnl(side models.Direction, positionSize float64, entryPrice float64, exitPrice float64) (pnl float64, pnlUsd float64) {
func CalcPnl(side Direction, positionSize float64, entryPrice float64, exitPrice float64) (pnl float64, pnlUsd float64) {
//side := "Short" // "Short"
//positionSize := 3850.0
//entryPrice := 3850.0
Expand All @@ -22,10 +24,10 @@ func CalcPnl(side models.Direction, positionSize float64, entryPrice float64, ex
if positionSize == 0 {
return
}
if side == models.Buy {
if side == Buy {
pnl = (((entryPrice - exitPrice) / exitPrice) * (positionSize / entryPrice)) * -1
pnlUsd = ((entryPrice - exitPrice) * (positionSize / entryPrice)) * -1
} else if side == models.Sell {
} else if side == Sell {
pnl = ((entryPrice - exitPrice) / exitPrice) * (positionSize / entryPrice)
pnlUsd = (entryPrice - exitPrice) * (positionSize / entryPrice)
}
Expand Down
4 changes: 2 additions & 2 deletions brokers/deribit-sim-broker/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package deribit_sim_broker

import (
"github.com/coinrust/gotrader/models"
. "github.com/coinrust/gotrader"
"testing"
)

func TestCalcPnl(t *testing.T) {
size := 50.0
entryPrice := 10351.5
exitPrice := 10348.5
pnl, pnlUsd := CalcPnl(models.Buy, size, entryPrice, exitPrice)
pnl, pnlUsd := CalcPnl(Buy, size, entryPrice, exitPrice)
t.Logf("pnl: %.8f", pnl)
t.Logf("pnlUsd: %.8f", pnlUsd)
}
Loading

0 comments on commit 67a1d7e

Please sign in to comment.