Class: RTCBX::Candles::Candle

Inherits:
Object
  • Object
show all
Defined in:
lib/rtcbx/candles/candle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(epoch, matches) ⇒ Candle

Create a new Candle from an epoch, and all the messages sent during the interval of the candle



10
11
12
13
14
15
16
17
# File 'lib/rtcbx/candles/candle.rb', line 10

def initialize(epoch, matches)
  @time = Time.at(epoch)
  @low = matches.map {|message| BigDecimal.new(message.fetch('price'))}.min
  @high = matches.map {|message| BigDecimal.new(message.fetch('price'))}.max
  @open = BigDecimal.new(matches.first.fetch('price'))
  @close = BigDecimal.new(matches.last.fetch('price'))
  @volume = matches.reduce(BigDecimal(0)) {|sum, message| sum + BigDecimal.new(message.fetch('size'))}
end

Instance Attribute Details

#closeObject (readonly)

Candle values, this is standard



6
7
8
# File 'lib/rtcbx/candles/candle.rb', line 6

def close
  @close
end

#highObject (readonly)

Candle values, this is standard



6
7
8
# File 'lib/rtcbx/candles/candle.rb', line 6

def high
  @high
end

#lowObject (readonly)

Candle values, this is standard



6
7
8
# File 'lib/rtcbx/candles/candle.rb', line 6

def low
  @low
end

#openObject (readonly)

Candle values, this is standard



6
7
8
# File 'lib/rtcbx/candles/candle.rb', line 6

def open
  @open
end

#timeObject (readonly)

Candle values, this is standard



6
7
8
# File 'lib/rtcbx/candles/candle.rb', line 6

def time
  @time
end

#volumeObject (readonly)

Candle values, this is standard



6
7
8
# File 'lib/rtcbx/candles/candle.rb', line 6

def volume
  @volume
end

Instance Method Details

#to_hObject

Return a Hash representation of the Candle



20
21
22
23
24
25
26
27
28
29
# File 'lib/rtcbx/candles/candle.rb', line 20

def to_h
  {
    start:  Time.at(@time),
    low:    @low.to_s("F"),
    high:   @high.to_s("F"),
    open:   @open.to_s("F"),
    close:  @close.to_s("F"),
    volume: @volume.to_s("F"),
  }
end