from otree.api import *
doc = """
Your app description
"""
class C(BaseConstants):
NAME_IN_URL = 'tsumitate_teigaku'
PLAYERS_PER_GROUP = 15
NUM_ROUNDS = 10
#決算が好景気(0)不景気(1)
SETTLEMENT = [0, 1, 1, 0, 1, 0, 1, 0, 1, 1]
#決算が好景気の時のメッセージと悪景気の時のメッセージ
SETTLEMENT_MESSAGE = [
'株式市場は好景気で、株式Aの株価は+15%、株式Bの株価は+20%上昇しました',
'株式市場は不景気で、株式Aの株価は+5%、株式Bの株価は+0%上昇しました'
]
#株価を定義
STOCK_PRICE_A = [4000, 4600, 4830, 5071.5, 5832.225, 6123.83625, 7042.411688, 7394.532272, 8503.712113, 8928.897718, 9375.342604]
STOCK_PRICE_B = [4000, 4800, 4800, 4800, 5760, 5760, 6912, 6912, 8294.4, 8294.4, 8294.4]
#毎月の積立額
TSUMITATE = 30000
#株式Aの割合が少ない順に並べ替えたポートフォリオ
PORTFOLIO = [
'株式Aを0%、株式Bを100%の割合で取得する',
'株式Aを25%、株式Bを75%の割合で取得する',
'株式Aを50%、株式Bを50%の割合で取得する',
'株式Aを75%、株式Bを25%の割合で取得する',
'株式Aを100%、株式Bを0%の割合で取得する'
]
#選択肢を表示するためのリスト
CHOICE = [
'ポートフォリオ1',
'ポートフォリオ2',
'ポートフォリオ3',
'ポートフォリオ4',
'ポートフォリオ5'
]
#インストラクションのテンプレートを呼び出し
INSTRUCTIONS_TEMPLATE = 'tsumitate_teigaku/instructions.html'
#最終ラウンドまでは含み益、最終ラウンドで最終的な利益と表現されるようにする
GAIN = [
'含み益は、',
'最終的な利益は、'
]
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
income_stock_choice = models.IntegerField(
choices=[
[4, C.CHOICE[0]],
[3, C.CHOICE[1]],
[2, C.CHOICE[2]],
[1, C.CHOICE[3]],
[0, C.CHOICE[4]]
],
verbose_name='あなたが取得する株式のポートフォリオ番号を選んでください。',
widget=widgets.RadioSelect
)
def set_payoff(player:Player):
number_A = 0
number_B = 0
#株価を求め、定額分の株式数を計算し、その後現在の株価*保有株式数から取得にかかったコスト(定額*ラウンド数)を引くことでコストを求める
#トータルの株式数*株価の計算
for i in range(player.round_number):
#ラウンドiでのプレイヤーを定義
prev_player = player.in_round(i+1)
#現在の株価を求める
stock_price_A = C.STOCK_PRICE_A[prev_player.round_number - 1]
stock_price_B = C.STOCK_PRICE_B[prev_player.round_number - 1]
#ラウンドiでプレイヤーが保有している株式数を計算
number_A = number_A + C.TSUMITATE * prev_player.income_stock_choice * 0.25 / stock_price_A
number_B = number_B + C.TSUMITATE * (4 - prev_player.income_stock_choice) * 0.25 / stock_price_B
#株式Aの利得
income_A = number_A * C.STOCK_PRICE_A[player.round_number]
income_B = number_B * C.STOCK_PRICE_B[player.round_number]
income_total = income_A + income_B - C.TSUMITATE * player.round_number
player.payoff = income_total
def set_payoffs(group: Group):
for p in group.get_players():
set_payoff(p)
# PAGES
class MyPage(Page):
pass
class Introduction(Page):
def is_displayed(self):
return self.round_number == 1
class ResultsWaitPage(WaitPage):
after_all_players_arrive = set_payoffs
class DecisionInvestment(Page):
timeout_seconds = 60
form_model = 'player'
form_fields = [
'income_stock_choice'
]
@staticmethod # 誰か使い方を教えて下さい
def vars_for_template(player: Player):
return dict(
tsumitate = C.TSUMITATE,
choiced_0 = C.PORTFOLIO[4],
choiced_1 = C.PORTFOLIO[3],
choiced_2 = C.PORTFOLIO[2],
choiced_3 = C.PORTFOLIO[1],
choiced_4 = C.PORTFOLIO[0]
)
class Results(Page):
timeout_seconds = 30
@staticmethod # 誰か使い方を教えて下さい
def vars_for_template(player: Player):
if player.round_number == 10:
return dict(
gain = C.GAIN[1],
my_decision = player.field_display('income_stock_choice'),
market_g_b = C.SETTLEMENT_MESSAGE[C.SETTLEMENT[player.round_number - 1]],
year = player.round_number,
choiced_0 = C.PORTFOLIO[4],
choiced_1 = C.PORTFOLIO[3],
choiced_2 = C.PORTFOLIO[2],
choiced_3 = C.PORTFOLIO[1],
choiced_4 = C.PORTFOLIO[0]
)
else:
return dict(
gain = C.GAIN[0],
my_decision = player.field_display('income_stock_choice'),
market_g_b = C.SETTLEMENT_MESSAGE[C.SETTLEMENT[player.round_number - 1]],
year = player.round_number,
choiced_0 = C.PORTFOLIO[4],
choiced_1 = C.PORTFOLIO[3],
choiced_2 = C.PORTFOLIO[2],
choiced_3 = C.PORTFOLIO[1],
choiced_4 = C.PORTFOLIO[0]
)
page_sequence = [Introduction, DecisionInvestment, ResultsWaitPage, Results]