Class: Tda::Order

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

Constant Summary collapse

STATUS_FILLED =
'FILLED'
STATUS_REPLACED =
'REPLACED'
STATUS_WORKING =
'WORKING'

Class Method Summary collapse

Class Method Details

.cancel_order!(id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/tda/order.rb', line 54

def self.cancel_order!( id )
  profile = Wco::Profile.pi
  results = self.delete("/accounts/#{profile.}/orders/#{id}", {
    headers: {
      accept:        'application/json',
      Authorization: "Bearer #{profile[:schwab_exec_access_token]}",
    },
  })
  puts! results, 'cancel_order!() results'
  puts! results.code, 'results.code'
  # if !results.code == '200'
  #   throw 'could not cancel order'
  # end
end

.check_status(order_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/tda/order.rb', line 12

def self.check_status order_id
  profile = Wco::Profile.pi
  results = self.get("/accounts/#{profile.}/orders/#{order_id}", {
    headers: {
      accept:        'application/json',
      Authorization: "Bearer #{profile[:schwab_exec_access_token]}",
    },
  })
  puts! results, 'results'
  return results.deep_symbolize_keys
end

.close_credit_spread_q(pos) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/tda/order.rb', line 70

def self.close_credit_spread_q pos
  query = {
    orderType: pos.pending_price > 0 ? "NET_CREDIT" : "NET_DEBIT",
    session: "NORMAL",
    duration: "DAY",
    price: pos.pending_price.abs,
    orderStrategyType: "SINGLE",
    orderLegCollection: [
      ## close
      {
        instruction: "BUY_TO_CLOSE",
        quantity: pos.q,
        instrument: {
          symbol: pos.inner.symbol,
          assetType: "OPTION",
        },
      },
      {
        instruction: "SELL_TO_CLOSE",
        quantity: pos.q,
        instrument: {
          symbol: pos.outer.symbol,
          assetType: "OPTION",
        },
      },

    ],
  }
  return query
end

.credit_spread_q(pos) ⇒ Object

open credit spread?!



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

def self.credit_spread_q pos
  query = {
    orderType: pos.place2_price > 0 ? "NET_CREDIT" : "NET_DEBIT",
    session: "NORMAL",
    duration: "DAY",
    price: pos.pending_price,
    orderStrategyType: "SINGLE",
    orderLegCollection: [
      ## open
      {
        instruction: "BUY_TO_OPEN",
        quantity: pos.q,
        instrument: {
          symbol: pos.outer.symbol,
          assetType: "OPTION",
        },
      },
      {
        instruction: "SELL_TO_OPEN",
        quantity: pos.q,
        instrument: {
          symbol: pos.inner.symbol,
          assetType: "OPTION",
        },
      },
    ],
  }
  return query
end

.get_account_hashObject

not used - the hash is stored



25
26
27
28
29
30
31
32
33
34
# File 'app/models/tda/order.rb', line 25

def self.
  profile = Wco::Profile.find_by email: 'piousbox@gmail.com'
  results = self.get("/accounts/accountNumbers", {
    headers: {
      accept:        'application/json',
      Authorization: "Bearer #{profile[:schwab_exec_access_token]}",
    },
  } )
  puts! results, 'results'
end

.get_ordersObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/tda/order.rb', line 36

def self.get_orders
  profile = Wco::Profile.pi
  today = Time.now.strftime("%Y-%m-%d")
  results = self.get("/accounts/#{profile.}/orders", {
    headers: {
      accept:        'application/json',
      Authorization: "Bearer #{profile[:schwab_exec_access_token]}",
    },
    query: {
      fromEnteredTime: "#{today}T00:00:00Z",
      toEnteredTime: "#{today}T23:59:59Z",
    },
  })
  puts! results, 'get_orders() results'
  puts! results.code, 'results.code'
  return results
end

.place_order!(query) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/tda/order.rb', line 133

def self.place_order! query
  # puts! query, '#place_order'

  profile = Wco::Profile.pi
  results = self.post("/accounts/#{profile.}/orders", {
    headers: {
      'content-type' => 'application/json',
      accept:        'application/json',
      Authorization: "Bearer #{profile[:schwab_exec_access_token]}",
    },
    body: query.to_json,
  })
  puts! results, 'place_order!() results'
  puts! results.code, 'results.code'
  # if 201 != results.code
  #   throw results
  # end
  order_id = results.headers['location'].split('/').last
  # response = JSON.parse results.body
  return { schwab_order_id: order_id, schwab_status: 'WORKING' }
end

.roll_covered_call_q(pos) ⇒ Object

obsolete, I don’t do covered calls anymore?



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

def self.roll_covered_call_q pos
  roll_price = pos.inner.begin_price - pos.autoprev.inner.end_price
  query = {
    orderType: "NET_CREDIT", ## pos.roll_price > 0 ? "NET_CREDIT" : "NET_DEBIT",
    session: "NORMAL",
    duration: "DAY",
    price: ( roll_price + 100 ).to_s, ## _TODO this order will never fill (net credit only)
    orderStrategyType: "SINGLE",
    orderLegCollection: [
      ## close
      {
        instruction: "BUY_TO_CLOSE",
        quantity: pos.q,
        instrument: {
          symbol: pos.autoprev.inner.symbol,
          assetType: "OPTION",
        },
      },

      ## open
      {
        instruction: "SELL_TO_OPEN",
        quantity: pos.q,
        instrument: {
          symbol: pos.inner.symbol,
          assetType: "OPTION",
        },
      },
    ],
  }
  # puts! query, 'query'
  return query
end

.roll_credit_call_spread_q(pos) ⇒ Object



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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/models/tda/order.rb', line 191

def self.roll_credit_call_spread_q pos
  query = {
    orderType: pos.roll_price > 0 ? "NET_CREDIT" : "NET_DEBIT",
    session: "NORMAL",
    duration: "DAY",
    price: pos.roll_price.abs.to_s,
    orderStrategyType: "SINGLE",
    orderLegCollection: [
      ## close
      {
        instruction: "BUY_TO_CLOSE",
        quantity: pos.q,
        instrument: {
          symbol: pos.autoprev.inner.symbol,
          assetType: "OPTION",
        },
      },
      {
        instruction: "SELL_TO_CLOSE",
        quantity: pos.q,
        instrument: {
          symbol: pos.autoprev.outer.symbol,
          assetType: "OPTION",
        },
      },

      ## open
      {
        instruction: "BUY_TO_OPEN",
        quantity: pos.q,
        instrument: {
          symbol: pos.outer.symbol,
          assetType: "OPTION",
        },
      },
      {
        instruction: "SELL_TO_OPEN",
        quantity: pos.q,
        instrument: {
          symbol: pos.inner.symbol,
          assetType: "OPTION",
        },
      },
    ],
  }
  # puts! query, 'query'
  return query
end