Class: SQA::Portfolio::Position

Inherits:
Struct
  • Object
show all
Defined in:
lib/sqa/portfolio.rb

Overview

Represents a single position in the portfolio

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#avg_costObject

Returns the value of attribute avg_cost

Returns:

  • (Object)

    the current value of avg_cost



11
12
13
# File 'lib/sqa/portfolio.rb', line 11

def avg_cost
  @avg_cost
end

#sharesObject

Returns the value of attribute shares

Returns:

  • (Object)

    the current value of shares



11
12
13
# File 'lib/sqa/portfolio.rb', line 11

def shares
  @shares
end

#tickerObject

Returns the value of attribute ticker

Returns:

  • (Object)

    the current value of ticker



11
12
13
# File 'lib/sqa/portfolio.rb', line 11

def ticker
  @ticker
end

#total_costObject

Returns the value of attribute total_cost

Returns:

  • (Object)

    the current value of total_cost



11
12
13
# File 'lib/sqa/portfolio.rb', line 11

def total_cost
  @total_cost
end

Instance Method Details

#profit_loss(current_price) ⇒ Object



16
17
18
# File 'lib/sqa/portfolio.rb', line 16

def profit_loss(current_price)
  (current_price - avg_cost) * shares
end

#profit_loss_percent(current_price) ⇒ Object



20
21
22
23
# File 'lib/sqa/portfolio.rb', line 20

def profit_loss_percent(current_price)
  return 0.0 if avg_cost.zero?
  ((current_price - avg_cost) / avg_cost) * 100.0
end

#value(current_price) ⇒ Object



12
13
14
# File 'lib/sqa/portfolio.rb', line 12

def value(current_price)
  shares * current_price
end