Skip to content

Commit

Permalink
alpha088-101
Browse files Browse the repository at this point in the history
  • Loading branch information
Menooker committed Jan 29, 2024
1 parent 135fdf5 commit 140880c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 42 deletions.
12 changes: 11 additions & 1 deletion KunQuant/ops/CompOp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .ReduceOp import ReduceAdd, ReduceMul, ReduceArgMax, ReduceRank, ReduceMin, ReduceMax, ReduceDecayLinear
from .ReduceOp import ReduceAdd, ReduceMul, ReduceArgMax, ReduceRank, ReduceMin, ReduceMax, ReduceDecayLinear, ReduceArgMin
from KunQuant.Op import ConstantOp, OpBase, CompositiveOp, WindowedTrait, ForeachBackWindow, WindowedTempOutput, Builder, IterValue
from .ElewiseOp import And, DivConst, GreaterThan, LessThan, Or, Select, SetInfOrNanToValue, Sub, Mul, Sqrt, SubConst, Div, CmpOp, Exp, Log
from collections import OrderedDict
Expand Down Expand Up @@ -120,6 +120,16 @@ def decompose(self) -> List[OpBase]:
v2 = ReduceArgMax(IterValue(v1, v0))
v3 = SubConst(v2, self.attrs["window"], True)
return b.ops

class TsArgMin(WindowedCompositiveOp):
def decompose(self) -> List[OpBase]:
b = Builder(self.get_parent())
with b:
v0 = WindowedTempOutput(self.inputs[0], self.attrs["window"])
v1 = ForeachBackWindow(v0, self.attrs["window"])
v2 = ReduceArgMin(IterValue(v1, v0))
v3 = SubConst(v2, self.attrs["window"], True)
return b.ops

class TsRank(WindowedCompositiveOp):
def decompose(self) -> List[OpBase]:
Expand Down
3 changes: 3 additions & 0 deletions KunQuant/ops/ReduceOp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class ReduceMax(ReductionOp):
class ReduceArgMax(ReductionOp):
pass

class ReduceArgMin(ReductionOp):
pass

class ReduceRank(ReductionOp):
pass

Expand Down
50 changes: 47 additions & 3 deletions KunQuant/predefined/Alpha101.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def returns(v: OpBase) -> OpBase:
def ts_argmax(v: OpBase, window: int) -> OpBase:
return TsArgMax(v, window)

def ts_argmin(v: OpBase, window: int) -> OpBase:
return TsArgMin(v, window)

def ts_rank(v: OpBase, window: int) -> OpBase:
return TsRank(v, window)

Expand Down Expand Up @@ -482,18 +485,59 @@ def alpha085(self: AllData):
expo = rank(correlation(ts_rank(((self.high + self.low) / 2), 4), ts_rank(self.volume, 10),7))
return Pow(base, expo)

def alpha086(self):
def alpha086(self: AllData):
adv20 = sma(self.volume, 20)
a = ts_rank(correlation(self.close, sma(adv20, 15), 6), 20)
b = rank(((self.open+ self.close) - (self.vwap +self.open)))
return (bool_to_10(a < b) * -1)


def alpha088(self: AllData):
adv60 = sma(self.volume, 60)
p1=rank(decay_linear(((rank(self.open) + rank(self.low)) - (rank(self.high) + rank(self.close))),8))
p2=ts_rank(decay_linear(SetInfOrNanToValue(correlation(ts_rank(self.close, 8), ts_rank(adv60,21), 8)), 7), 3)
return Min(p2, p1)

def alpha092(self: AllData):
adv30 = sma(self.volume, 30)
p1=ts_rank(decay_linear(bool_to_10((((self.high + self.low) / 2) + self.close) < (self.low + self.open)), 15),19)
p2=ts_rank(decay_linear(SetInfOrNanToValue(correlation(rank(self.low), rank(adv30), 8)), 7),7)
return Min(p2, p1)

def alpha094(self: AllData):
adv60 = sma(self.volume, 60)
base = rank((self.vwap - ts_min(self.vwap, 12)))
expo = ts_rank(SetInfOrNanToValue(correlation(ts_rank(self.vwap,20), ts_rank(adv60, 4), 18)), 3)
return ((Pow(base, expo) * -1))

