Use of Technical indicators
-
Thank you! I am looking at the indicator (TAs) documentation right now
You can get the fast stochastic indicator or k with:
stoch_k = qnta.stochastic_k(high, low, close, 14)
To get k and d for both, the fast and slow stochastic indicator:
stoch_fast_k, stoch_fast_d = qnta.stochastic(high, low, close, 14) stoch_slow_k, stoch_slow_d = qnta.slow_stochastic(high, low, close, 14)
as Investopedia and Wikipedia mentioned, the indicator should be in range of 0-100,
I tried the followingdef strategy(data): high = data.sel(field="high") low = data.sel(field="low") close = data.sel(field="close") is_liquid = data.sel(field='is_liquid') stoch_k = qnta.stochastic_k(high, low, close, 14) if stoch_k>30 : weights = 1 elif stoch_k<80 : weights = -1 else : weights = 0 weights = weights / 10.0 weights = weights * is_liquid return weights
but get the following error
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-33-b67077d72c3d> in <module> 39 strategy= strategy, 40 analyze= True, ---> 41 build_plots= True 42 ) ~/book/qnt/backtester.py in backtest(competition_type, strategy, load_data, lookback_period, test_period, start_date, end_date, window, step, analyze, build_plots, collect_all_states) 288 if is_submitted() and args_count > 1: 289 state = qnstate.read() --> 290 result = strategy_wrap(data, state) 291 result, state = unpack_result(result) 292 ~/book/qnt/backtester.py in <lambda>(d, s) 268 269 args_count = len(inspect.getfullargspec(strategy).args) --> 270 strategy_wrap = (lambda d, s: strategy(d)) if args_count < 2 else strategy 271 272 # --- <ipython-input-33-b67077d72c3d> in strategy(data) 19 stoch_k = qnta.stochastic_k(high, low, close, 14) 20 ---> 21 if stoch_k>30 : 22 weights = 1 23 elif stoch_k<80 : /usr/local/lib/python3.7/site-packages/xarray/core/common.py in __bool__(self) 127 128 def __bool__(self: Any) -> bool: --> 129 return bool(self.values) 130 131 def __float__(self: Any) -> float: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
-
@noka-sworld Hello, there are 2 problems here.
- The logic: you define a strange logic:
stoch_k > 30 => weights=1
stoch_k < 80 = > weights=-1are you sure you did not exchange the inequality symbols? The two conditions are not mutually exclusive.
- stoch_k is a datastructure from xarray. Imagine to have at your disposal a big matrix where the elements are indexed by time and asset. Then you can implement the following code using the "where" construct:
import xarray as xr import qnt.ta as qnta import qnt.backtester as qnbt import qnt.data as qndata def load_data(period): return qndata.cryptodaily_load_data(tail=period) def strategy(data): high = data.sel(field="high") low = data.sel(field="low") close = data.sel(field="close") is_liquid = data.sel(field='is_liquid') stoch_k = qnta.stochastic_k(high, low, close, 14) weights1 = xr.where(stoch_k < 30, 1, 0) weights2 = xr.where(stoch_k > 80, -1, 0) weights = weights1 + weights2 weights = weights / 10.0 weights = weights * is_liquid return weights weights = qnbt.backtest( competition_type= "crypto_daily_long_short", load_data= load_data, lookback_period= 365*4, start_date= "2014-01-01", strategy= strategy, analyze= True, build_plots= True )
-
@support that is really useful! thank you very much!