from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import math author = 'Obexer Simon' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'EXPERIMENT_OBEXER_1' players_per_group = None num_rounds = 1 endowment_cash = c(1000) endowment_stock = 10 class Subsession(BaseSubsession): def creating_session(self): global player for player in self.get_players(): player.number = random.choice([1, 2, 3, 4]) print('set player.number to', player.number) if player.number == 1: player.stock_price_1 = 100 player.stock_price_2 = 70 player.stock_price_3 = 30 player.stock_price_4 = 20 elif player.number == 2: player.stock_price_1 = 100 player.stock_price_2 = 70 player.stock_price_3 = 30 player.stock_price_4 = 20 elif player.number == 3: player.stock_price_1 = 100 player.stock_price_2 = 130 player.stock_price_3 = 180 player.stock_price_4 = 190 elif player.number == 4: player.stock_price_1 = 100 player.stock_price_2 = 130 player.stock_price_3 = 180 player.stock_price_4 = 190 pass class Group(BaseGroup): pass class Player(BasePlayer): number = models.IntegerField() stock_price_1 = models.CurrencyField() stock_price_2 = models.CurrencyField() stock_price_3 = models.CurrencyField() stock_price_4 = models.CurrencyField() # Questions # Kursentwicklung T0-T1 widget = widgets.SliderInput() Beliefs_1 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-4, max=4, doc='''Beliefs of participants''') buy_1 = models.IntegerField( min=0, doc="""Stocks willing to buy""", ) def buy_1_error_message(self, value): print('value is', value) if value > Constants.endowment_cash / self.stock_price_1: return 'Tut mir leid, die von Ihnen angegebene Anzahl an Aktien kann nicht gekauft werden!!' sell_1 = models.IntegerField( min=0, doc="""Stocks willing to sell""", ) def sell_1_error_message(self, value): print('value is', value) if value > Constants.endowment_stock: return 'Tut mir leid, die von Ihnen angegebene Anzahl an Aktien kann nicht verkauft werden!!' # Kursentwicklung T1_T2 Beliefs_2 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-4, max=4, doc='''Beliefs of participants''') buy_2 = models.IntegerField( min=0, doc="""stocks willing to buy""", ) def buy_2_error_message(self, value): print('value is', value) if value > math.floor(self.cash_amount_1() / self.stock_price_2): return 'Tut mir leid, die von Ihnen angegebene Anzahl an Aktien kann nicht gekauft werden!!' sell_2 = models.IntegerField( min=0, doc="""number of stocks willing to sell""", ) def sell_2_error_message(self, value): print('value is', value) if value > self.stock_amount_1(): return 'Tut mir leid, die von Ihnen angegebene Anzahl an Aktien kann nicht verkauft werden!!' TweetImpact_1 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-10, max=10, doc='''Impact of Tweets''') KursentwicklungImpact_1 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-10, max=10, doc='''Impact Kursetnwicklung''') # Kursentwicklung T2-T3 Beliefs_3 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-4, max=4, doc='''Beliefs of participants''') buy_3 = models.IntegerField( min=0, doc="""stocks willing to buy""" ) def buy_3_error_message(self, value): print('value is', value) if value > math.floor(self.cash_amount_2() / self.stock_price_3): return 'Tut mir leid, die von Ihnen angegebene Anzahl an Aktien kann nicht gekauft werden!!' sell_3 = models.IntegerField( min=0, doc="""number of stocks willing to sell""" ) def sell_3_error_message(self, value): print('value is', value) if value > self.stock_amount_2(): return 'Tut mir leid, die von Ihnen angegebene Anzahl an Aktien kann nicht verkauft werden!!' TweetImpact_2 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-10, max=10, doc='''Impact of Tweets''') KursentwicklungImpact_2 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-10, max=10, doc='''Impact Kursetnwicklung''') # Ergebnis TweetImpact_3 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-10, max=10, doc='''Impact of Tweets''') KursentwicklungImpact_3 = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-10, max=10, doc='''Impact Kursetnwicklung''') PastPerformance = models.IntegerField( choices=[-4, -3, -2, -1, 0, 1, 2, 3, 4], widget=widgets.RadioSelectHorizontal, min=-10, max=10, doc='''Impact Past Performance''') # Price_path # Kursentwicklung T1_T2 cash_1 = models.CurrencyField() stock_1 = models.IntegerField() stockvalue_1 = models.CurrencyField() final_1 = models.CurrencyField() change_1 = models.CurrencyField() def cash_amount_1(self): self.cash_1 = Constants.endowment_cash + (self.sell_1 - self.buy_1) * self.stock_price_1 return self.cash_1 def stock_amount_1(self): self.stock_1 = Constants.endowment_stock + self.buy_1 - self.sell_1 return self.stock_1 def stock_value_1(self): self.stockvalue_1 = self.stock_1 * self.stock_price_2 return self.stockvalue_1 def final_wealth_1(self): self.final_1 = self.cash_1 + self.stock_1 * self.stock_price_2 return self.final_1 def change_wealth_1(self): self.change_1 = (Constants.endowment_cash + (self.sell_1 - self.buy_1) * self.stock_price_1) + (Constants.endowment_stock + self.buy_1 - self.sell_1) * self.stock_price_2 - 2000 return self.change_1 # Kursentwicklung T2_T3 cash_2 = models.CurrencyField() stock_2 = models.IntegerField() stockvalue_2 = models.CurrencyField() final_2 = models.CurrencyField() change_2 = models.CurrencyField() def cash_amount_2(self): self.cash_2 = self.cash_1 + (self.sell_2 - self.buy_2) * self.stock_price_2 return self.cash_2 def stock_amount_2(self): self.stock_2 = self.stock_1 + self.buy_2 - self.sell_2 return self.stock_2 def stock_value_2(self): self.stockvalue_2 = self.stock_2 * self.stock_price_3 return self.stockvalue_2 def final_wealth_2(self): self.final_2 = self.cash_2 + self.stock_2 * self.stock_price_3 return self.final_2 def change_wealth_2(self): self.change_2 = self.final_2 - self.final_1 return self.change_2 # Ergebnis cash_3 = models.CurrencyField() stock_3 = models.IntegerField() stockvalue_3 = models.CurrencyField() final_3 = models.CurrencyField() change_3 = models.CurrencyField() final_end = models.CurrencyField() change_4 = models.CurrencyField() all_change = models.CurrencyField() def cash_amount_3(self): self.cash_3 = self.cash_2 + (self.sell_3 - self.buy_3) * self.stock_price_3 return self.cash_3 def stock_amount_3(self): self.stock_3 = self.stock_2 + self.buy_3 - self.sell_3 return self.stock_3 def stock_value_3(self): self.stockvalue_3 = self.stock_3 * self.stock_price_4 return self.stockvalue_3 def final_wealth_3(self): self.final_3 = self.cash_3 + self.stock_3 * self.stock_price_4 return self.final_3 def change_wealth_3(self): self.change_3 = (self.cash_3 + self.stock_3 * self.stock_price_4) - self.final_2 return self.change_3 def final_wealth_end(self): self.final_end = self.cash_3 + self.stock_3 * self.stock_price_4 return self.final_end def change_wealth_t3(self): self.change_t3 = self.final_end - (self.cash_3 + self.stock_3 * self.stock_price_3) return self.change_t3 def all_change_final(self): self.all_change = self.final_end - 2000 return self.all_change