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

Does it support multi data feeds ? #282

Open
evanlaw3 opened this issue Aug 9, 2023 · 5 comments
Open

Does it support multi data feeds ? #282

evanlaw3 opened this issue Aug 9, 2023 · 5 comments

Comments

@evanlaw3
Copy link

evanlaw3 commented Aug 9, 2023

Somethings like https://www.backtrader.com/blog/posts/2017-04-09-multi-example/multi-example/

@rodrigo-brito
Copy link
Owner

rodrigo-brito commented Aug 9, 2023

you can plot multiple informations in the chart, you have to include a chart indicator inside the strategy

func (e CrossEMA) Indicators(df *ninjabot.Dataframe) []strategy.ChartIndicator {
	df.Metadata["custom-data"] = fetchYourCustomData()

	return []strategy.ChartIndicator{
		{
			Overlay:   true, // plot over the price
			GroupName: "Custom Data",
			Time:      df.Time,
			Metrics: []strategy.IndicatorMetric{
				{
					Values: df.Metadata["custom-data"]
					Name:   "Custom",
					Color:  "red",
					Style:  strategy.StyleLine,
				},
			},
		},
	}
}
`` 

@evanlaw3
Copy link
Author

What I meant to say was that I can feed multiple data souces into one strategy. Let's say that my strategy find out pairs with top 3 chg and buy in. So I should import all live datas of trading pairs and ranking them.

@andreimerfu
Copy link
Contributor

@rodrigo-brito How can we implement this feature? We should generate multiple dataframes by timeframe or maybe a single dataframe that has some child dataframes? I think the goal is to have something like this in strategies:

  if df.Metadata["rsi"] < 30 && df.OtherDFs["weekly"].Metadata["rsi"] > 60 {
    // buy
  }

@evanlaw3 Correct me if I'm wrong.

@rodrigo-brito
Copy link
Owner

rodrigo-brito commented May 19, 2024

For the same currency pair, you can resample the data if you are in a smaller dataframe and want to view a larger one.
But to visualize a different data frame you have to implement a custom data fetcher, like this:

// exchange initalization
binance, err = exchange.NewBinance(ctx,
	exchange.WithBinanceCredentials(cfg.BinanceKey, cfg.BinanceSecret),
	exchange.WithMetadataFetcher(fetchFundingRate), // FETCH CUSTOM DATA
)

// your custom data
func fetchFundingRate(pair string, t time.Time) (string, float64) {
	fr, err := futureClient.NewPremiumIndexService().Symbol(pair).Do(context.Background())
	if err != nil {
		log.Printf("error fetching funding rate: %s", err)
		return "fr", 0
	}

	if len(fr) == 0 {
		return "fr", 0
	}

	value, err := strconv.ParseFloat(fr[0].LastFundingRate, 64)
	if err != nil {
		log.Printf("error fetching funding rate: %s", err)
		return "fr", 0
	}

	return "fr", value
}

The function must return the name of the index and the current value.
It will be called for each iteration given a timeframe. That is, every new candle.
Then, the date will be available in your strategy with the index fr, like this df.Metadata["fr"]

@rodrigo-brito
Copy link
Owner

For backtesting with custom metadata, your CSV must have a custom column with the same index name fr. And all will work fine.

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

No branches or pull requests

3 participants