Class: Position
- Inherits:
-
Object
- Object
- Position
- Defined in:
- lib/bitflyer/cli/model/position.rb
Instance Method Summary collapse
- #average ⇒ Object
-
#initialize(positions) ⇒ Position
constructor
A new instance of Position.
- #profit(current_price) ⇒ Object
- #size ⇒ Object
- #sum ⇒ Object
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
#average ⇒ Object
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 |
#size ⇒ Object
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 |
#sum ⇒ Object
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 |