Class: IGMarkets::Position
Overview
Contains details on a trading position. Returned by DealingPlatform::PositionMethods#all and DealingPlatform::PositionMethods#[].
Instance Attribute Summary
Attributes inherited from Model
Class Method Summary collapse
-
.validate_order_type_constraints(attributes) ⇒ Object
Validates the internal consistency of the
:order_type,:quote_idand:levelattributes.
Instance Method Summary collapse
-
#close(options = {}) ⇒ String
Closes this position.
-
#close_level ⇒ Float
Returns the level at which this position would close at given the current market price as stored in #market.
-
#contract_size ⇒ Float
The contract_size attribute.
-
#controlled_risk ⇒ Boolean
The controlled_risk attribute.
-
#created_date ⇒ Time
The created_date attribute.
-
#created_date_utc ⇒ Time
The created_date_utc attribute.
-
#currency ⇒ String
The currency attribute.
-
#deal_id ⇒ Object
The deal_id attribute.
-
#direction ⇒ Symbol
The direction attribute.
-
#level ⇒ Float
The level attribute.
-
#limit_level ⇒ Float
The limit_level attribute.
-
#market ⇒ MarketOverview
The market attribute.
- #price_delta ⇒ Float
- #profit_loss ⇒ Float
-
#profitable? ⇒ Boolean
Returns whether this position is currently profitable based on the current market state as stored in #market.
-
#size ⇒ Float
The size attribute.
-
#stop_level ⇒ Float
The stop_level attribute.
-
#trailing_step ⇒ Float
The trailing_step attribute.
-
#trailing_stop? ⇒ Boolean
Returns whether this position has a trailing stop.
-
#trailing_stop_distance ⇒ Fixnum
The trailing_stop_distance attribute.
-
#update(new_attributes) ⇒ String
Updates this position.
Methods inherited from Model
#==, allowed_values, attribute, attribute_type, defined_attribute_names, #initialize, #initialize_copy, #inspect
Constructor Details
This class inherits a constructor from IGMarkets::Model
Class Method Details
.validate_order_type_constraints(attributes) ⇒ Object
Validates the internal consistency of the :order_type, :quote_id and :level attributes.
124 125 126 127 128 129 130 131 132 |
# File 'lib/ig_markets/position.rb', line 124 def self.validate_order_type_constraints(attributes) if (attributes[:order_type] == :quote) == attributes[:quote_id].nil? raise ArgumentError, 'set quote_id if and only if order_type is :quote' end if [:limit, :quote].include?(attributes[:order_type]) == attributes[:level].nil? raise ArgumentError, 'set level if and only if order_type is :limit or :quote' end end |
Instance Method Details
#close(options = {}) ⇒ String
Closes this position. If called with no options then this position will be fully closed at current market prices, partial closes and greater control over the close conditions can be achieved by using the relevant options.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ig_markets/position.rb', line 82 def close( = {}) [:deal_id] = deal_id [:direction] = { buy: :sell, sell: :buy }.fetch(direction) [:size] ||= size model = PositionCloseAttributes.build model.validate payload = PayloadFormatter.format model @dealing_platform.session.delete('positions/otc', payload).fetch :deal_reference end |
#close_level ⇒ Float
Returns the level at which this position would close at given the current market price as stored in #market.
29 30 31 |
# File 'lib/ig_markets/position.rb', line 29 def close_level { buy: market.bid, sell: market.offer }.fetch direction end |
#contract_size ⇒ Float
The contract_size attribute.
5 |
# File 'lib/ig_markets/position.rb', line 5 attribute :contract_size, Float |
#controlled_risk ⇒ Boolean
The controlled_risk attribute.
6 |
# File 'lib/ig_markets/position.rb', line 6 attribute :controlled_risk, Boolean |
#created_date ⇒ Time
The created_date attribute.
7 |
# File 'lib/ig_markets/position.rb', line 7 attribute :created_date, Time, format: '%Y/%m/%d %T:%L' |
#created_date_utc ⇒ Time
The created_date_utc attribute.
8 |
# File 'lib/ig_markets/position.rb', line 8 attribute :created_date_utc, Time, format: '%FT%T' |
#currency ⇒ String
The currency attribute.
9 |
# File 'lib/ig_markets/position.rb', line 9 attribute :currency, String, regex: Regex::CURRENCY |
#deal_id ⇒ Object
The deal_id attribute.
10 |
# File 'lib/ig_markets/position.rb', line 10 attribute :deal_id |
#direction ⇒ Symbol
The direction attribute.
11 |
# File 'lib/ig_markets/position.rb', line 11 attribute :direction, Symbol, allowed_values: [:buy, :sell] |
#level ⇒ Float
The level attribute.
12 |
# File 'lib/ig_markets/position.rb', line 12 attribute :level, Float |
#limit_level ⇒ Float
The limit_level attribute.
13 |
# File 'lib/ig_markets/position.rb', line 13 attribute :limit_level, Float |
#market ⇒ MarketOverview
The market attribute.
19 |
# File 'lib/ig_markets/position.rb', line 19 attribute :market, MarketOverview |
#price_delta ⇒ Float
Returns the favorable difference in the price between #level and the current market price as stored in #market. If #direction is :buy and the market has since risen then this method will return a positive value, but if #direction is :sell and the market has since risen then this method will return a negative value.
38 39 40 41 42 43 44 |
# File 'lib/ig_markets/position.rb', line 38 def price_delta if direction == :buy close_level - level elsif direction == :sell level - close_level end end |
#profit_loss ⇒ Float
55 56 57 58 59 |
# File 'lib/ig_markets/position.rb', line 55 def profit_loss return size / level if market.instrument_type == :binary price_delta * size * contract_size end |
#profitable? ⇒ Boolean
Returns whether this position is currently profitable based on the current market state as stored in #market.
47 48 49 |
# File 'lib/ig_markets/position.rb', line 47 def profitable? price_delta > 0.0 end |
#size ⇒ Float
The size attribute.
14 |
# File 'lib/ig_markets/position.rb', line 14 attribute :size, Float |
#stop_level ⇒ Float
The stop_level attribute.
15 |
# File 'lib/ig_markets/position.rb', line 15 attribute :stop_level, Float |
#trailing_step ⇒ Float
The trailing_step attribute.
16 |
# File 'lib/ig_markets/position.rb', line 16 attribute :trailing_step, Float |
#trailing_stop? ⇒ Boolean
Returns whether this position has a trailing stop.
22 23 24 |
# File 'lib/ig_markets/position.rb', line 22 def trailing_stop? !trailing_step.nil? && !trailing_stop_distance.nil? end |
#trailing_stop_distance ⇒ Fixnum
The trailing_stop_distance attribute.
17 |
# File 'lib/ig_markets/position.rb', line 17 attribute :trailing_stop_distance, Fixnum |
#update(new_attributes) ⇒ String
Updates this position. No attributes are mandatory, and any attributes not specified will be kept at their current value.
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ig_markets/position.rb', line 107 def update(new_attributes) new_attributes = { limit_level: limit_level, stop_level: stop_level, trailing_stop: trailing_stop?, trailing_stop_distance: trailing_stop_distance, trailing_stop_increment: trailing_step }.merge new_attributes unless new_attributes[:trailing_stop] new_attributes[:trailing_stop_distance] = new_attributes[:trailing_stop_increment] = nil end payload = PayloadFormatter.format PositionUpdateAttributes.new new_attributes @dealing_platform.session.put("positions/otc/#{deal_id}", payload, API_V2).fetch(:deal_reference) end |