Class: Quant::Ticks::OHLC
- Includes:
- Quant::TimeMethods
- Defined in:
- lib/quant/ticks/ohlc.rb
Constant Summary
Constants included from Quant::TimeMethods
Quant::TimeMethods::EPOCH_DATE, Quant::TimeMethods::EPOCH_TIME
Instance Attribute Summary collapse
-
#base_volume ⇒ Object
(also: #volume)
readonly
Returns the value of attribute base_volume.
-
#close_price ⇒ Object
(also: #price)
readonly
Returns the value of attribute close_price.
-
#close_timestamp ⇒ Object
(also: #timestamp)
readonly
Returns the value of attribute close_timestamp.
-
#doji ⇒ Object
readonly
Returns the value of attribute doji.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#high_price ⇒ Object
readonly
Returns the value of attribute high_price.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#low_price ⇒ Object
readonly
Returns the value of attribute low_price.
-
#open_price ⇒ Object
readonly
Returns the value of attribute open_price.
-
#open_timestamp ⇒ Object
readonly
Returns the value of attribute open_timestamp.
-
#series ⇒ Object
readonly
Returns the value of attribute series.
-
#target_volume ⇒ Object
readonly
Returns the value of attribute target_volume.
-
#trades ⇒ Object
readonly
Returns the value of attribute trades.
Instance Method Summary collapse
-
#compute_doji ⇒ Boolean
Computes a doji candlestick pattern.
-
#compute_green ⇒ Object
Set the #green? property to true when the close_price is greater than or equal to the open_price.
-
#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.
-
#daily_price_change ⇒ Float
Returns the percent daily price change from open_price to close_price, ranging from 0.0 to 1.0.
-
#daily_price_change_ratio ⇒ Float
Calculates the absolute change from the open_price to the close_price, divided by the average of the open_price and close_price.
- #doji? ⇒ Boolean
- #green? ⇒ Boolean
- #hl2 ⇒ Object
- #hlc3 ⇒ Object
-
#initialize(open_timestamp:, close_timestamp:, open_price:, high_price:, low_price:, close_price:, interval: nil, volume: nil, base_volume: nil, target_volume: nil, trades: nil, green: nil, doji: nil) ⇒ OHLC
constructor
A new instance of OHLC.
- #oc2 ⇒ Object
- #ohlc4 ⇒ Object
- #red? ⇒ Boolean
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, #to_csv, #to_h, #to_json
Constructor Details
#initialize(open_timestamp:, close_timestamp:, open_price:, high_price:, low_price:, close_price:, interval: nil, volume: nil, base_volume: nil, target_volume: nil, trades: nil, green: nil, doji: nil) ⇒ OHLC
Returns a new instance of OHLC.
16 17 18 19 20 21 22 23 24 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 |
# File 'lib/quant/ticks/ohlc.rb', line 16 def initialize( open_timestamp:, close_timestamp:, open_price:, high_price:, low_price:, close_price:, interval: nil, volume: nil, base_volume: nil, target_volume: nil, trades: nil, green: nil, doji: nil ) @open_timestamp = extract_time() @close_timestamp = extract_time() @open_price = open_price.to_f @high_price = high_price.to_f @low_price = low_price.to_f @close_price = close_price.to_f @interval = Interval[interval] @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_volume ⇒ Object (readonly) Also known as: volume
Returns the value of attribute base_volume.
13 14 15 |
# File 'lib/quant/ticks/ohlc.rb', line 13 def base_volume @base_volume end |
#close_price ⇒ Object (readonly) Also known as: price
Returns the value of attribute close_price.
12 13 14 |
# File 'lib/quant/ticks/ohlc.rb', line 12 def close_price @close_price end |
#close_timestamp ⇒ Object (readonly) Also known as: timestamp
Returns the value of attribute close_timestamp.
11 12 13 |
# File 'lib/quant/ticks/ohlc.rb', line 11 def @close_timestamp end |
#doji ⇒ Object (readonly)
Returns the value of attribute doji.
14 15 16 |
# File 'lib/quant/ticks/ohlc.rb', line 14 def doji @doji end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
14 15 16 |
# File 'lib/quant/ticks/ohlc.rb', line 14 def green @green end |
#high_price ⇒ Object (readonly)
Returns the value of attribute high_price.
12 13 14 |
# File 'lib/quant/ticks/ohlc.rb', line 12 def high_price @high_price end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
10 11 12 |
# File 'lib/quant/ticks/ohlc.rb', line 10 def interval @interval end |
#low_price ⇒ Object (readonly)
Returns the value of attribute low_price.
12 13 14 |
# File 'lib/quant/ticks/ohlc.rb', line 12 def low_price @low_price end |
#open_price ⇒ Object (readonly)
Returns the value of attribute open_price.
12 13 14 |
# File 'lib/quant/ticks/ohlc.rb', line 12 def open_price @open_price end |
#open_timestamp ⇒ Object (readonly)
Returns the value of attribute open_timestamp.
11 12 13 |
# File 'lib/quant/ticks/ohlc.rb', line 11 def @open_timestamp end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
10 11 12 |
# File 'lib/quant/ticks/ohlc.rb', line 10 def series @series end |
#target_volume ⇒ Object (readonly)
Returns the value of attribute target_volume.
13 14 15 |
# File 'lib/quant/ticks/ohlc.rb', line 13 def target_volume @target_volume end |
#trades ⇒ Object (readonly)
Returns the value of attribute trades.
13 14 15 |
# File 'lib/quant/ticks/ohlc.rb', line 13 def trades @trades end |
Instance Method Details
#compute_doji ⇒ Boolean
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.
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/quant/ticks/ohlc.rb', line 110 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_green ⇒ Object
Set the #green? property to true when the close_price is greater than or equal to the open_price.
90 91 92 |
# File 'lib/quant/ticks/ohlc.rb', line 90 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.
66 67 68 |
# File 'lib/quant/ticks/ohlc.rb', line 66 def corresponding?(other) [, ] == [other., other.] end |
#daily_price_change ⇒ Float
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.
74 75 76 77 78 |
# File 'lib/quant/ticks/ohlc.rb', line 74 def daily_price_change ((open_price / close_price) - 1.0) rescue ZeroDivisionError 0.0 end |
#daily_price_change_ratio ⇒ Float
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.
85 86 87 |
# File 'lib/quant/ticks/ohlc.rb', line 85 def daily_price_change_ratio @price_change ||= ((open_price - close_price) / oc2).abs end |
#doji? ⇒ Boolean
102 103 104 |
# File 'lib/quant/ticks/ohlc.rb', line 102 def doji? @doji end |
#green? ⇒ Boolean
94 95 96 |
# File 'lib/quant/ticks/ohlc.rb', line 94 def green? @green end |
#hl2 ⇒ Object
58 |
# File 'lib/quant/ticks/ohlc.rb', line 58 def hl2; ((high_price + low_price) / 2.0) end |
#hlc3 ⇒ Object
60 |
# File 'lib/quant/ticks/ohlc.rb', line 60 def hlc3; ((high_price + low_price + close_price) / 3.0) end |
#oc2 ⇒ Object
59 |
# File 'lib/quant/ticks/ohlc.rb', line 59 def oc2; ((open_price + close_price) / 2.0) end |
#ohlc4 ⇒ Object
61 |
# File 'lib/quant/ticks/ohlc.rb', line 61 def ohlc4; ((open_price + high_price + low_price + close_price) / 4.0) end |
#red? ⇒ Boolean
98 99 100 |
# File 'lib/quant/ticks/ohlc.rb', line 98 def red? !green? end |