Class: Position

Inherits:
Object
  • Object
show all
Defined in:
lib/bitflyer/cli/model/position.rb

Instance Method Summary collapse

Constructor Details

#initialize(positions) ⇒ Position

Returns a new instance of Position.



5
6
7
# File 'lib/bitflyer/cli/model/position.rb', line 5

def initialize(positions)
  @positions = positions
end

Instance Method Details

#averageObject



15
16
17
# File 'lib/bitflyer/cli/model/position.rb', line 15

def average
  @positions.size > 0 ? (sum / size.abs).to_i : 0
end

#profit(current_price) ⇒ Object



9
10
11
12
13
# File 'lib/bitflyer/cli/model/position.rb', line 9

def profit(current_price)
  @positions.inject('0'.to_d) do |sum, position|
    sum + (current_price - position['price'].to_s.to_d) * position['size'].to_s.to_d * coefficient(position)
  end
end

#sizeObject



23
24
25
# File 'lib/bitflyer/cli/model/position.rb', line 23

def size
  @positions.inject('0'.to_d) { |sum, position| sum + position['size'].to_s.to_d * coefficient(position) }
end

#sumObject



19
20
21
# File 'lib/bitflyer/cli/model/position.rb', line 19

def sum
  @positions.inject('0'.to_d) { |sum, position| sum + position['price'].to_s.to_d * position['size'].to_s.to_d }
end