Class: RTCBX::Candles

Inherits:
RTCBX
  • Object
show all
Defined in:
lib/rtcbx/candles.rb,
lib/rtcbx/candles/candle.rb

Defined Under Namespace

Classes: Candle

Constant Summary

Constants inherited from RTCBX

PING_INTERVAL, VERSION

Instance Attribute Summary collapse

Attributes inherited from RTCBX

#api_key, #client, #last_pong, #message_callbacks, #product_id, #queue, #start, #websocket, #websocket_thread

Instance Method Summary collapse

Methods inherited from RTCBX

#reset!, #stop!

Constructor Details

#initialize(options = {}, &block) ⇒ Candles

Create a new Candles object to start and track candles Pass a block to run a block whenever a candle is created.



44
45
46
47
# File 'lib/rtcbx/candles.rb', line 44

def initialize(options = {}, &block)
  super(options, &block)
  @buckets_lock = Mutex.new
end

Instance Attribute Details

#bucket_threadObject (readonly)

This thread monitors the websocket object and puts each match object into the proper bucket. This thread maintains the buckets object.



16
17
18
# File 'lib/rtcbx/candles.rb', line 16

def bucket_thread
  @bucket_thread
end

#bucketsObject (readonly)

A hash of buckets Each key is an epoch which stores every match message for that minute (The epoch plus 60 seconds) Each minute interval is a bucket, which is used to calculate that minute’s Candle



12
13
14
# File 'lib/rtcbx/candles.rb', line 12

def buckets
  @buckets
end

#buckets_lockObject (readonly)

Mutex to allow our two threads to produce and consume buckets



39
40
41
# File 'lib/rtcbx/candles.rb', line 39

def buckets_lock
  @buckets_lock
end

#candle_threadObject (readonly)

The candle_thread consumes the buckets created by the bucket_thread in buckets and turns them into Candle objects. These are then appended to the candles array. This functionality could be improved. Ideally you’re consuming this array into a database to keep history in realtime.



22
23
24
# File 'lib/rtcbx/candles.rb', line 22

def candle_thread
  @candle_thread
end

#candlesObject (readonly)

An array of generated candles. You should process these by putting them into a database and removing them from the array. If you want to help me abstract this to a pluggable database system, open an issue.



30
31
32
# File 'lib/rtcbx/candles.rb', line 30

def candles
  @candles
end

#current_bucketObject (readonly)

The epoch representing the current bucket



25
26
27
# File 'lib/rtcbx/candles.rb', line 25

def current_bucket
  @current_bucket
end

#first_bucketObject (readonly)

The epoch of the first bucket



36
37
38
# File 'lib/rtcbx/candles.rb', line 36

def first_bucket
  @first_bucket
end

#initial_timeObject (readonly)

The first full minute that we can collect for. (Time object)



33
34
35
# File 'lib/rtcbx/candles.rb', line 33

def initial_time
  @initial_time
end

Instance Method Details

#start!Object

Start tracking candles



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rtcbx/candles.rb', line 50

def start!
  super
  #
  # Calculate the first minute to start relying on just the websocket for
  # data.
  #
  @initial_time = Time.now
  @first_bucket = initial_time.to_i + (60 - initial_time.sec)

  start_bucket_thread
  start_candle_thread
end