Class: Iro::PositionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/iro/positions_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home

Instance Method Details

#_prepare_covered_callObject



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 167

def _prepare_covered_call
  @positions = []
  (-@nn..@nn).each do |idx|
    next_ = Iro::Position.new({
      stock: @stock,
      inner_strike: @prev.inner_strike - idx*@stock.options_price_increment,
      expires_on: @next_expires_on,
      purse: @position.purse,
      strategy: @position.strategy,
      quantity: @position.quantity,
    })
    next_.sync
    next_.begin_inner_price = next_.end_inner_price
    next_.begin_inner_delta = next_.end_inner_delta
    next_.next_gain_loss_amount  = next_.begin_inner_price  - @prev.end_inner_price
    puts! next_, 'next_'
    puts! next_.next_gain_loss_amount, 'amount'
    @positions.push next_
  end
end

#_prepare_long_debit_call_spreadObject



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
# File 'app/controllers/iro/positions_controller.rb', line 188

def _prepare_long_debit_call_spread
  @positions = []
  (-@nn..@nn).each do |idx|
    next_ = Iro::Position.new({
      stock: @stock,
      inner_strike: @prev.inner_strike - idx*@stock.options_price_increment,
      outer_strike: @prev.outer_strike - idx*@stock.options_price_increment,
      expires_on:   @next_expires_on,
      purse:        @position.purse,
      strategy:     @position.strategy,
      quantity:     @position.quantity,
    })
    next_.sync
    next_.begin_inner_price = next_.end_inner_price
    next_.begin_inner_delta = next_.end_inner_delta

    next_.begin_outer_price = next_.end_outer_price
    next_.begin_outer_delta = next_.end_outer_delta
    next_.next_gain_loss_amount  = @prev.end_outer_price   - @prev.end_inner_price
    next_.next_gain_loss_amount += next_.begin_inner_price - next_.begin_outer_price
    puts! next_, 'next_'
    puts! next_.next_gain_loss_amount, 'next_gain_loss_amount'
    @positions.push next_
  end
  @positions = @positions.reverse
end

#_prepare_short_debit_put_spreadObject



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
# File 'app/controllers/iro/positions_controller.rb', line 215

def _prepare_short_debit_put_spread
  @positions = []
  (-@nn..@nn).each do |idx|
    next_ = Iro::Position.new({
      stock: @stock,
      inner_strike: @prev.inner_strike - idx*@stock.options_price_increment,
      outer_strike: @prev.outer_strike - idx*@stock.options_price_increment,
      expires_on:   @next_expires_on,
      purse:        @position.purse,
      strategy:     @position.strategy,
      quantity:     @position.quantity,
    })
    next_.sync
    next_.begin_inner_price = next_.end_inner_price
    next_.begin_inner_delta = next_.end_inner_delta

    next_.begin_outer_price = next_.end_outer_price
    next_.begin_outer_delta = next_.end_outer_delta
    next_.next_gain_loss_amount  = @prev.end_outer_price - @prev.end_inner_price
    next_.next_gain_loss_amount += next_.begin_inner_price - next_.begin_outer_price
    puts! next_, 'next_'
    puts! next_.next_gain_loss_amount, 'next_gain_loss_amount'
    @positions.push next_
  end
end

#createObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/iro/positions_controller.rb', line 22

def create
  @position = Iro::Position.new params[:position].permit!
  authorize! :create, @position

  @position.sync

  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
  end
end

#destroyObject



37
38
39
40
41
42
43
# File 'app/controllers/iro/positions_controller.rb', line 37

def destroy
  @position = Iro::Position.find params[:id]
  authorize! :destroy, @position
  @position.delete
  flash_notice "Probably ok"
  redirect_to request.referrer
end

#editObject



45
46
47
48
# File 'app/controllers/iro/positions_controller.rb', line 45

def edit
  @position = Iro::Position.find params[:id]
  authorize! :edit, @position
end

#newObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/iro/positions_controller.rb', line 5

