def strategy(data): # calc weights: close = data.sel(field="close") is_liquid = data.sel(field='is_liquid') ma_slow = qnta.lwma(close, 50) ma_fast = qnta.lwma(close, 10) return xr.where(ma_fast > ma_slow, 1, 0) * is_liquid
def strategy(data): close = data['futures'].sel(field="close") currency = data['currency'] ma1 = qnta.lwma(currency,10) ma2 = qnta.lwma(currency,50) ma3 = qnta.lwma(currency,250) if ma1.isel(time=-1) > ma2.isel(time=-1) and ma2.isel(time=-1) < ma3.isel(time=-1): return xr.ones_like(close.isel(time=-1)) else: return xr.zeros_like(close.isel(time=-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_long(data, asset=None, ma_period=150): # filter by asset, we will need it for further optimization if asset is not None: data = data.sel(asset=[asset]) close = data.sel(field='close') ma = qnta.lwma(close, ma_period) ma_roc = qnta.roc(ma, 1) # define signal buy_signal = ma_roc > 0 buy_stop_signal = ma_roc < 0 # transform signals to positions position = xr.where(buy_signal, 1, np.nan) position = xr.where(buy_stop_signal, 0, position) position = position.ffill('time').fillna(0) # clean the output (not necessary) # with qnlog.Settings(info=False,err=False): # suppress logging # position = qnout.clean(position, data) return position
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) roc = qnta.roc(close, 7) sroc = qnta.sroc(close, 13, 21)