Class: Iro::PositionsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #schwab_sync

Instance Method Details

#_prepare_covered_callObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/controllers/iro/positions_controller.rb', line 185

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: @prev.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
    @positions.push next_
  end
end

#_prepare_long_debit_call_spreadObject Also known as: _prepare_short_debit_put_spread, _prepare_short_credit_call_spread



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
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/controllers/iro/positions_controller.rb', line 204

def _prepare_long_debit_call_spread
  @positions = []
  (-@nn..@nn).each do |idx|
    next_ = Iro::Position.find_or_create_by({
      expires_on:   @prev.next_expires_on,
      inner_strike: @prev.inner.strike - idx*@stock.options_price_increment,
      outer_strike: @prev.outer.strike - idx*@stock.options_price_increment,
      prev_id:      @prev.id,
      purse:        @position.purse,
      quantity:     @position.quantity,
      status:       'prepare',
      stock:        @stock,
      strategy:     @position.strategy,
    })
    pos = next_
    next_.inner ||= Iro::Option.new({
      # begin_price: pos[:begin_inner_price],
      # begin_delta: pos[:begin_inner_delta],
      expires_on: pos[:expires_on],
      inner:      pos,
      put_call:   pos.put_call,
      stock_id:   pos[:stock_id],
      strike:     pos[:inner_strike],
    })
    next_.outer ||= Iro::Option.new({
      # begin_price: pos[:begin_inner_price],
      # begin_delta: pos[:begin_inner_delta],
      expires_on: pos[:expires_on],
      outer:      pos,
      put_call:   pos.put_call,
      stock_id:   pos[:stock_id],
      strike:     pos[:outer_strike],
    })

    next_.sync
    next_.inner.begin_price = next_.inner.end_price
    next_.inner.begin_delta = next_.inner.end_delta

    next_.outer.begin_price = next_.outer.end_price
    next_.outer.begin_delta = next_.outer.end_delta

    next_.next_gain_loss_amount  = @prev.outer.end_price   - @prev.inner.end_price
    next_.next_gain_loss_amount += next_.inner.begin_price - next_.outer.begin_price
    next_.save
    @positions.push next_
  end
  @positions = @positions.reverse
end

#createObject



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

def create
  pos = @position = Iro::Position.new pos_params
  authorize! :create, @position

  o_attrs = {
    expires_on: pos.expires_on,
    put_call: pos.put_call, # I need this. _vp_ 2024-04-26
    stock_id: pos.stock_id,
  }
  pos.inner = Iro::Option.new params[:inner].permit!.merge( o_attrs )
  pos.outer = Iro::Option.new params[:outer].permit!.merge( o_attrs )

  if @position.save
    flash_notice @position
    redirect_to controller: :purses, action: :show, id: @position.purse_id.to_s
  else
    flash_alert @position
    render action: :new # redirect_to request.referrer
  end
end

#destroyObject



26
27
28
29
30
31
32
# File 'app/controllers/iro/positions_controller.rb', line 26

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

#editObject



34
35
36
37
# File 'app/controllers/iro/positions_controller.rb', line 34

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

#newObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/iro/positions_controller.rb', line 39

def new
  strategy    = Iro::Strategy.find params[:position][:strategy_id]
  @position   = strategy.next_position
  @position ||= Iro::Position.new( params[:position].permit!.merge({
    status:   Iro::Position::STATUS_PROPOSED,
    # inner:    Iro::Option.new,
    # outer:    Iro::Option.new,
    stock_id: strategy.stock_id,
  }) )
  authorize! :new, @posision

  @position.calc_nxt

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

#prepareObject



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

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

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

  ## dealing with too many strikes in the chain
  while true
    @nn = ( @position.purse.n_next_positions/2 ).ceil
    upper = Tda::Option.get_quote({
      contractType: @position.inner.put_call,
      strike: @prev.inner.strike + @nn*@stock.options_price_increment,
      expirationDate: @prev.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: @position.inner.put_call,
      strike: @prev.inner.strike - @nn*@stock.options_price_increment,
      expirationDate: @prev.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

#prepare2Object

long debit call spread



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

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

  pos   = @position
  stock = @position.stock

  @query = {
    orderType: price > 0 ? "NET_CREDIT" : "NET_DEBIT",
    session: "NORMAL",
    price: price,
    duration: "DAY",
    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'
end

#prepare3Object

long debit call spread



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 163

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

  # out = Tda::Option.roll_long_debit_call_spread( position )

  ## @TODO: it's pending here, the order has not been placed.

  flags = []

  flags.push pos.prev.update({ status: Iro::Position::STATUS_CLOSED })
  flags.push pos.update({ status: Iro::Position::STATUS_ACTIVE })
  flags.push pos.purse.update({
    available_amount: pos.purse.available_amount + price + pos.q*100,
  })

  flash_notice flags
  redirect_to controller: :purses, action: :show, template: :gameui, id: pos.purse_id
end

#syncObject



256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/controllers/iro/positions_controller.rb', line 256

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

  @position.sync
  @position.calc_rollp
  if true # @position.rollp > 0.5
    @position.calc_nxt
  end

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

#updateObject

only updates some attributes



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'app/controllers/iro/positions_controller.rb', line 272

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

  if @position.update pos_params
    o_attrs = {
      expires_on: pos.expires_on,
    }
    pos.inner.update params[:inner].permit!.merge( o_attrs )
    pos.outer.update params[:outer].permit!.merge( o_attrs )

    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