Class: Quant::Ticks::OHLC

Inherits:
Tick
  • Object
show all
Includes:
Quant::TimeMethods
Defined in:
lib/quant/ticks/ohlc.rb

Overview

A OHLC is a bar or candle for a point in time that has an open, high, low, and close price. It is the most common form of a Tick and is usually used to representa time period such as a minute, hour, day, week, or month.

The OHLC is used to represent the price action of an asset The interval of the OHLC is the time period that the OHLC represents, such has hourly, daily, weekly, etc.

Constant Summary

Constants included from Quant::TimeMethods

Quant::TimeMethods::EPOCH_DATE, Quant::TimeMethods::EPOCH_TIME

Instance Attribute Summary collapse

Attributes inherited from Tick

#indicators

Instance Method Summary collapse

Methods included from Quant::TimeMethods

epoch_date, epoch_time, #extract_time

Methods inherited from Tick

#assign_series, #assign_series!, default_serializer_class, #default_serializer_class, from, from_json, #interval, #series?, #to_csv, #to_h, #to_json

Constructor Details

#initialize(open_timestamp:, close_timestamp:, open_price:, high_price:, low_price:, close_price:, volume: nil, base_volume: nil, target_volume: nil, trades: nil, green: nil, doji: nil) ⇒ OHLC

Returns a new instance of OHLC.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/quant/ticks/ohlc.rb', line 25

def initialize(
  open_timestamp:,
  close_timestamp:,

  open_price:,
  high_price:,
  low_price:,
  close_price:,

  volume: nil,
  base_volume: nil,
  target_volume: nil,

  trades: nil,
  green: nil,
  doji: nil
)
  @open_timestamp = extract_time(open_timestamp)
  @close_timestamp = extract_time(close_timestamp)

  @open_price = open_price.to_f
  @high_price = high_price.to_f
  @low_price = low_price.to_f
  @close_price = close_price.to_f

  @base_volume = (volume || base_volume).to_i
  @target_volume = (target_volume || @base_volume).to_i
  @trades = trades.to_i

  @green = green.nil? ? compute_green : green
  @doji = doji.nil? ? compute_doji : doji
  super()
end

Instance Attribute Details

#base_volumeObject (readonly) Also known as: volume

Returns the value of attribute base_volume.



22
23
24
# File 'lib/quant/ticks/ohlc.rb', line 22

def base_volume
  @base_volume
end

#close_priceObject (readonly) Also known as: price

Returns the value of attribute close_price.



21
22
23
# File 'lib/quant/ticks/ohlc.rb', line 21

def close_price
  @close_price
end

#close_timestampObject (readonly) Also known as: timestamp

Returns the value of attribute close_timestamp.



20
21
22
# File 'lib/quant/ticks/ohlc.rb', line 20

def close_timestamp
  @close_timestamp
end

#dojiObject (readonly)

Returns the value of attribute doji.



23
24
25
# File 'lib/quant/ticks/ohlc.rb', line 23

def doji
  @doji
end

#greenObject (readonly)

Returns the value of attribute green.



23
24
25
# File 'lib/quant/ticks/ohlc.rb', line 23

def green
  @green
end

#high_priceObject (readonly)

Returns the value of attribute high_price.



21
22
23
# File 'lib/quant/ticks/ohlc.rb', line 21

def high_price
  @high_price
end

#low_priceObject (readonly)

Returns the value of attribute low_price.



21
22
23
# File 'lib/quant/ticks/ohlc.rb', line 21

def low_price
  @low_price
end

#open_priceObject (readonly)

Returns the value of attribute open_price.



21
22
23
# File 'lib/quant/ticks/ohlc.rb', line 21

def open_price
  @open_price
end

#open_timestampObject (readonly)

Returns the value of attribute open_timestamp.



20
21
22
# File 'lib/quant/ticks/ohlc.rb', line 20

def open_timestamp
  @open_timestamp
end

#seriesObject (readonly)

Returns the value of attribute series.



19
20
21
# File 'lib/quant/ticks/ohlc.rb', line 19

def series
  @series
end

#target_volumeObject (readonly)

Returns the value of attribute target_volume.



22
23
24
# File 'lib/quant/ticks/ohlc.rb', line 22

