Class: Quant::Asset

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

Overview

A Asset 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.

Not all data sources have a rich set of attributes for their assets or securities. The Asset is designed to be flexible and to support a wide variety of data sources and use-cases. The most common use-cases are supported while allowing for additional attributes to be added via the meta attribute, which is tyipically just a Hash, but can be any object that can hold useful information about the asset such as currency formatting, precision, etc.

Examples:

asset = Quant::Asset.new(symbol: "AAPL", name: "Apple Inc.", asset_class: :stock, exchange: "NASDAQ")
asset.symbol # => "AAPL"
asset.name # => "Apple Inc."
asset.stock? # => true
asset.option? # => false
asset.future? # => false
asset.currency? # => false
asset.exchange # => "NASDAQ"

# Can serialize two ways:
asset.to_h # => { "s" => "AAPL" }
asset.to_h(full: true) # => { "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, asset_class: nil, created_at: Quant.current_time, updated_at: Quant.current_time, meta: {}) ⇒ Asset

Returns a new instance of Asset.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/quant/asset.rb', line 27

def initialize(
  symbol:,
  name: nil,
  id: nil,
  active: true,
  tradeable: true,
  exchange: nil,
  source: nil,
  asset_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
  @asset_class = AssetClass.new(asset_class)
  @created_at = created_at
  @updated_at = updated_at
  @meta = meta
end

Instance Attribute Details

#asset_classObject (readonly)

Returns the value of attribute asset_class.



25
26
27
# File 'lib/quant/asset.rb', line 25

def asset_class
  @asset_class
end

#created_atObject (readonly)

Returns the value of attribute created_at.



25
26
27
# File 'lib/quant/asset.rb', line 25

def created_at
  @created_at
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



25
26
27
# File 'lib/quant/asset.rb', line 25

def exchange
  @exchange
end

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'lib/quant/asset.rb', line 25

def id
  @id
end

#metaObject (readonly)

Returns the value of attribute meta.



25
26
27
# File 'lib/quant/asset.rb', line 25

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/quant/asset.rb', line 25

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



25
26
27
# File 'lib/quant/asset.rb', line 25

def source
  @source
end

#symbolObject (readonly)

Returns the value of attribute symbol.



25
26
27
# File 'lib/quant/asset.rb', line 25

def symbol
  @symbol
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



25
26
27
# File 'lib/quant/asset.rb', line 25

def updated_at
  @updated_at
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/quant/asset.rb', line 55

def active?
  !!@active
end

#to_h(full: false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/quant/asset.rb', line 69

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

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

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



82
83
84
# File 'lib/quant/asset.rb', line 82

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

#tradeable?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/quant/asset.rb', line 59

def tradeable?
  !!@tradeable
end