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.



2
3
4
# File 'lib/bitflyer/cli/model/position.rb', line 2

def initialize(positions)
  @positions = positions
end

Instance Method Details

#averageObject



12
13
14
# File 'lib/bitflyer/cli/model/position.rb', line 12

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

#profit(current_price) ⇒ Object



6
7
8
9
10
# File 'lib/bitflyer/cli/model/position.rb', line 6

def profit(current_price)
  @positions.inject(0.0) do |sum, position|
    sum + (current_price - position['price'].to_f) * position['size'].to_f * (position['side'] == 'BUY' ? 1.0 : -1.0)
  end
end

#sizeObject



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

def size
  @positions.inject(0.0) { |sum, position| sum + position['size'].to_f * (position['side'] == 'BUY' ? 1.0 : -1.0) }.round(2)
end

#sumObject



16
17
18
19
20
# File 'lib/bitflyer/cli/model/position.rb', line 16

def sum
  @positions.inject(0.0) do |sum, position|
    sum + position['price'].to_f * position['size'].to_f
  end
end