from otree.api import (
models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer,
Currency as c, currency_range, Page, WaitPage
)
cu = c
doc = '\nIn a common value auction game, players simultaneously bid on the item being\nauctioned.
\nPrior to bidding, they are given an estimate of the actual value of the item.\nThis actual value is revealed after the bidding.
\nBids are private. The player with the highest bid wins the auction, but\npayoff depends on the bid amount and the actual value.
\n'
class Constants(BaseConstants):
name_in_url = 'auction_asset_one_realized'
players_per_group = None
num_rounds = 1
min_allowable_bid = c(0)
max_allowable_bid = c(12.5)
instructions_template = 'auction_asset_one_realized/instructions.html'
def creating_session(subsession):
pass
class Subsession(BaseSubsession):
creating_session = creating_session
def set_winner_Type1(group):
import random
players = group.get_players()
playerbidnumber = max([p.id_in_group for p in players])
allbids = []
firstcount = 0
othercount = 0
while firstcount < playerbidnumber:
freshplayer = firstcount + 1
currentperson = group.get_player_by_id(freshplayer)
while othercount < currentperson.number_Type1:
allbids.append(currentperson.bid_amount_type1)
othercount = othercount + 1
firstcount = firstcount + 1
othercount = 0
bidsinorder = sorted(allbids, reverse=True)
counter = 0
bidslots = 0
endcheck = 0
totalbids = 0
for p in players:
if p.bid_amount_type1 > 0:
totalbids = totalbids + p.number_Type1
if totalbids < playerbidnumber*2:
for p in players:
if p.bid_amount_type1 > 0 and p.number_Type1 > 0:
p.owned_type1 = p.number_Type1
counter = counter + p.number_Type1
else:
p.owned_type1 = 1
counter = counter + 1
players_with_marginal_bid = [p for p in players if p.bid_amount_type1 == 0 or p.number_Type1 == 0]
while counter < playerbidnumber*2:
receiver = random.choice(players)
if receiver.owned_type1 < 4:
receiver.owned_type1 = receiver.owned_type1 + 1
counter = counter + 1
endcheck = 3
print('Insufficient bids')
if totalbids >= playerbidnumber*2:
for p in players:
if p.bid_amount_type1 > bidsinorder[playerbidnumber*2-1]:
p.owned_type1 = p.number_Type1
counter = counter + p.number_Type1
elif p.bid_amount_type1 == bidsinorder[playerbidnumber*2-1]:
p.owned_type1 = p.number_Type1
counter = counter + p.number_Type1
players_with_marginal_bid = [
p for p in players if p.bid_amount_type1 == bidsinorder[playerbidnumber*2-1]
]
else:
p.owned_type1 = 0
endcheck = 1
if counter > playerbidnumber*2:
while counter > playerbidnumber*2:
winner = random.choice(
players_with_marginal_bid
) # if tie, winner is chosen at random
winner.owned_type1 = winner.owned_type1 - 1
if winner.owned_type1 > - 1:
counter = counter - 1
else:
winner.owned_type1 = 0
for p in players:
if endcheck == 1:
p.auction_spending1 = p.owned_type1*bidsinorder[playerbidnumber*2-1]
p.asset1auctionprice = bidsinorder[playerbidnumber*2-1]
p.available_funds = p.available_funds - p.auction_spending1
if endcheck == 2:
p.auction_spending1 = p.owned_type1*bidsinorder[bidslots-1]
p.asset1auctionprice = bidsinorder[bidslots-1]
p.available_funds = p.available_funds - p.auction_spending1
if endcheck == 3:
p.auction_spending1 = 0
p.asset1auctionprice = 0
p.available_funds = p.available_funds - p.auction_spending1
def my_function(group):
players = group.get_players()
for p in players:
set_this_data(p)
def create_outcome(group):
import random
players = group.get_players()
asset1gen = random.random()
if asset1gen > .5:
group.asset1outcome = "Asset 1 earned 25 cents."
for p in players:
p.available_funds = p.available_funds + .25*p.owned_type1
p.asset1earningschange = .25*p.owned_type1
p.asset1endvalue = 2.5
else:
group.asset1outcome = "Asset 1 lost 25 cents."
for p in players:
p.available_funds = p.available_funds - .25*p.owned_type1
p.asset1earningschange = -.25*p.owned_type1
p.asset1endvalue = 2.5
class Group(BaseGroup):
item_value = models.CurrencyField(doc='Common value of the item to be auctioned random for treatment')
highest_bid = models.CurrencyField()
u = models.IntegerField()
asset1outcome = models.StringField()
set_winner_Type1 = set_winner_Type1
my_function = my_function
create_outcome = create_outcome
def set_this_data(player):
participant = player.participant
participant.vars['type1'] = player.owned_type1
participant.vars['funds'] = 12.50
participant.vars['bankedaccount'] = player.available_funds
participant.vars['asset1value'] = player.asset1endvalue
participant.vars['asset1auctionprice'] = player.asset1auctionprice
participant.vars['purchasehistory'] = player.purchasehistory
class Player(BasePlayer):
bid_amount_type1 = models.CurrencyField(doc='Amount bidded by the player', label='Bid amount for assets', max=Constants.max_allowable_bid, min=Constants.min_allowable_bid)
number_Type1 = models.IntegerField(label='Number of assets to purchase (maximum)', max=4, min=0)
initial_spending = models.CurrencyField()
owned_type1 = models.IntegerField(initial=0)
auction_spending1 = models.CurrencyField()
available_funds = models.CurrencyField(initial=12.5)
asset1endvalue = models.CurrencyField()
asset1auctionprice = models.CurrencyField()
purchasehistory = models.LongStringField(initial='Nothing')
Question1 = models.StringField(choices=[['True', 'True'], ['False', 'False']], label='The redemption value of the asset changes in each period.', widget=widgets.RadioSelect)
Question2 = models.StringField(choices=[['True', 'True'], ['False', 'False']], label='You will be paid for all three rounds of the experiment.', widget=widgets.RadioSelect)
Question3 = models.StringField(choices=[['True', 'True'], ['False', 'False']], label='Everyone receives an asset after the bidding period.', widget=widgets.RadioSelect)
Question4 = models.StringField(choices=[['True', 'True'], ['False', 'False']], label='Players who sell goods receive a price at least as high as their price willing to sell in the trading periods. Thus, if the price willing to sell of a person equals $2.15, the person receives a minimum price of $2.15 when selling any assets.', widget=widgets.RadioSelect)
Question5 = models.StringField(choices=[['True', 'True'], ['False', 'False']], label='Players who buy goods pay a price of less than or equal to their price willing to pay in the trading periods. Thus, if the price willing to pay of a person equals $5.20, the person pays a maximum price of $5.20 for any assets.', widget=widgets.RadioSelect)
asset1earningschange = models.CurrencyField()
set_this_data = set_this_data