Class: Quant::Security

Inherits:
Object
  • Object
show all
Defined in:
lib/quant/security.rb

Overview

A Security is a representation of a financial instrument such as a stock, option, future, or currency. It is used to represent the instrument that is being traded, analyzed, or managed.

Examples:

security = Quant::Security.new(symbol: "AAPL", name: "Apple Inc.", security_class: :stock, exchange: "NASDAQ")
security.symbol # => "AAPL"
security.name # => "Apple Inc."
security.stock? # => true
security.option? # => false
security.future? # => false
security.currency? # => false
security.exchange # => "NASDAQ"
security.to_h # => { "s" => "AAPL", "n" => "Apple Inc.", "sc" => "stock", "x" => "NASDAQ" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol:, name: nil, id: nil, active: true, tradeable: true, exchange: nil, source: nil, security_class: nil, created_at: Quant.current_time, updated_at: Quant.current_time, meta: {}) ⇒ Security

Returns a new instance of Security.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/quant/security.rb', line 21

def initialize(
  symbol:,
  name: nil,
  id: nil,
  active: true,
  tradeable: true,
  exchange: nil,
  source: nil,
  security_class: nil,
  created_at: Quant.current_time,
  updated_at: Quant.current_time,
  meta: {}
)
  raise ArgumentError, "symbol is required" unless symbol

  @symbol = symbol.to_s.upcase
  @name = name
  @id = id
  @tradeable = tradeable
  @active = active
  @exchange = exchange
  @source = source
  @security_class = SecurityClass.new(security_class)
  @created_at = created_at
  @updated_at = updated_at
  @meta = meta
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



19
20
21
# File 'lib/quant/security.rb', line 19

def created_at
  @created_at
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



19
20
21
# File 'lib/quant/security.rb', line 19

def exchange
  @exchange
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/quant/security.rb', line 19

def id
  @id
end

#metaObject (readonly)

Returns the value of attribute meta.



19
20
21
# File 'lib/quant/security.rb', line 19

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/quant/security.rb', line 19

def name
  @name
end

#security_classObject (readonly)

Returns the value of attribute security_class.



19
20
21
# File 'lib/quant/security.rb', line 19

def security_class
  @security_class
end

#sourceObject (readonly)

Returns the value of attribute source.



19
20
21
# File 'lib/quant/security.rb', line 19

def source
  @source
end

#symbolObject (readonly)

Returns the value of attribute symbol.



19
20
21
# File 'lib/quant/security.rb', line 19

def symbol
  @symbol
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



19
20
21
# File 'lib/quant/security.rb', line 19

def updated_at
  @updated_at
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/quant/security.rb', line 49

def active?
  !!@active
end

#to_h(full: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/quant/security.rb', line 63

def to_h(full: false)
  return { "s" => symbol } unless full

  { "s" => symbol,
    "n" => name,
    "id" => id,
    "t" => tradeable?,
    "a" => active?,
    "x" => exchange,
    "sc" => security_class.to_s,
    "src" => source.to_s }
end

#to_json(*args, full: false) ⇒ Object



76
77
78
# File 'lib/quant/security.rb', line 76

def to_json(*args, full: false)
  Oj.dump(to_h(full: full), *args)
end

#tradeable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/quant/security.rb', line 53

def tradeable?
  !!@tradeable
end