涨跌停
原始的qlib对涨跌停支持的比较简陋:
1、需要feature中有 change 数据
2、设置 limit_threshold=0.095
更精确一点的做法是需要手工设置涨跌停数据(此方案暂未测试)
extra_quote=pd.DataFrame(data={"$close":[10.0,10.5,11.0],# 必需字段"$factor":[1.0,1.0,1.0],# 必需字段"limit_buy":[False,True,False],# 自定义买入限制"limit_sell":[False,False,True],# 自定义卖出限制# 其他字段...},index=pd.MultiIndex.from_tuples([("SH600000","2020-01-01"),("SH600000","2020-01-02"),("SH600000","2020-01-03")],names=["instrument","datetime"]))停牌股
qlib 默认处理停牌是要判断 $close 是否为NaN
有些数据源的处理不是如此
defcheck_stock_suspended(self,stock_id:str,start_time:pd.Timestamp,end_time:pd.Timestamp,)->bool:"""if stock is suspended(hence not tradable), True will be returned"""# is suspendedifstock_idinself.quote.get_all_stock():# suspended stocks are represented by None $close stock# The $close may contain NaN,close=self.quote.get_data(stock_id,start_time,end_time,"$close")ifcloseisNone:# if no close record existsreturnTrueelifisinstance(close,IndexData):# **any** non-NaN $close represents trading opportunity may exist# if all returned is nan, then the stock is suspendedreturncast(bool,cast(IndexData,close).isna().all())else:# it is single value, make sure is not Nonereturnnp.isnan(close)else:# if the stock is not in the stock list, then it is not tradable and regarded as suspendedreturnTrue