Current section
Files
Jump to
Current section
Files
src/ursatoro@volume.erl
-module(ursatoro@volume).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/ursatoro/volume.gleam").
-export([vwap/1, obv/1]).
-file("src/ursatoro/volume.gleam", 11).
-spec vwap(list(ursatoro@candle:candle())) -> {ok, list(float())} |
{error, ursatoro@util:indicator_error()}.
vwap(Candles) ->
gleam@result:'try'(
ursatoro@util:validate_length(Candles, 1),
fun(_) ->
Three = ursatoro@util:int_to_bd(3),
{Results@1, _, _} = gleam@list:fold(
Candles,
{[], bigdecimal:zero(), bigdecimal:zero()},
fun(Acc, C) ->
{Results, Cum_tp_vol, Cum_vol} = Acc,
Tp = bigdecimal:divide(
bigdecimal:add(
bigdecimal:add(
ursatoro@candle:high(C),
ursatoro@candle:low(C)
),
ursatoro@candle:close(C)
),
Three,
half_up
),
Tp_vol = bigdecimal:multiply(Tp, ursatoro@candle:volume(C)),
New_cum_tp_vol = bigdecimal:add(Cum_tp_vol, Tp_vol),
New_cum_vol = bigdecimal:add(
Cum_vol,
ursatoro@candle:volume(C)
),
Vwap_val = case bigdecimal:signum(New_cum_vol) of
0 ->
bigdecimal:zero();
_ ->
bigdecimal:divide(
New_cum_tp_vol,
New_cum_vol,
half_up
)
end,
{[Vwap_val | Results], New_cum_tp_vol, New_cum_vol}
end
),
{ok,
begin
_pipe = lists:reverse(Results@1),
ursatoro@util:bds_to_floats(_pipe)
end}
end
).
-file("src/ursatoro/volume.gleam", 47).
-spec obv(list(ursatoro@candle:candle())) -> {ok, list(float())} |
{error, ursatoro@util:indicator_error()}.
obv(Candles) ->
gleam@result:'try'(
ursatoro@util:validate_length(Candles, 1),
fun(_) -> case Candles of
[] ->
{ok, []};
[First | Rest] ->
{Results@1, _, _} = gleam@list:fold(
Rest,
{[bigdecimal:zero()], bigdecimal:zero(), First},
fun(Acc, C) ->
{Results, Prev_obv, Prev_candle} = Acc,
New_obv = case bigdecimal:compare(
ursatoro@candle:close(C),
ursatoro@candle:close(Prev_candle)
) of
gt ->
bigdecimal:add(
Prev_obv,
ursatoro@candle:volume(C)
);
lt ->
bigdecimal:subtract(
Prev_obv,
ursatoro@candle:volume(C)
);
eq ->
Prev_obv
end,
{[New_obv | Results], New_obv, C}
end
),
{ok,
begin
_pipe = lists:reverse(Results@1),
ursatoro@util:bds_to_floats(_pipe)
end}
end end
).