The following code can be used to implement the calculation of the Alpha 147 factor:
// 1. Simulate data
tmp_table = table(
concatDateTime(take(2023.12.12, 4802000), (rand((09:30:00.000+0..2400*3*1000) join (13:00:00.000+0..2400*3*1000), 4802000).sort())) as dt,
rand(string(1001..1200), 4802000) as symbol,
rand(100.0, 4802000) as price,
rand(100, 4802000) as vol);
// 2. Alpha 147 implementation
defg olss(x) : ols(x, 1..12)[1]
def alpha147SQL(vector) : moving(olss, mavg(vector, 12), 12)
alpha147DDBSql = select alpha147SQL(price)
from loadTable("dfs://ohlc_test", "ohlc_test")
where date(ts) = 2023.12.12
context by symbol;
Test Data: 4.3GB (4,802,000 rows)
Method | Time |
---|---|
Single-threaded in-memory computation | 6.8s |
Distributed multi-threaded computation | 2s |