Class: Gekko::Tape

Inherits:
Array
  • Object
show all
Defined in:
lib/gekko/tape.rb

Overview

Records the trading engine messages sequentially

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Tape

Returns a new instance of Tape.



10
11
12
13
14
15
16
# File 'lib/gekko/tape.rb', line 10

def initialize(logger = nil)
  @logger           = logger
  @cursor           = 0
  @cursor_24h       = 0
  @volume_24h       = 0
  @quote_volume_24h = 0
end

Instance Attribute Details

#last_trade_priceObject

Returns the value of attribute last_trade_price.



8
9
10
# File 'lib/gekko/tape.rb', line 8

def last_trade_price
  @last_trade_price
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/gekko/tape.rb', line 8

def logger
  @logger
end

Instance Method Details

#<<(message) ⇒ Object

Prints a message on the tape

Parameters:

  • message (Hash)

    The message to record



23
24
25
26
27
28
29
30
31
32
# File 'lib/gekko/tape.rb', line 23

def <<(message)
  message[:sequence] = length
  logger && logger.info(message)

  super(message)

  if message[:type] == :execution
    update_ticker(message)
  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



161
162
163
164
165
166
167
168
# File 'lib/gekko/tape.rb', line 161

def fall_out_of_24h_window(execution)
  @volume_24h       -= execution[:base_size]
  @quote_volume_24h -= execution[:quote_size]

  if [@high_24h, @low_24h].include?(execution[:price])
    recalc_high_low_24h!
  end
end

#high_24hFixnum

Returns the highest trade price that occurred during the last 24h

Returns:

  • (Fixnum)

    The last 24h high



62
63
64
65
# File 'lib/gekko/tape.rb', line 62

def high_24h
  move_24h_cursor!
  @high_24h
end

#low_24hFixnum

Returns the lowest trade price that occurred during the last 24h

Returns:

  • (Fixnum)

    The last 24h low



72
73
74
75
# File 'lib/gekko/tape.rb', line 72

def low_24h
  move_24h_cursor!
  @low_24h
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



147
148
149
150
151
152
153
154
155
# File 'lib/gekko/tape.rb', line 147

def move_24h_cursor!
  while(self[@cursor_24h] && ((self[@cursor_24h][:type] != :execution) || (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

#nextHash

Returns the next unread element from the tape

Returns:

  • (Hash)

    The next unread element



39
40
41
42
43
44
45
# File 'lib/gekko/tape.rb', line 39

def next
  if @cursor < length
    n = self[@cursor]
    @cursor += 1
    n
  end
end

#quote_volume_24hFixnum

Returns the traded amount of quote currency in the last 24h

Returns:

  • (Fixnum)

    The last 24h quote currency volume



103
104
105
106
# File 'lib/gekko/tape.rb', line 103

def quote_volume_24h
  move_24h_cursor!
  @quote_volume_24h
end

#recalc_high_low_24h!Object

Recalculates the previous 24h high and low



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gekko/tape.rb', line 80

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
    @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

    tmp_cursor -= 1
    evt = (tmp_cursor >= 0) && self[tmp_cursor]
  end
end

#time_24h_agoFloat

Returns the float timestamp of 24h ago

Returns:

  • (Float)

    Yesterday’s cut-off timestamp



138
139
140
# File 'lib/gekko/tape.rb', line 138

def time_24h_ago
  Time.now.to_f - 24*3600
end

#update_ticker(execution) ⇒ Object

Updates the ticker after an execution has been recorded



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gekko/tape.rb', line 111

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

#volume_24hFixnum

Returns the traded volume for the last 24h

Returns:

  • (Fixnum)

    The last 24h volume



52
53
54
55
# File 'lib/gekko/tape.rb', line 52

def volume_24h
  move_24h_cursor!
  @volume_24h
end