Class: Iro::PositionsController
- Inherits:
-
ApplicationController
- Object
- Wco::ApplicationController
- ApplicationController
- Iro::PositionsController
- Defined in:
- app/controllers/iro/positions_controller.rb
Instance Method Summary collapse
- #_prepare_covered_call ⇒ Object
-
#_prepare_long_credit_put_spread ⇒ Object
2025-10-14 _TODO: move to a model? 2026-02-18 continue.
-
#_prepare_short_credit_call_spread ⇒ Object
2026-02-21 its not working 2026-02-26 I assume its working?.
- #check ⇒ Object
- #close_prep2 ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #destroy_multi ⇒ Object
- #edit ⇒ Object
- #eval ⇒ Object
- #index ⇒ Object
-
#new ⇒ Object
only callable from _new.haml, with position partially pre-filled.
-
#place2 ⇒ Object
place2 = credit-spread should be renamped to open_prep2.
-
#place3 ⇒ Object
credit-spread.
- #place_order ⇒ Object
-
#prepare ⇒ Object
2025-10-14 long_credit_put_spread 2026-02-21 short_credit_call_spread.
- #prepare2 ⇒ Object
-
#prepare3 ⇒ Object
2026-02-26 continue…
-
#reprice ⇒ Object
only credit-spread.
- #sync ⇒ Object
-
#update ⇒ Object
only updates some attributes.
Methods inherited from ApplicationController
#home, #schwab_sync, #schwab_sync_exec
Instance Method Details
#_prepare_covered_call ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'app/controllers/iro/positions_controller.rb', line 280 def _prepare_covered_call @positions = [] (-@nn..@nn).each do |idx| inner_strike = @prev.inner.strike - idx*@stock. outer_strike = 0 puts! [idx, outer_strike, inner_strike], '[idx, outer_strike, inner_strike]' next_ = Iro::Position.where({ prev_id: @prev.id, expires_on: @prev.next_expires_on, inner_strike: inner_strike, outer_strike: outer_strike, }).first if !next_ next_ = Iro::Position.create({ prev_id: @prev.id, expires_on: @prev.next_expires_on, inner_strike: inner_strike, outer_strike: outer_strike, purse: @position.purse, quantity: @position.quantity, status: Iro::Position::STATUS_PREPARE, stock: @stock, strategy: @position.strategy, }) pos = next_ next_.inner ||= Iro::Option.create({ expires_on: pos[:expires_on], pos_of_inner: pos, put_call: pos.put_call, stock_id: pos[:stock_id], strike: pos[:inner_strike], }) end @quotes.map do |quote| if quote[:strikePrice] == next_.inner.strike price = ( quote[:bid] + quote[:ask] )/2 next_.inner.begin_price = price next_.inner.end_price = price next_.inner.begin_delta = quote[:delta] next_.inner.end_delta = quote[:delta] next_.inner.save end end next_.next_gain_loss_amount = next_.inner.begin_price - @prev.inner.end_price next_.save @positions.push next_ end @positions = @positions.reverse end |
#_prepare_long_credit_put_spread ⇒ Object
2025-10-14 _TODO: move to a model? 2026-02-18 continue
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'app/controllers/iro/positions_controller.rb', line 337 def _prepare_long_credit_put_spread @positions = [] (-@nn..@nn).each do |idx| outer_strike = @prev.outer.strike - idx*@stock. inner_strike = @prev.inner.strike - idx*@stock. puts! [idx, outer_strike, inner_strike], '[idx, outer_strike, inner_strike]' next_ = Iro::Position.where({ prev_id: @prev.id, expires_on: @prev.next_expires_on, inner_strike: inner_strike, outer_strike: outer_strike, }).first if !next_ next_ = Iro::Position.create({ prev_id: @prev.id, expires_on: @prev.next_expires_on, inner_strike: inner_strike, outer_strike: outer_strike, purse: @position.purse, quantity: @position.quantity, status: Iro::Position::STATUS_PREPARE, stock: @stock, strategy: @position.strategy, }) pos = next_ next_.inner ||= Iro::Option.create({ # begin_price: pos[:begin_inner_price], # begin_delta: pos[:begin_inner_delta], expires_on: pos[:expires_on], pos_of_inner: pos, put_call: pos.put_call, stock_id: pos[:stock_id], strike: pos[:inner_strike], }) next_.outer ||= Iro::Option.create({ # begin_price: pos[:begin_inner_price], # begin_delta: pos[:begin_inner_delta], expires_on: pos[:expires_on], pos_of_outer: pos, put_call: pos.put_call, stock_id: pos[:stock_id], strike: pos[:outer_strike], }) end @quotes.map do |quote| if quote[:strikePrice] == next_.inner.strike price = ( quote[:bid] + quote[:ask] )/2 next_.inner.begin_price = price next_.inner.end_price = price next_.inner.begin_delta = quote[:delta] next_.inner.end_delta = quote[:delta] next_.inner.save end if quote[:strikePrice] == next_.outer.strike price = ( quote[:bid] + quote[:ask] )/2 next_.outer.begin_price = price next_.outer.end_price = price next_.outer.begin_delta = quote[:delta] next_.outer.end_delta = quote[:delta] next_.outer.save end end next_.next_gain_loss_amount = @prev.outer.end_price - next_.outer.begin_price next_.next_gain_loss_amount += next_.inner.begin_price - @prev.inner.end_price next_.save @positions.push next_ end end |
#_prepare_short_credit_call_spread ⇒ Object
2026-02-21 its not working 2026-02-26 I assume its working?
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'app/controllers/iro/positions_controller.rb', line 412 def _prepare_short_credit_call_spread @positions = [] (-@nn..@nn).each do |idx| inner_strike = @prev.inner.strike - idx*@stock. outer_strike = @prev.outer.strike - idx*@stock. puts! [idx, inner_strike, outer_strike], '[idx, inner_strike, outer_strike]' next_ = Iro::Position.where({ prev_id: @prev.id, expires_on: @prev.next_expires_on, inner_strike: inner_strike, outer_strike: outer_strike, }).first if !next_ next_ = Iro::Position.create({ prev_id: @prev.id, expires_on: @prev.next_expires_on, inner_strike: inner_strike, outer_strike: outer_strike, purse: @position.purse, quantity: @position.quantity, status: Iro::Position::STATUS_PREPARE, stock: @stock, strategy: @position.strategy, }) pos = next_ next_.inner ||= Iro::Option.create({ expires_on: pos[:expires_on], pos_of_inner: pos, put_call: pos.put_call, stock_id: pos[:stock_id], strike: pos[:inner_strike], }) next_.outer ||= Iro::Option.create({ expires_on: pos[:expires_on], pos_of_outer: pos, put_call: pos.put_call, stock_id: pos[:stock_id], strike: pos[:outer_strike], }) end @quotes.map do |quote| if quote[:strikePrice] == next_.inner.strike price = ( quote[:bid] + quote[:ask] )/2 next_.inner.begin_price = price next_.inner.end_price = price next_.inner.begin_delta = quote[:delta] next_.inner.end_delta = quote[:delta] next_.inner.save end if quote[:strikePrice] == next_.outer.strike price = ( quote[:bid] + quote[:ask] )/2 next_.outer.begin_price = price next_.outer.end_price = price next_.outer.begin_delta = quote[:delta] next_.outer.end_delta = quote[:delta] next_.outer.save end end next_.next_gain_loss_amount = @prev.outer.end_price - next_.outer.begin_price next_.next_gain_loss_amount += next_.inner.begin_price - @prev.inner.end_price next_.save @positions.push next_ end @positions = @positions.reverse end |
#check ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/iro/positions_controller.rb', line 4 def check @position = Iro::Position.find params[:id] :check, @position outs = Tda::Order.check_status @position.schwab_order_id puts! outs, 'outs' if outs[:errors] flash[:alert] = 'need to sync?!' redirect_to request.referrer return end attrs = { schwab_status: outs[:status] } if 'FILLED' == outs[:status] attrs[:status] = Iro::Position::STATUS_CLOSED outs[:orderLegCollection].each do |leg| hash = Iro::Option.symbol_to_h leg[:instrument][:symbol] price = outs[:orderActivityCollection][0][:executionLegs].select do |exec_leg| exec_leg[:instrumentId] == leg[:instrument][:instrumentId] end[0][:price] if @position.inner.matches_h( hash ) attrs[:inner_attributes] = { end_price: price } end if @position.outer.matches_h( hash ) attrs[:outer_attributes] = { end_price: price } end end end puts! attrs,' attrs' @position.update(attrs) redirect_to request.referrer end |
#close_prep2 ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/controllers/iro/positions_controller.rb', line 38 def close_prep2 @position = Iro::Position.find params[:id] :close, @position @position.inner.sync @position.inner.update({ begin_price: @position.inner.end_price }) @position.outer.sync @position.outer.update({ begin_price: @position.outer.end_price }) @position.update({ pending_price: @position.close_price, intent: Iro::Position::INTENT_CLOSE }) @query = Tda::Order.close_credit_spread_q @position @page_title = "Closing #{@position} ..." end |
#create ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/controllers/iro/positions_controller.rb', line 50 def create @position = Iro::Position.new pos_params :create, @position # @position.inner_strike = params[:inner][:strike] # @position.outer_strike = params[:outer][:strike] o_attrs = { expires_on: @position.expires_on, put_call: @position.put_call, # I need this. _vp_ 2024-04-26 stock_id: @position.stock_id, } @position.inner = Iro::Option.create!({ strike: params[:position][:inner_strike] }.merge( o_attrs )) @position.inner.sync @position.inner.begin_price = @position.inner.end_price @position.inner.begin_delta = @position.inner.end_delta if [ Iro::Strategy::KIND_LONG_CREDIT_PUT_SPREAD, Iro::Strategy::KIND_SHORT_CREDIT_CALL_SPREAD ].include?( @position.strategy.kind ) @position.outer = Iro::Option.create!({ strike: params[:position][:outer_strike] }.merge( o_attrs )) @position.outer.sync @position.outer.begin_price = @position.outer.end_price @position.outer.begin_delta = @position.outer.end_delta end if @position.save flash_notice @position redirect_to controller: :purses, action: :show, id: @position.purse_id.to_s else flash_alert @position redirect_to request.referrer # render action: :new end end |
#destroy ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/controllers/iro/positions_controller.rb', line 82 def destroy @position = Iro::Position.find params[:id] :destroy, @position @position.delete flash_notice "Probably ok" redirect_to request.referrer end |
#destroy_multi ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'app/controllers/iro/positions_controller.rb', line 90 def destroy_multi positions = Iro::Position.find params[:positions_xmulti].split(',') :destroy, Iro::Position puts! positions, 'positions' flags = positions.map { |p| p.delete } flash_notice "Probably ok: #{flags}" redirect_to request.referrer end |
#edit ⇒ Object
100 101 102 103 104 |
# File 'app/controllers/iro/positions_controller.rb', line 100 def edit @position = Iro::Position.find params[:id] :edit, @position set_position_lists end |
#eval ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/controllers/iro/positions_controller.rb', line 106 def eval @position = Iro::Position.find params[:id] :edit, @position @position.calc_rollp if @position.rollp > 0.5 @position.calc_nxt end flash[:notice] = 'called position.calc_rollp , maybe position.calc_nxt .' redirect_to request.referrer end |
#index ⇒ Object
119 120 121 122 |
# File 'app/controllers/iro/positions_controller.rb', line 119 def index :index, Iro::Position @positions = Iro::Position.active end |
#new ⇒ Object
only callable from _new.haml, with position partially pre-filled
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/controllers/iro/positions_controller.rb', line 125 def new :new, Iro::Position strategy = Iro::Strategy.find params[:position][:strategy_id] @position = strategy.next_position @position ||= Iro::Position.new( params[:position].permit!.merge({ put_call: strategy.put_call, status: Iro::Position::STATUS_PROPOSED, # inner: Iro::Option.new, # outer: Iro::Option.new, stock_id: strategy.stock_id, }) ) set_position_lists end |
#place2 ⇒ Object
place2 = credit-spread should be renamped to open_prep2
185 186 187 188 189 190 191 192 193 194 |
# File 'app/controllers/iro/positions_controller.rb', line 185 def place2 @position = Iro::Position.find params[:id] :roll, @position @position.inner.sync @position.inner.update({ begin_price: @position.inner.end_price }) @position.outer.sync @position.outer.update({ begin_price: @position.outer.end_price }) @position.update({ pending_price: @position.place2_price }) @query = Tda::Order.credit_spread_q @position end |
#place3 ⇒ Object
credit-spread
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/controllers/iro/positions_controller.rb', line 245 def place3 @position = Iro::Position.find params[:id] @position.update({ pending_price: params[:pending_price] }) :place_order, @position outs = Tda::Order.place_order!( Tda::Order.credit_spread_q @position ) flag = @position.update({ schwab_order_id: outs[:schwab_order_id], schwab_status: outs[:schwab_status], status: Iro::Position::STATUS_PENDING, }) flash_notice flag redirect_to controller: :purses, action: :show, template: 'show', view_status: 'pending', id: @position.purse_id end |
#place_order ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'app/controllers/iro/positions_controller.rb', line 161 def place_order @position = Iro::Position.find params[:id] :place_order, @position query = case @position.intent when Iro::Position::INTENT_CLOSE Tda::Order.close_credit_spread_q @position else throw '_TODO: hls - placing order, not implemented' end outs = Tda::Order.place_order!( query ) flag = @position.update({ schwab_order_id: outs[:schwab_order_id], schwab_status: outs[:schwab_status], status: Iro::Position::STATUS_PENDING, }) flash_notice flag redirect_to controller: :purses, action: :show, template: 'show', view_status: 'pending', id: @position.purse_id end |
#prepare ⇒ Object
2025-10-14 long_credit_put_spread 2026-02-21 short_credit_call_spread
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'app/controllers/iro/positions_controller.rb', line 198 def prepare @position = Iro::Position.find params[:id] :roll, @position @prev = @position @purse = @position.purse @stock = @position.stock @nn = @position.purse.n_next_positions @n_dollars = 50 ## * unit * 2 = length of the grid quotes_params = { contractType: @position.put_call, ticker: @stock.ticker, expirationDate: @prev.next_expires_on } # puts! quotes_params, 'quotes_params' @quotes = Tda::Option.get_quotes(quotes_params) self.send("_prepare_#{@position.strategy.kind}") end |
#prepare2 ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'app/controllers/iro/positions_controller.rb', line 215 def prepare2 @position = Iro::Position.find params[:id] :roll, @position @position.update({ status: Iro::Position::STATUS_PROPOSED, }) if params[:prev_id] prev = Iro::Position.find params[:prev_id] prev.update({ autonxt_id: params[:id] }) @position.reload else if @position.autoprev ; else throw 'Must pass prev_id here.' end end @query = case @position.strategy.kind when Iro::Strategy::KIND_LONG_CREDIT_PUT_SPREAD, Iro::Strategy::KIND_SHORT_CREDIT_CALL_SPREAD Tda::Order.roll_credit_call_spread_q @position when Iro::Strategy::KIND_COVERED_CALL Tda::Order.roll_covered_call_q @position else throw 'pp0 - not implemented' end end |
#prepare3 ⇒ Object
2026-02-26 continue… short credit call spread
263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'app/controllers/iro/positions_controller.rb', line 263 def prepare3 @position = Iro::Position.find params[:id] :place_order, @position outs = Tda::Order.place_order!( Tda::Order.roll_credit_call_spread_q @position ) flag = @position.update({ schwab_order_id: outs[:schwab_order_id], schwab_status: outs[:schwab_status], status: Iro::Position::STATUS_PENDING, }) flash_notice flag redirect_to controller: :purses, action: :show, template: :gameui, id: @position.purse_id end |
#reprice ⇒ Object
only credit-spread
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'app/controllers/iro/positions_controller.rb', line 142 def reprice @position = Iro::Position.find params[:id] :roll, @position @position.update({ pending_price: params[:pending_price] }) Tda::Order.cancel_order!( @position.schwab_order_id ) outs = Tda::Order.place_order!( Tda::Order.credit_spread_q @position ) flag = @position.update({ schwab_order_id: outs[:schwab_order_id], schwab_status: outs[:schwab_status], status: Iro::Position::STATUS_PENDING, }) flash_notice flag # redirect_to controller: :purses, action: :show, template: 'show', view_status: 'pending', id: @position.purse_id redirect_to request.referrer end |
#sync ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'app/controllers/iro/positions_controller.rb', line 482 def sync @position = Iro::Position.find params[:id] :refresh, @position @position.sync @position.calc_rollp ## _TODO: this craps out in a bad way. 2026-02-18 # if @position.rollp > 0.5 # @position.calc_nxt # end redirect_to request.referrer || purse_path( @position.purse ) end |
#update ⇒ Object
only updates some attributes
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'app/controllers/iro/positions_controller.rb', line 500 def update pos = @position = Iro::Position.find params[:id] :update, @position if @position.update pos_params o_attrs = { expires_on: pos.expires_on, } pos.inner.update params[:inner].permit!.merge( o_attrs ) if pos.outer pos.outer.update params[:outer].permit!.merge( o_attrs ) end flash_notice @position redirect_to controller: :purses, action: :show, id: @position.purse_id.to_s else flash_alert @position redirect_to request.referrer end end |