Class: Tda::Option

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
app/models/tda/option.rb

Overview

class Schwab

include HTTParty
debug_output $stdout
base_uri 'https://api.schwabapi.com/marketdata/v1'

end

Class Method Summary collapse

Class Method Details

.close_credit_callObject



150
151
# File 'app/models/tda/option.rb', line 150

def self.close_credit_call
end

.close_long_debit_call_spreadObject



152
153
# File 'app/models/tda/option.rb', line 152

def self.close_long_debit_call_spread
end

.close_short_debit_put_spreadObject



154
155
# File 'app/models/tda/option.rb', line 154

def self.close_short_debit_put_spread
end

.create_credit_call(outer:, inner:, q:, price:) ⇒ Object



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
# File 'app/models/tda/option.rb', line 165

def self.create_credit_call outer:, inner:, q:, price:
  query = {
    orderType: "NET_DEBIT",
    session: "NORMAL",
    price: price,
    duration: "DAY",
    orderStrategyType: "SINGLE",
    orderLegCollection: [
      {
        instruction: "BUY_TO_OPEN",
        quantity: q,
        instrument: {
          symbol: outer.symbol,
          assetType: "OPTION",
        },
      },
      {
        instruction: "SELL_TO_OPEN",
        quantity: q,
        instrument: {
          symbol: inner.symbol,
          assetType: "OPTION",
        },
      },
    ],
  }
  File.write('tmp/query.json', JSON.pretty_generate( query ))
  puts! query, 'query'

  return

  headers = {
    Authorize: "Bearer #{::TD_AMERITRADE[:access_token]}",
  }

  path = "/v1/accounts/#{::TD_AMERITRADE[:accountId]}/orders"
  puts! path, 'path'
  out = self.post path, { query: query, headers: headers }
  timestamp = DateTime.parse out.headers['date']
  out = out.parsed_response.deep_symbolize_keys
  puts! out, 'created credit call?'
end

.create_long_debit_call_spreadObject



207
208
# File 'app/models/tda/option.rb', line 207

def self.create_long_debit_call_spread
end

.create_short_debit_put_spreadObject



209
210
# File 'app/models/tda/option.rb', line 209

def self.create_short_debit_put_spread
end

.get_chain(params) ⇒ Object

2023-02-05 vp

Gets the entire chain



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
61
62
# File 'app/models/tda/option.rb', line 23

def self.get_chain params
  opts = { symbol: params[:ticker] } ## use 'GME' as symbol here even though a symbol is eg 'GME_021023P2.5'
  query = { apikey: ::TD_AMERITRADE[:apiKey] }.merge opts
  puts! query, 'input opts'

  path = "/v1/marketdata/chains"
  out = self.get path, { query: query }
  timestamp = DateTime.parse out.headers['date']
  out = out.parsed_response.deep_symbolize_keys


  outs = []
  %w| put call |.each do |contractType|
    tmp_sym = "#{contractType}ExpDateMap".to_sym
    _out = out[tmp_sym]
    _out.each do |date, vs| ## date="2023-02-10:5"
      vs.each do |strike, _v| ## strike="18.5"
        v = _v[0] ## v={} many attrs
        v = v.except( :lastSize, :optionDeliverablesList, :settlementType,
          :deliverableNote, :pennyPilot, :mini )
        v.each do |k, i|
          if i == 'NaN'
            v[k] = nil
          end
        end

        v[:timestamp] = timestamp
        v[:ticker] = params[:ticker]
        outs.push( v )
      end
    end
  end

  outs.each do |x|
    opi = ::Iro::OptionPriceItem.create( x )
    if !opi.persisted?
      puts! opi.errors.full_messages, "Cannot create OptionPriceItem"
    end
  end
end

.get_quote(params) ⇒ Object

2023-03-18 vp This is what I should be using to check if a position should be rolled.



67
68
69
# File 'app/models/tda/option.rb', line 67

def self.get_quote params
  OpenStruct.new ::Tda::Option.get_quotes(params)[0]
end

.get_quotes(params) ⇒ Object

params: contractType, strike, expirationDate, ticker

ow = { contractType: ‘PUT’, ticker: ‘GME’, date: ‘2022-12-09’ } query = :toDate=>“2022-12-09”, :fromDate=>“2022-12-09”, :symbol=>“GME”

2023-02-04 vp

Too specific, but I want the entire chain, every 1-min

2023-02-06 vp

Continue.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/tda/option.rb', line 80

def self.get_quotes params
  puts! params, 'Tda::Option#get_quotes'

  profile = Wco::Profile.find_by email: 'piousbox@gmail.com'
  opts = {}

  #
  # Validate input ???
  #
  validOpts = %i| contractType |
  validOpts.each do |s|
    if params[s]
      opts[s] = params[s]
    else
      raise Iro::InputError.new("Invalid input, missing '#{s}'.")
    end
  end
  if params[:expirationDate]
    opts[:fromDate] = opts[:toDate] = params[:expirationDate].to_s[0...10]
  else
    raise Iro::InputError.new("Invalid input, missing 'date'.")
  end
  if params[:ticker]
    opts[:symbol] = params[:ticker].upcase
  else
    raise Iro::InputError.new("Invalid input, missing 'ticker'.")
  end

  if params[:strike]
    opts[:strike] = params[:strike]
  end

  query = { }.merge opts
  puts! query, 'input opts'

  headers = {
    accept:    'application/json',
    Authorization: "Bearer #{profile[:schwab_access_token]}",
  }

  path = "/chains"
  out = self.get path, {
    # basic_auth: { username: SCHWAB_DATA[:key], password: SCHWAB_DATA[:secret] },
    headers: headers,
    query: query,
  }
  puts! out, 'out'
  timestamp = DateTime.parse out.headers['date']
  # out = HTTParty.get "https://api.tdameritrade.com#{path}", { query: query }
  out = out.parsed_response.deep_symbolize_keys


  tmp_sym = "#{opts[:contractType].to_s.downcase}ExpDateMap".to_sym
  outs = []
  out = out[tmp_sym]
  out.each do |date, vs|
    vs.each do |strike, _v|
      v = _v[0]
      v = v.except( :lastSize, :optionDeliverablesList, :settlementType,
        :deliverableNote, :pennyPilot, :mini )
      v[:timestamp] = timestamp
      outs.push( v )
    end
  end

  # puts! outs, 'outs'
  return outs
end

.get_tokenObject



157
158
159
160
161
162
163
# File 'app/models/tda/option.rb', line 157

def self.get_token
  opts = {
    grant_type: 'authorization_code',
    access_type: 'offline',
    code: ::TD_AMERITRADE[:code],
  }
end

.roll_credit_callObject



212
213
# File 'app/models/tda/option.rb', line 212

def self.roll_credit_call
end

.roll_long_debit_call_spreadObject



214
215
# File 'app/models/tda/option.rb', line 214

def self.roll_long_debit_call_spread
end

.roll_short_debit_put_spreadObject



216
217
# File 'app/models/tda/option.rb', line 216

def self.roll_short_debit_put_spread
end