Skip to content

Commit

Permalink
050. 060-072
Browse files Browse the repository at this point in the history
  • Loading branch information
Menooker committed Jan 25, 2024
1 parent da6685e commit e9940ef
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 35 deletions.
3 changes: 3 additions & 0 deletions KunQuant/ops/ElewiseOp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Or(BinaryElementwiseOp):
class And(BinaryElementwiseOp):
pass

class Max(BinaryElementwiseOp):
pass

class Div(BinaryElementwiseOp):
pass

Expand Down
29 changes: 17 additions & 12 deletions KunQuant/passes/Partitioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,20 @@ def _partition(f: Function, partition_thres = 3) -> List[_Partition]:
# print("============\nnew partition:", partition)
selected = _select_next(ready_ops, opinfo, partition, f)
while selected:
num_batch += 1
# maintain the ready queue for topology sort
for user in f.op_to_id[selected].uses:
pendingset = opinfo[user].pending_dep
del pendingset[selected]
if len(pendingset) == 0:
ready_ops.append((user, num_batch))
del to_visit[selected]
del_from_ready_op(ready_ops, selected)
# remove the pending dependency. If an op is ready, put into ready queue
def maintain_ready_queue(s_op: OpBase):
nonlocal num_batch
num_batch += 1
# maintain the ready queue for topology sort
for user in f.op_to_id[s_op].uses:
pendingset = opinfo[user].pending_dep
del pendingset[s_op]
if len(pendingset) == 0:
ready_ops.append((user, num_batch))
del to_visit[s_op]
del_from_ready_op(ready_ops, s_op)
# end of maintain_ready_queue()
maintain_ready_queue(selected)
if isinstance(selected, GraphSourceTrait):
# don't put input in partition yet
pass
Expand All @@ -154,9 +159,8 @@ def _partition(f: Function, partition_thres = 3) -> List[_Partition]:
# if an output op is directly connected with CrossSectionalOp, merge it in the partition
for user in f.op_to_id[selected].uses:
if isinstance(user, Output):
del_from_ready_op(ready_ops, user)
maintain_ready_queue(user)
single_partition.add(opinfo, user)
del to_visit[user]
partitions.append(single_partition)
else:
# add op to partition
Expand All @@ -181,7 +185,8 @@ def _partition(f: Function, partition_thres = 3) -> List[_Partition]:
selected = _select_next(ready_ops, opinfo, partition, f)
if partition.ops.__len__():
partitions.append(partition)
assert(to_visit.__len__()==0)
if to_visit.__len__() != 0:
raise RuntimeError("Some ops are not visited") #"Some ops not visited: "+ "\n".join([str(eop) for eop in to_visit]))
return partitions

def _search_output_use(op: OpBase, info: OpInfo) -> OpBase:
Expand Down
2 changes: 2 additions & 0 deletions KunQuant/passes/SpecialOpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def _is_const_1(op: OpBase) -> bool:
def _is_mul_1(op: OpBase) -> OpBase:
if isinstance(op, MulConst) and op.attrs["value"] == -1:
return SubConst(op.inputs[0], 0, True)
if isinstance(op, MulConst) and op.attrs["value"] == 1:
return op.inputs[0]
if isinstance(op, Mul):
if _is_const_1(op.inputs[0]):
return op.inputs[1]
Expand Down
74 changes: 72 additions & 2 deletions KunQuant/predefined/Alpha101.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,84 @@ def alpha054(self: AllData):
inner = Select(Equals(inner, ConstantOp(0)), ConstantOp(0.0001), inner)
return -1 * (self.low - self.close) * (Pow(self.open, ConstantOp(5))) / (inner * Pow(self.close, ConstantOp(5)))

def alpha055(self: AllData):
divisor = (ts_max(self.high, 12) - ts_min(self.low, 12))
divisor = Select(Equals(divisor, ConstantOp(0)), ConstantOp(0.0001), divisor)
inner = (self.close - ts_min(self.low, 12)) / (divisor)
df = correlation(rank(inner), rank(self.volume), 6)
return -1 * SetInfOrNanToValue(df)

def alpha057(self: AllData):
return (0 - (1 * ((self.close - self.vwap) / DecayLinear(rank(ts_argmax(self.close, 30)), 2))))

