Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 16, 2023
1 parent b5ff5a4 commit ef1303b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 37 deletions.
32 changes: 29 additions & 3 deletions cmd/haobase/clearing/clearing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,38 @@ import (

"github.com/sirupsen/logrus"
"github.com/yzimhao/trading_engine/cmd/haobase/base"
"github.com/yzimhao/trading_engine/cmd/haobase/base/symbols"
"github.com/yzimhao/trading_engine/trading_core"
"github.com/yzimhao/trading_engine/types"
"github.com/yzimhao/trading_engine/utils"
)

func RunClearing(symbol string) {
watch_redis_list(symbol)
func Run() {
//load symbols
db := base.DB().NewSession()
defer db.Close()

var rows []symbols.TradingVarieties
db.Table(new(symbols.TradingVarieties)).Find(&rows)

for _, row := range rows {
run_clearing(row.Symbol)
}
}

func run_clearing(symbol string) {
go watch_redis_list(symbol)
}

func watch_redis_list(symbol string) {
key := types.FormatTradeResult.Format(symbol)
quote_key := types.FormatQuoteTradeResult.Format(symbol)
logrus.Infof("结算,正在监听%s成交日志...", symbol)
for {
func() {
cx := context.Background()
rdc := base.RDC()
defer rdc.Close()

if n, _ := rdc.LLen(cx, key).Result(); n == 0 {
time.Sleep(time.Duration(50) * time.Millisecond)
Expand All @@ -39,7 +55,17 @@ func watch_redis_list(symbol string) {
return
}

newClean(data)
logrus.Infof("%s成交记录 ask: %s bid: %s price: %s vol: %s", data.Symbol, data.AskOrderId, data.BidOrderId, data.TradePrice.String(), data.TradeQuantity.String())

err = newClean(data)
if err != nil {
logrus.Warnf("结算错误: %s %s", raw, err.Error())
return
}

//通知kline系统
rdc.RPush(cx, quote_key, raw)

// if !data.Last {
// go newClean(data)
// } else {
Expand Down
7 changes: 4 additions & 3 deletions cmd/haobase/clearing/clearing_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type clean struct {
err error
}

func newClean(raw trading_core.TradeResult) {
func newClean(raw trading_core.TradeResult) error {
db := base.DB().NewSession()
defer db.Close()

Expand All @@ -34,10 +34,10 @@ func newClean(raw trading_core.TradeResult) {
tlog: raw,
}

item.flow()
return item.flow()
}

func (c *clean) flow() {
func (c *clean) flow() error {

c.db.Begin()
defer func() {
Expand All @@ -54,6 +54,7 @@ func (c *clean) flow() {
c.err = c.update_order(trading_core.OrderSideBuy)

c.err = c.transfer()
return c.err
}

func (c *clean) check_order() error {
Expand Down
9 changes: 9 additions & 0 deletions cmd/haobase/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/yzimhao/trading_engine/cmd/haobase/assets"
"github.com/yzimhao/trading_engine/cmd/haobase/base"
"github.com/yzimhao/trading_engine/cmd/haobase/base/symbols"
"github.com/yzimhao/trading_engine/cmd/haobase/clearing"
"github.com/yzimhao/trading_engine/cmd/haobase/www"
"github.com/yzimhao/trading_engine/utils/app"
)
Expand Down Expand Up @@ -76,6 +77,8 @@ func main() {

}

initDemoBaseData()
clearing.Run()
www.Run()
return nil
},
Expand All @@ -85,3 +88,9 @@ func main() {
logrus.Fatal(err)
}
}

func initDemoBaseData() {
if app.RunMode == app.ModeDemo {
symbols.DemoData()
}
}
12 changes: 0 additions & 12 deletions cmd/haobase/www/demo.go

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/haobase/www/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
)

func Run() {
demoBaseData()

g := gin.New()
router(g)
g.Run(viper.GetString("haobase.http.host"))
Expand Down
6 changes: 3 additions & 3 deletions example/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ mode="test"
host="0.0.0.0:8080"

[api]
haobase_host = "//0.0.0.0:8082"
haoquote_host = "//0.0.0.0:8081"
haoquote_ws_host = "ws://0.0.0.0:8081"
haobase_host = "//127.0.0.1:8082"
haoquote_host = "//127.0.0.1:8081"
haoquote_ws_host = "ws://127.0.0.1:8081"
19 changes: 5 additions & 14 deletions haotrader/tengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/yzimhao/trading_engine/trading_core"
"github.com/yzimhao/trading_engine/types"
)
Expand Down Expand Up @@ -267,15 +266,7 @@ func (t *tengine) monitor_result() {
select {
case data := <-t.tp.ChTradeResult:
go func() {
relog := map[string]any{
"trade_price": t.tp.Price2String(data.TradePrice),
"trade_quantity": t.tp.Qty2String(data.TradeQuantity),
"trade_time": data.TradeTime,
"ask": data.AskOrderId,
"bid": data.BidOrderId,
}

raw, _ := json.Marshal(relog)
raw, _ := json.Marshal(data)
t.push_match_result(raw)
}()
case uniq := <-t.tp.ChCancelResult:
Expand Down Expand Up @@ -305,8 +296,8 @@ func (t *tengine) push_match_result(data []byte) {
key := types.FormatTradeResult.Format(t.symbol)
err := rdc.RPush(cx, key, data).Err()
logrus.Infof("往%s队列RPush: %s %s", key, data, err)
if viper.GetBool("haotrader.notify_quote") {
quote_key := types.FormatQuoteTradeResult.Format(t.symbol)
rdc.RPush(cx, quote_key, data)
}
// if viper.GetBool("haotrader.notify_quote") {
// quote_key := types.FormatQuoteTradeResult.Format(t.symbol)
// rdc.RPush(cx, quote_key, data)
// }
}

0 comments on commit ef1303b

Please sign in to comment.