Class: Tastytrade::Models::CurrentPosition
- Defined in:
- lib/tastytrade/models/current_position.rb
Overview
Represents a current position in an account
Instance Attribute Summary collapse
-
#account_number ⇒ Object
readonly
Returns the value of attribute account_number.
-
#average_daily_market_close_price ⇒ Object
readonly
Returns the value of attribute average_daily_market_close_price.
-
#average_open_price ⇒ Object
readonly
Returns the value of attribute average_open_price.
-
#average_yearly_market_close_price ⇒ Object
readonly
Returns the value of attribute average_yearly_market_close_price.
-
#close_price ⇒ Object
readonly
Returns the value of attribute close_price.
-
#contract_size ⇒ Object
readonly
Returns the value of attribute contract_size.
-
#cost_effect ⇒ Object
readonly
Returns the value of attribute cost_effect.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#exercise_style ⇒ Object
readonly
Returns the value of attribute exercise_style.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#instrument_type ⇒ Object
readonly
Returns the value of attribute instrument_type.
-
#is_frozen ⇒ Object
readonly
Returns the value of attribute is_frozen.
-
#is_suppressed ⇒ Object
readonly
Returns the value of attribute is_suppressed.
-
#mark ⇒ Object
readonly
Returns the value of attribute mark.
-
#mark_price ⇒ Object
readonly
Returns the value of attribute mark_price.
-
#multiplier ⇒ Object
readonly
Returns the value of attribute multiplier.
-
#option_expiration_type ⇒ Object
readonly
Returns the value of attribute option_expiration_type.
-
#option_type ⇒ Object
readonly
Returns the value of attribute option_type.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#quantity_direction ⇒ Object
readonly
Returns the value of attribute quantity_direction.
-
#realized_day_gain ⇒ Object
readonly
Returns the value of attribute realized_day_gain.
-
#realized_today ⇒ Object
readonly
Returns the value of attribute realized_today.
-
#restricted_quantity ⇒ Object
readonly
Returns the value of attribute restricted_quantity.
-
#root_symbol ⇒ Object
readonly
Returns the value of attribute root_symbol.
-
#strike_price ⇒ Object
readonly
Returns the value of attribute strike_price.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
-
#underlying_symbol ⇒ Object
readonly
Returns the value of attribute underlying_symbol.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Attributes inherited from Base
Instance Method Summary collapse
-
#closed? ⇒ Boolean
Check if position is closed (zero quantity).
-
#display_symbol ⇒ Object
Get display symbol (simplified for options).
-
#equity? ⇒ Boolean
Check if this is an equity position.
-
#futures? ⇒ Boolean
Check if this is a futures position.
-
#futures_option? ⇒ Boolean
Check if this is a futures option position.
-
#initialize(data) ⇒ CurrentPosition
constructor
A new instance of CurrentPosition.
-
#long? ⇒ Boolean
Check if this is a long position.
-
#option? ⇒ Boolean
Check if this is an option position.
-
#position_value ⇒ Object
Calculate position value (quantity * price * multiplier).
-
#short? ⇒ Boolean
Check if this is a short position.
-
#total_pnl ⇒ Object
Calculate total P&L (realized + unrealized).
-
#unrealized_pnl ⇒ Object
Calculate unrealized P&L.
-
#unrealized_pnl_percentage ⇒ Object
Calculate unrealized P&L percentage.
Constructor Details
#initialize(data) ⇒ CurrentPosition
Returns a new instance of CurrentPosition.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tastytrade/models/current_position.rb', line 18 def initialize(data) super @account_number = data["account-number"] @symbol = data["symbol"] @instrument_type = data["instrument-type"] @underlying_symbol = data["underlying-symbol"] # Quantity information @quantity = parse_decimal(data["quantity"]) @quantity_direction = data["quantity-direction"] @restricted_quantity = parse_decimal(data["restricted-quantity"]) # Price information @close_price = parse_decimal(data["close-price"]) @average_open_price = parse_decimal(data["average-open-price"]) @average_yearly_market_close_price = parse_decimal(data["average-yearly-market-close-price"]) @average_daily_market_close_price = parse_decimal(data["average-daily-market-close-price"]) @mark = parse_decimal(data["mark"]) @mark_price = parse_decimal(data["mark-price"]) # Position details @multiplier = data["multiplier"]&.to_i || 1 @cost_effect = data["cost-effect"] @is_suppressed = data["is-suppressed"] || false @is_frozen = data["is-frozen"] || false # Realized gains @realized_day_gain = parse_decimal(data["realized-day-gain"]) @realized_today = parse_decimal(data["realized-today"]) # Timestamps @created_at = parse_time(data["created-at"]) @updated_at = parse_time(data["updated-at"]) @expires_at = parse_time(data["expires-at"]) # Option-specific fields @root_symbol = data["root-symbol"] @option_expiration_type = data["option-expiration-type"] @strike_price = parse_decimal(data["strike-price"]) @option_type = data["option-type"] @contract_size = data["contract-size"]&.to_i @exercise_style = data["exercise-style"] end |
Instance Attribute Details
#account_number ⇒ Object (readonly)
Returns the value of attribute account_number.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def account_number @account_number end |
#average_daily_market_close_price ⇒ Object (readonly)
Returns the value of attribute average_daily_market_close_price.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def average_daily_market_close_price @average_daily_market_close_price end |
#average_open_price ⇒ Object (readonly)
Returns the value of attribute average_open_price.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def average_open_price @average_open_price end |
#average_yearly_market_close_price ⇒ Object (readonly)
Returns the value of attribute average_yearly_market_close_price.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def average_yearly_market_close_price @average_yearly_market_close_price end |
#close_price ⇒ Object (readonly)
Returns the value of attribute close_price.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def close_price @close_price end |
#contract_size ⇒ Object (readonly)
Returns the value of attribute contract_size.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def contract_size @contract_size end |
#cost_effect ⇒ Object (readonly)
Returns the value of attribute cost_effect.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def cost_effect @cost_effect end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def created_at @created_at end |
#exercise_style ⇒ Object (readonly)
Returns the value of attribute exercise_style.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def exercise_style @exercise_style end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def expires_at @expires_at end |
#instrument_type ⇒ Object (readonly)
Returns the value of attribute instrument_type.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def instrument_type @instrument_type end |
#is_frozen ⇒ Object (readonly)
Returns the value of attribute is_frozen.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def is_frozen @is_frozen end |
#is_suppressed ⇒ Object (readonly)
Returns the value of attribute is_suppressed.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def is_suppressed @is_suppressed end |
#mark ⇒ Object (readonly)
Returns the value of attribute mark.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def mark @mark end |
#mark_price ⇒ Object (readonly)
Returns the value of attribute mark_price.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def mark_price @mark_price end |
#multiplier ⇒ Object (readonly)
Returns the value of attribute multiplier.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def multiplier @multiplier end |
#option_expiration_type ⇒ Object (readonly)
Returns the value of attribute option_expiration_type.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def option_expiration_type @option_expiration_type end |
#option_type ⇒ Object (readonly)
Returns the value of attribute option_type.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def option_type @option_type end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def quantity @quantity end |
#quantity_direction ⇒ Object (readonly)
Returns the value of attribute quantity_direction.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def quantity_direction @quantity_direction end |
#realized_day_gain ⇒ Object (readonly)
Returns the value of attribute realized_day_gain.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def realized_day_gain @realized_day_gain end |
#realized_today ⇒ Object (readonly)
Returns the value of attribute realized_today.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def realized_today @realized_today end |
#restricted_quantity ⇒ Object (readonly)
Returns the value of attribute restricted_quantity.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def restricted_quantity @restricted_quantity end |
#root_symbol ⇒ Object (readonly)
Returns the value of attribute root_symbol.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def root_symbol @root_symbol end |
#strike_price ⇒ Object (readonly)
Returns the value of attribute strike_price.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def strike_price @strike_price end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def symbol @symbol end |
#underlying_symbol ⇒ Object (readonly)
Returns the value of attribute underlying_symbol.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def @underlying_symbol end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
9 10 11 |
# File 'lib/tastytrade/models/current_position.rb', line 9 def updated_at @updated_at end |
Instance Method Details
#closed? ⇒ Boolean
Check if position is closed (zero quantity)
73 74 75 |
# File 'lib/tastytrade/models/current_position.rb', line 73 def closed? quantity_direction == "Zero" || quantity.zero? end |
#display_symbol ⇒ Object
Get display symbol (simplified for options)
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tastytrade/models/current_position.rb', line 132 def display_symbol if option? # Format: ROOT MM/DD/YY C/P STRIKE return symbol unless expires_at && strike_price && option_type exp_date = expires_at.strftime("%m/%d/%y") type_char = option_type == "Call" ? "C" : "P" strike_str = strike_price.to_s("F") "#{root_symbol} #{exp_date} #{type_char} #{strike_str}" else symbol end end |
#equity? ⇒ Boolean
Check if this is an equity position
78 79 80 |
# File 'lib/tastytrade/models/current_position.rb', line 78 def equity? instrument_type == "Equity" end |
#futures? ⇒ Boolean
Check if this is a futures position
88 89 90 |
# File 'lib/tastytrade/models/current_position.rb', line 88 def futures? instrument_type == "Future" end |
#futures_option? ⇒ Boolean
Check if this is a futures option position
93 94 95 |
# File 'lib/tastytrade/models/current_position.rb', line 93 def futures_option? instrument_type == "Future Option" end |
#long? ⇒ Boolean
Check if this is a long position
63 64 65 |
# File 'lib/tastytrade/models/current_position.rb', line 63 def long? quantity_direction == "Long" end |
#option? ⇒ Boolean
Check if this is an option position
83 84 85 |
# File 'lib/tastytrade/models/current_position.rb', line 83 def option? instrument_type == "Equity Option" end |
#position_value ⇒ Object
Calculate position value (quantity * price * multiplier)
98 99 100 101 102 |
# File 'lib/tastytrade/models/current_position.rb', line 98 def position_value return BigDecimal("0") if closed? price = mark_price.zero? ? close_price : mark_price quantity.abs * price * multiplier end |
#short? ⇒ Boolean
Check if this is a short position
68 69 70 |
# File 'lib/tastytrade/models/current_position.rb', line 68 def short? quantity_direction == "Short" end |
#total_pnl ⇒ Object
Calculate total P&L (realized + unrealized)
127 128 129 |
# File 'lib/tastytrade/models/current_position.rb', line 127 def total_pnl realized_today + unrealized_pnl end |
#unrealized_pnl ⇒ Object
Calculate unrealized P&L
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tastytrade/models/current_position.rb', line 105 def unrealized_pnl return BigDecimal("0") if closed? || average_open_price.zero? current_price = mark_price.zero? ? close_price : mark_price if long? (current_price - average_open_price) * quantity * multiplier else (average_open_price - current_price) * quantity.abs * multiplier end end |
#unrealized_pnl_percentage ⇒ Object
Calculate unrealized P&L percentage
117 118 119 120 121 122 123 124 |
# File 'lib/tastytrade/models/current_position.rb', line 117 def unrealized_pnl_percentage return BigDecimal("0") if closed? || average_open_price.zero? cost_basis = average_open_price * quantity.abs * multiplier return BigDecimal("0") if cost_basis.zero? (unrealized_pnl / cost_basis * 100).round(2) end |