Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: add csvsource #1389

Closed
wants to merge 29 commits into from
Closed

FEATURE: add csvsource #1389

wants to merge 29 commits into from

Conversation

go-dockly
Copy link

Add a tick and kline level reader interface for multiple exchanges (binance, bybit) or fin-apps like meta-trader.
The goal is to expose the stream interface on the csvsource so arbitrary tick level simulations can be run against strategy

@bbgokarma-bot
Copy link

Hi @go-dockly,

This is KarmaBot, and we reward your contributions with tokens sent directly to your wallet to support development.

This pull request may get 3955 BBG.

To receive BBG tokens, please provide your Polygon (can be Ethereum) address as an issue comment in this pull request, following this format:

polygon:0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B

Once this pull request is merged, your BBG tokens will be transferred to your wallet.

--
If you're interested in our project, feel free to join our Telegram group https://t.me/bbgo_intl

Best,
KarmaBot

@go-dockly go-dockly changed the title FEATURE: csvsource WIP FEATURE: csvsource Nov 2, 2023
@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 4072 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 4126 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 4152 BBG

@c9s
Copy link
Owner

c9s commented Nov 2, 2023

@go-dockly can do you git rebase against the main branch? and maybe squash some fix commits? I saw you have merge-point commits, thanks

@c9s
Copy link
Owner

c9s commented Nov 2, 2023

git checkout feat/csvsource
git fetch upstream
git rebase upstream/main
git push -f origin HEAD

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 4222 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 4228 BBG

@@ -375,12 +376,12 @@ func (e *Exchange) SubscribeMarketData(
for symbol := range loadedSymbols {
symbols = append(symbols, symbol)
}
symbols = append(symbols, "FXSUSDT")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debugging code?

Copy link
Author

@go-dockly go-dockly Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really find a way yet to init the loadedSymbols from config

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nor did the backtest execute any trades so I assume the trade service is not properly connected to the backtest exchange. Do you have any ideas what I am missing here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to set it in the config yaml. check pkg/bbgo/config.go in the Backtest struct.

backtest:
    symbols:
        - BTCUSDT
        - ETHUSDT

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also you need to set the sync section maybe.

sync:
  userDataStream:
    trades: true
    filledOrders: true
  sessions:
    - binance
  symbols:
    - BTCUSDT

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gonna try that thanks

type BacktestService struct {
DB *sqlx.DB
}

func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange types.Exchange, symbol string, interval types.Interval, startTime, endTime time.Time) error {
func NewBacktestService(db *sqlx.DB) IBacktestService {
Copy link
Owner

@c9s c9s Nov 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid setting the interface type as the return type in the constructor, you can force the interface type from the caller. e.g.,

var s IBacktestService = NewBacktestService(db)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and where would you take db from?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's initialized from the bbgo.Environment

pkg/service/backtest_sql.go Outdated Show resolved Hide resolved
@c9s
Copy link
Owner

c9s commented Nov 18, 2023

@go-dockly I saw you're adding more changes to this PR, this could make this PR too large to review as the previous PR issue, could you please narrow down the scope of this PR, and if you need to continue the work, you can create another PR that sets the base branch to this one algo-boyz:feat/csvsource

@c9s
Copy link
Owner

c9s commented Nov 18, 2023

Your original changes was almost ready to be merged, it just needs to be rebased (git rebase)

case types.ExchangeBybit:
csvContent, err = gunzip(body)
if err != nil {
return nil, fmt.Errorf("gunzip data %s: %w", exchange, err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the exchange variable is only used in the log?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch exchange {
case types.ExchangeBybit:

@go-dockly
Copy link
Author

go-dockly commented Nov 20, 2023

It wouldn't have worked if we had merged before. Please check the last commit about what I mean addMissingKLines...
I will squash once it's working. I won't add more than what it's now

@@ -150,7 +151,8 @@ type Backtest struct {
Sessions []string `json:"sessions" yaml:"sessions"`

// sync 1 second interval KLines
SyncSecKLines bool `json:"syncSecKLines,omitempty" yaml:"syncSecKLines,omitempty"`
SyncSecKLines bool `json:"syncSecKLines,omitempty" yaml:"syncSecKLines,omitempty"`
CsvSource *csvsource.CsvConfig `json:"csvConfig,omitempty" yaml:"csvConfig,omitempty"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to put the Config struct under the bbgo package (avoid importing csvsource, could cause cyclic import in the future)

@@ -163,7 +162,14 @@ var BacktestCmd = &cobra.Command{

environ := bbgo.NewEnvironment()

backtestService := service.NewBacktestServiceCSV(outputDirectory, csvsource.SPOT, csvsource.AGGTRADES) // todo make configurable
if userConfig.Backtest.CsvSource == nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be optional

@@ -20,6 +26,10 @@ type CsvTick struct {
}

func (c *CsvTick) ToGlobalTrade() (*types.Trade, error) {
var isFutures bool
if c.Market == FUTURES {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constants should be in CamelCase (like this)

@@ -5,8 +5,14 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

type CsvConfig struct {
Market MarketType `json:"market"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the MarketType to the types package

func (c *CsvTick) ToGlobalTrade() (*types.Trade, error) {
var isFutures bool
if c.Market == FUTURES {
isFutures = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be just:

isFutures = c.Market == FUTURES

@@ -0,0 +1,16 @@
trade_id/���id,side/���׷���,size/����,price/�۸�,created_time/�ɽ�ʱ��
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the text encoding here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question not sure what okex is doing here. It's their raw content but we skip this line anyway on ingest

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's chinese, maybe you need to use UTF-8 to decode it?

@c9s
Copy link
Owner

c9s commented Nov 21, 2023

It wouldn't have worked if we had merged before. Please check the last commit about what I mean addMissingKLines... I will squash once it's working. I won't add more than what it's now

got it, thanks

@c9s
Copy link
Owner

c9s commented Nov 21, 2023

FYI, best practice: rebase while you're git pulling

https://sdq.kastel.kit.edu/wiki/Git_pull_--rebase_vs._--merge


1: Through csv data source (supported right now are binance, bybit and OkEx)

2: Alternatively run backtests through [MySQL](../../README.md#configure-mysql-database) or [SQLite3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest to put mysql as the default backtest engine.

Let's put the csv data source as the second one

@c9s c9s assigned c9s and unassigned c9s Nov 28, 2023
@c9s
Copy link
Owner

c9s commented Dec 8, 2023

@go-dockly waiting for you updates ;-)

@c9s
Copy link
Owner

c9s commented Dec 14, 2023

@go-dockly I am trying to rebase your commits, and found you have the "reset main" commits,

please avoid doing this in the pull request, or it could make it harder to do the rebase

you should avoid merge the main branch into your branch, instead you can use rebase

@c9s
Copy link
Owner

c9s commented Dec 14, 2023

@go-dockly this is what the "reset main" causes during the rebase process :/

image

@c9s
Copy link
Owner

c9s commented Dec 14, 2023

I have rebased the commits in this PR #1454

@c9s
Copy link
Owner

c9s commented Dec 14, 2023

@go-dockly please continue your work from the pull request #1454 (branch algo-boyz/feat/csvsource)

@c9s c9s closed this Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants