Longbridge.DCAContext (longbridge v0.1.0)

Copy Markdown View Source

Dollar-cost averaging (DCA) plan context.

Manages recurring buy plans: create, list, update, pause, resume, and stop.

All functions accept a Longbridge.Config struct and return {:ok, data} | {:error, reason} tuples.

Usage

config = Longbridge.Config.new(...)

{:ok, plan} = Longbridge.DCAContext.create_plan(config,
  symbol: "AAPL.US",
  amount: "100.00",
  frequency: :weekly
)

Summary

Functions

Stops (permanently finishes) a plan by plan_id.

Lists all DCA plans. Supports pagination.

Pauses an active plan by plan_id.

Fetches a single plan by plan_id from the list endpoint.

Resumes a paused plan by plan_id.

Updates an existing plan. Same options as create_plan/2 plus :plan_id.

Types

frequency()

@type frequency() :: :daily | :weekly | :biweekly | :monthly

Functions

create_plan(config, opts, http_opts \\ [])

@spec create_plan(Longbridge.Config.t(), keyword(), keyword()) ::
  {:ok, map()} | {:error, term()}

Creates a new DCA plan.

Options

  • :symbol — target symbol (required)
  • :amount — amount per period (required)
  • :frequency:daily, :weekly, :biweekly, :monthly (required)
  • :day_of_week — for weekly frequency, e.g. "Monday"
  • :day_of_month — for monthly frequency, "1" through "31"
  • :allow_margin — boolean, defaults to false

Trailing http_opts is forwarded to HTTPClient.request_json/5, so callers may override :http_url, :finch, etc. on a per-call basis.

delete_plan(config, plan_id, http_opts \\ [])

@spec delete_plan(Longbridge.Config.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, term()}

Stops (permanently finishes) a plan by plan_id.

The upstream API does not expose a true DELETE; finishing is the irreversible terminal state.

list_plans(config, opts \\ [])

@spec list_plans(
  Longbridge.Config.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Lists all DCA plans. Supports pagination.

Options

  • :page — 1-indexed page (default 1)
  • :limit — page size (default 100)
  • :status:active, :suspended, or :finished
  • :symbol — filter by symbol

HTTP-level keys such as :http_url and :finch may be passed in the same keyword list; they are forwarded to HTTPClient.request_json/5 alongside the built query string. Function-built :params take precedence over any caller-supplied :params.

pause_plan(config, plan_id, http_opts \\ [])

@spec pause_plan(Longbridge.Config.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, term()}

Pauses an active plan by plan_id.

plan_detail(config, plan_id, http_opts \\ [])

@spec plan_detail(Longbridge.Config.t(), String.t(), keyword()) ::
  {:ok, map() | nil} | {:error, term()}

Fetches a single plan by plan_id from the list endpoint.

The upstream API does not have a per-plan detail endpoint; this helper filters the list response so callers that only want one plan don't have to walk the pagination.

resume_plan(config, plan_id, http_opts \\ [])

@spec resume_plan(Longbridge.Config.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, term()}

Resumes a paused plan by plan_id.

update_plan(config, opts, http_opts \\ [])

@spec update_plan(Longbridge.Config.t(), keyword(), keyword()) ::
  {:ok, map()} | {:error, term()}

Updates an existing plan. Same options as create_plan/2 plus :plan_id.

Trailing http_opts is forwarded to HTTPClient.request_json/5, so callers may override :http_url, :finch, etc. on a per-call basis.