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
# File 'lib/finexclub/manager.rb', line 10

def initialize(core)
  @core = core
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

#add_signal(indicator, params, time) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/finexclub/manager.rb', line 57

def add_signal(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_create(signal.symbol, signal.timeframe, time)

  c = if chart._id
    {"_id" => chart._id}
  else
    chart.to_doc
  end

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

#collectionObject

Raises:



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

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

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



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

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

#find_latest(symbol, tf) ⇒ Object



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

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



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

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, -1]])
end

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



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

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, -1]]).first
end

#find_one_or_create(symbol, tf, time) ⇒ Object



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

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

  id = collection.insert(Chart.build_new(core, symbol, tf, time).to_doc)
  find({"_id" => id}).first
end

#last_updated(tf) ⇒ Object



47
48
49
50
# File 'lib/finexclub/manager.rb', line 47

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



52
53
54
55
# File 'lib/finexclub/manager.rb', line 52

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