ninjabot icon indicating copy to clipboard operation
ninjabot copied to clipboard

Does it support multi data feeds ?

Open evanlaw3 opened this issue 1 year ago • 5 comments

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

evanlaw3 avatar Aug 09 '23 19:08 evanlaw3

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,
				},
			},
		},
	}
}
`` 

rodrigo-brito avatar Aug 09 '23 19:08 rodrigo-brito

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.

evanlaw3 avatar Aug 10 '23 17:08 evanlaw3

@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.

andreimerfu avatar Mar 19 '24 07:03 andreimerfu

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 avatar May 19 '24 21:05 rodrigo-brito

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

rodrigo-brito avatar May 19 '24 21:05 rodrigo-brito