# Alpha#95 (rank((open - ts_min(open, 12.4105))) < Ts_Rank((rank(correlation(sum(((high + low)/ 2), 19.1351), sum(adv40, 19.1351), 12.8742))^5), 11.7584))
def alpha095(self: AllData):
adv40 = sma(self.volume, 40)
return bool_to_10(rank((self.open - ts_min(self.open, 12))) < ts_rank(Pow(rank(correlation(sma(((self.high + self.low)/ 2), 19), sma(adv40, 19), 13)), ConstantOp(5)), 12))

def alpha096(self: AllData):
adv60 = sma(self.volume, 60)
p1=ts_rank(decay_linear(SetInfOrNanToValue(correlation(rank(self.vwap), rank(self.volume), 4)),4), 8)
p2=ts_rank(decay_linear(ts_argmax(SetInfOrNanToValue(correlation(ts_rank(self.close, 7),ts_rank(adv60, 4), 4)), 13), 14), 13)
return -1* Max(p1, p2)

def alpha098(self: AllData):
adv5 = sma(self.volume, 5)
adv15 = sma(self.volume, 15)
return (rank(decay_linear(correlation(self.vwap, sma(adv5, 26), 5), 7)) -rank(decay_linear(ts_rank(ts_argmin(SetInfOrNanToValue(correlation(rank(self.open), rank(adv15), 21)), 9),7), 8)))

def alpha099(self: AllData):
adv60 = sma(self.volume, 60)
return (bool_to_10(rank(correlation(ts_sum(((self.high + self.low) / 2), 20), ts_sum(adv60, 20), 9)) <rank(correlation(self.low, self.volume, 6))) * -1)

def alpha101(self: AllData):
return (self.close - self.open) /((self.high - self.low) + 0.001)

all_alpha = [alpha001, alpha002, alpha003, alpha004, alpha005, alpha006, alpha007, alpha008, alpha009, alpha010,
alpha011, alpha012, alpha013, alpha014, alpha015, alpha016, alpha017, alpha018, alpha019, alpha020, alpha021,
alpha022, alpha023, alpha024, alpha025, alpha026, alpha027, alpha028, alpha029, alpha030, alpha031, alpha032,
alpha033, alpha034, alpha035, alpha036, alpha037, alpha038, alpha039, alpha040, alpha041, alpha042, alpha043,
alpha044, alpha045, alpha046, alpha047, alpha049, alpha050, alpha051, alpha052, alpha053, alpha054, alpha055,
alpha057, alpha060, alpha061, alpha062, alpha064, alpha065, alpha066, alpha068, alpha071, alpha072, alpha073,
alpha074, alpha075, alpha077, alpha078, alpha081, alpha083, alpha084, alpha085, alpha086
alpha074, alpha075, alpha077, alpha078, alpha081, alpha083, alpha084, alpha085, alpha086, alpha088, alpha092,
alpha094, alpha095, alpha096, alpha098, alpha099, alpha101
]
14 changes: 14 additions & 0 deletions cpp/Kun/Ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ struct ReduceArgMax {
operator f32x8() { return idx; }
};

struct ReduceArgMin {
f32x8 v = _mm256_set1_ps(std::numeric_limits<float>::infinity());
f32x8 idx = _mm256_setzero_ps();
void step(f32x8 input, size_t index) {
auto is_nan = isNAN(v, input);
auto cmp = GreaterThan(v, input);
v = Select(cmp, input, v);
v = Select(is_nan, _mm256_set1_ps(NAN), v);
idx = Select(cmp, _mm256_set1_ps(float(index)), idx);
idx = Select(is_nan, _mm256_set1_ps(NAN), idx);
}
operator f32x8() { return idx; }
};