# Alpha#60 (0 - (1 * ((2 * scale(rank(((((close - low) - (high - close)) / (high - low)) * volume)))) -scale(rank(ts_argmax(close, 10))))))
def alpha060(self: AllData):
divisor = (self.high - self.low)
divisor = Select(Equals(divisor, ConstantOp(0)), ConstantOp(0.0001), divisor)
inner = ((self.close - self.low) - (self.high - self.close)) * self.volume / divisor
return 0 - ((2 * scale(rank(inner))) - scale(rank(ts_argmax(self.close, 10))))

# Alpha#61 (rank((vwap - ts_min(vwap, 16.1219))) < rank(correlation(vwap, adv180, 17.9282)))
def alpha061(self: AllData):
adv180 = sma(self.volume, 180)
return Select(rank((self.vwap - ts_min(self.vwap, 16))) < rank(correlation(self.vwap, adv180, 18)), ConstantOp(1), ConstantOp(0))

# Alpha#62 ((rank(correlation(vwap, sum(adv20, 22.4101), 9.91009)) < rank(((rank(open) +rank(open)) < (rank(((high + low) / 2)) + rank(high))))) * -1)
def alpha062(self: AllData):
adv20 = sma(self.volume, 20)
v1 = rank(correlation(self.vwap, sma(adv20, 22), 10))
v2 = (rank(self.open) +rank(self.open))
v3 = (rank(((self.high + self.low) / 2)) + rank(self.high))
v4 = Select(v2 < v3, ConstantOp(1), ConstantOp(0))
v5 = Select(v1 < rank(v4), ConstantOp(1), ConstantOp(0))
return (v5 * -1)

# Alpha#64 ((rank(correlation(sum(((open * 0.178404) + (low * (1 - 0.178404))), 12.7054),sum(adv120, 12.7054), 16.6208)) < rank(delta(((((high + low) / 2) * 0.178404) + (vwap * (1 -0.178404))), 3.69741))) * -1)
def alpha064(self: AllData):
adv120 = sma(self.volume, 120)
a = rank(correlation(sma(((self.open * 0.178404) + (self.low * (1 - 0.178404))), 13),sma(adv120, 13), 17))
b = rank(delta(((((self.high + self.low) / 2) * 0.178404) + (self.vwap * (1 -0.178404))), 4))
c = Select(a < b, ConstantOp(1), ConstantOp(0))
return (c * -1)

# Alpha#65 ((rank(correlation(((open * 0.00817205) + (vwap * (1 - 0.00817205))), sum(adv60,8.6911), 6.40374)) < rank((open - ts_min(open, 13.635)))) * -1)
def alpha065(self: AllData):
adv60 = sma(self.volume, 60)
a = rank(correlation(((self.open * 0.00817205) + (self.vwap * (1 - 0.00817205))), sma(adv60,9), 6))
b = rank((self.open - ts_min(self.open, 14)))
return (Select(a < b, ConstantOp(1), ConstantOp(0)) * -1)

# Alpha#66 ((rank(decay_linear(delta(vwap, 3.51013), 7.23052)) + Ts_Rank(decay_linear(((((low* 0.96633) + (low * (1 - 0.96633))) - vwap) / (open - ((high + low) / 2))), 11.4157), 6.72611)) * -1)
def alpha066(self: AllData):
return ((rank(decay_linear(delta(self.vwap, 4), 7)) + ts_rank(decay_linear(((((self.low* 0.96633) + (self.low * (1 - 0.96633))) - self.vwap) / (self.open - ((self.high + self.low) / 2))), 11), 7)) * -1)

# Alpha#68 ((Ts_Rank(correlation(rank(high), rank(adv15), 8.91644), 13.9333) <rank(delta(((close * 0.518371) + (low * (1 - 0.518371))), 1.06157))) * -1)
def alpha068(self):
adv15 = sma(self.volume, 15)
a = ts_rank(correlation(rank(self.high), rank(adv15), 9), 14)
b = rank(delta(((self.close * 0.518371) + (self.low * (1 - 0.518371))), 1))
return (Select(a < b, ConstantOp(1), ConstantOp(0)) * -1)

# Alpha#71 max(Ts_Rank(decay_linear(correlation(Ts_Rank(close, 3.43976), Ts_Rank(adv180,12.0647), 18.0175), 4.20501), 15.6948), Ts_Rank(decay_linear((rank(((low + open) - (vwap +vwap)))^2), 16.4662), 4.4388))
def alpha071(self):
adv180 = sma(self.volume, 180)
p1=ts_rank(decay_linear(correlation(ts_rank(self.close, 3), ts_rank(adv180,12), 18), 4), 16)
inner = Pow(rank(((self.low + self.open) - (self.vwap +self.vwap))), ConstantOp(2))
p2=ts_rank(decay_linear(inner, 16), 4)
return Max(p1, p2)
#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))

