vectorbt
vectorbt copied to clipboard
Getting a KeyError when using IndicatorFactory.run()
Hello, I am trying to play around with some simple strategies to learn about the library, so I started with this :
import numpy as np
import pandas as pd
import pytz import talib
data = vbt.BinanceData.download('ETHUSDT',
start = datetime.datetime(2017, 1, 2,tzinfo=pytz.timezone('UTC')),
end = datetime.datetime(2018, 1, 1, tzinfo=pytz.timezone('UTC'))).get(['Close'])
def dummy_strat(Close, fast_ema, slow_ema):
ema1 = vbt.talib('EMA').run(Close, fast_ema).real.to_numpy()
ema2 = vbt.talib('EMA').run(Close, slow_ema).real.to_numpy()
stoch_rsi = vbt.talib('STOCHRSI').run(Close).fastk.to_numpy()
entries = (ema1 >ema2) & (stoch_rsi <80)
exits = (ema1 <ema2) & (stoch_rsi > 20)
#print(help(ema1))
return entries, exits
DummyStrat = vbt.IndicatorFactory(
class_name= 'TrueStrat',
short_name = 'TS',
input_names = ["Close"] ,
param_names = ["fast_ema", "slow_ema"],
output_names= ["entries", _"exits"]
).from_apply_func(dummy_strat )
When I run
fast_ema = 10
slow_ema = 20
entries, exits = true_strat(data, fast_ema, slow_ema)
pf = vbt.Portfolio.from_signals(data, entries, exits, freq = '1H')
returns = pf.total_return()
it works as expected. But when I try this :
entries, exits = TrueStrat.run(data, fast_ema = np.arange(10, 50), slow_ema = np.arange(30, 100), param_product = True)
I get a KeyError: 0
Can someone please help me and explain to me what I'm doing wrong?
Thanks
TrueStrat.run returns an indicator instance, not arrays.