fix kline record insert fields

This commit is contained in:
c9s 2021-06-01 01:08:02 +08:00
parent 4bec8984c0
commit b5c4fc3e4e
7 changed files with 67 additions and 36 deletions

View File

@ -1,35 +1,41 @@
-- +up -- +up
-- +begin -- +begin
ALTER TABLE `binance_klines` ALTER TABLE `binance_klines`
ADD COLUMN `quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0,
ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0, ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,
ADD COLUMN `taker_buy_quote_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0; ADD COLUMN `taker_buy_quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0;
-- +end -- +end
-- +begin -- +begin
ALTER TABLE `max_klines` ALTER TABLE `max_klines`
ADD COLUMN `quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0,
ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0, ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,
ADD COLUMN `taker_buy_quote_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0; ADD COLUMN `taker_buy_quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0;
-- +end -- +end
-- +begin -- +begin
ALTER TABLE `okex_klines` ALTER TABLE `okex_klines`
ADD COLUMN `quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0,
ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0, ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,
ADD COLUMN `taker_buy_quote_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0; ADD COLUMN `taker_buy_quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0;
-- +end -- +end
-- +down -- +down
-- +begin -- +begin
ALTER TABLE `binance_klines` ALTER TABLE `binance_klines`
DROP COLUMN `quote_volume`,
DROP COLUMN `taker_buy_base_volume`, DROP COLUMN `taker_buy_base_volume`,
DROP COLUMN `taker_buy_quote_volume`; DROP COLUMN `taker_buy_quote_volume`;
-- +end -- +end
-- +begin -- +begin
ALTER TABLE `max_klines` ALTER TABLE `max_klines`
DROP COLUMN `quote_volume`,
DROP COLUMN `taker_buy_base_volume`, DROP COLUMN `taker_buy_base_volume`,
DROP COLUMN `taker_buy_quote_volume`; DROP COLUMN `taker_buy_quote_volume`;
-- +end -- +end
-- +begin -- +begin
ALTER TABLE `okex_klines` ALTER TABLE `okex_klines`
DROP COLUMN `quote_volume`,
DROP COLUMN `taker_buy_base_volume`, DROP COLUMN `taker_buy_base_volume`,
DROP COLUMN `taker_buy_quote_volume`; DROP COLUMN `taker_buy_quote_volume`;
-- +end -- +end

View File

@ -1,10 +1,27 @@
-- +up -- +up
-- +begin -- +begin
SELECT 'up SQL query'; ALTER TABLE `binance_klines`
ADD COLUMN `quote_volume` DECIMAL NOT NULL DEFAULT 0.0;
ALTER TABLE `binance_klines`
ADD COLUMN `taker_buy_base_volume` DECIMAL NOT NULL DEFAULT 0.0;
ALTER TABLE `binance_klines`
ADD COLUMN `taker_buy_quote_volume` DECIMAL NOT NULL DEFAULT 0.0;
-- +end
-- +begin
ALTER TABLE `max_klines`
ADD COLUMN `quote_volume` DECIMAL NOT NULL DEFAULT 0.0;
ALTER TABLE `max_klines`
ADD COLUMN `taker_buy_base_volume` DECIMAL NOT NULL DEFAULT 0.0;
ALTER TABLE `max_klines`
ADD COLUMN `taker_buy_quote_volume` DECIMAL NOT NULL DEFAULT 0.0;
-- +end
-- +begin
ALTER TABLE `okex_klines`
ADD COLUMN `quote_volume` DECIMAL NOT NULL DEFAULT 0.0;
ALTER TABLE `okex_klines`
ADD COLUMN `taker_buy_base_volume` DECIMAL NOT NULL DEFAULT 0.0;
ALTER TABLE `okex_klines`
ADD COLUMN `taker_buy_quote_volume` DECIMAL NOT NULL DEFAULT 0.0;
-- +end -- +end
-- +down -- +down
-- +begin
SELECT 'down SQL query';
-- +end

View File

@ -782,20 +782,22 @@ func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval type
var kLines []types.KLine var kLines []types.KLine
for _, k := range resp { for _, k := range resp {
kLines = append(kLines, types.KLine{ kLines = append(kLines, types.KLine{
Exchange: types.ExchangeBinance, Exchange: types.ExchangeBinance,
Symbol: symbol, Symbol: symbol,
Interval: interval, Interval: interval,
StartTime: time.Unix(0, k.OpenTime*int64(time.Millisecond)), StartTime: time.Unix(0, k.OpenTime*int64(time.Millisecond)),
EndTime: time.Unix(0, k.CloseTime*int64(time.Millisecond)), EndTime: time.Unix(0, k.CloseTime*int64(time.Millisecond)),
Open: util.MustParseFloat(k.Open), Open: util.MustParseFloat(k.Open),
Close: util.MustParseFloat(k.Close), Close: util.MustParseFloat(k.Close),
High: util.MustParseFloat(k.High), High: util.MustParseFloat(k.High),
Low: util.MustParseFloat(k.Low), Low: util.MustParseFloat(k.Low),
Volume: util.MustParseFloat(k.Volume), Volume: util.MustParseFloat(k.Volume),
QuoteVolume: util.MustParseFloat(k.QuoteAssetVolume), QuoteVolume: util.MustParseFloat(k.QuoteAssetVolume),
LastTradeID: 0, TakerBuyBaseAssetVolume: util.MustParseFloat(k.TakerBuyBaseAssetVolume),
NumberOfTrades: uint64(k.TradeNum), TakerBuyQuoteAssetVolume: util.MustParseFloat(k.TakerBuyQuoteAssetVolume),
Closed: true, LastTradeID: 0,
NumberOfTrades: uint64(k.TradeNum),
Closed: true,
}) })
} }
return kLines, nil return kLines, nil

View File

@ -14,17 +14,17 @@ func init() {
func upAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is applied. // This code is executed when the migration is applied.
_, err = tx.ExecContext(ctx, "ALTER TABLE `binance_klines`\n ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_quote_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0;") _, err = tx.ExecContext(ctx, "ALTER TABLE `binance_klines`\n ADD COLUMN `quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0;")
if err != nil { if err != nil {
return err return err
} }
_, err = tx.ExecContext(ctx, "ALTER TABLE `max_klines`\n ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_quote_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0;") _, err = tx.ExecContext(ctx, "ALTER TABLE `max_klines`\n ADD COLUMN `quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0;")
if err != nil { if err != nil {
return err return err
} }
_, err = tx.ExecContext(ctx, "ALTER TABLE `okex_klines`\n ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_quote_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0;") _, err = tx.ExecContext(ctx, "ALTER TABLE `okex_klines`\n ADD COLUMN `quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_base_volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0,\n ADD COLUMN `taker_buy_quote_volume` DECIMAL(32, 4) NOT NULL DEFAULT 0.0;")
if err != nil { if err != nil {
return err return err
} }
@ -35,17 +35,17 @@ func upAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (
func downAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func downAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is rolled back. // This code is executed when the migration is rolled back.
_, err = tx.ExecContext(ctx, "ALTER TABLE `binance_klines`\n DROP COLUMN `taker_buy_base_volume`,\n DROP COLUMN `taker_buy_quote_volume`;") _, err = tx.ExecContext(ctx, "ALTER TABLE `binance_klines`\n DROP COLUMN `quote_volume`,\n DROP COLUMN `taker_buy_base_volume`,\n DROP COLUMN `taker_buy_quote_volume`;")
if err != nil { if err != nil {
return err return err
} }
_, err = tx.ExecContext(ctx, "ALTER TABLE `max_klines`\n DROP COLUMN `taker_buy_base_volume`,\n DROP COLUMN `taker_buy_quote_volume`;") _, err = tx.ExecContext(ctx, "ALTER TABLE `max_klines`\n DROP COLUMN `quote_volume`,\n DROP COLUMN `taker_buy_base_volume`,\n DROP COLUMN `taker_buy_quote_volume`;")
if err != nil { if err != nil {
return err return err
} }
_, err = tx.ExecContext(ctx, "ALTER TABLE `okex_klines`\n DROP COLUMN `taker_buy_base_volume`,\n DROP COLUMN `taker_buy_quote_volume`;") _, err = tx.ExecContext(ctx, "ALTER TABLE `okex_klines`\n DROP COLUMN `quote_volume`,\n DROP COLUMN `taker_buy_base_volume`,\n DROP COLUMN `taker_buy_quote_volume`;")
if err != nil { if err != nil {
return err return err
} }

View File

@ -14,7 +14,17 @@ func init() {
func upAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is applied. // This code is executed when the migration is applied.
_, err = tx.ExecContext(ctx, "SELECT 'up SQL query';") _, err = tx.ExecContext(ctx, "ALTER TABLE `binance_klines`\n ADD COLUMN `quote_volume` DECIMAL NOT NULL DEFAULT 0.0;\nALTER TABLE `binance_klines`\n ADD COLUMN `taker_buy_base_volume` DECIMAL NOT NULL DEFAULT 0.0;\nALTER TABLE `binance_klines`\n ADD COLUMN `taker_buy_quote_volume` DECIMAL NOT NULL DEFAULT 0.0;")
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "ALTER TABLE `max_klines`\n ADD COLUMN `quote_volume` DECIMAL NOT NULL DEFAULT 0.0;\nALTER TABLE `max_klines`\n ADD COLUMN `taker_buy_base_volume` DECIMAL NOT NULL DEFAULT 0.0;\nALTER TABLE `max_klines`\n ADD COLUMN `taker_buy_quote_volume` DECIMAL NOT NULL DEFAULT 0.0;")
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "ALTER TABLE `okex_klines`\n ADD COLUMN `quote_volume` DECIMAL NOT NULL DEFAULT 0.0;\nALTER TABLE `okex_klines`\n ADD COLUMN `taker_buy_base_volume` DECIMAL NOT NULL DEFAULT 0.0;\nALTER TABLE `okex_klines`\n ADD COLUMN `taker_buy_quote_volume` DECIMAL NOT NULL DEFAULT 0.0;")
if err != nil { if err != nil {
return err return err
} }
@ -25,10 +35,5 @@ func upAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (
func downAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func downAddKlineTakerBuyColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is rolled back. // This code is executed when the migration is rolled back.
_, err = tx.ExecContext(ctx, "SELECT 'down SQL query';")
if err != nil {
return err
}
return err return err
} }

