Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed May 16, 2020
1 parent a49a4df commit 9cf4593
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 14 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
## 回测
示例 [@backtest](https://github.com/coinrust/crex/blob/master/examples/backtest/main.go)

## 实盘
示例 [@live trading](https://github.com/coinrust/crex/blob/master/examples/live/main.go)
### 交易结果
<div align=center><img src="https://raw.githubusercontent.com/coinrust/crex/master/images/trade_result.png" /></div>
<div align=center><img src="https://raw.githubusercontent.com/coinrust/crex/master/images/trade_history_report.png" /></div>

## 开源策略
[https://github.com/coinrust/trading-strategies](https://github.com/coinrust/trading-strategies)
Expand Down Expand Up @@ -162,7 +163,7 @@ func main() {
```

## 回测数据
### 标准 CSV 数据格式
### 1. 标准 CSV 数据格式
* 列定界符: , (逗号)
* 换行标记: \n (LF)
* 日期时间格式: Unix 时间戳 (ms)
Expand All @@ -189,6 +190,23 @@ t,asks[0].price,asks[0].amount,asks[1].price,asks[1].amount,asks[2].price,asks[2
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
```

### 2. MongoDB (推荐)
* Database. for example: tick_db
* Collection. for example: deribit:BTC-PERPETUAL
* Document:
t: timestamp (ms)
a: asks Array [[ask_price_0,ask_amount_0],[ask_price_1,ask_amount_1],...]
b: bids Array [[bid_price_0,ask_amount_0],[bid_price_1,bid_amount_1],...]
```json
{"_id":{"$oid":"5eb946d88316c7a9c541705c"},"t":{"$numberLong":"1569888000143"},"a":[[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]],"b":[[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]]}
```

### 数据处理示例
Example of importing data into a database:
cd ./cmd/deribit-data-to-db
go build
./deribit-data-to-db

## TODO
* Paper trading.

Expand Down
22 changes: 20 additions & 2 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
### Backtesting
See [@backtest](https://github.com/coinrust/crex/blob/master/examples/backtest/main.go)

### Live trading
See [@live trading](https://github.com/coinrust/crex/blob/master/examples/live/main.go)
### trade results:
<div align=center><img src="https://raw.githubusercontent.com/coinrust/crex/master/images/trade_result.png" /></div>
<div align=center><img src="https://raw.githubusercontent.com/coinrust/crex/master/images/trade_history_report.png" /></div>

## Open-source trading strategies
[https://github.com/coinrust/trading-strategies](https://github.com/coinrust/trading-strategies)
Expand Down Expand Up @@ -188,6 +189,23 @@ t,asks[0].price,asks[0].amount,asks[1].price,asks[1].amount,asks[2].price,asks[2
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
```

### 2. MongoDB (recommend)
* Database. for example: tick_db
* Collection. for example: deribit:BTC-PERPETUAL
* Document:
t: timestamp (ms)
a: asks Array [[ask_price_0,ask_amount_0],[ask_price_1,ask_amount_1],...]
b: bids Array [[bid_price_0,ask_amount_0],[bid_price_1,bid_amount_1],...]
```json
{"_id":{"$oid":"5eb946d88316c7a9c541705c"},"t":{"$numberLong":"1569888000143"},"a":[[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]],"b":[[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]]}
```

### Data processing example
Example of importing data into a database:
cd ./cmd/deribit-data-to-db
go build
./deribit-data-to-db

### TODO
* Paper trading.

Expand Down
172 changes: 172 additions & 0 deletions cmd/deribit-data-to-db/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package main

import (
"bufio"
"flag"
"github.com/spf13/cast"
"io"
"log"
"os"
"strconv"
"strings"
"time"
)

var (
input string
output string
sample int

depth int // 10档
mode int // 0-42列 1-41列
database string
exchange string
symbol string

fileCache = map[string]*os.File{}

store *Store
n int
)

// --m 1

func main() {
flag.StringVar(&input, "i", `../../data-samples/deribit/deribit_BTC-PERPETUAL_and_futures_tick_by_tick_book_snapshots_10_levels_2019-10-01_2019-11-01.csv`, "")
flag.StringVar(&output, "o", `./BTC-PERPETUAL`, "")
flag.IntVar(&sample, "s", 0, "")
flag.IntVar(&depth, "d", 10, "")
flag.IntVar(&mode, "m", 1, "")
flag.StringVar(&database, "db", "tick_db", "")
flag.StringVar(&exchange, "e", "deribit", "")
flag.StringVar(&symbol, "symbol", "BTC-PERPETUAL", "")
flag.Parse()

store = NewStore("mongodb://localhost:27017",
database)

defer store.Close()

store.GetCollection(exchange, symbol, true)

// headerString = "symbol,timestamp,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"
//timestamp(ms),ask_price_0,ask_size_0,bid_price_0,bid_size_0,ask_price_1,ask_size_1,bid_price_1,bid_size_1,...
n = 0
err := ReadFile(input, handleLine)
if err != nil {
log.Fatal(err)
}

return
}

func ReadFile(filePath string, handle func(string) bool) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()

buf := bufio.NewReader(f)

for {
rawLine, _, err := buf.ReadLine()
line := strings.TrimSpace(string(rawLine))
if !handle(line) {
break
}
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
return nil
}

func handleLine(s string) bool {
ss := strings.Split(s, ",")
if mode == 0 && len(ss) != 42 || mode == 1 && len(ss) != 41 {
log.Printf("[line: %v]", s)
return false
}

// 或略标题行
if mode == 0 && ss[1] == "timestamp" {
return true
} else if mode == 1 && ss[0] == "t" {
return true
}

writeTo(symbol, ss...)

if sample != 0 {
n++
if n >= sample {
return false
}
}
return true
}

func writeTo(symbol string, ss ...string) {
// BTC-PERPETUAL,2019-10-01T00:00:00.531Z,8304.5,10270,8305,60,8305.5,1220,8306,80,8307,200,8307.5,20370,8308,65760,8308.5,120000,8309,38400,8309.5,8400,8304,185010,8303.5,52200,8303,20600,8302.5,4500,8302,2000,8301.5,18200,83 01,18000,8300.5,5090,8300,71320,8299.5,310
//log.Printf("line=%v", ss)
var time2 time.Time
var err error
if mode == 0 {
time2, err = time.Parse("2006-01-02T15:04:05.000Z", ss[1])
if err != nil {
log.Fatal(err)
}
symbol = ss[0]
} else if mode == 1 {
timestamp, err := strconv.ParseInt(ss[0], 10, 64)
if err != nil {
log.Fatal(err)
}
time2 = time.Unix(0, timestamp*int64(time.Millisecond))
}

if err != nil {
log.Fatal(err)
}

// 10档
var asks []Item
var bids []Item

var offset = 2
if mode == 1 {
offset = 1
}

for i := offset; i < offset+20; i += 2 {
price := cast.ToFloat64(ss[i])
amount := cast.ToFloat64(ss[i+1])
asks = append(asks, Item{
Price: price,
Amount: amount,
})
}

offset += 20

for i := offset; i < offset+20; i += 2 {
price := cast.ToFloat64(ss[i])
amount := cast.ToFloat64(ss[i+1])
bids = append(bids, Item{
Price: price,
Amount: amount,
})
}

ob := &OrderBook{
Symbol: symbol,
Timestamp: time2,
Asks: asks,
Bids: bids,
}
store.Insert(ob)
}
Loading

0 comments on commit 9cf4593

Please sign in to comment.