Class: Tastytrade::Models::CurrentPosition

Inherits:
Base
  • Object
show all
Defined in:
lib/tastytrade/models/current_position.rb

Overview

Represents a current position in an account

Instance Attribute Summary collapse

Attributes inherited from Base

#data

Instance Method Summary collapse

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_numberObject (readonly)

Returns the value of attribute account_number.



9
10
11
# File 'lib/tastytrade/models/current_position.rb', line 9

def 
  @account_number
end

#average_daily_market_close_priceObject (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_priceObject (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_priceObject (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_priceObject (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_sizeObject (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_effectObject (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_atObject (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_styleObject (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_atObject (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_typeObject (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_frozenObject (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_suppressedObject (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

#markObject (readonly)

Returns the value of attribute mark.



9
10
11
# File 'lib/tastytrade/models/current_position.rb', line 9

def mark
  @mark
end

#mark_priceObject (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

#multiplierObject (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_typeObject (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_typeObject (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

#quantityObject (readonly)

Returns the value of attribute quantity.



9
10
11
# File 'lib/tastytrade/models/current_position.rb', line 9

def quantity
  @quantity
end

#quantity_directionObject (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_gainObject (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_todayObject (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_quantityObject (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_symbolObject (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_priceObject (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

#symbolObject (readonly)

Returns the value of attribute symbol.



9
10
11
# File 'lib/tastytrade/models/current_position.rb', line 9

def symbol
  @symbol
end

#underlying_symbolObject (readonly)

Returns the value of attribute underlying_symbol.



9
10
11
# File 'lib/tastytrade/models/current_position.rb', line 9

def underlying_symbol
  @underlying_symbol
end

#updated_atObject (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)

Returns:

  • (Boolean)


73
74
75
# File 'lib/tastytrade/models/current_position.rb', line 73

def closed?
  quantity_direction == "Zero" || quantity.zero?
end

#display_symbolObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


83
84
85
# File 'lib/tastytrade/models/current_position.rb', line 83

def option?
  instrument_type == "Equity Option"
end

#position_valueObject

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/tastytrade/models/current_position.rb', line 68

def short?
  quantity_direction == "Short"
end

#total_pnlObject

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_pnlObject

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_percentageObject

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