Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save requaos/678543fd8585d6e6c3d9540909d99546 to your computer and use it in GitHub Desktop.
Save requaos/678543fd8585d6e6c3d9540909d99546 to your computer and use it in GitHub Desktop.
-- 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