Multi-pass Backtesting
-
Hello !
I'd like to rewrite this code for Multi-pass Backtesting to pass the competition filter.
What should I do ?def pair_trading(data): pair = ['NAS:FWLT', 'NAS:PCAR'] close = data.sel(field='close') price = close.sel(asset=pair).transpose('time', 'asset').fillna(0) #欠損値処理 modelCoint = VECM(price.values, k_ar_diff=0, deterministic='na') res = modelCoint.fit() pairs_value = res.beta[0]*price.sel(asset=pair[0])+ res.beta[1]*price.sel(asset=pair[1]) #ポートフォリオ価値の作成 diff = pairs_value.shift(time=-1) - pairs_value threshold = diff.std(dim='time')*2 #閾値の設定 signal = np.sign(diff) weights = xr.zeros_like(close) #ポートフォリオウェイトの初期化 for t in data.time: if (weights.sel(time=t)==[0,0]).any() and pairs_value.sel(time=t) >= threshold: weights.loc[dict(time=t)] = [1,-1] if (weights.sel(time=t)==[0,0]).any() and pairs_value.sel(time=t) <= -threshold: weights.loc[dict(time=t)] = [-1,1] if (weights.sel(time=t)==[1,-1]).any() and signal.sel(time=t) < 0: weights.loc[dict(time=t)] = [0,0] if (weights.sel(time=t)==[-1,1]).any() and signal.sel(time=t) >= 0: weights.loc[dict(time=t)] = [0,0] return weights
Best regards
-
@cyan-gloom Hi, before that, are you sure that your code is not looking forward? There is a shift with a negative value "-1" for the variable "diff", which in turns is used via "signal" for defining "weights". Naively, we would say that the code is looking into the future, correct?
-
@support is ".shift(time=-1)" really forward looking?
In the Quantiacs Ridge Classifier example ".shift(time=-1)" is used, so I guess this operation is not forward looking.def get_target_classes(data): # define categorical targets price_current = data.sel(field="close").dropna("time") # rm NaN price_future = price_current.shift(time=-1).dropna("time") class_positive = 1 class_negative = 0 target_is_price_up = xr.where(price_future > price_current, class_positive, class_negative) return target_is_price_up.to_pandas()
-
Hello.
This code looks to the future.
It is needed to train the model.
Pay attention to the name of the variable. -
This post is deleted!