Class: IGMarkets::DealingPlatform::PositionMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/dealing_platform/position_methods.rb

Overview

Provides methods for working with positions. Returned by #positions.

Instance Method Summary collapse

Constructor Details

#initialize(dealing_platform) ⇒ PositionMethods

Initializes this helper class with the specified dealing platform.


8
9
10
# File 'lib/ig_markets/dealing_platform/position_methods.rb', line 8

def initialize(dealing_platform)
  @dealing_platform = WeakRef.new dealing_platform
end

Instance Method Details

#[](deal_id) ⇒ Position

Returns the position with the specified deal ID, or nil if there is no position with that ID.


26
27
28
29
30
# File 'lib/ig_markets/dealing_platform/position_methods.rb', line 26

def [](deal_id)
  all.detect do |position|
    position.deal_id == deal_id
  end
end

#allArray<Position>

Returns all positions.


15
16
17
18
19
# File 'lib/ig_markets/dealing_platform/position_methods.rb', line 15

def all
  @dealing_platform.session.get('positions', API_V2).fetch(:positions).map do |attributes|
    position_from_attributes attributes
  end
end

#create(attributes) ⇒ String

Creates a new position.

Options Hash (attributes):

  • :currency_code (String)

    The 3 character currency code, must be one of the instrument's currencies (see Instrument#currencies). Required.

  • :direction (:buy, :sell)

    The position direction. Required.

  • :epic (String)

    The EPIC of the instrument to create a position for. Required.

  • :expiry (Date)

    The expiry date of the instrument, if it has one. Optional.

  • :force_open (Boolean)

    Whether a force open is required. Defaults to false.

  • :guaranteed_stop (Boolean)

    Whether a guaranteed stop is required. Defaults to false.

  • :level (Float)

    Required if and only if :order_type is :limit or :quote.

  • :limit_distance (Integer)

    The distance away in pips to place the limit. If this is set then :limit_level must be nil. Optional.

  • :limit_level (Float)

    The limit level. If this is set then :limit_distance must be nil. Optional.

  • :order_type (:limit, :market, :quote)

    The order type. :market indicates to fill the order at current market level(s). :limit indicates to fill at the price specified by :level (or a more favorable one). :quote is only permitted following agreement with IG Markets. Defaults to :market.

  • :quote_id (String)

    The Lightstreamer quote ID. Required when :order_type is :quote.

  • :size (Float)

    The size of the position to create. Required.

  • :stop_distance (Integer)

    The distance away in pips to place the stop. If this is set then :stop_level must be nil. Optional.

  • :stop_level (Float)

    The stop level. If this is set then :stop_distance must be nil. Optional.

  • :time_in_force (:execute_and_eliminate, :fill_or_kill)

    The order fill strategy. :execute_and_eliminate will fill this order as much as possible within the constraints set by :order_type, :level and :quote_id, which may result in only part of the requested order being filled. :fill_or_kill will try to fill the whole order within the constraints, however if this is not possible then the order will not be filled at all. If :order_type is :market (the default) then :time_in_force will be automatically set to :execute_and_eliminate.

  • :trailing_stop (Boolean)

    Whether to use a trailing stop. Defaults to false. Optional.

  • :trailing_stop_increment (Integer)

    The increment step in pips for the trailing stop. Required when :trailing_stop is true.


70
71
72
73
74
75
76
# File 'lib/ig_markets/dealing_platform/position_methods.rb', line 70

def create(attributes)
  model = PositionCreateAttributes.new attributes

  body = RequestBodyFormatter.format model, expiry: '-'

  @dealing_platform.session.post('positions/otc', body, API_V2).fetch(:deal_reference)
end