Class: Iro::StocksController

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

Overview

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #schwab_sync, #schwab_sync_exec

Instance Method Details

#createObject



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

def create
  @stock = Iro::Stock.new(stock_params)
  authorize! :create, @stock

  if @stock.save
    flash_notice @stock
  else
    @stock = Iro::Stock.unscoped.find_by ticker: stock_params[:ticker]
    flag = @stock.update( deleted_at: nil, options_price_increment: stock_params[:options_price_increment] )
    flash_alert @stock
  end
  redirect_to action: :index
end

#destroyObject



22
23
24
25
26
# File 'app/controllers/iro/stocks_controller.rb', line 22

def destroy
  authorize! :destroy, @stock
  @stock.destroy
  redirect_to stocks_url, notice: 'Stock was destroyed.'
end

#editObject



28
29
30
# File 'app/controllers/iro/stocks_controller.rb', line 28

def edit
  authorize! :edit, @stock
end

#get_historic_dataObject



32
33
34
35
36
37
# File 'app/controllers/iro/stocks_controller.rb', line 32

def get_historic_data
  authorize! :show, Iro::Stock
  @stock = Iro::Stock.find params[:id]
  @stock.get_historic_data( params[:date_from].to_date )
  redirect_to action: :show, id: params[:id]
end

#indexObject



46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/iro/stocks_controller.rb', line 46

def index
  @all_stocks = Iro::Stock.all
  authorize! :index, Iro::Stock

  respond_to do |format|
    format.html
    format.json do
      render layout: false
    end
  end
end

#newObject



58
59
60
61
# File 'app/controllers/iro/stocks_controller.rb', line 58

def new
  @stock = Iro::Stock.new
  authorize! :new, @stock
end

#recompute_volatilityObject



39
40
41
42
43
44
# File 'app/controllers/iro/stocks_controller.rb', line 39

def recompute_volatility
  authorize! :show, Iro::Stock
  @stock = Iro::Stock.find params[:id]
  @stock.volatility( recompute: true )
  redirect_to action: :show, id: params[:id]
end

#showObject



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/stocks_controller.rb', line 75

def show
  authorize! :show, @stock

  @priceitems = ::Iro::Priceitem.where({
    ticker: @stock.ticker,
  })
  @datapoints = Iro::Datapoint.where({
    symbol: @stock.ticker,
  }).order_by({ date: :desc })
  if @datapoints.length == 0
    @stock.get_historic_data
    @datapoints = Iro::Datapoint.where({
      symbol: @stock.ticker,
    }).order_by({ date: :desc })
  end
  @datapoints_h = {}
  @datapoints.each do |dp|
    @datapoints_h[dp.date.to_s] = dp.value
  end
  @datapoints_arr = @datapoints.map { |dp| { date: dp.date, value: dp.value } }


  respond_to do |format|
    format.html
    format.json do
      render layout: false
    end
  end
end

#syncObject



63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/iro/stocks_controller.rb', line 63

def sync
  authorize! :refresh, Iro::Stock
  tickers = Iro::Stock.all.map { |s| s.ticker }.join(',')
  outs = Tda::Stock.get_quotes tickers

  outs.map do |out|
    Iro::Stock.where( ticker: out[:symbol] ).update_all( last: out[:last] )
  end
  flash_notice 'refreshed stocks'
  redirect_to request.referrer
end

#updateObject



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

def update
  @stock = Iro::Stock.find params[:id]
  authorize! :update, @stock
  if @stock.update(stock_params)
    flash_notice @stock
  else
    flash_alert @stock
  end
  redirect_to request.referrer
end

#viz_1Object



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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/iro/stocks_controller.rb', line 116

def viz_1
  @stock = Iro::Stock.find params[:id]
  authorize! :show, @stock

  @chart_data = { puts: [], puts_1: [], calls: [], calls_1: [] }

  @quotes = Tda::Option.get_quotes({ contractType: 'CALL', ticker: @stock.ticker, expirationDate: params[:expires_on] })
  # @quotes = @quotes.reverse
  # puts! @quotes, '@quotes'
  @quotes.each do |q|
    implied = q[:strikePrice] + ( q[:bid] + q[:ask] )/2
    obj = {
      strike: q[:strikePrice],
      iv: implied,
      price: ( q[:bid] + q[:ask] )/2,
      put_call: q[:putCall],
    }
    # puts! obj, 'obj'
    @chart_data[:calls].push(obj)
  end

  exp_1 = (params[:expires_on].to_date+21.days).to_date
  puts! exp_1, 'exp_1'
  @quotes = Tda::Option.get_quotes({ contractType: 'CALL', ticker: @stock.ticker, expirationDate: exp_1 })
  # @quotes = @quotes.reverse
  # puts! @quotes, '@quotes'
  @quotes.each do |q|
    implied = q[:strikePrice] + ( q[:bid] + q[:ask] )/2
    obj = {
      strike: q[:strikePrice],
      iv: implied,
      price: ( q[:bid] + q[:ask] )/2,
      put_call: "#{q[:putCall]}-1",
    }
    # puts! obj, 'obj'
    @chart_data[:calls_1].push(obj)
  end




  @quotes = Tda::Option.get_quotes({ contractType: 'PUT', ticker: @stock.ticker, expirationDate: params[:expires_on] })
  # puts! @quotes, '@quotes'
  @quotes.each do |q|
    implied = q[:strikePrice] - ( q[:bid] + q[:ask] )/2
    obj = {
      strike: q[:strikePrice],
      iv: implied,
      price: ( q[:bid] + q[:ask] )/2,
      put_call: q[:putCall],
    }
    # puts! obj, 'obj'
    @chart_data[:puts].push(obj)
  end
  @quotes = Tda::Option.get_quotes({ contractType: 'PUT', ticker: @stock.ticker, expirationDate: exp_1 })
  # puts! @quotes, '@quotes'
  @quotes.each do |q|
    implied = q[:strikePrice] - ( q[:bid] + q[:ask] )/2
    obj = {
      strike: q[:strikePrice],
      iv: implied,
      price: ( q[:bid] + q[:ask] )/2,
      put_call: q[:putCall],
    }
    # puts! obj, 'obj'
    @chart_data[:puts_1].push(obj)
  end

  puts! @chart_data, '@chart_data'

  # get all options at this expiration. call only.
  # plot price + premium.
end