View File

@ -205,8 +205,8 @@ func (s *BacktestService) Insert(kline types.KLine) error {
return errors.New("kline.Exchange field should not be empty") return errors.New("kline.Exchange field should not be empty")
} }
sql := "INSERT INTO `binance_klines` (`exchange`, `start_time`, `end_time`, `symbol`, `interval`, `open`, `high`, `low`, `close`, `closed`, `volume`)" + sql := "INSERT INTO `binance_klines` (`exchange`, `start_time`, `end_time`, `symbol`, `interval`, `open`, `high`, `low`, `close`, `closed`, `volume`, `quote_volume`, `taker_buy_base_volume`, `taker_buy_quote_volume`)" +
"VALUES (:exchange, :start_time, :end_time, :symbol, :interval, :open, :high, :low, :close, :closed, :volume)" "VALUES (:exchange, :start_time, :end_time, :symbol, :interval, :open, :high, :low, :close, :closed, :volume, :quote_volume, :taker_buy_base_volume, :taker_buy_quote_volume)"
sql = strings.ReplaceAll(sql, "binance_klines", kline.Exchange.String()+"_klines") sql = strings.ReplaceAll(sql, "binance_klines", kline.Exchange.String()+"_klines")
_, err := s.DB.NamedExec(sql, kline) _, err := s.DB.NamedExec(sql, kline)

View File

@ -2,4 +2,5 @@
driver: mysql driver: mysql
dialect: mysql dialect: mysql
dsn: "root@tcp(localhost:3306)/bbgo_dev?parseTime=true" dsn: "root@tcp(localhost:3306)/bbgo_dev?parseTime=true"
# dsn: "root@tcp(localhost:3306)/bbgo_backtest?parseTime=true"
migrationsDir: migrations/mysql migrationsDir: migrations/mysql