TradingView
gorx1
31 สิงหา 2022 เวลา 13 นาฬิกา 46 นาที

Weighted percentile nearest rank 

Petco Health and Wellness Company, Inc.NASDAQ

คำอธิบาย

Yo, posting it for the whole internet, took the whole day to find / to design the actual working solution for weighted percentile 'nearest rank' algorithm, almost no reliable info online and a lot of library-style/textbook-style solutions that don't provide on real world production level.

The principle:

0) initial data
data = 22, 33, 11, 44, 55
weights = 5 , 3 , 2 , 1 , 4

array(s) size = 5

1) sort data array, apply the sorting pattern to the weights array, resulting:
data = 11, 22, 33, 44, 55
weights = 2 , 5 , 3 , 1 , 4

2) get weights cumsum and sum:
weights = 2, 5, 3 , 1 , 4
weights_cum = 2, 7, 10, 11, 15
weights_sum = 15

3) say we wanna find 50th percentile, get a threshold value:
n = 50
thres = weights_sum / 100 * n
7.5 = 15 / 100 * 50

4) iterate through weights_cum until you find a value that >= the threshold:
for i = 0 to size - 1
2 >= 7.5 ? nah
7 >= 7.5 ? nah
10 >= 7.5 ? aye

5) take the iteration index that resulted "aye", and find the data value with the same index, that's gonna be the resulting percentile.
i = 2
data = 33

This one is not an approximation, not an estimator, it's the actual weighted percentile nearest rank as it is.

I tested the thing extensively and it works perfectly.
For the skeptics, check lines 40, 41, 69 in the code, you can comment/uncomment dem to switch for unit (1) weights, resulting in the usual non-weighted percentile nearest rank that ideally matches the TV's built-in function.

Shoutout for @wallneradam for the sorting function mane
...
Live Long and Prosper
ความคิดเห็น
sjakk
Perfect! Im thinking intresting results if it had an option to throw out f.ex 5% tails
gorx1
@sjakk, yeah I see what you mean, I always forget whether it's called trimming or truncating xD aye aye, will keep in mind
PineCoders
vietthien2
really appreciate your hard work, bro!

Wish you success and happiness <3
gorx1
peacefulLizard50262
gorx1
R&D Log, Nov 18 2022:

Wassup every1, people been asking how to use the stuff, here's what I got at this point:

1) Resist the temptation to use percentiles to 'model' micro-structural support & resistances. First, organics that know what they're doing, ones who really know how to do it, are unbeatable there, ain't to no algo / auto learning method that will scalable & efficient enough to beat real traders in business env in terms for trading the levels. Second, if you really wanna try, algorithms 'can' be constructed to find support & resistances, but not to approximate via a formula (eg WPNR). Third, SR 'modelling' is hard to prove / disprove, who knows whether a bounce from SR was an accident or nah?;

2) Current use case for WPNRs that I've come up with is switching between different strategies. I don't wanna get too deep, just trust me shit has so delicate, beautiful & deep roots in mathematics & how markets, and the universe itself work, that all these textbook quants can sit 20 more years in a library and dem still won't get it. Alas,
Take a window length 50-150, I suggest starting with 64, that will be your window for percentiles.
Now, consider another window length 10-30, that will be the window for the actual trades, start with 16.

For volatility trades, get into momentum trades above 90th and below 10th percentiles, otherwise consider mean-reversion trades;
For direction trades, notice the ''overall' slope of the 50th percentile:
- If it's positive, directional trades above 50th percentile and counter directional trades below 50th percentile;
- If it's negative, directional trades below 50th percentile and counter directional trades above 50th percentile.

Other use cases & including using 1st and 100th percentiles are possible as well, but so far that's it.
amamit2080
@gorx1, excellent
A_Traders_Edge
I keep seeing you use the acronym "dem". For the life of me I cannot figure out what it stands for?
gorx1
@chasinalts, bro 4 real xd ?

youtube.com/watch?v=MYp_gJQwRx8

^^ check the lyrics, dem will explain
เพิ่มเติม