Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Commit

Permalink
optimize code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
miaolz123 committed Dec 20, 2016
1 parent 8dbac40 commit 721046c
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 151 deletions.
2 changes: 1 addition & 1 deletion api/chbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (e *Chbtc) GetRecords(stockType, period string, sizes ...interface{}) inter
recordJSON := json.GetIndex(i - 1)
recordTime := recordJSON.GetIndex(0).MustInt64() / 1000
if recordTime > timeLast {
recordsNew = append([]Record{Record{
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Expand Down
4 changes: 2 additions & 2 deletions api/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Huobi struct {
}

// NewHuobi : create an exchange struct of huobi.com
func NewHuobi(opt Option) *Huobi {
func NewHuobi(opt Option) Exchange {
return &Huobi{
stockTypeMap: map[string]string{
"BTC/CNY": "1",
Expand Down Expand Up @@ -503,7 +503,7 @@ func (e *Huobi) GetRecords(stockType, period string, sizes ...interface{}) inter
t, _ := time.Parse("20060102150405000", recordJSON.GetIndex(0).MustString("19700101000000000"))
recordTime := t.Unix()
if recordTime > timeLast {
recordsNew = append([]Record{Record{
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Expand Down
6 changes: 3 additions & 3 deletions api/kline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var sosobtcSymbolMap = map[string]map[string]string{
constant.Btcc: map[string]string{
constant.Btcc: {
"BTC/CNY": "btcchinabtccny",
"LTC/CNY": "btcchinaltccny",
},
Expand Down Expand Up @@ -59,7 +59,7 @@ func getSosobtcRecords(recordsOld []Record, exchangeType, stockType, period stri
recordJSON := json.GetIndex(i - 1)
recordTime := recordJSON.GetIndex(0).MustInt64()
if recordTime > timeLast {
recordsNew = append([]Record{Record{
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Expand All @@ -82,7 +82,7 @@ func getSosobtcRecords(recordsOld []Record, exchangeType, stockType, period stri
}
records = append(recordsOld, recordsNew...)
if len(records) > size {
records = records[len(records)-size : len(records)]
records = records[len(records)-size:]
}
return
}
16 changes: 8 additions & 8 deletions api/oanda.v20.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ type OandaV20 struct {
}

// NewOandaV20 : create an exchange struct of okcoin.cn
func NewOandaV20(opt Option) *OandaV20 {
func NewOandaV20(opt Option) Exchange {
return &OandaV20{
stockTypeMap: map[string][2]string{
"BTC.WEEK/USD": [2]string{"btc_usd", "this_week"},
"BTC.WEEK2/USD": [2]string{"btc_usd", "next_week"},
"BTC.MONTH3/USD": [2]string{"btc_usd", "quarter"},
"LTC.WEEK/USD": [2]string{"ltc_usd", "this_week"},
"LTC.WEEK2/USD": [2]string{"ltc_usd", "next_week"},
"LTC.MONTH3/USD": [2]string{"ltc_usd", "quarter"},
"BTC.WEEK/USD": {"btc_usd", "this_week"},
"BTC.WEEK2/USD": {"btc_usd", "next_week"},
"BTC.MONTH3/USD": {"btc_usd", "quarter"},
"LTC.WEEK/USD": {"ltc_usd", "this_week"},
"LTC.WEEK2/USD": {"ltc_usd", "next_week"},
"LTC.MONTH3/USD": {"ltc_usd", "quarter"},
},
tradeTypeMap: map[string]string{
constant.TradeTypeLong: "1",
Expand Down Expand Up @@ -553,7 +553,7 @@ func (e *OandaV20) GetRecords(stockType, period string, sizes ...interface{}) in
recordJSON := json.GetIndex(i - 1)
recordTime := recordJSON.GetIndex(0).MustInt64() / 1000
if recordTime > timeLast {
recordsNew = append([]Record{Record{
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Expand Down
4 changes: 2 additions & 2 deletions api/okcoin.cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type OKCoinCn struct {
}

// NewOKCoinCn : create an exchange struct of okcoin.cn
func NewOKCoinCn(opt Option) *OKCoinCn {
func NewOKCoinCn(opt Option) Exchange {
return &OKCoinCn{
stockTypeMap: map[string]string{
"BTC/CNY": "btc",
Expand Down Expand Up @@ -501,7 +501,7 @@ func (e *OKCoinCn) GetRecords(stockType, period string, sizes ...interface{}) in
recordJSON := json.GetIndex(i - 1)
recordTime := recordJSON.GetIndex(0).MustInt64() / 1000
if recordTime > timeLast {
recordsNew = append([]Record{Record{
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Expand Down
16 changes: 8 additions & 8 deletions api/okcoin.future.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ type OKCoinFuture struct {
}

// NewOKCoinFuture : create an exchange struct of okcoin.cn
func NewOKCoinFuture(opt Option) *OKCoinFuture {
func NewOKCoinFuture(opt Option) Exchange {
return &OKCoinFuture{
stockTypeMap: map[string][2]string{
"BTC.WEEK/USD": [2]string{"btc_usd", "this_week"},
"BTC.WEEK2/USD": [2]string{"btc_usd", "next_week"},
"BTC.MONTH3/USD": [2]string{"btc_usd", "quarter"},
"LTC.WEEK/USD": [2]string{"ltc_usd", "this_week"},
"LTC.WEEK2/USD": [2]string{"ltc_usd", "next_week"},
"LTC.MONTH3/USD": [2]string{"ltc_usd", "quarter"},
"BTC.WEEK/USD": {"btc_usd", "this_week"},
"BTC.WEEK2/USD": {"btc_usd", "next_week"},
"BTC.MONTH3/USD": {"btc_usd", "quarter"},
"LTC.WEEK/USD": {"ltc_usd", "this_week"},
"LTC.WEEK2/USD": {"ltc_usd", "next_week"},
"LTC.MONTH3/USD": {"ltc_usd", "quarter"},
},
tradeTypeMap: map[string]string{
constant.TradeTypeLong: "1",
Expand Down Expand Up @@ -533,7 +533,7 @@ func (e *OKCoinFuture) GetRecords(stockType, period string, sizes ...interface{}
recordJSON := json.GetIndex(i - 1)
recordTime := recordJSON.GetIndex(0).MustInt64() / 1000
if recordTime > timeLast {
recordsNew = append([]Record{Record{
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Expand Down
4 changes: 2 additions & 2 deletions api/poloniex.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Poloniex struct {
}

// NewPoloniex : create an exchange struct of poloniex
func NewPoloniex(opt Option) *Poloniex {
func NewPoloniex(opt Option) Exchange {
return &Poloniex{
stockTypeMap: map[string]string{
"BTC/1CR": "BTC_1CR",
Expand Down Expand Up @@ -597,7 +597,7 @@ func (e *Poloniex) GetRecords(stockType, period string, sizes ...interface{}) in
recordJSON := json.GetIndex(i - 1)
recordTime := recordJSON.Get("date").MustInt64()
if recordTime > timeLast {
recordsNew = append([]Record{Record{
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.Get("open").MustFloat64(),
High: recordJSON.Get("high").MustFloat64(),
Expand Down
38 changes: 19 additions & 19 deletions constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package constant

// Error constant
// error constants
const (
ErrAuthorizationError = "Authorization Error"
ErrInsufficientPermissions = "Insufficient Permissions"
)

// log type
// exchange types
const (
OkCoinCn = "okcoin.cn"
Huobi = "huobi"
Poloniex = "poloniex"
Btcc = "btcc"
Chbtc = "chbtc"
OkcoinFuture = "okcoin.future"
OandaV20 = "oanda.v20"
)

// log types
const (
ERROR = -1
INFO = 0
Expand All @@ -16,26 +27,15 @@ const (
CANCEL = 4
)

// delete log time type
// delete log time types
const (
LastTime = "0"
Day = "1"
Week = "2"
Month = "3"
)

// exchange type
const (
OkCoinCn = "okcoin.cn"
Huobi = "huobi"
Poloniex = "poloniex"
Btcc = "btcc"
Chbtc = "chbtc"
OkcoinFuture = "okcoin.future"
OandaV20 = "oanda.v20"
)

// trade type
// trade types
const (
TradeTypeBuy = "BUY"
TradeTypeSell = "SELL"
Expand All @@ -45,14 +45,14 @@ const (
TradeTypeShortClose = "SHORT_CLOSE"
)

// stock type (will useless)
// stock types (will useless)
const (
BTC = "BTC"
LTC = "LTC"
)

// CONSTS : Javascript Global Constants
// some variables
var (
CONSTS = []string{"BTC", "LTC", "M", "M5", "M15", "M30", "H", "D", "W"}
ExchangeTypes = []string{"okcoin.cn", "huobi", "poloniex", "chbtc", "okcoin.future", "oanda.v20"}
Consts = []string{"BTC", "LTC", "M", "M5", "M15", "M30", "H", "D", "W"}
ExchangeTypes = []string{OkCoinCn, Huobi, Poloniex, Btcc, Chbtc, OkcoinFuture, OandaV20}
)
55 changes: 1 addition & 54 deletions handler/trader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,61 +52,8 @@ func (runner) Put(req model.Trader, ctx rpc.Context) (resp response) {
}
defer db.Close()
db = db.Begin()
runner := req
if req.ID > 0 {
if err := db.First(&runner, req.ID).Error; err != nil {
db.Rollback()
resp.Message = fmt.Sprint(err)
return
}
runner.Name = req.Name
runner.Environment = req.Environment
rs, err := self.GetTraderExchanges(runner.ID)
if err != nil {
db.Rollback()
resp.Message = fmt.Sprint(err)
return
}
for i, r := range rs {
if i >= len(req.Exchanges) {
if err := db.Delete(&r).Error; err != nil {
db.Rollback()
resp.Message = fmt.Sprint(err)
return
}
continue
}
if r.Exchange.ID == req.Exchanges[i].ID {
continue
}
r.ExchangeID = req.Exchanges[i].ID
if err := db.Save(&r).Error; err != nil {
db.Rollback()
resp.Message = fmt.Sprint(err)
return
}
}
for i, e := range req.Exchanges {
if i < len(rs) {
continue
}
r := model.TraderExchange{
TraderID: runner.ID,
ExchangeID: e.ID,
}
if err := db.Create(&r).Error; err != nil {
db.Rollback()
resp.Message = fmt.Sprint(err)
return
}
}
if err := db.Save(&runner).Error; err != nil {
db.Rollback()
resp.Message = fmt.Sprint(err)
return
}
if err := db.Commit().Error; err != nil {
db.Rollback()
if err := req.Update(self); err != nil {
resp.Message = fmt.Sprint(err)
return
}
Expand Down
64 changes: 63 additions & 1 deletion model/trader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type TraderExchange struct {
ID int64 `gorm:"primary_key"`
TraderID int64 `gorm:"index"`
ExchangeID int64 `gorm:"index"`
Exchange `gorm:"-"`

Exchange `gorm:"-"`
}

// TraderList ...
Expand Down Expand Up @@ -81,3 +82,64 @@ func (user User) GetTraderExchanges(id interface{}) (traderExchanges []TraderExc
}
return
}

// Update ...
func (req Trader) Update(self User) (err error) {
db, err := NewOrm()
if err != nil {
return err
}
defer db.Close()
db = db.Begin()
runner := Trader{}
if err := db.First(&runner, req.ID).Error; err != nil {
db.Rollback()
return err
}
runner.Name = req.Name
runner.Environment = req.Environment
rs, err := self.GetTraderExchanges(runner.ID)
if err != nil {
db.Rollback()
return err
}
for i, r := range rs {
if i >= len(req.Exchanges) {
if err := db.Delete(&r).Error; err != nil {
db.Rollback()
return err
}
continue
}
if r.Exchange.ID == req.Exchanges[i].ID {
continue
}
r.ExchangeID = req.Exchanges[i].ID
if err := db.Save(&r).Error; err != nil {
db.Rollback()
return err
}
}
for i, e := range req.Exchanges {
if i < len(rs) {
continue
}
r := TraderExchange{
TraderID: runner.ID,
ExchangeID: e.ID,
}
if err := db.Create(&r).Error; err != nil {
db.Rollback()
return err
}
}
if err := db.Save(&runner).Error; err != nil {
db.Rollback()
return err
}
if err := db.Commit().Error; err != nil {
db.Rollback()
return err
}
return
}
Loading

0 comments on commit 721046c

Please sign in to comment.