Class: Gekko::Tape
- Inherits:
-
Array
- Object
- Array
- Gekko::Tape
- Includes:
- Serialization
- Defined in:
- lib/gekko/tape.rb
Overview
Records the trading engine messages sequentially
Constant Summary collapse
- SECONDS_IN_24H =
The number of seconds in 24h
60 * 60 * 24
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#high_24h ⇒ Object
readonly
Returns the value of attribute high_24h.
-
#last_trade_price ⇒ Object
Returns the value of attribute last_trade_price.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#low_24h ⇒ Object
readonly
Returns the value of attribute low_24h.
-
#open_24h ⇒ Object
readonly
Returns the value of attribute open_24h.
-
#var_24h ⇒ Object
readonly
Returns the value of attribute var_24h.
-
#volume_24h ⇒ Object
readonly
Returns the value of attribute volume_24h.
Class Method Summary collapse
-
.from_hash(hsh) ⇒ Object
Loads a
Tape
object from a hash.
Instance Method Summary collapse
-
#<<(message) ⇒ Object
Prints a message on the tape.
-
#fall_out_of_24h_window(execution) ⇒ Object
Updates the low, high, and volumes when an execution falls out of the rolling previous 24h window.
-
#initialize(opts = {}) ⇒ Tape
constructor
A new instance of Tape.
-
#move_24h_cursor! ⇒ Object
Moves the cursor pointing to the first trade that happened during the last 24h.
-
#next ⇒ Hash
Returns the next unread element from the tape.
-
#quote_volume_24h ⇒ Fixnum
Returns the traded amount of quote currency in the last 24h.
-
#recalc_high_low_24h! ⇒ Object
Recalculates the previous 24h high and low.
-
#time_24h_ago ⇒ Float
Returns the float timestamp of 24h ago.
-
#to_hash ⇒ Hash
Returns this
Tape
object as aHash
for the purpose of serialization. -
#update_ticker(execution) ⇒ Object
Updates the ticker after an execution has been recorded.
Methods included from Serialization
Constructor Details
#initialize(opts = {}) ⇒ Tape
Returns a new instance of Tape.
16 17 18 19 20 21 22 23 |
# File 'lib/gekko/tape.rb', line 16 def initialize(opts = {}) @logger = opts[:logger] @cursor = 0 @cursor_24h = 0 @volume_24h = 0 @quote_volume_24h = 0 end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
14 15 16 |
# File 'lib/gekko/tape.rb', line 14 def cursor @cursor end |
#high_24h ⇒ Object (readonly)
Returns the value of attribute high_24h.
14 15 16 |
# File 'lib/gekko/tape.rb', line 14 def high_24h @high_24h end |
#last_trade_price ⇒ Object
Returns the value of attribute last_trade_price.
13 14 15 |
# File 'lib/gekko/tape.rb', line 13 def last_trade_price @last_trade_price end |
#logger ⇒ Object
Returns the value of attribute logger.
13 14 15 |
# File 'lib/gekko/tape.rb', line 13 def logger @logger end |
#low_24h ⇒ Object (readonly)
Returns the value of attribute low_24h.
14 15 16 |
# File 'lib/gekko/tape.rb', line 14 def low_24h @low_24h end |
#open_24h ⇒ Object (readonly)
Returns the value of attribute open_24h.
14 15 16 |
# File 'lib/gekko/tape.rb', line 14 def open_24h @open_24h end |
#var_24h ⇒ Object (readonly)
Returns the value of attribute var_24h.
14 15 16 |
# File 'lib/gekko/tape.rb', line 14 def var_24h @var_24h end |
#volume_24h ⇒ Object (readonly)
Returns the value of attribute volume_24h.
14 15 16 |
# File 'lib/gekko/tape.rb', line 14 def volume_24h @volume_24h end |
Class Method Details
.from_hash(hsh) ⇒ Object
Loads a Tape
object from a hash
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/gekko/tape.rb', line 179 def self.from_hash(hsh) tape = Tape.new tape.instance_variable_set(:@cursor, hsh[:cursor]) hsh[:events].each do |evt| e = symbolize_keys(evt) e[:type] = e[:type].to_sym tape << e end tape end |
Instance Method Details
#<<(message) ⇒ Object
Prints a message on the tape
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gekko/tape.rb', line 30 def <<() .merge!({ sequence: length, time: Time.now.to_f }) logger && logger.info() super() if [:type] == :execution update_ticker() end end |
#fall_out_of_24h_window(execution) ⇒ Object
Updates the low, high, and volumes when an execution falls out of the rolling previous 24h window
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/gekko/tape.rb', line 143 def fall_out_of_24h_window(execution) @volume_24h -= execution[:base_size] @quote_volume_24h -= execution[:quote_size] @open_24h = execution[:price] @var_24h = @last_trade_price && ((@last_trade_price - @open_24h) / @open_24h.to_f) if [@high_24h, @low_24h].include?(execution[:price]) recalc_high_low_24h! end end |
#move_24h_cursor! ⇒ Object
Moves the cursor pointing to the first trade that happened during the last 24h. Every execution getting out of the 24h rolling window is passed to Tape#fall_out_of_24h_window
129 130 131 132 133 134 135 136 137 |
# File 'lib/gekko/tape.rb', line 129 def move_24h_cursor! while(self[@cursor_24h] && (self[@cursor_24h][:time] < time_24h_ago)) if self[@cursor_24h][:type] == :execution fall_out_of_24h_window(self[@cursor_24h]) end @cursor_24h += 1 end end |
#next ⇒ Hash
Returns the next unread element from the tape
50 51 52 53 54 55 56 |
# File 'lib/gekko/tape.rb', line 50 def next if @cursor < length n = self[@cursor] @cursor += 1 n end end |
#quote_volume_24h ⇒ Fixnum
Returns the traded amount of quote currency in the last 24h
86 87 88 |
# File 'lib/gekko/tape.rb', line 86 def quote_volume_24h @quote_volume_24h end |
#recalc_high_low_24h! ⇒ Object
Recalculates the previous 24h high and low
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gekko/tape.rb', line 61 def recalc_high_low_24h! @high_24h = nil @low_24h = nil # Work backwards from current position until the cursor points to an event # that's older than 24h tmp_cursor = (length - 1) evt = self[tmp_cursor] while (evt && (evt[:time] >= time_24h_ago)) do if evt[:type] == :execution @high_24h = ((@high_24h.nil? || (evt[:price] > @high_24h)) && evt[:price]) || @high_24h @low_24h = ((@low_24h.nil? || (evt[:price] < @low_24h)) && evt[:price]) || @low_24h end tmp_cursor -= 1 evt = (tmp_cursor >= 0) && self[tmp_cursor] end end |
#time_24h_ago ⇒ Float
Returns the float timestamp of 24h ago
120 121 122 |
# File 'lib/gekko/tape.rb', line 120 def time_24h_ago Time.now.to_f - SECONDS_IN_24H end |
#to_hash ⇒ Hash
Returns this Tape
object as a Hash
for the purpose of serialization
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/gekko/tape.rb', line 159 def to_hash { cursor: @cursor, cursor_24h: @cursor_24h, volume_24h: @volume_24h, high_24h: @high_24h, low_24h: @low_24h, open_24h: @open_24h, var_24h: @var_24h, quote_volume_24h: @quote_volume_24h, last_trade_price: @last_trade_price, events: self } end |
#update_ticker(execution) ⇒ Object
Updates the ticker after an execution has been recorded
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gekko/tape.rb', line 93 def update_ticker(execution) price = execution[:price] # Keep last price up to date @last_trade_price = price # Keep 24h volume up to date @volume_24h += execution[:base_size] @quote_volume_24h += execution[:quote_size] # Record new high/lows if @high_24h.nil? || (@high_24h < price) @high_24h = price end if @low_24h.nil? || (price < @low_24h) @low_24h = price end move_24h_cursor! end |