from otree.api import * import random c = cu doc = '' class C(BaseConstants): NAME_IN_URL = 'secondexperiment' PLAYERS_PER_GROUP = None NUM_ROUNDS = 6 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): question_displayed_this_round = models.IntegerField() leverage1 = models.StringField(choices=[['1:1', '1:1'], ['2:1', '2:1'], ['3:1', '3:1'], ['4:1', '4:1'], ['5:1', '5:1'], ['6:1', '6:1'], ['7:1', '7:1'], ['8:1', '8:1'], ['9:1', '9:1'], ['10:1', '10:1'], ['15:1', '15:1'], ['20:1', '20:1']], label='Please specify the leverage you want to apply to your investment. ') leverage2 = models.StringField(choices=[['1:1', '1:1'], ['2:1', '2:1'], ['3:1', '3:1'], ['4:1', '4:1'], ['5:1', '5:1'], ['6:1', '6:1'], ['7:1', '7:1'], ['8:1', '8:1'], ['9:1', '9:1'], ['10:1', '10:1'], ['15:1', '15:1'], ['20:1', '20:1']], label='Please specify the leverage you want to apply to your investment. ') leverage3 = models.StringField(choices=[['1:1', '1:1'], ['2:1', '2:1'], ['3:1', '3:1'], ['4:1', '4:1'], ['5:1', '5:1'], ['6:1', '6:1'], ['7:1', '7:1'], ['8:1', '8:1'], ['9:1', '9:1'], ['10:1', '10:1'], ['15:1', '15:1'], ['20:1', '20:1']], label='Please specify the leverage you want to apply to your investment. ') #stock1 = models.StringField(widget=widgets.RadioSelect, choices=["Stock 1", "Stock 2", "Stock 3", # "Stock 4", "Stock 5", "Stock 6", "Stock 7", # "Stock 8", "Stock 9"]) #stock2 = models.StringField(widget=widgets.RadioSelect, choices=["Stock 1", "Stock 2", "Stock 3", "Stock 4", # "Stock 5", "Stock 6", "Stock 7", "Stock 8", # "Stock 9"]) #stock3 = models.StringField(widget=widgets.RadioSelect, choices=["Stock 1", "Stock 2", "Stock 3", "Stock 4", # "Stock 5", "Stock 6", "Stock 7", "Stock 8", "Stock 9"]) stock1 = models.StringField() stock2 = models.StringField() stock3 = models.StringField() #Adjustment risk perception start Riskperception1 = models.StringField(choices=[['1 (not risky at all)', '1 (not risky at all)'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'], ['7 (very risky)', '7 (very risky)']], label='How risky do you perceive this investment to be?', widget=widgets.RadioSelectHorizontal) Riskperception2 = models.StringField(choices=[['1 (not risky at all)', '1 (not risky at all)'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'], ['7 (very risky)', '7 (very risky)']], label='How risky do you perceive this investment to be?', widget=widgets.RadioSelectHorizontal) Riskperception3 = models.StringField(choices=[['1 (not risky at all)', '1 (not risky at all)'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'], ['7 (very risky)', '7 (very risky)']], label='How risky do you perceive this investment to be?', widget=widgets.RadioSelectHorizontal) #Adjustment risk perception end global no_of_question_displayed, is_leverage_set, session_details, session_history import os os.environ["OTREE_PRODUCTION"] = "1" def creating_session(subsession: Subsession): import random global no_of_question_displayed, session_details, session_history no_of_question_displayed = set() session_details = () session_history = dict() if subsession.round_number == 1: list_of_question_ids = [(1,2), (3,4), (5,6)] for player in subsession.get_players(): temp_list = list_of_question_ids.copy() random.shuffle(temp_list) print(f"Question list: " + str(temp_list)) temp_list = list(sum(temp_list, ())) player.in_round(1).question_displayed_this_round = temp_list.pop(0) player.in_round(2).question_displayed_this_round = temp_list.pop(0) player.in_round(3).question_displayed_this_round = temp_list.pop(0) player.in_round(4).question_displayed_this_round = temp_list.pop(0) player.in_round(5).question_displayed_this_round = temp_list.pop(0) player.in_round(6).question_displayed_this_round = temp_list.pop(0) def question_displayed_in_session(question_no: int, player: Player): global no_of_question_displayed, is_leverage_set, session_details, session_history session_in_question = (player.session.id, player.id_in_group, player.participant.id) if session_details == session_in_question: print(f"session remains the same {session_details}") no_of_question_displayed.add(question_no) else: print(f"session new or changed from {session_details} to {session_in_question}. UPDATING!") print("===================================================================================") if len(no_of_question_displayed) < 6 and no_of_question_displayed: print("Recording IE session as the experiment is not completed") session_history[session_details] = no_of_question_displayed.copy() print(f"Session History: {session_history}") session_details = session_in_question if session_details in session_history: print("Session found in history so question are continued from where they left") print(f"Session History: {session_history}") no_of_question_displayed = session_history[session_details].copy() print(f"Question Display set {no_of_question_displayed}") else: no_of_question_displayed.clear() is_leverage_set = False no_of_question_displayed.add(question_no) print(f"leverage-set: {is_leverage_set}") print(f"question_no_displayed: {question_no}") print(f"no_of_questions_displayed_so_far: {len(no_of_question_displayed)}") def make_image_data_ds1(): image_num=list(range(1,10)) random.shuffle(image_num) print(f"Randomly chosen image numbers: {image_num}") return [dict(name=f'Stock {str(num)}', path=f'Payoff_distributions_2ndexperiment/hist_dist_p{str(num)}a.png') for num in image_num] def make_image_data_ds2(): image_num=list(range(1,10)) random.shuffle(image_num) print(f"Randomly chosen image numbers: {image_num}") return [dict(name=f'Stock {str(num)}', path=f'Payoff_distributions_2ndexperiment/hist_dist_p{str(num)}b.png') for num in image_num] def make_image_data_ds3(): image_num=list(range(1,10)) random.shuffle(image_num) print(f"Randomly chosen image numbers: {image_num}") return [dict(name=f'Stock {str(num)}', path=f'Payoff_distributions_2ndexperiment/hist_dist_p{str(num)}c.png') for num in image_num] class Decisionstock1(Page): form_model = 'player' form_fields = ['stock1'] @staticmethod def vars_for_template(player: Player): return dict(image_data=make_image_data_ds1()) @staticmethod def is_displayed(player: Player): if player.question_displayed_this_round == 1: question_displayed_in_session(1, player) return True @staticmethod def before_next_page(player, timeout_happened): print(f"Result for Decisionstock1__: {player.stock1}") result = player.stock1[-1] player.participant.stock_selected = result player.participant.stock_selected_image = f"Payoff_distributions_2ndexperiment/hist_dist_p{result}a.png" class Decisionleverage1(Page): form_model = 'player' form_fields = ['Riskperception1'] #'leverage1'raus @staticmethod def is_displayed(player: Player): if player.question_displayed_this_round == 2: question_displayed_in_session(2, player) return True class Decisionstock2(Page): form_model = 'player' form_fields = ['stock2'] @staticmethod def vars_for_template(player: Player): return dict(image_data=make_image_data_ds2()) @staticmethod def is_displayed(player: Player): if player.question_displayed_this_round == 3: question_displayed_in_session(3, player) return True @staticmethod def before_next_page(player, timeout_happened): print(f"Result for Decisionstock2__: {player.stock2}") result = player.stock2[-1] player.participant.stock_selected = result player.participant.stock_selected_image = f"Payoff_distributions_2ndexperiment/hist_dist_p{result}b.png" class Decisionleverage2(Page): form_model = 'player' form_fields = ['Riskperception2'] #'leverage2' raus @staticmethod def is_displayed(player: Player): if player.question_displayed_this_round == 4: question_displayed_in_session(4, player) return True class Decisionstock3(Page): form_model = 'player' form_fields = ['stock3'] @staticmethod def vars_for_template(player: Player): return dict(image_data=make_image_data_ds3()) @staticmethod def is_displayed(player: Player): if player.question_displayed_this_round == 5: question_displayed_in_session(5, player) return True @staticmethod def before_next_page(player, timeout_happened): print(f"Result for Decisionstock3__: {player.stock3}") result = player.stock3[-1] player.participant.stock_selected = result player.participant.stock_selected_image = f"Payoff_distributions_2ndexperiment/hist_dist_p{result}c.png" class Decisionleverage3(Page): form_model = 'player' form_fields = ['Riskperception3'] #'leverage3' raus @staticmethod def is_displayed(player: Player): if player.question_displayed_this_round == 6: question_displayed_in_session(6, player) return True class Results(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): from random import randrange global no_of_question_displayed, is_leverage_set participant = player.participant print(f"Leverage: {is_leverage_set}") if len(no_of_question_displayed) == C.NUM_ROUNDS: if not is_leverage_set: print("Calculating Results") is_leverage_set = True random_stock_option = randrange(1, 4) if random_stock_option == 1: stock = "stock1" #leverage = "leverage1" elif random_stock_option == 2: stock = "stock2" #leverage = "leverage2" elif random_stock_option == 3: stock = "stock3" #leverage = "leverage3" # result_leverage = None result_stock = None for round_no in range(1, 7): # if result_leverage is None: # print(f"Round Number {round_no} leverage - " # f"{player.in_round(round_no).field_maybe_none(leverage)} ") # result_leverage = player.in_round(round_no).field_maybe_none(leverage) if result_stock is None: print(f"Round Number {round_no} stock - " f"{player.in_round(round_no).field_maybe_none(stock)} ") result_stock = player.in_round(round_no).field_maybe_none(stock) print(f"Randomly chosen stock: {random_stock_option}") print(f"User chosen result stock: {result_stock}") # print(f"User chosen result leverage: {result_leverage}") import csv chosen_observation = None with open('_static/IE_returns/all_returns_2nd.csv', 'r') as file: my_reader = csv.reader(file, delimiter=',') random_distribution = randrange(1, 201) random_observation = int(result_stock[-1]) + ((random_stock_option-1) * 9) print(f"random distribution {random_distribution}") print(f"random investment_option {random_observation}") for row in my_reader: row_no = int(row[0]) if row[0].isdigit() else None if row_no == random_distribution: print(f"Choosen Row {row}") chosen_observation = float(row[random_observation]) break print(f"Chosen Obs: {chosen_observation}") chosen_observation_percentage = str(round(float(chosen_observation * 100),2)) + "%" print(f"Chosen Obs Percentage: {chosen_observation_percentage}") # leverage_factor = result_leverage.split(':', 1)[0] # print("Leverage factor:"+leverage_factor) # payoff_calculated = 3 * (1 + (float(leverage_factor) * chosen_observation)) payoff_calculated = 3 * (1 + chosen_observation) payoff_final = str(round(payoff_calculated, 2) if round(payoff_calculated, 2) > 1 else 1) print("Payoff Calculated:" + payoff_final) # payoff value for the player(DATA TAB) player.payoff = payoff_final participant.t_payoff_value = payoff_final participant.t_leverage = result_leverage = "None" participant.t_stock = random_observation participant.t_random_decision_stock = random_stock_option participant.t_return_investment = chosen_observation_percentage return False page_sequence = [Decisionstock1, Decisionleverage1, Decisionstock2, Decisionleverage2, Decisionstock3, Decisionleverage3, Results]