Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
from systems.provided.futures_chapter15.basesystem import *
import numpy as np
from syscore.algos import robust_vol_calc
## test kurtosis AND skew with trading rules
# The trading rules
def factor_trading_rule(demean_factor_value, smooth=90):
vol =robust_vol_calc(demean_factor_value)
normalised_factor_value = demean_factor_value / vol
smoothed_normalised_factor_value = normalised_factor_value.ewm(span=smooth).mean()
return smoothed_normalised_factor_value
def conditioned_factor_trading_rule(demean_factor_value, condition_demean_factor_value, smooth=90):
vol = robust_vol_calc(demean_factor_value)
normalised_factor_value = demean_factor_value / vol
sign_condition = condition_demean_factor_value.apply(np.sign)
sign_condition_resample = sign_condition.reindex(normalised_factor_value.index).ffill()
conditioned_factor = normalised_factor_value *sign_condition_resample
smoothed_conditioned_factor = conditioned_factor.ewm(span=smooth).mean()
return smoothed_conditioned_factor
list_of_lookbacks=[7,14,30,90,180,365]
temp_system = futures_system()
existing_trading_rules = temp_system.config.trading_rules
all_trading_rules = existing_trading_rules
temp_config = temp_system.config
from systems.forecasting import TradingRule
for lookback_days in list_of_lookbacks:
smooth = int(np.ceil(lookback_days/4.0))
# The skew rules
skew_abs = TradingRule(factor_trading_rule, data=['rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth,
_factor_name="neg_skew",
_demean_method="historic_average_factor_value_all_assets",
_lookback_days = lookback_days))
all_trading_rules["skew_abs_%d" % lookback_days] = skew_abs
skew_ts = TradingRule(factor_trading_rule, data=['rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth,
_factor_name="neg_skew",
_demean_method="average_factor_value_for_instrument",
_lookback_days = lookback_days))
all_trading_rules["skew_ts_%d" % lookback_days] = skew_ts
skew_cs = TradingRule(factor_trading_rule, data=['rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth,
_factor_name="neg_skew",
_demean_method="current_average_factor_values_over_all_assets",
_lookback_days = lookback_days))
all_trading_rules["skew_cs_%d" % lookback_days] = skew_cs
skew_rv = TradingRule(factor_trading_rule, data=['rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth,
_factor_name="neg_skew",
_demean_method="average_factor_value_in_asset_class_for_instrument",
_lookback_days = lookback_days))
all_trading_rules["skew_rv_%d" % lookback_days] = skew_rv
# The skew conditioned on kurtosis rules
#{(skew - Average skew) / Sigma [Skew]}
#* sign (Kurtosis - average kurtosis)
skewK_abs = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="skew",
_demean_method="historic_average_factor_value_all_assets",
_lookback_days = lookback_days,
__factor_name="kurtosis",
__demean_method="historic_average_factor_value_all_assets",
__lookback_days = lookback_days
))
all_trading_rules["skewK_abs_%d" % lookback_days] = skewK_abs
skewK_ts = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="skew",
_demean_method="average_factor_value_for_instrument",
_lookback_days = lookback_days,
__factor_name="kurtosis",
__demean_method="average_factor_value_for_instrument",
__lookback_days = lookback_days
))
all_trading_rules["skewK_ts_%d" % lookback_days] = skewK_ts
skewK_cs = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="skew",
_demean_method="current_average_factor_values_over_all_assets",
_lookback_days = lookback_days,
__factor_name="kurtosis",
__demean_method="current_average_factor_values_over_all_assets",
__lookback_days = lookback_days
))
all_trading_rules["skewK_cs_%d" % lookback_days] = skewK_cs
skewK_rv = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="skew",
_demean_method="average_factor_value_in_asset_class_for_instrument",
_lookback_days = lookback_days,
__factor_name="kurtosis",
__demean_method="average_factor_value_in_asset_class_for_instrument",
__lookback_days = lookback_days
))
all_trading_rules["skewK_rv_%d" % lookback_days] = skewK_rv
# Now we reverse condition
kurtS_abs = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="kurtosis",
_demean_method="historic_average_factor_value_all_assets",
_lookback_days = lookback_days,
__factor_name="skew",
__demean_method="historic_average_factor_value_all_assets",
__lookback_days = lookback_days
))
all_trading_rules["kurtS_abs_%d" % lookback_days] = kurtS_abs
kurtS_ts = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="kurtosis",
_demean_method="average_factor_value_for_instrument",
_lookback_days = lookback_days,
__factor_name="skew",
__demean_method="average_factor_value_for_instrument",
__lookback_days = lookback_days
))
all_trading_rules["kurtS_ts_%d" % lookback_days] = kurtS_ts
kurtS_cs = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="kurtosis",
_demean_method="current_average_factor_values_over_all_assets",
_lookback_days = lookback_days,
__factor_name="skew",
__demean_method="current_average_factor_values_over_all_assets",
__lookback_days = lookback_days
))
all_trading_rules["kurtS_cs_%d" % lookback_days] = kurtS_cs
kurtS_rv = TradingRule(conditioned_factor_trading_rule, data=['rawdata.get_demeanded_factor_value',
'rawdata.get_demeanded_factor_value'],
other_args=dict(smooth = smooth, _factor_name="kurtosis",
_demean_method="average_factor_value_in_asset_class_for_instrument",
_lookback_days = lookback_days,
__factor_name="skew",
__demean_method="average_factor_value_in_asset_class_for_instrument",
__lookback_days = lookback_days
))
all_trading_rules["kurtS_rv_%d" % lookback_days] = kurtS_rv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.