anchor.diff:    close
   }).
   
  --define(GRANULATION, 30000).
  +-define(DEFAULT_PERIOD, 30000).
   
   candle(Event, Candle) when not is_record(Event, md) andalso Event =/= eof ->
     {[Event], Candle};
  
positive.code: % -export([average/2]).
  
  -record(candle, {
    granulation,
    current_segment,
    open,
    high_ask,
  
negative.code: candle(Event, undefined) ->
    candle(Event, []);
  
  candle(#md{timestamp = Timestamp} = MD, Opts) when is_list(Opts) ->
    Period = proplists:get_value(period, Opts, ?DEFAULT_PERIOD),
    Segment = case Period of
      undefined -> undefined;
      Int when is_integer(Int) -> Timestamp div Period
    end,
    {[], open_segment(MD, Segment, #candle{period = Period})};
  
  candle(#md{timestamp = Timestamp} = MD, #candle{period = Period, current_segment = Seg} = Candle) 
    when Timestamp div Period > Seg ->
    {Events, Candle1} = flush_segment(Candle),
    {Events, open_segment(MD, Timestamp div Period, Candle1)};
  
  candle(eof, #candle{} = Candle) ->
    flush_segment(Candle);
  