# 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):
adv40 = sma(self.volume, 40)
a = rank(decay_linear(correlation(((self.high + self.low) / 2), adv40, 9), 10)) + 0.0001
b = rank(decay_linear(correlation(ts_rank(self.vwap, 4), ts_rank(self.volume, 19), 7),3)) + 0.0001
return (a / b)

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,
alpha057
alpha044, alpha045, alpha046, alpha047, alpha049, alpha050, alpha051, alpha052, alpha053, alpha054, alpha055,
alpha057, alpha060, alpha061, alpha062, alpha064, alpha065, alpha066, alpha068, alpha071, alpha072
]
4 changes: 4 additions & 0 deletions cpp/Kun/Ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ struct ReduceRank {
operator f32x8() { return less_count + (eq_count + 1.0f) / 2.0f; }
};

inline f32x8 Max(f32x8 a, f32x8 b) {
return kun_simd::sc_max(a, b);
}

inline f32x8 Abs(f32x8 a) { return kun_simd::sc_abs(kun_simd::vec_f32x8(a)); }

inline f32x8 Add(f32x8 a, f32x8 b) { return _mm256_add_ps(a, b); }
Expand Down
41 changes: 21 additions & 20 deletions tests/KunTestUtil/ref_alpha101.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ def get_alpha(df):
df['alpha052']=stock.alpha052()
df['alpha053']=stock.alpha053()
df['alpha054']=stock.alpha054()
# df['alpha055']=stock.alpha055()
df['alpha055']=stock.alpha055()
df['alpha057']=stock.alpha057()
# df['alpha060']=stock.alpha060()
# df['alpha061']=stock.alpha061()
# df['alpha062']=stock.alpha062()
# df['alpha064']=stock.alpha064()
# df['alpha065']=stock.alpha065()
# df['alpha066']=stock.alpha066()
# df['alpha068']=stock.alpha068()
# df['alpha071']=stock.alpha071()
# df['alpha072']=stock.alpha072()
df['alpha060']=stock.alpha060()
df['alpha061']=stock.alpha061()
df['alpha062']=stock.alpha062()
df['alpha064']=stock.alpha064()
df['alpha065']=stock.alpha065()
df['alpha066']=stock.alpha066()
df['alpha068']=stock.alpha068()
df['alpha071']=stock.alpha071()
df['alpha072']=stock.alpha072()
# df['alpha073']=stock.alpha073()
# df['alpha074']=stock.alpha074()
# df['alpha075']=stock.alpha075()
Expand Down Expand Up @@ -644,7 +644,7 @@ def alpha062(self):
# Alpha#64 ((rank(correlation(sum(((open * 0.178404) + (low * (1 - 0.178404))), 12.7054),sum(adv120, 12.7054), 16.6208)) < rank(delta(((((high + low) / 2) * 0.178404) + (vwap * (1 -0.178404))), 3.69741))) * -1)
def alpha064(self):
adv120 = sma(self.volume, 120)
return ((rank(correlation(sma(((self.open * 0.178404) + (self.low * (1 - 0.178404))), 13),sma(adv120, 13), 17)) < rank(delta(((((self.high + self.low) / 2) * 0.178404) + (self.vwap * (1 -0.178404))), 3.69741))) * -1)
return ((rank(correlation(sma(((self.open * 0.178404) + (self.low * (1 - 0.178404))), 13),sma(adv120, 13), 17)) < rank(delta(((((self.high + self.low) / 2) * 0.178404) + (self.vwap * (1 -0.178404))), 4))) * -1)

# Alpha#65 ((rank(correlation(((open * 0.00817205) + (vwap * (1 - 0.00817205))), sum(adv60,8.6911), 6.40374)) < rank((open - ts_min(open, 13.635)))) * -1)
def alpha065(self):
Expand All @@ -653,15 +653,15 @@ def alpha065(self):

# Alpha#66 ((rank(decay_linear(delta(vwap, 3.51013), 7.23052)) + Ts_Rank(decay_linear(((((low* 0.96633) + (low * (1 - 0.96633))) - vwap) / (open - ((high + low) / 2))), 11.4157), 6.72611)) * -1)
def alpha066(self):
return ((rank(decay_linear(delta(self.vwap, 4).to_frame(), 7).CLOSE) + ts_rank(decay_linear(((((self.low* 0.96633) + (self.low * (1 - 0.96633))) - self.vwap) / (self.open - ((self.high + self.low) / 2))).to_frame(), 11).CLOSE, 7)) * -1)
return ((rank(decay_linear(delta(self.vwap, 4), 7)) + ts_rank(decay_linear(((((self.low* 0.96633) + (self.low * (1 - 0.96633))) - self.vwap) / (self.open - ((self.high + self.low) / 2))), 11), 7)) * -1)

