OHLC (ohlc v1.0.0)

Library for generating OHLC candles from trades.

OHLC takes ordered list of trade events as input and outputs OHLC candles list.

Library includes few options for appending candles to existing candles lists and more.

This library could be useful if you want to create your own charting engine or trading bot.

Documentation can be found here: https://hexdocs.pm/ohlc/1.0.0/OHLC.html

Installation

The package can be installed by adding ohlc to your list of dependencies in mix.exs:

def deps do
  [
    {:ohlc, "~> 1.0"}
  ]
end

Example usage

defmodule Example do
  def calculate_ohlc() do
    trades = [
      [price: 12, volume: 22, time: 1616439602],
      [price: 12.56, volume: 18.3, time: 1616440572],
      [price: 18.9, volume: 12, time: 1616440692],
      [price: 11, volume: 43, time: 1616440759]
    ]

    case OHLC.create_candles(trades, :minute, [validate_trades: true]) do
      {:ok, data} -> IO.inspect data
      {:error, msg} -> IO.puts msg
    end
  end
end

Example output

%{
  candles: [
    %{
      close: 11.0,
      etime: 1616440800,
      high: 11.0,
      low: 11.0,
      open: 11.0,
      processed: true,
      stime: 1616440740,
      trades: 1,
      type: :bearish,
      volume: 43.0
    },
    %{
      close: 18.9,
      etime: 1616440740,
      high: 18.9,
      low: 18.9,
      open: 18.9,
      processed: true,
      stime: 1616440680,
      trades: 1,
      type: :bullish,
      volume: 12.0
    },
    %{
      close: 12.56,
      etime: 1616440620,
      high: 12.56,
      low: 12.56,
      open: 12.56,
      processed: true,
      stime: 1616440560,
      trades: 1,
      type: :bullish,
      volume: 18.3
    },
    %{
      close: 12.0,
      etime: 1616439660,
      high: 12.0,
      low: 12.0,
      open: 12.0,
      processed: true,
      stime: 1616439600,
      trades: 1,
      type: :bullish,
      volume: 22.0
    }
  ],
  pair: nil,
  timeframe: :minute
}

Link to this section Summary

Types

Single candle generated.

A list of candles.

Available options for create_candles/3

Available timeframes for create_candles/3

Single trade.

A list of trades.

Functions

Function for generating candles from trades and timeframe provided.

Link to this section Types

Specs

candle() :: %{
  :open => number(),
  :high => number(),
  :low => number(),
  :close => number(),
  :volume => number(),
  :trades => number(),
  :stime => number(),
  :etime => number(),
  :type => :bullish | :bearish | nil,
  optional(:processed) => boolean()
}

Single candle generated.

Specs

candles() :: [candle()]

A list of candles.

Specs

opts() :: [
  forward_fill: boolean(),
  validate_trades: boolean(),
  previous_candle: candle(),
  pair: atom() | binary()
]

Available options for create_candles/3

  • :forward_fill - When set true copies the previous candles closing price to the next candle if no trades happend to be in between. Useful when you don't want to get empty time gap between the generated candles.
  • :validate_trades - When set true all trades are being validated before generating the candles to avoid errors and misinformation.
  • :previous_candle - Trades are appended to the previous candle if possible before generating the new candles.
  • :pair - Adds the pair name to the returned outputs metadata.

Specs

timeframe() :: :minute | :hour | :day | :week

Available timeframes for create_candles/3

Specs

trade() :: [price: number(), volume: number(), time: number()]

Single trade.

Specs

trades() :: [trade()]

A list of trades.

Link to this section Functions

Link to this function

create_candles(trades, timeframe, opts \\ [])

Specs

create_candles(trades(), timeframe(), opts() | nil) ::
  {:ok, %{pair: binary() | atom(), timeframe: timeframe(), candles: candles()}}
  | {:error, binary()}

Function for generating candles from trades and timeframe provided.

Parameters:

  • trades - A list containing all the trades. Trades must be chronologically arranged(ASC) by the timestamp field.
  • timeframe - Timeframe for the candles.
  • opts - Option values for the data proccessing.

Returns a tuple containing the metadata for the candles and a list of generated candles.