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_id` and `:level` attributes.
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_utc ⇒ Time
The created_date_utc attribute.
-
#currency ⇒ String
The currency attribute.
-
#deal_id ⇒ Object
The deal_id attribute.
-
#deal_reference ⇒ Object
The deal_reference attribute.
-
#direction ⇒ Symbol
The direction attribute.
-
#level ⇒ Float
The level attribute.
-
#limit_level ⇒ Float
The limit_level attribute.
-
#limited_risk_premium ⇒ LimitedRiskPremium
The limited_risk_premium 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.
-
#reload ⇒ Object
Reloads this position’s attributes by re-querying the IG Markets API.
-
#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 ⇒ Integer
The trailing_stop_distance attribute.
-
#update(new_attributes) ⇒ String
Updates this position.
Methods inherited from Model
#==, allowed_values, attribute, attribute_type, attribute_value_allowed?, defined_attribute_names, deprecated_attribute, #initialize, #initialize_copy, #inspect, #to_h, valid_attribute?
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.
132 133 134 135 136 137 138 139 140 |
# File 'lib/ig_markets/position.rb', line 132 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 return unless %i[limit quote].include?(attributes[:order_type]) == attributes[:level].nil? raise ArgumentError, 'set level if and only if order_type is :limit or :quote' 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.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ig_markets/position.rb', line 90 def close( = {}) [:deal_id] = deal_id [:direction] = { buy: :sell, sell: :buy }.fetch(direction) [:size] ||= size model = PositionCloseAttributes.build model.validate body = RequestBodyFormatter.format model @dealing_platform.session.delete('positions/otc', body).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.
32 33 34 |
# File 'lib/ig_markets/position.rb', line 32 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_utc ⇒ Time
The created_date_utc attribute.
7 |
# File 'lib/ig_markets/position.rb', line 7 attribute :created_date_utc, Time, format: '%FT%T' |
#currency ⇒ String
The currency attribute.
8 |
# File 'lib/ig_markets/position.rb', line 8 attribute :currency, String, regex: Regex::CURRENCY |
#deal_id ⇒ Object
The deal_id attribute.
9 |
# File 'lib/ig_markets/position.rb', line 9 attribute :deal_id |
#deal_reference ⇒ Object
The deal_reference attribute.
10 |
# File 'lib/ig_markets/position.rb', line 10 attribute :deal_reference |
#direction ⇒ Symbol
The direction attribute.
11 |
# File 'lib/ig_markets/position.rb', line 11 attribute :direction, Symbol, allowed_values: %i[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 |
#limited_risk_premium ⇒ LimitedRiskPremium
The limited_risk_premium attribute.
14 |
# File 'lib/ig_markets/position.rb', line 14 attribute :limited_risk_premium, LimitedRiskPremium |
#market ⇒ MarketOverview
The market attribute.
20 |
# File 'lib/ig_markets/position.rb', line 20 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.
41 42 43 44 45 46 47 |
# File 'lib/ig_markets/position.rb', line 41 def price_delta if direction == :buy close_level - level elsif direction == :sell level - close_level end end |
#profit_loss ⇒ Float
58 59 60 61 62 |
# File 'lib/ig_markets/position.rb', line 58 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.
50 51 52 |
# File 'lib/ig_markets/position.rb', line 50 def profitable? price_delta > 0.0 end |
#reload ⇒ Object
Reloads this position’s attributes by re-querying the IG Markets API.
65 66 67 |
# File 'lib/ig_markets/position.rb', line 65 def reload self.attributes = @dealing_platform.positions[deal_id].attributes end |
#size ⇒ Float
The size attribute.
15 |
# File 'lib/ig_markets/position.rb', line 15 attribute :size, Float |
#stop_level ⇒ Float
The stop_level attribute.
16 |
# File 'lib/ig_markets/position.rb', line 16 attribute :stop_level, Float |
#trailing_step ⇒ Float
The trailing_step attribute.
17 |
# File 'lib/ig_markets/position.rb', line 17 attribute :trailing_step, Float |
#trailing_stop? ⇒ Boolean
Returns whether this position has a trailing stop.
25 26 27 |
# File 'lib/ig_markets/position.rb', line 25 def trailing_stop? !trailing_step.nil? && !trailing_stop_distance.nil? end |
#trailing_stop_distance ⇒ Integer
The trailing_stop_distance attribute.
18 |
# File 'lib/ig_markets/position.rb', line 18 attribute :trailing_stop_distance, Integer |
#update(new_attributes) ⇒ String
Updates this position. No attributes are mandatory, and any attributes not specified will be kept at their current value.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ig_markets/position.rb', line 115 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 body = RequestBodyFormatter.format PositionUpdateAttributes.new(new_attributes) @dealing_platform.session.put("positions/otc/#{deal_id}", body, API_V2).fetch(:deal_reference) end |