## helper function for wager_to_shares (see betreal paper) #def g(d,w,q,y,r,n,b) # return d*b*Math.log((1.0+Math.exp(d*q/b))*Math.exp((n*(w*r+y))/b)-1)-q #end # ## worst-case net worth after moving the outstanding shares from q to q+x #def w(q,y,r,x,n # ## qty demanded by an agent wanting to move the price in direction d (+1 or -1) ## who has y yootles and r shares, and the outstanding qty is q, the liquidity ## b, and n is always 1 for now (see betreal paper) #def dem0(d,q,y,r,n,b) # if #end ## takes wager amount y, market price p, liquidity (b); returns shares acquirable #def wager_to_shares(y,p,b) # #d = (w<0 ? -1 : 1) # return b*Math.log((p+Math.exp(y/b)-1)/p) #end class MarketController < ApplicationController def info @team = Team.find(params[:team]) @markets = @team.markets.find(:all, :order => "round") @round_names = ["Sweet Sixteen", "Elite Eight", "Final Four", "Championship Game"] @round_up = params[:round_up] @transaction = params[:transaction] end def cash_out holding = Holding.find(params[:holding_id]) params[:market_id] = holding.market holding.cost > 0 ? sell : buy end def buy wager = params[:trade][:wager].to_f market = Market.find(params[:market_id]) num_shares = market.num_shares(wager) trade = Trade.new trade.shares = num_shares trade.market = market trade.user = @user trade.cost = wager.abs trade.save market.outstanding_shares += num_shares market.volume += (num_shares).abs market.save unless holding = @user.holdings.find(:first, :conditions => ["market_id = ?", market.id]) holding = Holding.new holding.market = market holding.user = @user holding.shares = 0 holding.cost = 0 end holding.shares += trade.shares holding.cost += trade.cost holding.save @user.portfolio_value -= trade.cost @user.save # recompute prices # set_prices redirect_to :action => "holding_info" end def sell params[:trade][:wager] = -params[:trade][:wager].to_i buy end end