def strategy(data): close = data['futures'].sel(field='close') close_prev = data['futures'].sel(field='close').shift(time=1) close_change = (close - close_prev)/close_prev close_crypto = data['crypto'].sel(field='close') close_crypto_prev = data['crypto'].sel(field='close').shift(time=1) close_change_crypto = (close_crypto - close_crypto_prev)/close_crypto_prev sma200 = qnta.sma(close_change, 20).fillna(0).mean('asset').isel(time=-1) sma200_crypto = qnta.sma(close_change_crypto, 20).isel(time=-1) return xr.where(sma200 < sma200_crypto, 1, -1)
def get_features(data): trend = qnta.roc(qnta.lwma(data.sel(field="close"), 90), 1) k, d = qnta.stochastic(data.sel(field="high"), data.sel(field="low"), data.sel(field="close"), 21) volatility = qnta.tr(data.sel(field="high"), data.sel(field="low"), data.sel(field="close")) volatility = volatility / data.sel(field="close") volatility = qnta.lwma(volatility, 21) volume = data.sel(field="vol") volume = qnta.sma(volume, 7) / qnta.sma(volume, 80) volume = volume.where(np.isfinite(volume), 0) # combine features to one array result = xr.concat([trend, d, volatility, volume], pd.Index( ['trend', 'stochastic_d', 'volatility', 'volume'], name='field')) return result.transpose('time', 'field', 'asset')
def strategy(data): close = data.sel(field='close') sma200 = qnta.sma(close, 200).isel(time=-1) sma20 = qnta.sma(close, 20).isel(time=-1) return xr.where(sma200 < sma20, 1, -1)
# os.environ['API_KEY'] = "{your_api_key_here}" # you may need it for local development import qnt.ta as qnta import qnt.data as qndata data_all = qndata.futures.load_data(tail=120) close_all = data_all.sel(field='close') data = qndata.futures.load_data(assets=['F_GC', 'F_DX'], tail=120) high = data.sel(field='close') low = data.sel(field='low') close = data.sel(field='close') volume = data.sel(field='vol') print("Moving averages:") sma = qnta.sma(close, 20) ema = qnta.ema(close, 20) lwma = qnta.lwma(close, 20) wma = qnta.wma(close, [3, 2, 1]) wi_ma = qnta.wilder_ma(close, 20) vwma = qnta.vwma(close, volume, 20) print("SMA(20)") print(sma.to_pandas().tail().T) print("---") print("Oscillators:") stoch_k = qnta.stochastic_k(high, low, close, 14) stoch_fast_k, stoch_fast_d = qnta.stochastic(high, low, close, 14) stoch_slow_k, stoch_slow_d = qnta.stochastic(high, low, close, 14) rsi = qnta.rsi(close, 14)