Skip to content

Commit

Permalink
adding some docs and small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tspenov committed Feb 22, 2019
1 parent 8387f6d commit e9f9db0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
Expand Up @@ -25,7 +25,7 @@ defmodule Sanbase.Signals.History.DailyActiveAddressesHistory do
@historical_days_from 90
@historical_days_to 1
@historical_days_interval "1d"
# Minimal time window is 2 days. That is due to data being bucketed in `1 day` intervals.
# Because the data is bucketed in daily intervals
@minimal_time_window_in_days 2

alias Sanbase.Signals.History.DailyActiveAddressesHistory
Expand Down Expand Up @@ -53,6 +53,7 @@ defmodule Sanbase.Signals.History.DailyActiveAddressesHistory do
) do
prices = Enum.map(prices, fn [dt, price | _rest] -> %{datetime: dt, price: price} end)

# merge daily_active_addresses and prices for 90 days
daa_result =
Enum.zip(daa_result, prices)
|> Enum.map(fn {daa_item, price_item} -> Map.merge(daa_item, price_item) end)
Expand All @@ -63,6 +64,9 @@ defmodule Sanbase.Signals.History.DailyActiveAddressesHistory do
active_addresses =
daa_result |> Enum.map(fn %{active_addresses: active_addresses} -> active_addresses end)

# chunk in time window days + 1 buckets,
# calculate percent changes between average of all but last and last value
# calculate tuples {percent_change, is_trigger_point} where is_trigger_point considers cooldown
percent_change_calculations =
active_addresses
|> Enum.chunk_every(time_window_in_days + 1, 1, :discard)
Expand All @@ -72,8 +76,10 @@ defmodule Sanbase.Signals.History.DailyActiveAddressesHistory do
cooldown_in_days
)

# for the first time_window days we don't check whether they are trigger points
empty_calculations = Stream.cycle([{0.0, false}]) |> Enum.take(time_window_in_days)

# add percent_change and boolean triggered? to the daily_active_addresses and prices
points =
merge_percent_change_into_points(
daa_result,
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/signals/history/prices_history.ex
Expand Up @@ -26,8 +26,8 @@ defmodule Sanbase.Signals.History.PricesHistory do
triggered?: boolean()
}

def get_prices(settings) do
with measurement when not is_nil(measurement) <- Measurement.name_from_slug(settings.target),
def get_prices(%{target: target} = settings) when is_binary(target) do
with measurement when not is_nil(measurement) <- Measurement.name_from_slug(target),
{from, to, interval} <- get_timeseries_params(),
{:ok, price_list} when is_list(price_list) and price_list != [] <-
PricesStore.fetch_prices_with_resolution(measurement, from, to, interval) do
Expand Down
13 changes: 13 additions & 0 deletions lib/sanbase/signals/history/utils.ex
@@ -1,6 +1,19 @@
defmodule Sanbase.Signals.History.Utils do
import Sanbase.Signals.Utils

@type percent_change_calculations :: {float(), boolean()}

@doc ~s"""
* Takes a list of tuples: {grouped_value, current}
* calculates the percent_change between grouped_value and current
* then calculates a list of tuples {percent_change, condition_met} where condition_met is
the percent_change bigger than threshold param and not in cooldown
"""
@spec percent_change_calculations_with_cooldown(
list({number(), number()}),
float(),
non_neg_integer()
) :: list(percent_change_calculations)
def percent_change_calculations_with_cooldown(values, percent_threshold, cooldown) do
{percent_change_calculations, _} =
values
Expand Down
12 changes: 2 additions & 10 deletions lib/sanbase/utils/datetime/utils.ex
Expand Up @@ -96,22 +96,14 @@ defmodule Sanbase.DateTimeUtils do
interval_in_seconds = compound_duration_to_seconds(interval)
one_day_in_seconds = 3600 * 24

if interval_in_seconds < one_day_in_seconds do
0
else
Sanbase.Utils.Math.to_integer(interval_in_seconds / one_day_in_seconds)
end
div(interval_in_seconds, one_day_in_seconds)
end

def compound_duration_to_hours(interval) do
interval_in_seconds = compound_duration_to_seconds(interval)
one_hour_in_seconds = 3600

if interval_in_seconds < one_hour_in_seconds do
0
else
Sanbase.Utils.Math.to_integer(interval_in_seconds / one_hour_in_seconds)
end
div(interval_in_seconds, one_hour_in_seconds)
end

def compound_duration_to_text(interval) do
Expand Down

0 comments on commit e9f9db0

Please sign in to comment.