Class: Quant::Ticks::OHLC

Inherits:
Value
  • Object
show all
Defined in:
lib/quant/ticks/ohlc.rb

Overview

t: trades g: green j: doji

Constant Summary

Constants included from Quant::TimeMethods

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

Instance Attribute Summary

Attributes inherited from Value

#base_volume, #close_price, #close_timestamp, #doji, #green, #high_price, #interval, #low_price, #open_price, #open_timestamp, #series, #target_volume, #trades

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#==, #assign_series, #assign_series!, #inspect, #to_json

Methods included from Quant::TimeMethods

epoch_date, epoch_time, #extract_time

Constructor Details

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

Returns a new instance of OHLC.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/quant/ticks/ohlc.rb', line 46

def initialize(open_timestamp:,
               close_timestamp:,
               interval: nil,

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

               base_volume: 0.0,
               target_volume: 0.0,

               trades: 0,
               green: false,
               doji: nil)

  super(price: close_price, timestamp: close_timestamp, interval:, trades:)
  @open_timestamp = extract_time(open_timestamp)
  @open_price = open_price.to_f
  @high_price = high_price.to_f
  @low_price = low_price.to_f

  @base_volume = base_volume.to_i
  @target_volume = target_volume.to_i

  @green = green.nil? ? compute_green : green
  @doji = doji.nil? ? compute_doji : doji
end

Class Method Details

.from(hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quant/ticks/ohlc.rb', line 23

def self.from(hash)
  new \
    open_timestamp: hash["ot"],
    close_timestamp: hash["ct"],
    interval: hash["iv"],

    open_price: hash["o"],
    high_price: hash["h"],
    low_price: hash["l"],
    close_price: hash["c"],

    base_volume: hash["bv"],
    target_volume: hash["tv"],

    trades: hash["t"],
    green: hash["g"],
    doji: hash["j"]
end

.from_json(json) ⇒ Object



42
43
44
# File 'lib/quant/ticks/ohlc.rb', line 42

def self.from_json(json)
  from Oj.load(json)
end

Instance Method Details

#as_price(value) ⇒ Object



107
108
109
# File 'lib/quant/ticks/ohlc.rb', line 107

def as_price(value)
  series.nil? ? value : series.as_price(value)
end

#compute_dojiObject



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/quant/ticks/ohlc.rb', line 137

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



117
118
119
# File 'lib/quant/ticks/ohlc.rb', line 117

def compute_green
  close_price >= open_price
end

#corresponding?(other) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/quant/ticks/ohlc.rb', line 80

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

#deltaObject

percent change from open to close



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

def delta
  ((open_price / close_price) - 1.0) * 100
end

#doji?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/quant/ticks/ohlc.rb', line 129

def doji?
  @doji
end

#green?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/quant/ticks/ohlc.rb', line 121

def green?
  close_price > open_price
end

#hl2Object



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

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

#hlc3Object



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

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

#oc2Object



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

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

#ohlc4Object



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

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

#price_changeObject



133
134
135
# File 'lib/quant/ticks/ohlc.rb', line 133

def price_change
  @price_change ||= ((open_price - close_price) / oc2).abs
end

#red?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/quant/ticks/ohlc.rb', line 125

def red?
  !green?
end

#to_hObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/quant/ticks/ohlc.rb', line 89

def to_h
  { "ot" => open_timestamp,
    "ct" => close_timestamp,
    "iv" => interval.to_s,

    "o" => open_price,
    "h" => high_price,
    "l" => low_price,
    "c" => close_price,

    "bv" => base_volume,
    "tv" => target_volume,

    "t" => trades,
    "g" => green,
    "j" => doji }
end

#to_sObject



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

def to_s
  ots = interval.daily? ? open_timestamp.strftime('%Y-%m-%d') : open_timestamp.strftime('%Y-%m-%d %H:%M:%S')
  cts = interval.daily? ? close_timestamp.strftime('%Y-%m-%d') : close_timestamp.strftime('%Y-%m-%d %H:%M:%S')
  "#{ots}: o: #{as_price(open_price)}, h: #{as_price(high_price)}, l: #{as_price(low_price)}, c: #{as_price(close_price)} :#{cts}"
end