from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, ) import itertools import decimal import random from decimal import * import requests # api_key = "Z4O9QM9M2ABU6WMK" # api_key = "RN9V69FDUG983GS5" # 'DK4QT64ZLJSGV62M' def get_exchange_rate(from_currency, to_currency, api_key): result = 1.11 alpha_url = r"https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE" final_url = alpha_url + "&from_currency=" + from_currency + "&to_currency=" + to_currency + "&apikey=" + api_key req_ob = requests.get(final_url) result = req_ob.json() result = result["Realtime Currency Exchange Rate"]['5. Exchange Rate'] result = round((float(result)), 3) return result class Constants(BaseConstants): name_in_url = "study_uu" players_per_group = None num_rounds = 1 new_template = "DCC_app/new.html" deviation_steps = 9 baseline_markup = 1.5 amount_in_USD = 36.99 negative_markup = baseline_markup - deviation_steps high_markup = baseline_markup + deviation_steps extreme_markup = high_markup + deviation_steps added_markup = 6 class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: for p in self.get_players(): p.participant.vars['treatment_assignment'] = random.choice(['nineteen', 'twenty', 'twentyone', 'twentytwo', 'twentythree', 'twentyfour']) p.treatment = p.participant.vars['treatment_assignment'] # p.participant.vars['treatment_assignment'] = random.choice(['seven', 'nine']) # p.treatment = p.participant.vars['treatment_assignment'] # p.participant.vars['treatment_assignment'] = random.choice( # ['one', 'two', 'three', 'four', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', # 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'thirteen', 'fourteen', 'fifteen', # 'sixteen', 'seventeen', 'eighteen', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', # 'eighteen']) # # # def creating_session(self): # # randomize to treatments # for player in self.get_players(): # player.color = random.choice(['blue', 'red']) # print('set player.color to', player.color) # def creating_session(self): # treatment = itertools.cycle(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve']) # for p in self.get_players(): # p.treatment = next(treatment) class Group(BaseGroup): pass class Player(BasePlayer): exchange_rate = models.StringField() def exchange_rate_new(self): bla = get_exchange_rate('EUR', 'USD', 'C91AE02Q9A01SBGO') self.participant.vars['exchange'] = get_exchange_rate('EUR', 'USD', 'C91AE02Q9A01SBGO') b = decimal.getcontext().copy() if bla > 1: b.prec = 4 elif bla < 1: b.prec = 3 a = b.create_decimal(bla) return a def rate_negative(self): b = decimal.getcontext().copy() bla = float(self.exchange_rate) / (1 + (Constants.negative_markup / 100)) if bla > 1: b.prec = 4 elif bla < 1: b.prec = 3 a = b.create_decimal(bla) return a def rate_low(self): b = decimal.getcontext().copy() bla = float(self.exchange_rate) / (1 + (Constants.baseline_markup / 100)) if bla > 1: b.prec = 4 elif bla < 1: b.prec = 3 a = b.create_decimal(bla) return a # def rate_high(self): # b = decimal.getcontext().copy() # c = decimal.getcontext().copy() # b.prec = 3 # c.prec = 4 # bla = float(self.exchange_rate) / (1 + (Constants.high_markup / 100)) # if bla < 1: # a = b.create_decimal(bla) # elif bla > 1 : # a = c.create_decimal(bla) # return a def rate_high(self): b = decimal.getcontext().copy() bla = float(self.exchange_rate) / (1 + (Constants.high_markup / 100)) if bla > 1: b.prec = 4 elif bla < 1: b.prec = 3 a = b.create_decimal(bla) return a def rate_extreme(self): b = decimal.getcontext().copy() bla = float(self.exchange_rate) / (1 + (Constants.extreme_markup / 100)) if bla > 1: b.prec = 4 elif bla < 1: b.prec = 3 a = b.create_decimal(bla) return a def rate_added(self): b = decimal.getcontext().copy() bla = float(self.exchange_rate) / (1 + (Constants.added_markup / 100)) if bla > 1: b.prec = 4 elif bla < 1: b.prec = 3 a = b.create_decimal(bla) return a def EUR_negative(self): b = decimal.getcontext().copy() b.prec = 4 bla = Constants.amount_in_USD/ (float(self.exchange_rate) / (1 + (Constants.negative_markup / 100))) a = b.create_decimal(bla) return a def EUR_low(self): b = decimal.getcontext().copy() b.prec = 4 bla = Constants.amount_in_USD / (float(self.exchange_rate) / (1 + (Constants.baseline_markup / 100))) a = b.create_decimal(bla) return a def EUR_high(self): b = decimal.getcontext().copy() b.prec = 4 bla = Constants.amount_in_USD / (float(self.exchange_rate) / (1 + (Constants.high_markup / 100))) a = b.create_decimal(bla) return a def EUR_extreme(self): b = decimal.getcontext().copy() b.prec = 4 bla = Constants.amount_in_USD / (float(self.exchange_rate) / (1 + (Constants.extreme_markup / 100))) a = b.create_decimal(bla) return a def EUR_added(self): b = decimal.getcontext().copy() b.prec = 4 bla = Constants.amount_in_USD / (float(self.exchange_rate) / (1 + (Constants.added_markup / 100))) a = b.create_decimal(bla) return a treatment = models.StringField( choices=[['one', 'one'], ['two', 'two',], ['three', 'three'], ['four', 'four'], ['five', 'five',], ['six', 'six'], ['seven', 'seven'], ['eight', 'eight',], ['nine', 'nine'], ['ten', 'ten'], ['eleven', 'eleven'], ['twelve', 'twelve'], ['thirteen','thirteen' ], ['fourteen', 'fourteen'], ['fifteen', 'fifteen'], ['sixteen', 'sixteen'], ['seventeen', 'seventeen'], ['eighteen', 'eighteen']] ) # ingestelde_rate = models.StringField( # blank= True, # initial=1.112 # ) accept = models.BooleanField( choices=[[True, "Yes"], [False, "No"]], label="Hereby I provide permission to use my anonymized data for scientific purposes", widget=widgets.RadioSelectHorizontal, ) # browser_type = models.StringField() my_hidden_input = models.StringField() DCC = models.StringField( choices=[['EUR', 'EUR'],['USD', 'USD']], doc="""This is player's currency decision""", widget=widgets.RadioSelect, blank=True, ) destination_1 = models.StringField( choices=[ ["Miami International Airport", "Miami"], ["New York John F. Kennedy International Airport", "New York"], ["Boston Logan International Airport", "Boston"], ], widget=widgets.RadioSelect, ) purchase_1 = models.StringField( choices=[ ["book", "Book"], ["T-shirt", "T-shirt"], ["Meal", "3-course meal"], ], label="", widget=widgets.RadioSelectHorizontal, ) def set_payoff(self): if self.treatment == "two" or self.treatment == "seven" or self.treatment == "ten": self.payoff = c(0.55) elif self.DCC == "EUR" and self.treatment == "one": self.payoff = c(1) elif self.DCC == "USD" and self.treatment == "one": self.payoff = c(0) elif self.DCC == "EUR": self.payoff = c(0) else: self.payoff = c(1)