Class: Finexclub::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/finexclub/manager.rb

Defined Under Namespace

Classes: Cursor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core) ⇒ Manager

Returns a new instance of Manager.



10
11
12
13
# File 'lib/finexclub/manager.rb', line 10

def initialize(core)
  @core = core
  @db = nil
end

Instance Attribute Details

#coreObject (readonly)

Returns the value of attribute core.



7
8
9
# File 'lib/finexclub/manager.rb', line 7

def core
  @core
end

#dbObject

Returns the value of attribute db.



8
9
10
# File 'lib/finexclub/manager.rb', line 8

def db
  @db
end

Instance Method Details

#collectionObject

Raises:



15
16
17
18
# File 'lib/finexclub/manager.rb', line 15

def collection
  raise ConfigurationError.new("No db is configured") if db.nil?
  db.collection('charts')
end

#find(query, opts = {}) ⇒ Object



20
21
22
# File 'lib/finexclub/manager.rb', line 20

def find(query, opts = {})
  Cursor.new(core, Finexclub::Chart, collection.find(query, opts))
end

#find_latest(symbol, tf) ⇒ Object



36
37
38
# File 'lib/finexclub/manager.rb', line 36

def find_latest(symbol, tf)
  find({:symbol => symbol, :timeframe => tf}, :sort => [[:expires, :desc]], :limit => 1).first
end

#find_many(symbol_or_array, tf, time = nil) ⇒ Object



30
31
32
33
34
# File 'lib/finexclub/manager.rb', line 30

def find_many(symbol_or_array, tf, time = nil)
  time ||= Time.now.utc
  time = time.respond_to?(:to_i) ? time.to_i : time
  find({:symbol => {:$in => symbol_or_array}, :timeframe => tf, :starts => {:$lte => time}, :expires => {:$gt => time}}, :limit => 1, :sort => [[:expires, :desc]])
end

#find_one(symbol, tf, time = nil) ⇒ Object



24
25
26
27
28
# File 'lib/finexclub/manager.rb', line 24

def find_one(symbol, tf, time = nil)
  time ||= Time.now.utc
  time = time.respond_to?(:to_i) ? time.to_i : time
  find({:symbol => symbol, :timeframe => tf, :starts => {:$lte => time}, :expires => {:$gt => time}}, :limit => 1, :sort => [[:expires, :desc]]).first
end

#find_one_or_duplicate(symbol, tf, time) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/finexclub/manager.rb', line 40

def find_one_or_duplicate(symbol, tf, time)
  chart = find_one(symbol, tf, time)
  return chart if chart

  latest = find_latest(symbol, tf) 
  if latest
    chart = latest
    chart.build(Chart.timeframe_range(tf, time))
  else
    chart = Chart.build_new(core, symbol, tf, time)
  end

  id = collection.insert( chart.to_doc )
  find({"_id" => id}).first
end

#last_updated(tf) ⇒ Object



56
57
58
59
# File 'lib/finexclub/manager.rb', line 56

def last_updated(tf)
  doc = collection.find({:timeframe => tf}, :fields => {:updated => 1}, :sort => [[:updated, :desc]], :limit => 1).first
  doc.nil?? nil : doc["updated"]
end

#last_updated_at(tf) ⇒ Object



61
62
63
64
# File 'lib/finexclub/manager.rb', line 61

def last_updated_at(tf)
  ts = last_updated(tf)
  ts.nil? ? nil : Time.at(ts).utc
end

#store(indicator, params, time) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/finexclub/manager.rb', line 66

def store(indicator, params, time)
  time = time.respond_to?(:to_i) ? time.to_i : time

  signal = Signal.build(core, indicator, params.merge(:updated => time))
  chart = find_one_or_duplicate(signal.symbol, signal.timeframe, time)

  collection.update({"_id" => chart._id}, {"$set" => {:updated => signal.updated, "indicators.#{indicator}" => signal.to_doc}}, :upsert => true)
end