def target_volume
  @target_volume
end

#tradesObject (readonly)

Returns the value of attribute trades.



22
23
24
# File 'lib/quant/ticks/ohlc.rb', line 22

def trades
  @trades
end

Instance Method Details

#==(other) ⇒ Object

Two OHLC ticks are equal if their interval, close_timestamp, and close_price are equal.



76
77
78
# File 'lib/quant/ticks/ohlc.rb', line 76

def ==(other)
  [interval, close_timestamp, close_price] == [other.interval, other.close_timestamp, other.close_price]
end

#compute_dojiBoolean

Computes a doji candlestick pattern. A doji is a candlestick pattern that occurs when the open and close are the same or very close to the same. The high and low are also very close to the same. The doji pattern is a sign of indecision in the market. It is a sign that the market is not sure which way to go.

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/quant/ticks/ohlc.rb', line 121

def compute_doji
  body_bottom, body_top = [open_price, close_price].sort

  body_length = body_top - body_bottom
  head_length = high_price - [open_price, close_price].max
  tail_length = [open_price, close_price].max - low_price

  body_ratio = 100.0 * (1 - (body_bottom / body_top))
  head_ratio = head_length / body_length
  tail_ratio = tail_length / body_length

  body_ratio < 0.025 && head_ratio > 1.0 && tail_ratio > 1.0
end

#compute_greenObject

Set the #green? property to true when the close_price is greater than or equal to the open_price.



101
102
103
# File 'lib/quant/ticks/ohlc.rb', line 101

def compute_green
  close_price >= open_price
end

#corresponding?(other) ⇒ Boolean

The corresponding? method helps determine that the other tick’s timestamp is the same as this tick’s timestamp, which is useful when aligning ticks between two separate series where one starts or ends at a different time, or when there may be gaps in the data between the two series.

Returns:

  • (Boolean)


71
72
73
# File 'lib/quant/ticks/ohlc.rb', line 71

def corresponding?(other)
  [open_timestamp, close_timestamp] == [other.open_timestamp, other.close_timestamp]
end

#daily_price_changeFloat

Returns the percent daily price change from open_price to close_price, ranging from 0.0 to 1.0. A positive value means the price increased, and a negative value means the price decreased. A value of 0.0 means no change.

Returns:

  • (Float)


84
85
86
87
88
89
# File 'lib/quant/ticks/ohlc.rb', line 84

def daily_price_change
  return open_price.zero? ? 0.0 : -1.0 if close_price.zero?
  return 0.0 if open_price == close_price

  (open_price / close_price) - 1.0
end

#daily_price_change_ratioFloat

Calculates the absolute change from the open_price to the close_price, divided by the average of the open_price and close_price. This method will give a value between 0 and 2, where 0 means no change, 1 means the price doubled, and 2 means the price went to zero. This method is useful for comparing the volatility of different assets.

Returns:

  • (Float)


96
97
98
# File 'lib/quant/ticks/ohlc.rb', line 96

def daily_price_change_ratio
  (open_price - close_price).abs / oc2
end

#doji?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/quant/ticks/ohlc.rb', line 113

def doji?
  @doji
end

#green?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/quant/ticks/ohlc.rb', line 105

def green?
  @green
end

#hl2Object



63
# File 'lib/quant/ticks/ohlc.rb', line 63

def hl2; ((high_price + low_price) / 2.0) end

#hlc3Object



65
# File 'lib/quant/ticks/ohlc.rb', line 65

def hlc3; ((high_price + low_price + close_price) / 3.0) end

#inspectObject



135
136
137
# File 'lib/quant/ticks/ohlc.rb', line 135

def inspect
  "#<#{self.class.name} ct=#{close_timestamp.iso8601} o=#{open_price} h=#{high_price} l=#{low_price} c=#{close_price} v=#{volume}>"
end

#oc2Object



64
# File 'lib/quant/ticks/ohlc.rb', line 64

def oc2; ((open_price + close_price) / 2.0) end

#ohlc4Object



66
# File 'lib/quant/ticks/ohlc.rb', line 66

def ohlc4; ((open_price + high_price + low_price + close_price) / 4.0) end

#red?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/quant/ticks/ohlc.rb', line 109

def red?
  !green?
end