def new
  @position = Iro::Position.new
  authorize! :new, @posision

  if params[:id]
    old = Iro::Position.find params[:id]
    old = old.attributes
    old.delete :_id
    puts! old, 'old'
    @position = Iro::Position.new old
  end
  if params[:purse_id]
    @position.purse_id = params[:purse_id]
  end

end

#prepareObject



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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/iro/positions_controller.rb', line 115

def prepare
  @position = Iro::Position.find params[:id]
  authorize! :roll, @position

  @prev  = @position
  @purse = @position.purse
  @stock = @position.stock
  @n_dollars = 100

  ## holiday schedule
  @next_expires_on = @prev.expires_on.to_datetime.next_occurring(:monday).next_occurring(:friday)
  if !@next_expires_on.workday?
    @next_expires_on = Time.previous_business_day( @next_expires_on )
  end

  ## dealing with too many strikes in the chain
  while true
    @nn = ( @position.purse.n_next_positions/2 ).ceil
    upper = Tda::Option.get_quote({
      contractType: 'CALL',
      strike: @prev.inner_strike + @nn*@stock.options_price_increment,
      expirationDate: @next_expires_on,
      ticker: @stock.ticker,
    })
    if !upper.symbol
      puts! 'too high'
      flash_alert 'too high'
      @purse.n_next_positions = @purse.n_next_positions - 1
      @purse.n_next_positions = 1 if @purse.n_next_positions < 1
      @purse.save!
      next
    end
    lower = Tda::Option.get_quote({
      contractType: 'CALL',
      strike: @prev.inner_strike - @nn*@stock.options_price_increment,
      expirationDate: @next_expires_on,
      ticker: @stock.ticker,
    })
    if !lower.symbol
      puts! 'too low'
      flash_alert 'too low'
      @purse.n_next_positions = @purse.n_next_positions - 1
      @purse.n_next_positions = 1 if @purse.n_next_positions < 1
      @purse.save!
      next
    end
    break
  end

  self.send("_prepare_#{@position.strategy.kind}")
end

#proposeObject



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/iro/positions_controller.rb', line 50

def propose
  @strategy = Iro::Strategy.find params[:strategy_id]
  authorize! :show, @strategy

  @purse = Iro::Purse.find params[:purse_id]

  ## short debit put spread
  outs = Tda::Option.get_quotes({
    contractType:   'PUT',
    expirationDate: '2024-03-28',
    ticker:         @strategy.stock.ticker,
  })
  outs = outs.select do |out|
    out[:bidSize]+out[:askSize] > 0
  end
  outs = outs.select do |out|
    out[:strikePrice] > @strategy.buffer_above_water + @strategy.stock.last
  end
  outs = outs.select do |out|
    out[:strikePrice] > @strategy.next_inner_strike
  end
  outs = outs.select do |out|
    out[:delta] < @strategy.next_inner_delta
  end

  inner = outs[0]
  outs = outs.select do |out|
    out[:strikePrice] >= inner[:strikePrice] + @strategy.next_spread_amount
  end
  outer = outs[0]

  if inner && outer
    next_position = Iro::Position.new({
      status: 'proposed',
      stock: @strategy.stock,
      inner_strike: inner[:strikePrice],
      outer_strike: outer[:strikePrice],
      begin_outer_price: outer[:last],
      begin_outer_delta: outer[:delta],
      begin_inner_price: inner[:last],
      begin_inner_delta: inner[:delta],
      begin_on: Time.now.to_date,
      expires_on: '2024-03-28',
      purse: @purse,
      strategy: @strategy,
      quantity: 1,
    })
    next_position.sync
    next_position.save
  else
    flash_alert 'cannot propose a new one'
  end
  redirect_to request.referrer
end

#refreshObject



105
106
107
108
109
110
111
112
113
# File 'app/controllers/iro/positions_controller.rb', line 105

def refresh
  @position = pos = Iro::Position.find params[:id]
  authorize! :refresh, @position

  @position.sync
  @position.calc_rollp

  redirect_to request.referrer || purse_path( @position.purse )
end

#updateObject



241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/controllers/iro/positions_controller.rb', line 241

def update
  @position = Iro::Position.find params[:id]
  authorize! :update, @position

  if @position.update params[:position].permit!
    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