Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions qlib/backtest/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

class Exchange:
# `quote_df` is a pd.DataFrame class that contains basic information for backtesting
# After some processing, the data will later be maintained by `quote_cls` object for faster data retriving.
# After some processing, the data will later be maintained by `quote_cls` object for faster data retrieving.
# Some conventions for `quote_df`
# - $close is for calculating the total value at end of each day.
# - if $close is None, the stock on that day is reguarded as suspended.
# - if $close is None, the stock on that day is regarded as suspended.
# - $factor is for rounding to the trading unit;
# - if any $factor is missing when $close exists, trading unit rounding will be disabled
quote_df: pd.DataFrame
Expand Down Expand Up @@ -141,7 +141,7 @@ def __init__(
if deal_price is None:
deal_price = C.deal_price

# we have some verbose information here. So logging is enable
# we have some verbose information here. So logging is enabled
self.logger = get_module_logger("online operator")

# TODO: the quote, trade_dates, codes are not necessary.
Expand All @@ -168,7 +168,7 @@ def __init__(
self.codes = codes
# Necessary fields
# $close is for calculating the total value at end of each day.
# - if $close is None, the stock on that day is reguarded as suspended.
# - if $close is None, the stock on that day is regarded as suspended.
# $factor is for rounding to the trading unit
# $change is for calculating the limit of the stock

Expand Down Expand Up @@ -271,7 +271,7 @@ def _get_limit_type(self, limit_threshold: Union[tuple, float, None]) -> str:
raise NotImplementedError(f"This type of `limit_threshold` is not supported")

def _update_limit(self, limit_threshold: Union[Tuple, float, None]) -> None:
# $close is may contains NaN, the nan indicates that the stock is not tradable at that timestamp
# $close may contain NaN, the nan indicates that the stock is not tradable at that timestamp
suspended = self.quote_df["$close"].isna()
# check limit_threshold
limit_type = self._get_limit_type(limit_threshold)
Expand Down Expand Up @@ -356,12 +356,12 @@ def check_stock_limit(

Returns
-------
True: the trading of the stock is limted (maybe hit the highest/lowest price), hence the stock is not tradable
True: the trading of the stock is limited (maybe hit the highest/lowest price), hence the stock is not tradable
False: the trading of the stock is not limited, hence the stock may be tradable
"""
# NOTE:
# **all** is used when checking limitation.
# For example, the stock trading is limited in a day if every miniute is limited in a day if every miniute is limited.
# For example, the stock trading is limited in a day if every minute is limited in a day if every minute is limited.
if direction is None:
# The trading limitation is related to the trading direction
# if the direction is not provided, then any limitation from buy or sell will result in trading limitation
Expand All @@ -385,17 +385,17 @@ def check_stock_suspended(
# is suspended
if stock_id in self.quote.get_all_stock():
# suspended stocks are represented by None $close stock
# The $close may contains NaN,
# The $close may contain NaN,
close = self.quote.get_data(stock_id, start_time, end_time, "$close")
if close is None:
# if no close record exists
return True
elif isinstance(close, IndexData):
# **any** non-NaN $close represents trading opportunity may exists
# **any** non-NaN $close represents trading opportunity may exist
# if all returned is nan, then the stock is suspended
return cast(bool, cast(IndexData, close).isna().all())
else:
# it is single value, make sure is is not None
# it is single value, make sure is not None
return np.isnan(close)
else:
# if the stock is not in the stock list, then it is not tradable and regarded as suspended
Expand Down Expand Up @@ -540,8 +540,8 @@ def generate_amount_position_from_weight_position(
direction: OrderDir = OrderDir.BUY,
) -> dict:
"""
The generate the target position according to the weight and the cash.
NOTE: All the cash will assigned to the tradable stock.
Generates the target position according to the weight and the cash.
NOTE: All the cash will be assigned to the tradable stock.
Parameter:
weight_position : dict {stock_id : weight}; allocate cash by weight_position
among then, weight must be in this range: 0 < weight < 1
Expand Down Expand Up @@ -639,7 +639,7 @@ def generate_order_for_target_amount_position(
random.shuffle(sorted_ids)
for stock_id in sorted_ids:

# Do not generate order for the nontradable stocks
# Do not generate order for the non-tradable stocks
if not self.is_stock_tradable(stock_id=stock_id, start_time=start_time, end_time=end_time):
continue

Expand Down
45 changes: 24 additions & 21 deletions qlib/contrib/data/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
fit_end_time=None,
filter_pipe=None,
inst_processor=None,
**kwargs,
**kwargs
):
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
learn_processors = check_transform_proc(learn_processors, fit_start_time, fit_end_time)
Expand All @@ -67,7 +67,7 @@ def __init__(
"kwargs": {
"config": {
"feature": self.get_feature_config(),
"label": kwargs.get("label", self.get_label_config()),
"label": kwargs.pop("label", self.get_label_config()),
},
"filter_pipe": filter_pipe,
"freq": freq,
Expand All @@ -82,12 +82,14 @@ def __init__(
data_loader=data_loader,
learn_processors=learn_processors,
infer_processors=infer_processors,
**kwargs
)

def get_label_config(self):
return (["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"])
return ["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"]

def get_feature_config(self):
@staticmethod
def get_feature_config():
# NOTE:
# Alpha360 tries to provide a dataset with original price data
# the original price data includes the prices and volume in the last 60 days.
Expand All @@ -99,33 +101,33 @@ def get_feature_config(self):
names = []

for i in range(59, 0, -1):
fields += ["Ref($close, %d)/$close" % (i)]
names += ["CLOSE%d" % (i)]
fields += ["Ref($close, %d)/$close" % i]
names += ["CLOSE%d" % i]
fields += ["$close/$close"]
names += ["CLOSE0"]
for i in range(59, 0, -1):
fields += ["Ref($open, %d)/$close" % (i)]
names += ["OPEN%d" % (i)]
fields += ["Ref($open, %d)/$close" % i]
names += ["OPEN%d" % i]
fields += ["$open/$close"]
names += ["OPEN0"]
for i in range(59, 0, -1):
fields += ["Ref($high, %d)/$close" % (i)]
names += ["HIGH%d" % (i)]
fields += ["Ref($high, %d)/$close" % i]
names += ["HIGH%d" % i]
fields += ["$high/$close"]
names += ["HIGH0"]
for i in range(59, 0, -1):
fields += ["Ref($low, %d)/$close" % (i)]
names += ["LOW%d" % (i)]
fields += ["Ref($low, %d)/$close" % i]
names += ["LOW%d" % i]
fields += ["$low/$close"]
names += ["LOW0"]
for i in range(59, 0, -1):
fields += ["Ref($vwap, %d)/$close" % (i)]
names += ["VWAP%d" % (i)]
fields += ["Ref($vwap, %d)/$close" % i]
names += ["VWAP%d" % i]
fields += ["$vwap/$close"]
names += ["VWAP0"]
for i in range(59, 0, -1):
fields += ["Ref($volume, %d)/($volume+1e-12)" % (i)]
names += ["VOLUME%d" % (i)]
fields += ["Ref($volume, %d)/($volume+1e-12)" % i]
names += ["VOLUME%d" % i]
fields += ["$volume/($volume+1e-12)"]
names += ["VOLUME0"]

Expand All @@ -134,7 +136,7 @@ def get_feature_config(self):

class Alpha360vwap(Alpha360):
def get_label_config(self):
return (["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"])
return ["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"]


class Alpha158(DataHandlerLP):
Expand All @@ -151,7 +153,7 @@ def __init__(
process_type=DataHandlerLP.PTYPE_A,
filter_pipe=None,
inst_processor=None,
**kwargs,
**kwargs
):
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
learn_processors = check_transform_proc(learn_processors, fit_start_time, fit_end_time)
Expand All @@ -161,7 +163,7 @@ def __init__(
"kwargs": {
"config": {
"feature": self.get_feature_config(),
"label": kwargs.get("label", self.get_label_config()),
"label": kwargs.pop("label", self.get_label_config()),
},
"filter_pipe": filter_pipe,
"freq": freq,
Expand All @@ -176,6 +178,7 @@ def __init__(
infer_processors=infer_processors,
learn_processors=learn_processors,
process_type=process_type,
**kwargs
)

def get_feature_config(self):
Expand All @@ -190,7 +193,7 @@ def get_feature_config(self):
return self.parse_config_to_fields(conf)

def get_label_config(self):
return (["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"])
return ["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"]

@staticmethod
def parse_config_to_fields(config):
Expand Down Expand Up @@ -426,4 +429,4 @@ def use(x):

class Alpha158vwap(Alpha158):
def get_label_config(self):
return (["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"])
return ["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"]
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_adarnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ADARNN(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ADD(Model):
d_feat : int
input dimensions for each time step
metric : str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : int
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_alstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ALSTM(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : int
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_alstm_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ALSTM(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : int
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_gats.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GATs(Model):
d_feat : int
input dimensions for each time step
metric : str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : int
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_gats_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class GATs(Model):
d_feat : int
input dimensions for each time step
metric : str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : int
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_gru.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GRU(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_gru_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GRU(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HIST(Model):
d_feat : int
input dimensions for each time step
metric : str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_igmtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class IGMTF(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LSTM(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_lstm_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LSTM(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_tcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TCN(Model):
n_chans: int
number of channels
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_tcn_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TCN(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
2 changes: 1 addition & 1 deletion qlib/contrib/model/pytorch_tcts.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TCTS(Model):
d_feat : int
input dimension for each time step
metric: str
the evaluate metric used in early stop
the evaluation metric used in early stop
optimizer : str
optimizer name
GPU : str
Expand Down
Loading