Class: Iro::PursesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #schwab_sync, #schwab_sync_exec

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/iro/purses_controller.rb', line 6

def create
  @purse = Iro::Purse.new params[:purse].permit!
  authorize! :create, @purse
  if @purse.save
    ;
  else
    flash_alert @purse
  end
  redirect_to action: :index
end

#destroyObject



17
18
19
20
21
22
# File 'app/controllers/iro/purses_controller.rb', line 17

def destroy
  @purse = Iro::Purse.find(params[:id])
  authorize! :destroy, @purse
  @purse.destroy
  redirect_to action: :index, notice: 'Purse was successfully destroyed.'
end

#editObject



24
25
26
27
# File 'app/controllers/iro/purses_controller.rb', line 24

def edit
  @purse = Iro::Purse.find(params[:id])
  authorize! :edit, @purse
end

#indexObject



29
30
31
32
# File 'app/controllers/iro/purses_controller.rb', line 29

def index
  @purses = Iro::Purse.all.order_by( slug: :asc )
  authorize! :index, Iro::Purse
end

#showObject

table or gameui



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/iro/purses_controller.rb', line 35

def show
  @purse = Iro::Purse.find(params[:id])
  authorize! :show, @purse
  params[:template]    ||= 'show'
  params[:view_status] ||= 'active'
  @unit      = @purse.unit # 12  ## pixels per dollar
  @height    = @purse.height # 100  ## pixels
  @n_dollars = 50 ## * unit * 2 = length of the grid

  @positions = @purse.positions.where( status: params[:view_status]
    ).includes( :strategy
    ).order_by( expires_on: :asc, ticker: :asc, long_or_short: :asc, inner_strike: :asc )

  if 'all' == params[:view_status]
    @positions = @positions.where( :status.in => Iro::Position::STATUSES )
  end

  calc_summary

  @page_title = @purse.to_s
  render params[:template]
end

#syncObject



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

def sync
  @purse = Iro::Purse.find(params[:id])
  authorize! :show, @purse

  @positions = @purse.positions
  expiration_dates = @positions.map { |p| p.expires_on.to_s }.sort
  quotes_h = Tda::Option.get_quotes_h({
    contractType: 'ALL',
    ticker:  @positions[0].ticker,
    fromDate: expiration_dates.first,
    toDate: expiration_dates.last,
  })
  count = 1
  @positions.each do |pos|
    pos.inner.end_price = quotes_h[pos.expires_on.to_s][pos.put_call][pos.inner.strike][:price]
    pos.inner.end_delta = quotes_h[pos.expires_on.to_s][pos.put_call][pos.inner.strike][:delta]
    pos.inner.save ? print("#{count}^") : print("#{count}X")
    if [ Iro::Strategy::KIND_LONG_CREDIT_PUT_SPREAD, Iro::Strategy::KIND_SHORT_CREDIT_CALL_SPREAD ].include?( pos.strategy.kind )
      pos.outer.end_price = quotes_h[pos.expires_on.to_s][pos.put_call][pos.outer.strike][:price]
      pos.outer.end_delta = quotes_h[pos.expires_on.to_s][pos.put_call][pos.outer.strike][:delta]
      pos.outer.save ? print('^') : print('X')
    end
    count = count+1
  end

  flash[:notice] = 'Probably synced the purse.'
  redirect_to request.referrer
end

#updateObject



87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/iro/purses_controller.rb', line 87

def update
  @purse = Iro::Purse.find(params[:id])
  authorize! :update, @purse
  if @purse.update params[:purse].permit!
    flash[:notice] = 'ok'
    redirect_to request.referrer # purse_path(@purse)
  else
    flash_alert @purse
    render :edit
  end
end