struct ReduceRank {
kun_simd::vec_f32x8 v;
kun_simd::vec_f32x8 less_count = 0;
Expand Down
74 changes: 39 additions & 35 deletions tests/KunTestUtil/ref_alpha101.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ def correlation(x, y, window=10):
"""
return x.rolling(window).corr(y)

# def correlation_safe(x, y, window=10):
# """
# Wrapper function to estimate rolling corelations.
# :param df: a pandas DataFrame.
# :param window: the rolling window.
# :return: a pandas DataFrame with the time-series min over the past 'window' days.
# """
# ret = x.rolling(window).corr(y)
# ret.fillna(value=1, inplace=True)
# ret.clip(lower=-1.0, upper=1.0, inplace=True)
# return ret

def covariance(x, y, window=10):
"""
Wrapper function to estimate rolling covariance.
Expand Down Expand Up @@ -261,14 +273,14 @@ def get_alpha(df):
df['alpha084']=stock.alpha084()
df['alpha085']=stock.alpha085()
df['alpha086']=stock.alpha086()
# df['alpha088']=stock.alpha088()
# df['alpha092']=stock.alpha092()
# df['alpha094']=stock.alpha094()
# df['alpha095']=stock.alpha095()
# df['alpha096']=stock.alpha096()
# df['alpha098']=stock.alpha098()
# df['alpha099']=stock.alpha099()
# df['alpha101']=stock.alpha101()
df['alpha088']=stock.alpha088()
df['alpha092']=stock.alpha092()
df['alpha094']=stock.alpha094()
df['alpha095']=stock.alpha095()
df['alpha096']=stock.alpha096()
df['alpha098']=stock.alpha098()
df['alpha099']=stock.alpha099()
df['alpha101']=stock.alpha101()
return df

class Alphas(object):
Expand Down Expand Up @@ -678,7 +690,7 @@ def alpha071(self):
# df.at[df['p2']>=df['p1'],'max']=df['p2']
# return df['max']
return p1.where(p1.values > p2.values, p2.values)
#return max(ts_rank(decay_linear(correlation(ts_rank(self.close, 3), ts_rank(adv180,12), 18).to_frame(), 4).CLOSE, 16), ts_rank(decay_linear((rank(((self.low + self.open) - (self.vwap +self.vwap))).pow(2)).to_frame(), 16).CLOSE, 4))
#return max(ts_rank(decay_linear(correlation(ts_rank(self.close, 3), ts_rank(adv180,12), 18), 4), 16), ts_rank(decay_linear((rank(((self.low + self.open) - (self.vwap +self.vwap))).pow(2)), 16), 4))

# Alpha#72 (rank(decay_linear(correlation(((high + low) / 2), adv40, 8.93345), 10.1519)) /rank(decay_linear(correlation(Ts_Rank(vwap, 3.72469), Ts_Rank(volume, 18.5188), 6.86671),2.95011)))
def alpha072(self):
Expand All @@ -691,7 +703,7 @@ def alpha073(self):
p2=ts_rank(decay_linear(((delta(((self.open * 0.147155) + (self.low * (1 - 0.147155))), 2) / ((self.open *0.147155) + (self.low * (1 - 0.147155)))) * -1), 3), 17)
df= p1.where(p1.values > p2.values, p2.values)
return -1*df
#return (max(rank(decay_linear(delta(self.vwap, 5).to_frame(), 3).CLOSE),ts_rank(decay_linear(((delta(((self.open * 0.147155) + (self.low * (1 - 0.147155))), 2) / ((self.open *0.147155) + (self.low * (1 - 0.147155)))) * -1).to_frame(), 3).CLOSE, 17)) * -1)
#return (max(rank(decay_linear(delta(self.vwap, 5), 3)),ts_rank(decay_linear(((delta(((self.open * 0.147155) + (self.low * (1 - 0.147155))), 2) / ((self.open *0.147155) + (self.low * (1 - 0.147155)))) * -1), 3), 17)) * -1)

# Alpha#74 ((rank(correlation(close, sum(adv30, 37.4843), 15.1365)) <rank(correlation(rank(((high * 0.0261661) + (vwap * (1 - 0.0261661)))), rank(volume), 11.4791)))* -1)
def alpha074(self):
Expand All @@ -712,7 +724,7 @@ def alpha077(self):
p1=rank(decay_linear(((((self.high + self.low) / 2) + self.high) - (self.vwap + self.high)), 20))
p2=rank(decay_linear(correlation(((self.high + self.low) / 2), adv40, 3), 6))
return p1.where(p1.values < p2.values, p2.values)
#return min(rank(decay_linear(((((self.high + self.low) / 2) + self.high) - (self.vwap + self.high)).to_frame(), 20).CLOSE),rank(decay_linear(correlation(((self.high + self.low) / 2), adv40, 3).to_frame(), 6).CLOSE))
#return min(rank(decay_linear(((((self.high + self.low) / 2) + self.high) - (self.vwap + self.high)), 20)),rank(decay_linear(correlation(((self.high + self.low) / 2), adv40, 3), 6)))

# Alpha#78 (rank(correlation(sum(((low * 0.352233) + (vwap * (1 - 0.352233))), 19.7428),sum(adv40, 19.7428), 6.83313))^rank(correlation(rank(vwap), rank(volume), 5.77492)))
def alpha078(self):
Expand Down Expand Up @@ -758,13 +770,10 @@ def alpha086(self):
# Alpha#88 min(rank(decay_linear(((rank(open) + rank(low)) - (rank(high) + rank(close))),8.06882)), Ts_Rank(decay_linear(correlation(Ts_Rank(close, 8.44728), Ts_Rank(adv60,20.6966), 8.01266), 6.65053), 2.61957))
def alpha088(self):
adv60 = sma(self.volume, 60)
p1=rank(decay_linear(((rank(self.open) + rank(self.low)) - (rank(self.high) + rank(self.close))).to_frame(),8).CLOSE)
p2=ts_rank(decay_linear(correlation(ts_rank(self.close, 8), ts_rank(adv60,21), 8).to_frame(), 7).CLOSE, 3)
df=pd.DataFrame({'p1':p1,'p2':p2})
df.at[df['p1']>=df['p2'],'min']=df['p2']
df.at[df['p2']>=df['p1'],'min']=df['p1']
return df['min']
#return min(rank(decay_linear(((rank(self.open) + rank(self.low)) - (rank(self.high) + rank(self.close))).to_frame(),8).CLOSE), ts_rank(decay_linear(correlation(ts_rank(self.close, 8), ts_rank(adv60,20.6966), 8).to_frame(), 7).CLOSE, 3))
p1=rank(decay_linear(((rank(self.open) + rank(self.low)) - (rank(self.high) + rank(self.close))),8))
p2=ts_rank(decay_linear(correlation(ts_rank(self.close, 8), ts_rank(adv60,21), 8), 7).fillna(value=0), 3)
return p2.where(p2.values < p1.values, p1.values)
#return min(rank(decay_linear(((rank(self.open) + rank(self.low)) - (rank(self.high) + rank(self.close))),8)), ts_rank(decay_linear(correlation(ts_rank(self.close, 8), ts_rank(adv60,20.6966), 8), 7), 3))

# Alpha#89 (Ts_Rank(decay_linear(correlation(((low * 0.967285) + (low * (1 - 0.967285))), adv10,6.94279), 5.51607), 3.79744) - Ts_Rank(decay_linear(delta(IndNeutralize(vwap,IndClass.industry), 3.48158), 10.1466), 15.3012))

Expand All @@ -776,21 +785,18 @@ def alpha088(self):
# Alpha#92 min(Ts_Rank(decay_linear(((((high + low) / 2) + close) < (low + open)), 14.7221),18.8683), Ts_Rank(decay_linear(correlation(rank(low), rank(adv30), 7.58555), 6.94024),6.80584))
def alpha092(self):
adv30 = sma(self.volume, 30)
p1=ts_rank(decay_linear(((((self.high + self.low) / 2) + self.close) < (self.low + self.open)).to_frame(), 15).CLOSE,19)
p2=ts_rank(decay_linear(correlation(rank(self.low), rank(adv30), 8).to_frame(), 7).CLOSE,7)
df=pd.DataFrame({'p1':p1,'p2':p2})
df.at[df['p1']>=df['p2'],'min']=df['p2']
df.at[df['p2']>=df['p1'],'min']=df['p1']
return df['min']
#return min(ts_rank(decay_linear(((((self.high + self.low) / 2) + self.close) < (self.low + self.open)).to_frame(), 15).CLOSE,19), ts_rank(decay_linear(correlation(rank(self.low), rank(adv30), 8).to_frame(), 7).CLOSE,7))
p1=ts_rank(decay_linear(((((self.high + self.low) / 2) + self.close) < (self.low + self.open)), 15),19)
p2=ts_rank(decay_linear(correlation(rank(self.low), rank(adv30), 8).fillna(value=0), 7),7)
return p2.where(p2.values < p1.values, p1.values)
#return min(ts_rank(decay_linear(((((self.high + self.low) / 2) + self.close) < (self.low + self.open)), 15),19), ts_rank(decay_linear(correlation(rank(self.low), rank(adv30), 8), 7),7))

# Alpha#93 (Ts_Rank(decay_linear(correlation(IndNeutralize(vwap, IndClass.industry), adv81,17.4193), 19.848), 7.54455) / rank(decay_linear(delta(((close * 0.524434) + (vwap * (1 -0.524434))), 2.77377), 16.2664)))


# Alpha#94 ((rank((vwap - ts_min(vwap, 11.5783)))^Ts_Rank(correlation(Ts_Rank(vwap,19.6462), Ts_Rank(adv60, 4.02992), 18.0926), 2.70756)) * -1)
def alpha094(self):
adv60 = sma(self.volume, 60)
return ((rank((self.vwap - ts_min(self.vwap, 12))).pow(ts_rank(correlation(ts_rank(self.vwap,20), ts_rank(adv60, 4), 18), 3)) * -1))
return ((rank((self.vwap - ts_min(self.vwap, 12))).pow(ts_rank(correlation(ts_rank(self.vwap,20), ts_rank(adv60, 4), 18).fillna(value = 0), 3)) * -1))

# Alpha#95 (rank((open - ts_min(open, 12.4105))) < Ts_Rank((rank(correlation(sum(((high + low)/ 2), 19.1351), sum(adv40, 19.1351), 12.8742))^5), 11.7584))
def alpha095(self):
Expand All @@ -800,13 +806,10 @@ def alpha095(self):
# Alpha#96 (max(Ts_Rank(decay_linear(correlation(rank(vwap), rank(volume), 3.83878),4.16783), 8.38151), Ts_Rank(decay_linear(Ts_ArgMax(correlation(Ts_Rank(close, 7.45404),Ts_Rank(adv60, 4.13242), 3.65459), 12.6556), 14.0365), 13.4143)) * -1)
def alpha096(self):
adv60 = sma(self.volume, 60)
p1=ts_rank(decay_linear(correlation(rank(self.vwap), rank(self.volume).to_frame(), 4),4).CLOSE, 8)
p2=ts_rank(decay_linear(ts_argmax(correlation(ts_rank(self.close, 7),ts_rank(adv60, 4), 4), 13).to_frame(), 14).CLOSE, 13)
df=pd.DataFrame({'p1':p1,'p2':p2})
df.at[df['p1']>=df['p2'],'max']=df['p1']
df.at[df['p2']>=df['p1'],'max']=df['p2']
return -1*df['max']
#return (max(ts_rank(decay_linear(correlation(rank(self.vwap), rank(self.volume).to_frame(), 4),4).CLOSE, 8), ts_rank(decay_linear(ts_argmax(correlation(ts_rank(self.close, 7),ts_rank(adv60, 4), 4), 13).to_frame(), 14).CLOSE, 13)) * -1)
p1=ts_rank(decay_linear(correlation(rank(self.vwap), rank(self.volume), 4).fillna(value = 0),4), 8)
p2=ts_rank(decay_linear(ts_argmax(correlation(ts_rank(self.close, 7),ts_rank(adv60, 4), 4).fillna(value = 0), 13), 14), 13)
return -1* p1.where(p1.values > p2.values, p2.values)
#return (max(ts_rank(decay_linear(correlation(rank(self.vwap), rank(self.volume), 4),4), 8), ts_rank(decay_linear(ts_argmax(correlation(ts_rank(self.close, 7),ts_rank(adv60, 4), 4), 13), 14), 13)) * -1)

# Alpha#97 ((rank(decay_linear(delta(IndNeutralize(((low * 0.721001) + (vwap * (1 - 0.721001))),IndClass.industry), 3.3705), 20.4523)) - Ts_Rank(decay_linear(Ts_Rank(correlation(Ts_Rank(low,7.87871), Ts_Rank(adv60, 17.255), 4.97547), 18.5925), 15.7152), 6.71659)) * -1)

Expand All @@ -815,8 +818,9 @@ def alpha096(self):
def alpha098(self):
adv5 = sma(self.volume, 5)
adv15 = sma(self.volume, 15)
return (rank(decay_linear(correlation(self.vwap, sma(adv5, 26), 5).to_frame(), 7).CLOSE) -rank(decay_linear(ts_rank(ts_argmin(correlation(rank(self.open), rank(adv15), 21), 9),7).to_frame(), 8).CLOSE))

ret = (rank(decay_linear(correlation(self.vwap, sma(adv5, 26), 5), 7)) -rank(decay_linear(ts_rank(ts_argmin(correlation(rank(self.open), rank(adv15), 21).fillna(value = 0), 9),7), 8)))
return ret

# Alpha#99 ((rank(correlation(sum(((high + low) / 2), 19.8975), sum(adv60, 19.8975), 8.8136)) <rank(correlation(low, volume, 6.28259))) * -1)
def alpha099(self):
adv60 = sma(self.volume, 60)
Expand Down
22 changes: 19 additions & 3 deletions tests/test_alpha101.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def make_data_and_ref(num_stock, num_time, ischeck):
"alpha078": 0.1,
"alpha085": 0.1,
"alpha083": 0.05,
"alpha088": 0.05,
"alpha094": 0.05,
"alpha096": 1,
"alpha098": 0.1,
},
"rtol" : {
"alpha013": 0.1,
Expand All @@ -156,6 +160,10 @@ def make_data_and_ref(num_stock, num_time, ischeck):
"alpha072": 2e-1,
"alpha077": 1e-1,
"alpha078": 1e-1,
"alpha083": 0.05,
"alpha088": 1e-1,
"alpha094": 1e-1,
"alpha098": 1e-1,
},
"bad_count": {
"alpha008": 0.001,
Expand All @@ -167,7 +175,7 @@ def make_data_and_ref(num_stock, num_time, ischeck):
"alpha050": 0.003,
# hard selecting numbers >0
"alpha053": 0.001,
"alpha061": 0.001,
"alpha061": 0.002,
"alpha065": 0.002,
"alpha066": 0.002,
# corr on rank, will produce NAN
Expand All @@ -178,8 +186,16 @@ def make_data_and_ref(num_stock, num_time, ischeck):
"alpha077": 0.001,
"alpha078": 0.015,
"alpha081": 0.27,
"alpha008": 0.0005,
"alpha085": 0.005,
}
"alpha088": 0.005,
"alpha092": 0.15,
"alpha094": 0.01,
"alpha096": 0.10,
"alpha098": 0.06,
"alpha099": 0.0005
},
"skip_head": {"alpha096","alpha098"}
}

def test(modu, executor, start_window, num_stock, num_time, my_input, ref, ischeck, start_time):
Expand Down Expand Up @@ -216,7 +232,7 @@ def test(modu, executor, start_window, num_stock, num_time, my_input, ref, ische
cur_rtol = tolerance["rtol"].get(k, rtol)
cur_atol = tolerance["atol"].get(k, atol)
check_start = 0
if start_time:
if start_time or k in tolerance["skip_head"]:
check_start = start_window[k] + start_time
v = out[k][:,check_start-start_time:]
refv = ref[k][check_start:].to_numpy().transpose()
Expand Down

0 comments on commit 140880c

Please sign in to comment.