Skip to content

Instantly share code, notes, and snippets.

@joachimdb
Last active September 26, 2019 10:03
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 joachimdb/7cdd4888a5ba5a1ab2d7ee2d66099bde to your computer and use it in GitHub Desktop.
Save joachimdb/7cdd4888a5ba5a1ab2d7ee2d66099bde to your computer and use it in GitHub Desktop.
defmodule ActivationPolicy do
alias __MODULE__
@moduledoc """
Provides a generic structure for representing activation policies and their state, as well as an
interface for updating and querying state.
See submodules for specific implementations
"""
defstruct [
:market,
:type,
:parameters,
:state
]
@doc """
Returns {:ok, %ActivationPolicy{}} of type with params and given the current market state
"""
def new(market, type, params, market_state) do
with {:ok, initial_state} <- type.init(params, market_state) do
{:ok,
%ActivationPolicy{market: market, type: type, parameters: params, state: initial_state}}
end
end
@doc """
Update an ActivationPolicy with new market state data
Returns {:ok, updated_policy}
"""
def update(%ActivationPolicy{} = ap, market_state) do
with {:ok, new_state} <- ap.type.update(ap.parameters, ap.state, market_state) do
{:ok, %{ap | state: new_state}}
end
end
@doc """
Returns true if the policy is active
"""
def active?(%ActivationPolicy{} = ap) do
ap.type.active?(ap.state)
end
@doc """
Returns a probability (a number between 0 and 1) that the policy will change state in the nexr 24h hours (or
another timespan in milliseconds if specified) given the current market state
"""
def probability(%ActivationPolicy{} = ap, market_state, timespan \\ 86_400_000) do
ap.type.probability(ap.parameters, ap.state, market_state, timespan)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment