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_diag_long_call_spread ⇒ Object
2026-05-01 continue.
-
#_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.
-
#open ⇒ Object
credit-spread.
-
#place3 ⇒ Object
credit-spread.
- #place_order ⇒ Object
-
#prepare ⇒ Object
2025-10-14 long_credit_put_spread 2026-02-21 short_credit_call_spread That’s for looking at the ui, with buttons ‘select’.
-
#prepare2 ⇒ Object
Manually selected one, I suppose _TODO: pos is autonext position, but should be this position? rename to: def select ?.
-
#prepare2_intent ⇒ Object
2026-04-08 try-close only so far.
-
#prepare3 ⇒ Object
2026-02-26 continue…
-
#reprice ⇒ Object
roll a credit-spread.
- #roll_inner ⇒ Object
- #roll_inner_commit ⇒ Object
- #show ⇒ Object
- #sync ⇒ Object
- #update ⇒ Object
Methods inherited from ApplicationController
#home, #schwab_sync, #schwab_sync_exec
Instance Method Details
#_prepare_covered_call ⇒ Object
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 |
# File 'app/controllers/iro/positions_controller.rb', line 351 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_diag_long_call_spread ⇒ Object
2026-05-01 continue
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
# File 'app/controllers/iro/positions_controller.rb', line 558 def _prepare_diag_long_call_spread @positions = [] (-@nn..@nn).each do |idx| inner_strike = @prev.inner.strike - idx*@stock. next_inner = Iro::Option.new({ expires_on: @position[:expires_on], put_call: @position.put_call, stock_id: @position[:stock_id], strike: inner_strike, }) @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 # puts! next_inner.begin_price, 'next_inner.begin_price' # byebug @positions.push Iro::Position.new({ inner: next_inner, long_or_short: @prev.long_or_short, next_gain_loss_amount: next_inner.begin_price - @prev.inner.end_price, outer: @prev.outer, purse: @prev.purse, strategy: @prev.strategy, stock: @prev.stock, }) end end |
#_prepare_long_credit_put_spread ⇒ Object
2025-10-14 _TODO: move to a model? 2026-02-18 continue
408 409 410 411 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 |
# File 'app/controllers/iro/positions_controller.rb', line 408 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?
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'app/controllers/iro/positions_controller.rb', line 483 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 81 |
# 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 params[:position][:outer_strike] @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
83 84 85 86 87 88 89 |
# File 'app/controllers/iro/positions_controller.rb', line 83 def destroy @position = Iro::Position.find params[:id] :destroy, @position @position.delete flash_notice "Probably ok" redirect_to request.referrer end |
#destroy_multi ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'app/controllers/iro/positions_controller.rb', line 91 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
101 102 103 104 105 |
# File 'app/controllers/iro/positions_controller.rb', line 101 def edit @position = Iro::Position.find params[:id] :edit, @position set_position_lists end |
#eval ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/controllers/iro/positions_controller.rb', line 107 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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/controllers/iro/positions_controller.rb', line 120 def index :index, Iro::Position params[:vcfg] ||= {} # "view config" template = params[:vcfg][:template] || Iro::Purse::TEMPLATE_TABLE @purse = Iro::Purse.find_by( slug: 'all' ) @positions = Iro::Position.all().includes( :strategy, :inner, :outer, :stock, :purse ).order_by( expires_on: :asc, ticker: :asc, long_or_short: :asc, inner_strike: :asc ) if params[:vcfg][:statuses] @positions = @positions.where( :status.in => params[:vcfg][:statuses] ) end puts! @positions @page_title = 'All Positions' end |
#new ⇒ Object
only callable from _new.haml, with position partially pre-filled
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/controllers/iro/positions_controller.rb', line 139 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, stock_id: strategy.stock_id, }) ) set_position_lists end |
#open ⇒ Object
credit-spread
155 156 157 158 159 160 161 162 163 164 |
# File 'app/controllers/iro/positions_controller.rb', line 155 def open @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.open_price }) @query = @position.schwab_query # Tda::Order.credit_spread_q @position end |
#place3 ⇒ Object
credit-spread
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'app/controllers/iro/positions_controller.rb', line 316 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
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/controllers/iro/positions_controller.rb', line 166 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 That’s for looking at the ui, with buttons ‘select’
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'app/controllers/iro/positions_controller.rb', line 244 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) @page_title = "Prepare #{@position}" self.send("_prepare_#{@position.strategy.kind}") end |
#prepare2 ⇒ Object
Manually selected one, I suppose _TODO: pos is autonext position, but should be this position? rename to: def select ?
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'app/controllers/iro/positions_controller.rb', line 265 def prepare2 if params[:prev_id] prev = Iro::Position.find params[:prev_id] prev.update({ autonxt_id: params[:id] }) Iro::Position.where( prev_id: params[:prev_id], status: 'proposed' ).update_all( status: 'prepare' ) end @position = Iro::Position.find params[:id] :roll, @position @position.update({ status: Iro::Position::STATUS_PROPOSED, intent: @position.strategy.intent, pending_price: @position.roll_price, }) @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 @page_title = "Prepare #{@position.stock.ticker} - #{@position}" end |
#prepare2_intent ⇒ Object
2026-04-08 try-close only so far. pos is this position, not autonext.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'app/controllers/iro/positions_controller.rb', line 295 def prepare2_intent @position = Iro::Position.find params[:id] :roll, @position case @position.strategy.intent when Iro::Strategy::INTENT_CLOSE @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 else throw 'unknown intent - trt' end end |
#prepare3 ⇒ Object
2026-02-26 continue… short credit call spread
334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'app/controllers/iro/positions_controller.rb', line 334 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
roll a credit-spread
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/controllers/iro/positions_controller.rb', line 190 def reprice @position = Iro::Position.find params[:id] :roll, @position flash_notice @position.update({ pending_price: params[:pending_price] }) if @position.schwab_order_id Tda::Order.cancel_order!( @position.schwab_order_id ) outs = Tda::Order.place_order!( @position.schwab_query ) flag = @position.update({ schwab_order_id: outs[:schwab_order_id], schwab_status: outs[:schwab_status], status: Iro::Position::STATUS_PENDING, }) flash_notice "Updated schwab order!" end redirect_to request.referrer end |
#roll_inner ⇒ Object
209 210 211 212 |
# File 'app/controllers/iro/positions_controller.rb', line 209 def roll_inner @position = Iro::Position.find params[:id] :roll, @position end |
#roll_inner_commit ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/controllers/iro/positions_controller.rb', line 213 def roll_inner_commit pos = @position = Iro::Position.find params[:id] :roll, @position old = pos.inner pos.inners.push old new_params = params[:new].permit!.merge({ put_call: pos.put_call, stock_id: pos.stock_id, }) pos.inner = Iro::Option.create!( new_params ) pos.inner.sync pos.inner.begin_price ||= pos.inner.end_price pos.inner.begin_delta ||= pos.inner.end_delta pos.realized_gain_loss_amount = pos.realized_gl + old.begin_price - old.end_price pos.expires_on = pos.inner.expires_on pos.save flash_notice "Probably ok." redirect_to purse_path(pos.purse) end |
#show ⇒ Object
236 237 238 239 |
# File 'app/controllers/iro/positions_controller.rb', line 236 def show @position = Iro::Position.find params[:id] :show, @position end |
#sync ⇒ Object
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'app/controllers/iro/positions_controller.rb', line 595 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 flash_notice 'did sync and calc_rollp' redirect_to request.referrer || purse_path( @position.purse ) end |
#update ⇒ Object
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'app/controllers/iro/positions_controller.rb', line 612 def update pos = @position = Iro::Position.find params[:id] :update, @position if @position.update pos_params pos.inner.update params[:inner].permit! if pos.outer pos.outer.update params[:outer].permit! 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 |