all: fix constant naming and type naming

This commit is contained in:
c9s 2023-12-14 15:08:22 +08:00
parent af1e63f345
commit 29301cbe7f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
6 changed files with 84 additions and 66 deletions

View File

@ -565,6 +565,7 @@ var BacktestCmd = &cobra.Command{
continue continue
} }
tradeState := sessionTradeStats[session.Name][symbol]
profitFactor := tradeState.ProfitFactor profitFactor := tradeState.ProfitFactor
winningRatio := tradeState.WinningRatio winningRatio := tradeState.WinningRatio
intervalProfits := tradeState.IntervalProfits[types.Interval1d] intervalProfits := tradeState.IntervalProfits[types.Interval1d]

View File

@ -19,47 +19,11 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
type MarketType string
type DataType string
const (
SPOT MarketType = "spot"
FUTURES MarketType = "futures"
TRADES DataType = "trades"
AGGTRADES DataType = "aggTrades"
// todo could be extended to:
// LEVEL2 = 2
// https://data.binance.vision/data/futures/um/daily/bookTicker/ADAUSDT/ADAUSDT-bookTicker-2023-11-19.zip
// update_id best_bid_price best_bid_qty best_ask_price best_ask_qty transaction_time event_time
// 3.52214E+12 0.3772 1632 0.3773 67521 1.70035E+12 1.70035E+12
// METRICS = 3
// https://data.binance.vision/data/futures/um/daily/metrics/ADAUSDT/ADAUSDT-metrics-2023-11-19.zip
// create_time symbol sum_open_interest sum_open_interest_value count_toptrader_long_short_ratio sum_toptrader_long_short_ratio count_long_short_ratio sum_taker_long_short_vol_ratio
// 19/11/2023 00:00 ADAUSDT 141979878.00000000 53563193.89339590 2.33412322 1.21401178 2.46604727 0.55265805
// KLINES DataType = 4
// https://public.bybit.com/kline_for_metatrader4/BNBUSDT/2021/BNBUSDT_15_2021-07-01_2021-07-31.csv.gz
// only few symbols but supported interval options 1m/ 5m/ 15m/ 30m/ 60m/ and only monthly
// https://data.binance.vision/data/futures/um/daily/klines/1INCHBTC/30m/1INCHBTC-30m-2023-11-18.zip
// supported interval options 1s/ 1m/ 3m/ 5m/ 15m/ 30m/ 1h/ 2h/ 4h/ 6h/ 8h/ 12h/ 1d/ daily or monthly futures
// this might be useful for backtesting against mark or index price
// especially index price can be used across exchanges
// https://data.binance.vision/data/futures/um/daily/indexPriceKlines/ADAUSDT/1h/ADAUSDT-1h-2023-11-19.zip
// https://data.binance.vision/data/futures/um/daily/markPriceKlines/ADAUSDT/1h/ADAUSDT-1h-2023-11-19.zip
// OKex or Bybit do not support direct kLine, metrics or level2 csv download
)
func Download( func Download(
path, symbol string, path, symbol string,
exchange types.ExchangeName, exchange types.ExchangeName,
market MarketType, market types.MarketType,
granularity DataType, granularity types.MarketDataType,
since, until time.Time, since, until time.Time,
) (err error) { ) (err error) {
for { for {
@ -104,8 +68,8 @@ func Download(
func buildURL( func buildURL(
exchange types.ExchangeName, exchange types.ExchangeName,
symbol string, symbol string,
market MarketType, market types.MarketType,
granularity DataType, granularity types.MarketDataType,
fileName string, fileName string,
start time.Time, start time.Time,
) (url string, err error) { ) (url string, err error) {
@ -119,11 +83,11 @@ func buildURL(
) )
case types.ExchangeBinance: case types.ExchangeBinance:
marketType := "spot" marketType := "spot"
if market == FUTURES { if market == types.MarketTypeFutures {
marketType = "futures/um" marketType = "futures/um"
} }
dataType := "aggTrades" dataType := "aggTrades"
if granularity == TRADES { if granularity == types.MarketDataTypeTrades {
dataType = "trades" dataType = "trades"
} }
url = fmt.Sprintf("https://data.binance.vision/data/%s/daily/%s/%s/%s-%s-%s.zip", url = fmt.Sprintf("https://data.binance.vision/data/%s/daily/%s/%s/%s-%s-%s.zip",
@ -144,11 +108,11 @@ func buildURL(
baseCoin := coins[0] baseCoin := coins[0]
quoteCoin := coins[1] quoteCoin := coins[1]
marketType := "" // for spot market marketType := "" // for spot market
if market == FUTURES { if market == types.MarketTypeFutures {
marketType = "-SWAP" marketType = "-SWAP"
} }
dataType := "aggtrades" dataType := "aggtrades"
if granularity == TRADES { if granularity == types.MarketDataTypeTrades {
dataType = "trades" dataType = "trades"
} }
url = fmt.Sprintf("https://static.okx.com/cdn/okex/traderecords/%s/daily/%s/%s-%s%s-%s-%s.zip", url = fmt.Sprintf("https://static.okx.com/cdn/okex/traderecords/%s/daily/%s/%s-%s%s-%s-%s.zip",

View File

@ -14,8 +14,8 @@ import (
type DownloadTester struct { type DownloadTester struct {
Exchange types.ExchangeName Exchange types.ExchangeName
Reader MakeCSVTickReader Reader MakeCSVTickReader
Market MarketType Market types.MarketType
Granularity DataType Granularity types.MarketDataType
Symbol string Symbol string
Path string Path string
} }
@ -35,24 +35,24 @@ func Test_CSV_Download(t *testing.T) {
{ {
Exchange: types.ExchangeBinance, Exchange: types.ExchangeBinance,
Reader: NewBinanceCSVTickReader, Reader: NewBinanceCSVTickReader,
Market: SPOT, Market: types.MarketTypeSpot,
Granularity: AGGTRADES, Granularity: types.MarketDataTypeAggTrades,
Symbol: "FXSUSDT", Symbol: "FXSUSDT",
Path: "testdata/binance/FXSUSDT", Path: "testdata/binance/FXSUSDT",
}, },
{ {
Exchange: types.ExchangeBybit, Exchange: types.ExchangeBybit,
Reader: NewBybitCSVTickReader, Reader: NewBybitCSVTickReader,
Market: FUTURES, Market: types.MarketTypeFutures,
Granularity: AGGTRADES, Granularity: types.MarketDataTypeAggTrades,
Symbol: "FXSUSDT", Symbol: "FXSUSDT",
Path: "testdata/bybit/FXSUSDT", Path: "testdata/bybit/FXSUSDT",
}, },
{ {
Exchange: types.ExchangeOKEx, Exchange: types.ExchangeOKEx,
Reader: NewOKExCSVTickReader, Reader: NewOKExCSVTickReader,
Market: SPOT, Market: types.MarketTypeSpot,
Granularity: AGGTRADES, Granularity: types.MarketDataTypeAggTrades,
Symbol: "BTCUSDT", Symbol: "BTCUSDT",
Path: "testdata/okex/BTCUSDT", Path: "testdata/okex/BTCUSDT",
}, },

View File

@ -6,13 +6,13 @@ import (
) )
type CsvConfig struct { type CsvConfig struct {
Market MarketType `json:"market"` Market types.MarketType `json:"market"`
Granularity DataType `json:"granularity"` Granularity types.MarketDataType `json:"granularity"`
} }
type CsvTick struct { type CsvTick struct {
Exchange types.ExchangeName `json:"exchange"` Exchange types.ExchangeName `json:"exchange"`
Market MarketType `json:"market"` Market types.MarketType `json:"market"`
TradeID uint64 `json:"tradeID"` TradeID uint64 `json:"tradeID"`
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
TickDirection string `json:"tickDirection"` TickDirection string `json:"tickDirection"`
@ -27,7 +27,7 @@ type CsvTick struct {
func (c *CsvTick) ToGlobalTrade() (*types.Trade, error) { func (c *CsvTick) ToGlobalTrade() (*types.Trade, error) {
var isFutures bool var isFutures bool
if c.Market == FUTURES { if c.Market == types.MarketTypeFutures {
isFutures = true isFutures = true
} }
return &types.Trade{ return &types.Trade{

View File

@ -16,14 +16,14 @@ import (
type BacktestServiceCSV struct { type BacktestServiceCSV struct {
kLines map[types.Interval][]types.KLine kLines map[types.Interval][]types.KLine
path string path string
market csvsource.MarketType market types.MarketType
granularity csvsource.DataType granularity types.MarketDataType
} }
func NewBacktestServiceCSV( func NewBacktestServiceCSV(
path string, path string,
market csvsource.MarketType, market types.MarketType,
granularity csvsource.DataType, granularity types.MarketDataType,
) BackTestable { ) BackTestable {
return &BacktestServiceCSV{ return &BacktestServiceCSV{
kLines: make(map[types.Interval][]types.KLine), kLines: make(map[types.Interval][]types.KLine),
@ -33,7 +33,9 @@ func NewBacktestServiceCSV(
} }
} }
func (s *BacktestServiceCSV) Verify(sourceExchange types.Exchange, symbols []string, startTime time.Time, endTime time.Time) error { func (s *BacktestServiceCSV) Verify(
sourceExchange types.Exchange, symbols []string, startTime time.Time, endTime time.Time,
) error {
// TODO: use isFutures here // TODO: use isFutures here
_, _, isIsolated, isolatedSymbol := exchange2.GetSessionAttributes(sourceExchange) _, _, isIsolated, isolatedSymbol := exchange2.GetSessionAttributes(sourceExchange)
// override symbol if isolatedSymbol is not empty // override symbol if isolatedSymbol is not empty
@ -55,7 +57,10 @@ func (s *BacktestServiceCSV) Verify(sourceExchange types.Exchange, symbols []str
return nil return nil
} }
func (s *BacktestServiceCSV) Sync(ctx context.Context, exchange types.Exchange, symbol string, intervals []types.Interval, startTime, endTime time.Time) error { func (s *BacktestServiceCSV) Sync(
ctx context.Context, exchange types.Exchange, symbol string, intervals []types.Interval,
startTime, endTime time.Time,
) error {
log.Infof("starting fresh csv sync %s %s: %s <=> %s", exchange.Name(), symbol, startTime, endTime) log.Infof("starting fresh csv sync %s %s: %s <=> %s", exchange.Name(), symbol, startTime, endTime)
@ -90,7 +95,9 @@ func (s *BacktestServiceCSV) Sync(ctx context.Context, exchange types.Exchange,
} }
// QueryKLine queries the klines from the database // QueryKLine queries the klines from the database
func (s *BacktestServiceCSV) QueryKLine(ex types.ExchangeName, symbol string, interval types.Interval, orderBy string, limit int) (*types.KLine, error) { func (s *BacktestServiceCSV) QueryKLine(
ex types.ExchangeName, symbol string, interval types.Interval, orderBy string, limit int,
) (*types.KLine, error) {
log.Infof("querying last kline exchange = %s AND symbol = %s AND interval = %s", ex, symbol, interval) log.Infof("querying last kline exchange = %s AND symbol = %s AND interval = %s", ex, symbol, interval)
if _, ok := s.kLines[interval]; !ok || len(s.kLines[interval]) == 0 { if _, ok := s.kLines[interval]; !ok || len(s.kLines[interval]) == 0 {
return nil, errors.New("interval not initialized") return nil, errors.New("interval not initialized")
@ -99,7 +106,9 @@ func (s *BacktestServiceCSV) QueryKLine(ex types.ExchangeName, symbol string, in
} }
// QueryKLinesForward is used for querying klines to back-testing // QueryKLinesForward is used for querying klines to back-testing
func (s *BacktestServiceCSV) QueryKLinesForward(exchange types.ExchangeName, symbol string, interval types.Interval, startTime time.Time, limit int) ([]types.KLine, error) { func (s *BacktestServiceCSV) QueryKLinesForward(
exchange types.ExchangeName, symbol string, interval types.Interval, startTime time.Time, limit int,
) ([]types.KLine, error) {
// Sample implementation (modify as needed): // Sample implementation (modify as needed):
var result []types.KLine var result []types.KLine
@ -122,7 +131,9 @@ func (s *BacktestServiceCSV) QueryKLinesForward(exchange types.ExchangeName, sym
return result, nil return result, nil
} }
func (s *BacktestServiceCSV) QueryKLinesBackward(exchange types.ExchangeName, symbol string, interval types.Interval, endTime time.Time, limit int) ([]types.KLine, error) { func (s *BacktestServiceCSV) QueryKLinesBackward(
exchange types.ExchangeName, symbol string, interval types.Interval, endTime time.Time, limit int,
) ([]types.KLine, error) {
var result []types.KLine var result []types.KLine
// Access klines data based on interval // Access klines data based on interval
@ -146,7 +157,9 @@ func (s *BacktestServiceCSV) QueryKLinesBackward(exchange types.ExchangeName, sy
return result, nil return result, nil
} }
func (s *BacktestServiceCSV) QueryKLinesCh(since, until time.Time, exchange types.Exchange, symbols []string, intervals []types.Interval) (chan types.KLine, chan error) { func (s *BacktestServiceCSV) QueryKLinesCh(
since, until time.Time, exchange types.Exchange, symbols []string, intervals []types.Interval,
) (chan types.KLine, chan error) {
if len(symbols) == 0 { if len(symbols) == 0 {
return returnError(errors.Errorf("symbols is empty when querying kline, please check your strategy setting. ")) return returnError(errors.Errorf("symbols is empty when querying kline, please check your strategy setting. "))
} }

40
pkg/types/csvsource.go Normal file
View File

@ -0,0 +1,40 @@
package types
type MarketType string
const (
MarketTypeSpot MarketType = "spot"
MarketTypeFutures MarketType = "futures"
)
type MarketDataType string
const (
MarketDataTypeTrades MarketDataType = "trades"
MarketDataTypeAggTrades MarketDataType = "aggTrades"
// TODO: could be extended to the following:
// LEVEL2 = 2
// https://data.binance.vision/data/futures/um/daily/bookTicker/ADAUSDT/ADAUSDT-bookTicker-2023-11-19.zip
// update_id best_bid_price best_bid_qty best_ask_price best_ask_qty transaction_time event_time
// 3.52214E+12 0.3772 1632 0.3773 67521 1.70035E+12 1.70035E+12
// METRICS = 3
// https://data.binance.vision/data/futures/um/daily/metrics/ADAUSDT/ADAUSDT-metrics-2023-11-19.zip
// create_time symbol sum_open_interest sum_open_interest_value count_toptrader_long_short_ratio sum_toptrader_long_short_ratio count_long_short_ratio sum_taker_long_short_vol_ratio
// 19/11/2023 00:00 ADAUSDT 141979878.00000000 53563193.89339590 2.33412322 1.21401178 2.46604727 0.55265805
// KLINES MarketDataType = 4
// https://public.bybit.com/kline_for_metatrader4/BNBUSDT/2021/BNBUSDT_15_2021-07-01_2021-07-31.csv.gz
// only few symbols but supported interval options 1m/ 5m/ 15m/ 30m/ 60m/ and only monthly
// https://data.binance.vision/data/futures/um/daily/klines/1INCHBTC/30m/1INCHBTC-30m-2023-11-18.zip
// supported interval options 1s/ 1m/ 3m/ 5m/ 15m/ 30m/ 1h/ 2h/ 4h/ 6h/ 8h/ 12h/ 1d/ daily or monthly futures
// this might be useful for backtesting against mark or index price
// especially index price can be used across exchanges
// https://data.binance.vision/data/futures/um/daily/indexPriceKlines/ADAUSDT/1h/ADAUSDT-1h-2023-11-19.zip
// https://data.binance.vision/data/futures/um/daily/markPriceKlines/ADAUSDT/1h/ADAUSDT-1h-2023-11-19.zip
// OKex or Bybit do not support direct kLine, metrics or level2 csv download
)