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.
-
#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.
-
#formatted_size ⇒ String
Returns this position’s #size as a string prefixed with a ‘+` if #direction is
:buy, or a-if #direction is:sell. -
#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, defined_attribute_names, from, #initialize, #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.
123 124 125 126 127 128 129 130 131 |
# File 'lib/ig_markets/position.rb', line 123 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.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ig_markets/position.rb', line 81 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, API_V1).fetch(:deal_reference) 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', time_zone: '+0000' |
#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', time_zone: '+0000' |
#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] |
#formatted_size ⇒ String
Returns this position’s #size as a string prefixed with a ‘+` if #direction is :buy, or a - if #direction is :sell.
56 57 58 |
# File 'lib/ig_markets/position.rb', line 56 def formatted_size "#{{ buy: '+', sell: '-' }.fetch(direction)}#{format '%g', size}" end |
#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.
31 32 33 34 35 36 37 |
# File 'lib/ig_markets/position.rb', line 31 def price_delta if direction == :buy market.bid - level elsif direction == :sell level - market.offer end end |
#profit_loss ⇒ Float
48 49 50 |
# File 'lib/ig_markets/position.rb', line 48 def profit_loss price_delta * size * market.lot_size * market.scaling_factor end |
#profitable? ⇒ Boolean
Returns whether this position is currently profitable based on the current market state as stored in #market.
40 41 42 |
# File 'lib/ig_markets/position.rb', line 40 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.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ig_markets/position.rb', line 106 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 |