oTree Forum >

adding returns from previous rounds (newbie)

#1 by tashb

Hello!

I am trying to programme a 10-round experiment whereby the invested amount has a 50% chance of success. In round 1 subjects start with an endowment of £5. If successful, the invested amount is tripled. Otherwise, the investment is lost. The return is then carried over to the next round. So for example, if you invest £2 of the £5 and the investment is successful, you would start the next round with £9.

I am having trouble carrying over the return from the previous round to the next.

I am currently getting the TypeError: Boolean value of this clause is not defined after submitting an investment amount.

from otree.api import *
import random

doc = "Risk Preferences"


class C(BaseConstants):
    NAME_IN_URL = 'risk_preferences'
    PLAYERS_PER_GROUP = None
    NUM_ROUNDS = 10
    ENDOWMENT = 5


class Subsession(BaseSubsession):
    round_number = models.IntegerField()


class Group(BaseGroup):
    pass


class Player(BasePlayer):
    cash = models.CurrencyField(initial=5)
    returns = models.CurrencyField(initial=0)
    #   cash = models.CurrencyField(initial=5)
    investment = models.CurrencyField(
        min=0, max=cash,
    )


#    def somefunction(self):
#        return self.in_round(-1).cash


# PAGES

class Introduction(Page):
    @staticmethod
    def is_displayed(player: Player):
        return round(1)  # only shown to players in round 1


class Investment(Page):
    form_model = 'player'
    form_fields = ['investment']


class Results(Page):
    form_model = 'player'


# FUNCTIONS

def returns(player):  # the return of the player's investment
    random_outcome = random.choice([0, 3]),
    player.returns = player.investment * random_outcome
    return player.returns


def payoff(player):  # the player's total payoff
    player.payoff = C.ENDOWMENT - player.investment + player.returns


def cash(player):  #otree doesn't like me using payoff for max value
    player.cash = player.payoff
    return player.cash


def retrieve_cash(player):  # trying to retrieve cash from previous round
    prev_player = player.in_round(player.round_number - 1)
    print(prev_player.cash)
    return prev_player.cash


page_sequence = [Introduction, Investment, Results]


I would really appreciate any help with this - there are not many people I can ask in my department.

#2 by Daniel_Frey

Participant variables should do the trick: https://otree.readthedocs.io/en/latest/rounds.html#participant-fields

Write a reply

Set forum username