Created
April 14, 2019 00:49
-
-
Save requaos/678543fd8585d6e6c3d9540909d99546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Forwarning: This function is not optimized, so please leave a coment if you see something that I didn't | |
start transaction; | |
create or replace function rsi_func(state circle, inval numeric) | |
returns circle | |
language plpgsql as $$ | |
begin | |
return case | |
when state is null then (select circle(point(sum(CASE WHEN t.gains >= 0 THEN t.gains ELSE 0 END)/14,(abs(sum(CASE WHEN t.gains < 0 THEN t.gains ELSE 0 END))/14)), inval) rs from (select price - lag(price, 1) over (rows between 14 preceding and current row) gains from quotes where type = 'last') as t) | |
else circle(point(((select g[0] from point(state) as g) * 13 + (CASE WHEN inval - radius(state) >= 0 THEN inval - radius(state) ELSE 0 END))/14,((select l[1] from point(state) as l) * 13 + (CASE WHEN inval - radius(state) < 0 THEN inval - radius(state) ELSE 0 END))/14), inval) | |
end; | |
end | |
$$; | |
create or replace function rsi_finalfunc(state circle) | |
returns numeric | |
immutable | |
strict | |
language plpgsql | |
as $$ | |
begin | |
return 100/(1+((select g[0] from point(state) as g)/(select l[1] from point(state) as l))); | |
end; | |
$$; | |
drop aggregate if exists rsi(numeric); | |
create aggregate rsi(numeric) (sfunc = rsi_func, stype = circle, finalfunc = rsi_finalfunc); | |
commit transaction; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment