Module: Tastytrade::CLIHelpers

Included in:
CLI, Tastytrade::CLI::Orders
Defined in:
lib/tastytrade/cli_helpers.rb

Overview

Common CLI helper methods

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#authenticated?Boolean

Check if user is authenticated

Returns:

  • (Boolean)


87
88
89
# File 'lib/tastytrade/cli_helpers.rb', line 87

def authenticated?
  !current_session.nil?
end

#color_value(value, format_as_currency: true) ⇒ Object

Color code value based on positive/negative



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tastytrade/cli_helpers.rb', line 66

def color_value(value, format_as_currency: true)
  return pastel.dim("$0.00") if value.nil? || value.zero?

  formatted = format_as_currency ? format_currency(value) : value.to_s

  if value.positive?
    pastel.green(formatted)
  else
    pastel.red(formatted)
  end
end

#configObject

Configuration helper



31
32
33
# File 'lib/tastytrade/cli_helpers.rb', line 31

def config
  @config ||= CLIConfig.new
end

#current_accountObject

Get the currently selected account



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tastytrade/cli_helpers.rb', line 101

def 
  return @current_account if @current_account

   = config.get("current_account_number")
  return nil unless 

  @current_account = Tastytrade::Models::Account.get(current_session, )
rescue StandardError => e
  # Only warn if debug mode is enabled - otherwise silently return nil
  # and let the caller handle the fallback
  warn "Failed to load current account: #{e.message}" if ENV["DEBUG_SESSION"]
  nil
end

#current_account_numberObject

Get the currently selected account number



116
117
118
# File 'lib/tastytrade/cli_helpers.rb', line 116

def 
  config.get("current_account_number")
end

#current_sessionObject

Get current session if authenticated



79
80
81
82
83
84
# File 'lib/tastytrade/cli_helpers.rb', line 79

def current_session
  @current_session ||= load_session
rescue StandardError => e
  error("Failed to load session: #{e.message}")
  nil
end

#display_trading_status(status) ⇒ Object

Display trading status information



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/tastytrade/cli_helpers.rb', line 137

def display_trading_status(status)
  puts "\n#{pastel.bold("Trading Status for Account:")} #{status.}"
  puts pastel.dim("" * 60)

  # Display restrictions first if any
  restrictions = status.active_restrictions
  if restrictions.any?
    puts "\n#{pastel.bold.red("⚠ Account Restrictions:")}"
    restrictions.each do |restriction|
      case restriction
      when "Margin Call", "Day Trade Equity Maintenance Call"
        puts "  #{pastel.red("#{restriction}")}"
      when "Pattern Day Trader"
        puts "  #{pastel.yellow("#{restriction}")}"
      when "Account Closed", "Account Frozen"
        puts "  #{pastel.bright_red("#{restriction}")}"
      else
        puts "  #{pastel.yellow("#{restriction}")}"
      end
    end
  else
    puts "\n#{pastel.green("✓ No account restrictions")}"
  end

  # Display trading permissions
  puts "\n#{pastel.bold("Trading Permissions:")}"
  permissions = status.permissions_summary

  # Options trading
  options_status = permissions[:options]
  options_color = options_status == "Disabled" ? :dim : :green
  puts "  Options Trading:      #{pastel.send(options_color, options_status)}"

  # Short calls
  short_calls_status = permissions[:short_calls]
  short_calls_color = short_calls_status == "Enabled" ? :green : :dim
  puts "  Short Calls:          #{pastel.send(short_calls_color, short_calls_status)}"

  # Futures trading
  futures_status = permissions[:futures]
  futures_color = case futures_status
                  when "Enabled" then :green
                  when "Closing Only" then :yellow
                  else :dim
  end
  puts "  Futures Trading:      #{pastel.send(futures_color, futures_status)}"

  # Cryptocurrency trading
  crypto_status = permissions[:cryptocurrency]
  crypto_color = case crypto_status
                 when "Enabled" then :green
                 when "Closing Only" then :yellow
                 else :dim
  end
  puts "  Cryptocurrency:       #{pastel.send(crypto_color, crypto_status)}"

  # Portfolio margin
  pm_status = permissions[:portfolio_margin]
  pm_color = pm_status == "Enabled" ? :green : :dim
  puts "  Portfolio Margin:     #{pastel.send(pm_color, pm_status)}"

  # Display account characteristics
  puts "\n#{pastel.bold("Account Characteristics:")}"
  pdt_status = permissions[:pattern_day_trader] == "Yes" ? pastel.yellow("Yes") : pastel.dim("No")
  puts "  Pattern Day Trader:   #{pdt_status}"

  if status.day_trade_count && status.day_trade_count > 0
    puts "  Day Trade Count:      #{pastel.cyan(status.day_trade_count.to_s)}"
  end

  if status.pdt_reset_on
    puts "  PDT Reset Date:       #{pastel.yellow(status.pdt_reset_on.strftime("%Y-%m-%d"))}"
  end

  # Display additional info
  puts "\n#{pastel.bold("Additional Information:")}"
  puts "  Fee Schedule:         #{pastel.cyan(status.fee_schedule_name)}"
  puts "  Margin Type:          #{pastel.cyan(status.equities_margin_calculation_type)}"
  puts "  Last Updated:         #{pastel.dim(status.updated_at.strftime("%Y-%m-%d %H:%M:%S %Z"))}"

  puts ""
end

#error(message) ⇒ Object

Print error message in red



36
37
38
# File 'lib/tastytrade/cli_helpers.rb', line 36

def error(message)
  warn pastel.red("Error: #{message}")
end

#format_bp_status(usage_percentage) ⇒ Object

Format buying power status based on usage percentage



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/tastytrade/cli_helpers.rb', line 121

def format_bp_status(usage_percentage)
  return pastel.dim("N/A") unless usage_percentage

  case usage_percentage.to_f
  when 0..50
    pastel.green("Low")
  when 50..80
    pastel.yellow("Moderate")
  when 80..90
    pastel.bright_yellow("High")
  else
    pastel.red("Critical")
  end
end

#format_currency(value) ⇒ Object

Format currency values



56
57
58
59
60
61
62
63
# File 'lib/tastytrade/cli_helpers.rb', line 56

def format_currency(value)
  return "$0.00" if value.nil? || value.zero?

  formatted = format("$%.2f", value.abs)
  # Add thousand separators
  formatted.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, '\\1,')
  value.negative? ? "-#{formatted}" : formatted
end

#info(message) ⇒ Object

Print info message



51
52
53
# File 'lib/tastytrade/cli_helpers.rb', line 51

def info(message)
  puts pastel.cyan("#{message}")
end

#pastelObject

Colorization helper



21
22
23
# File 'lib/tastytrade/cli_helpers.rb', line 21

def pastel
  @pastel ||= Pastel.new
end

#promptObject

Interactive prompt helper



26
27
28
# File 'lib/tastytrade/cli_helpers.rb', line 26

def prompt
  @prompt ||= TTY::Prompt.new
end

#require_authentication!Object

Require authentication or exit



92
93
94
95
96
97
98
# File 'lib/tastytrade/cli_helpers.rb', line 92

def require_authentication!
  return if authenticated?

  error("You must be logged in to use this command.")
  info("Run 'tastytrade login' to authenticate.")
  exit 1
end

#success(message) ⇒ Object

Print success message in green



46
47
48
# File 'lib/tastytrade/cli_helpers.rb', line 46

def success(message)
  puts pastel.green("#{message}")
end

#warning(message) ⇒ Object

Print warning message in yellow



41
42
43
# File 'lib/tastytrade/cli_helpers.rb', line 41

def warning(message)
  warn pastel.yellow("Warning: #{message}")
end