from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import itertools import os import decimal from decimal import * import requests, json def get_exchange_rate(from_currency, to_currency, api_key): 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_1" players_per_group = None num_rounds = 15 new_template = "DCC_app/new.html" deviation_steps = 9 baseline_markup = 1.5 USD_EUR_baseline_rate = 1.112 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): pass # 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): def get_exchange_rate(from_currency, to_currency, api_key): 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 def rate_negative(self): b = decimal.getcontext().copy() bla = float(self.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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() bla = float(self.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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.participant.vars['exchange']) / (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']] ) accept = models.BooleanField( choices=[[True, "Yes"], [False, "No"]], label="Hereby I provide permission to use my anonymized data for scientific purposes", widget=widgets.RadioSelectHorizontal, ) 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, ) confirm = models.StringField( blank=True ) # player_in_previous = self.player.in_previous_rounds() # previous_payoff = sum([p.payoff for p in player_in_previous]) # player_next = self.player.in_round(self.round_number + 1) # def set_payoff(self): # now = self.in_round(self.round_number) # if self.my_hidden_input == "clicked:/": # if self.confirm == "confirm" and (self.treatment == "two" or self.treatment == "seven" or self.treatment == "ten"): # now.payoff = c(0.55) # elif self.DCC == "EUR" and self.treatment == "one": # now.payoff= c(1) # elif self.DCC == "USD" and self.treatment == "one": # now.payoff = c(0) # elif self.DCC == "EUR" and (self.treatment == "three" or self.treatment == "four" or self.treatment == "five" or self.treatment == "six" or self.treatment == "eight" or self.treatment == "nine" or self.treatment == "eleven" or self.treatment == "twelve"): # now.payoff = c(0) # elif self.DCC == "USD" and (self.treatment == "three" or self.treatment == "four" or self.treatment == "five" or self.treatment == "six" or self.treatment == "eight" or self.treatment == "nine" or self.treatment == "eleven" or self.treatment == "twelve"): # now.payoff = c(1) def set_payoff(self): now = self.in_round(self.round_number) if self.my_hidden_input == "clicked:/": if self.confirm == "confirm" and self.participant.vars['treatment_assignment'] == "ten": now.payoff = c(0.55) elif self.DCC == "EUR" and self.confirm == "confirm" and ( self.participant.vars['treatment_assignment'] == "eleven" or self.participant.vars['treatment_assignment'] == "twelve" or self.participant.vars['treatment_assignment'] == "fifteen"): now.payoff = c(0) elif self.DCC == "USD" and self.confirm == "confirm" and (self.participant.vars['treatment_assignment'] == "eleven" or self.participant.vars['treatment_assignment'] == "twelve" or self.participant.vars['treatment_assignment'] == "fifteen"): now.payoff = c(1) # elif self.DCC == "EUR" and self.confirm == "confirm" and (self.treatment == "eleven" or self.treatment == "twelve"): # now.payoff = c(0) # elif self.DCC == "USD" and self.confirm == "confirm" and (self.treatment == "eleven" or self.treatment == "twelve"): # now.payoff = c(1)