# Alpha#67 ((rank((high - ts_min(high, 2.14593)))^rank(correlation(IndNeutralize(vwap,IndClass.sector), IndNeutralize(adv20, IndClass.subindustry), 6.02936))) * -1)


# Alpha#68 ((Ts_Rank(correlation(rank(high), rank(adv15), 8.91644), 13.9333) <rank(delta(((close * 0.518371) + (low * (1 - 0.518371))), 1.06157))) * -1)
def alpha068(self):
adv15 = sma(self.volume, 15)
return ((ts_rank(correlation(rank(self.high), rank(adv15), 9), 14) <rank(delta(((self.close * 0.518371) + (self.low * (1 - 0.518371))), 1.06157))) * -1)
return ((ts_rank(correlation(rank(self.high), rank(adv15), 9), 14) <rank(delta(((self.close * 0.518371) + (self.low * (1 - 0.518371))), 1))) * -1)

# Alpha#69 ((rank(ts_max(delta(IndNeutralize(vwap, IndClass.industry), 2.72412),4.79344))^Ts_Rank(correlation(((close * 0.490655) + (vwap * (1 - 0.490655))), adv20, 4.92416),9.0615)) * -1)

Expand All @@ -671,18 +671,19 @@ def alpha068(self):
# Alpha#71 max(Ts_Rank(decay_linear(correlation(Ts_Rank(close, 3.43976), Ts_Rank(adv180,12.0647), 18.0175), 4.20501), 15.6948), Ts_Rank(decay_linear((rank(((low + open) - (vwap +vwap)))^2), 16.4662), 4.4388))
def alpha071(self):
adv180 = sma(self.volume, 180)
p1=ts_rank(decay_linear(correlation(ts_rank(self.close, 3), ts_rank(adv180,12), 18).to_frame(), 4).CLOSE, 16)
p2=ts_rank(decay_linear((rank(((self.low + self.open) - (self.vwap +self.vwap))).pow(2)).to_frame(), 16).CLOSE, 4)
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 df['max']
p1=ts_rank(decay_linear(correlation(ts_rank(self.close, 3), ts_rank(adv180,12), 18), 4), 16)
p2=ts_rank(decay_linear((rank(((self.low + self.open) - (self.vwap +self.vwap))).pow(2)), 16), 4)
# 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 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))

# 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):
adv40 = sma(self.volume, 40)
return (rank(decay_linear(correlation(((self.high + self.low) / 2), adv40, 9).to_frame(), 10).CLOSE) /rank(decay_linear(correlation(ts_rank(self.vwap, 4), ts_rank(self.volume, 19), 7).to_frame(),3).CLOSE))
return (rank(decay_linear(correlation(((self.high + self.low) / 2), adv40, 9), 10)) /rank(decay_linear(correlation(ts_rank(self.vwap, 4), ts_rank(self.volume, 19), 7),3)))

# Alpha#73 (max(rank(decay_linear(delta(vwap, 4.72775), 2.91864)),Ts_Rank(decay_linear(((delta(((open * 0.147155) + (low * (1 - 0.147155))), 2.03608) / ((open *0.147155) + (low * (1 - 0.147155)))) * -1), 3.33829), 16.7411)) * -1)
def alpha073(self):
Expand Down
11 changes: 10 additions & 1 deletion tests/test_alpha101.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,29 @@ def make_data_and_ref(num_stock, num_time, ischeck):
# are not stable
"alpha045": 0.3,
"alpha050": 0.1,
"alpha055": 0.1,
},
"rtol" : {
"alpha013": 0.1,
"alpha014": 1e-2,
"alpha016": 0.1,
"alpha036": 2e-2,
"alpha043": 0.1,
"alpha045": 0.1,
"alpha050": 1e-1,
"alpha072": 1e-1,
},
"bad_count": {
"alpha027": 0.07,
"alpha021": 0.001,
"alpha045": 0.001,
"alpha045": 0.07,
"alpha050": 0.001,
# hard selecting numbers >0
"alpha053": 0.001
"alpha053": 0.001,
"alpha061": 0.001,
"alpha071": 0.002,
"alpha072": 0.009,
}
}

Expand Down

0 comments on commit e9940ef

Please sign in to comment.