from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class Introduction(Page): form_model = 'player' def before_next_page(self): self.player.draw_type() self.player.draw_changes_T1() self.player.draw_changes_T2() self.player.createStockPriceDictT1() self.player.createStockPriceDictT2() class Consent(Page): form_model = 'player' form_fields = ['Consent'] def error_message(self, values): print('values is', values) if values['Consent'] == 2: return 'You can only participate in this study if you give your consent.' class Instructions_general(Page): form_model = 'player' class Instructions_invest1_1(Page): form_model = 'player' class Instructions_invest1_2(Page): form_model = 'player' class Instructions_invest1_3(Page): form_model = 'player' class Instructions_questions(Page): form_model = 'player' class Instructions_invest2_1(Page): form_model = 'player' class Instructions_invest2_2(Page): form_model = 'player' class Instructions_invest2_2b(Page): form_model = 'player' class Instructions_invest2_3(Page): form_model = 'player' class Instructions_payment(Page): form_model = 'player' class StartTask1(Page): form_model = 'player' class StartTask1_2(Page): form_model = 'player' ### TRIAL ONE class Period0(Page): form_model = 'player' form_fields = ['Select_stock1', 'Select_stock2', 'Select_stock3', 'Select_stock4', 'Select_stock5', 'Select_stock6', 'Select_stock7', 'Select_stock8', 'Select_stock9', 'Select_stock10', 'Select_stock11', 'Select_stock12', 'Select_stock13', 'Select_stock14', 'Select_stock15', 'Select_stock16', 'Select_stock17', 'Select_stock18', 'Select_stock19', 'Select_stock20'] def error_message(self, values): print('values is', values) listSelectedStocks = [values['Select_stock1'], values['Select_stock2'], values['Select_stock3'], values['Select_stock4'], values['Select_stock5'], values['Select_stock6'], values['Select_stock7'], values['Select_stock8'], values['Select_stock9'], values['Select_stock10'], values['Select_stock11'], values['Select_stock12'], values['Select_stock13'], values['Select_stock14'], values['Select_stock15'], values['Select_stock16'], values['Select_stock17'], values['Select_stock18'], values['Select_stock19'], values['Select_stock20']] if listSelectedStocks.count(None) < 15: return 'You can only choose 5 stocks.' elif listSelectedStocks.count(None) > 15: return 'You have to choose 5 stocks.' def vars_for_template(self): stock1_p1_1 = self.participant.vars["priceChangeDict"].get('Stock1_p1_1') stock1_p2_1 = self.participant.vars["priceChangeDict"].get('Stock1_p2_1') stock1_p3_1 = self.participant.vars["priceChangeDict"].get('Stock1_p3_1') stock2_p1_1 = self.participant.vars["priceChangeDict"].get('Stock2_p1_1') stock2_p2_1 = self.participant.vars["priceChangeDict"].get('Stock2_p2_1') stock2_p3_1 = self.participant.vars["priceChangeDict"].get('Stock2_p3_1') stock3_p1_1 = self.participant.vars["priceChangeDict"].get('Stock3_p1_1') stock3_p2_1 = self.participant.vars["priceChangeDict"].get('Stock3_p2_1') stock3_p3_1 = self.participant.vars["priceChangeDict"].get('Stock3_p3_1') stock4_p1_1 = self.participant.vars["priceChangeDict"].get('Stock4_p1_1') stock4_p2_1 = self.participant.vars["priceChangeDict"].get('Stock4_p2_1') stock4_p3_1 = self.participant.vars["priceChangeDict"].get('Stock4_p3_1') stock5_p1_1 = self.participant.vars["priceChangeDict"].get('Stock5_p1_1') stock5_p2_1 = self.participant.vars["priceChangeDict"].get('Stock5_p2_1') stock5_p3_1 = self.participant.vars["priceChangeDict"].get('Stock5_p3_1') stock6_p1_1 = self.participant.vars["priceChangeDict"].get('Stock6_p1_1') stock6_p2_1 = self.participant.vars["priceChangeDict"].get('Stock6_p2_1') stock6_p3_1 = self.participant.vars["priceChangeDict"].get('Stock6_p3_1') stock7_p1_1 = self.participant.vars["priceChangeDict"].get('Stock7_p1_1') stock7_p2_1 = self.participant.vars["priceChangeDict"].get('Stock7_p2_1') stock7_p3_1 = self.participant.vars["priceChangeDict"].get('Stock7_p3_1') stock8_p1_1 = self.participant.vars["priceChangeDict"].get('Stock8_p1_1') stock8_p2_1 = self.participant.vars["priceChangeDict"].get('Stock8_p2_1') stock8_p3_1 = self.participant.vars["priceChangeDict"].get('Stock8_p3_1') stock9_p1_1 = self.participant.vars["priceChangeDict"].get('Stock9_p1_1') stock9_p2_1 = self.participant.vars["priceChangeDict"].get('Stock9_p2_1') stock9_p3_1 = self.participant.vars["priceChangeDict"].get('Stock9_p3_1') stock10_p1_1 = self.participant.vars["priceChangeDict"].get('Stock10_p1_1') stock10_p2_1 = self.participant.vars["priceChangeDict"].get('Stock10_p2_1') stock10_p3_1 = self.participant.vars["priceChangeDict"].get('Stock10_p3_1') stock11_p1_1 = self.participant.vars["priceChangeDict"].get('Stock11_p1_1') stock11_p2_1 = self.participant.vars["priceChangeDict"].get('Stock11_p2_1') stock11_p3_1 = self.participant.vars["priceChangeDict"].get('Stock11_p3_1') stock12_p1_1 = self.participant.vars["priceChangeDict"].get('Stock12_p1_1') stock12_p2_1 = self.participant.vars["priceChangeDict"].get('Stock12_p2_1') stock12_p3_1 = self.participant.vars["priceChangeDict"].get('Stock12_p3_1') stock13_p1_1 = self.participant.vars["priceChangeDict"].get('Stock13_p1_1') stock13_p2_1 = self.participant.vars["priceChangeDict"].get('Stock13_p2_1') stock13_p3_1 = self.participant.vars["priceChangeDict"].get('Stock13_p3_1') stock14_p1_1 = self.participant.vars["priceChangeDict"].get('Stock14_p1_1') stock14_p2_1 = self.participant.vars["priceChangeDict"].get('Stock14_p2_1') stock14_p3_1 = self.participant.vars["priceChangeDict"].get('Stock14_p3_1') stock15_p1_1 = self.participant.vars["priceChangeDict"].get('Stock15_p1_1') stock15_p2_1 = self.participant.vars["priceChangeDict"].get('Stock15_p2_1') stock15_p3_1 = self.participant.vars["priceChangeDict"].get('Stock15_p3_1') stock16_p1_1 = self.participant.vars["priceChangeDict"].get('Stock16_p1_1') stock16_p2_1 = self.participant.vars["priceChangeDict"].get('Stock16_p2_1') stock16_p3_1 = self.participant.vars["priceChangeDict"].get('Stock16_p3_1') stock17_p1_1 = self.participant.vars["priceChangeDict"].get('Stock17_p1_1') stock17_p2_1 = self.participant.vars["priceChangeDict"].get('Stock17_p2_1') stock17_p3_1 = self.participant.vars["priceChangeDict"].get('Stock17_p3_1') stock18_p1_1 = self.participant.vars["priceChangeDict"].get('Stock18_p1_1') stock18_p2_1 = self.participant.vars["priceChangeDict"].get('Stock18_p2_1') stock18_p3_1 = self.participant.vars["priceChangeDict"].get('Stock18_p3_1') stock19_p1_1 = self.participant.vars["priceChangeDict"].get('Stock19_p1_1') stock19_p2_1 = self.participant.vars["priceChangeDict"].get('Stock19_p2_1') stock19_p3_1 = self.participant.vars["priceChangeDict"].get('Stock19_p3_1') stock20_p1_1 = self.participant.vars["priceChangeDict"].get('Stock20_p1_1') stock20_p2_1 = self.participant.vars["priceChangeDict"].get('Stock20_p2_1') stock20_p3_1 = self.participant.vars["priceChangeDict"].get('Stock20_p3_1') return dict( Stock1_p1 = 30 - stock1_p1_1, Stock1_p2 = 30 - stock1_p1_1 - stock1_p2_1, Stock1_p3 = 30 - stock1_p1_1 - stock1_p2_1 - stock1_p3_1, Stock2_p1 = 30 - stock2_p1_1, Stock2_p2 = 30 - stock2_p1_1 - stock2_p2_1, Stock2_p3 = 30 - stock2_p1_1 - stock2_p2_1 - stock2_p3_1, Stock3_p1 = 30 - stock3_p1_1, Stock3_p2 = 30 - stock3_p1_1 - stock3_p2_1, Stock3_p3 = 30 - stock3_p1_1 - stock3_p2_1 - stock3_p3_1, Stock4_p1 = 30 - stock4_p1_1, Stock4_p2 = 30 - stock4_p1_1 - stock4_p2_1, Stock4_p3 = 30 - stock4_p1_1 - stock4_p2_1 - stock4_p3_1, Stock5_p1 = 30 - stock5_p1_1, Stock5_p2 = 30 - stock5_p1_1 - stock5_p2_1, Stock5_p3 = 30 - stock5_p1_1 - stock5_p2_1 - stock5_p3_1, Stock6_p1 = 30 - stock6_p1_1, Stock6_p2 = 30 - stock6_p1_1 - stock6_p2_1, Stock6_p3 = 30 - stock6_p1_1 - stock6_p2_1 - stock6_p3_1, Stock7_p1 = 30 - stock7_p1_1, Stock7_p2 = 30 - stock7_p1_1 - stock7_p2_1, Stock7_p3 = 30 - stock7_p1_1 - stock7_p2_1 - stock7_p3_1, Stock8_p1 = 30 - stock8_p1_1, Stock8_p2 = 30 - stock8_p1_1 - stock8_p2_1, Stock8_p3 = 30 - stock8_p1_1 - stock8_p2_1 - stock8_p3_1, Stock9_p1 = 30 - stock9_p1_1, Stock9_p2 = 30 - stock9_p1_1 - stock9_p2_1, Stock9_p3 = 30 - stock9_p1_1 - stock9_p2_1 - stock9_p3_1, Stock10_p1 = 30 - stock10_p1_1, Stock10_p2 = 30 - stock10_p1_1 - stock10_p2_1, Stock10_p3 = 30 - stock10_p1_1 - stock10_p2_1 - stock10_p3_1, Stock11_p1 = 30 - stock11_p1_1, Stock11_p2 = 30 - stock11_p1_1 - stock11_p2_1, Stock11_p3 = 30 - stock11_p1_1 - stock11_p2_1 - stock11_p3_1, Stock12_p1 = 30 - stock12_p1_1, Stock12_p2 = 30 - stock12_p1_1 - stock12_p2_1, Stock12_p3 = 30 - stock12_p1_1 - stock12_p2_1 - stock12_p3_1, Stock13_p1 = 30 - stock13_p1_1, Stock13_p2 = 30 - stock13_p1_1 - stock13_p2_1, Stock13_p3 = 30 - stock13_p1_1 - stock13_p2_1 - stock13_p3_1, Stock14_p1 = 30 - stock14_p1_1, Stock14_p2 = 30 - stock14_p1_1 - stock14_p2_1, Stock14_p3 = 30 - stock14_p1_1 - stock14_p2_1 - stock14_p3_1, Stock15_p1 = 30 - stock15_p1_1, Stock15_p2 = 30 - stock15_p1_1 - stock15_p2_1, Stock15_p3 = 30 - stock15_p1_1 - stock15_p2_1 - stock15_p3_1, Stock16_p1 = 30 - stock16_p1_1, Stock16_p2 = 30 - stock16_p1_1 - stock16_p2_1, Stock16_p3 = 30 - stock16_p1_1 - stock16_p2_1 - stock16_p3_1, Stock17_p1 = 30 - stock17_p1_1, Stock17_p2 = 30 - stock17_p1_1 - stock17_p2_1, Stock17_p3 = 30 - stock17_p1_1 - stock17_p2_1 - stock17_p3_1, Stock18_p1 = 30 - stock18_p1_1, Stock18_p2 = 30 - stock18_p1_1 - stock18_p2_1, Stock18_p3 = 30 - stock18_p1_1 - stock18_p2_1 - stock18_p3_1, Stock19_p1 = 30 - stock19_p1_1, Stock19_p2 = 30 - stock19_p1_1 - stock19_p2_1, Stock19_p3 = 30 - stock19_p1_1 - stock19_p2_1 - stock19_p3_1, Stock20_p1 = 30 - stock20_p1_1, Stock20_p2 = 30 - stock20_p1_1 - stock20_p2_1, Stock20_p3 = 30 - stock20_p1_1 - stock20_p2_1 - stock20_p3_1, ) #calculate period 1 stock price def before_next_page(self): self.player.period1Price_stock1 = 30 + self.participant.vars["priceChangeDict"].get('Stock1_p4_1') self.player.period1Price_stock2 = 30 + self.participant.vars["priceChangeDict"].get('Stock2_p4_1') self.player.period1Price_stock3 = 30 + self.participant.vars["priceChangeDict"].get('Stock3_p4_1') self.player.period1Price_stock4 = 30 + self.participant.vars["priceChangeDict"].get('Stock4_p4_1') self.player.period1Price_stock5 = 30 + self.participant.vars["priceChangeDict"].get('Stock5_p4_1') self.player.period1Price_stock6 = 30 + self.participant.vars["priceChangeDict"].get('Stock6_p4_1') self.player.period1Price_stock7 = 30 + self.participant.vars["priceChangeDict"].get('Stock7_p4_1') self.player.period1Price_stock8 = 30 + self.participant.vars["priceChangeDict"].get('Stock8_p4_1') self.player.period1Price_stock9 = 30 + self.participant.vars["priceChangeDict"].get('Stock9_p4_1') self.player.period1Price_stock10 = 30 + self.participant.vars["priceChangeDict"].get('Stock10_p4_1') self.player.period1Price_stock11 = 30 + self.participant.vars["priceChangeDict"].get('Stock11_p4_1') self.player.period1Price_stock12 = 30 + self.participant.vars["priceChangeDict"].get('Stock12_p4_1') self.player.period1Price_stock13 = 30 + self.participant.vars["priceChangeDict"].get('Stock13_p4_1') self.player.period1Price_stock14 = 30 + self.participant.vars["priceChangeDict"].get('Stock14_p4_1') self.player.period1Price_stock15 = 30 + self.participant.vars["priceChangeDict"].get('Stock15_p4_1') self.player.period1Price_stock16 = 30 + self.participant.vars["priceChangeDict"].get('Stock16_p4_1') self.player.period1Price_stock17 = 30 + self.participant.vars["priceChangeDict"].get('Stock17_p4_1') self.player.period1Price_stock18 = 30 + self.participant.vars["priceChangeDict"].get('Stock18_p4_1') self.player.period1Price_stock19 = 30 + self.participant.vars["priceChangeDict"].get('Stock19_p4_1') self.player.period1Price_stock20 = 30 + self.participant.vars["priceChangeDict"].get('Stock20_p4_1') self.createInitialPortfolio() #create player's portfolio def createInitialPortfolio(self): self.participant.vars['Portfolio'] = [] if self.player.Select_stock1 == 'on': self.participant.vars['Portfolio'].append('Stock1') if self.player.Select_stock2 == 'on': self.participant.vars['Portfolio'].append('Stock2') if self.player.Select_stock3 == 'on': self.participant.vars['Portfolio'].append('Stock3') if self.player.Select_stock4 == 'on': self.participant.vars['Portfolio'].append('Stock4') if self.player.Select_stock5 == 'on': self.participant.vars['Portfolio'].append('Stock5') if self.player.Select_stock6 == 'on': self.participant.vars['Portfolio'].append('Stock6') if self.player.Select_stock7 == 'on': self.participant.vars['Portfolio'].append('Stock7') if self.player.Select_stock8 == 'on': self.participant.vars['Portfolio'].append('Stock8') if self.player.Select_stock9 == 'on': self.participant.vars['Portfolio'].append('Stock9') if self.player.Select_stock10 == 'on': self.participant.vars['Portfolio'].append('Stock10') if self.player.Select_stock11 == 'on': self.participant.vars['Portfolio'].append('Stock11') if self.player.Select_stock12 == 'on': self.participant.vars['Portfolio'].append('Stock12') if self.player.Select_stock13 == 'on': self.participant.vars['Portfolio'].append('Stock13') if self.player.Select_stock14 == 'on': self.participant.vars['Portfolio'].append('Stock14') if self.player.Select_stock15 == 'on': self.participant.vars['Portfolio'].append('Stock15') if self.player.Select_stock16 == 'on': self.participant.vars['Portfolio'].append('Stock16') if self.player.Select_stock17 == 'on': self.participant.vars['Portfolio'].append('Stock17') if self.player.Select_stock18 == 'on': self.participant.vars['Portfolio'].append('Stock18') if self.player.Select_stock19 == 'on': self.participant.vars['Portfolio'].append('Stock19') if self.player.Select_stock20 == 'on': self.participant.vars['Portfolio'].append('Stock20') ###Show period 1 class Period1_show(Page): form_model = 'player' currentPeriod = 1 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period 1 price per row def showPrice(self): self.player.period1Price_row1 = 30 + self.player.period1PriceChange_row1 self.player.period1Price_row2 = 30 + self.player.period1PriceChange_row2 self.player.period1Price_row3 = 30 + self.player.period1PriceChange_row3 self.player.period1Price_row4 = 30 + self.player.period1PriceChange_row4 self.player.period1Price_row5 = 30 + self.player.period1PriceChange_row5 #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.showPriceChangeForStockName_T1(currentStock,self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold1 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold1 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.showPriceChangeForStockName_T1(currentStock,self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold1 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold1 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold1 = self.player.sellingPrice_T1(self.player.stockSold1, 1) #variables for template def vars_for_template(self): self.showStocks() self.player.period1PriceChange_row1 = self.player.showPriceChangeForStockName_T1(self.player.stockRow1,self.currentPeriod) self.player.period1PriceChange_row2 = self.player.showPriceChangeForStockName_T1(self.player.stockRow2,self.currentPeriod) self.player.period1PriceChange_row3 = self.player.showPriceChangeForStockName_T1(self.player.stockRow3,self.currentPeriod) self.player.period1PriceChange_row4 = self.player.showPriceChangeForStockName_T1(self.player.stockRow4,self.currentPeriod) self.player.period1PriceChange_row5 = self.player.showPriceChangeForStockName_T1(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod1Price_row1 = self.player.period1Price_row1 - self.player.period1PriceChange_row1 self.player.preperiod1Price_row2 = self.player.period1Price_row2 - self.player.period1PriceChange_row2 self.player.preperiod1Price_row3 = self.player.period1Price_row3 - self.player.period1PriceChange_row3 self.player.preperiod1Price_row4 = self.player.period1Price_row4 - self.player.period1PriceChange_row4 self.player.preperiod1Price_row5 = self.player.period1Price_row5 - self.player.period1PriceChange_row5 stock21_p1_1 = self.participant.vars["priceChangeDict"].get('Stock21_p1_1') stock21_p2_1 = self.participant.vars["priceChangeDict"].get('Stock21_p2_1') stock21_p3_1 = self.participant.vars["priceChangeDict"].get('Stock21_p3_1') stock22_p1_1 = self.participant.vars["priceChangeDict"].get('Stock22_p1_1') stock22_p2_1 = self.participant.vars["priceChangeDict"].get('Stock22_p2_1') stock22_p3_1 = self.participant.vars["priceChangeDict"].get('Stock22_p3_1') stock23_p1_1 = self.participant.vars["priceChangeDict"].get('Stock23_p1_1') stock23_p2_1 = self.participant.vars["priceChangeDict"].get('Stock23_p2_1') stock23_p3_1 = self.participant.vars["priceChangeDict"].get('Stock23_p3_1') stock24_p1_1 = self.participant.vars["priceChangeDict"].get('Stock24_p1_1') stock24_p2_1 = self.participant.vars["priceChangeDict"].get('Stock24_p2_1') stock24_p3_1 = self.participant.vars["priceChangeDict"].get('Stock24_p3_1') stock1_p4_1 = self.participant.vars["priceChangeDict"].get('Stock1_p4_1') stock2_p4_1 = self.participant.vars["priceChangeDict"].get('Stock2_p4_1') stock3_p4_1 = self.participant.vars["priceChangeDict"].get('Stock3_p4_1') stock4_p4_1 = self.participant.vars["priceChangeDict"].get('Stock4_p4_1') stock5_p4_1 = self.participant.vars["priceChangeDict"].get('Stock5_p4_1') stock6_p4_1 = self.participant.vars["priceChangeDict"].get('Stock6_p4_1') stock7_p4_1 = self.participant.vars["priceChangeDict"].get('Stock7_p4_1') stock8_p4_1 = self.participant.vars["priceChangeDict"].get('Stock8_p4_1') stock9_p4_1 = self.participant.vars["priceChangeDict"].get('Stock9_p4_1') stock10_p4_1 = self.participant.vars["priceChangeDict"].get('Stock10_p4_1') stock11_p4_1 = self.participant.vars["priceChangeDict"].get('Stock11_p4_1') stock12_p4_1 = self.participant.vars["priceChangeDict"].get('Stock12_p4_1') stock13_p4_1 = self.participant.vars["priceChangeDict"].get('Stock13_p4_1') stock14_p4_1 = self.participant.vars["priceChangeDict"].get('Stock14_p4_1') stock15_p4_1 = self.participant.vars["priceChangeDict"].get('Stock15_p4_1') stock16_p4_1 = self.participant.vars["priceChangeDict"].get('Stock16_p4_1') stock17_p4_1 = self.participant.vars["priceChangeDict"].get('Stock17_p4_1') stock18_p4_1 = self.participant.vars["priceChangeDict"].get('Stock18_p4_1') stock19_p4_1 = self.participant.vars["priceChangeDict"].get('Stock19_p4_1') stock20_p4_1 = self.participant.vars["priceChangeDict"].get('Stock20_p4_1') return dict( Stock21_p1 = self.add_plus_to_pos_numbers(stock21_p1_1), Stock21_p2 = self.add_plus_to_pos_numbers(stock21_p3_1), Stock21_p3 = self.add_plus_to_pos_numbers(stock21_p3_1), Stock22_p1 = self.add_plus_to_pos_numbers(stock22_p1_1), Stock22_p2 = self.add_plus_to_pos_numbers(stock22_p2_1), Stock22_p3 = self.add_plus_to_pos_numbers(stock22_p3_1), Stock23_p1 = self.add_plus_to_pos_numbers(stock23_p1_1), Stock23_p2 = self.add_plus_to_pos_numbers(stock23_p2_1), Stock23_p3 = self.add_plus_to_pos_numbers(stock23_p3_1), Stock24_p1 = self.add_plus_to_pos_numbers(stock24_p1_1), Stock24_p2 = self.add_plus_to_pos_numbers(stock24_p2_1), Stock24_p3 = self.add_plus_to_pos_numbers(stock24_p3_1), Stock1_period1Price = self.player.period1Price_stock1, Stock2_period1Price = self.player.period1Price_stock2, Stock3_period1Price = self.player.period1Price_stock3, Stock4_period1Price = self.player.period1Price_stock4, Stock5_period1Price = self.player.period1Price_stock5, Stock6_period1Price = self.player.period1Price_stock6, Stock7_period1Price = self.player.period1Price_stock7, Stock8_period1Price = self.player.period1Price_stock8, Stock9_period1Price = self.player.period1Price_stock9, Stock10_period1Price = self.player.period1Price_stock10, Stock11_period1Price = self.player.period1Price_stock11, Stock12_period1Price = self.player.period1Price_stock12, Stock13_period1Price = self.player.period1Price_stock13, Stock14_period1Price = self.player.period1Price_stock14, Stock15_period1Price = self.player.period1Price_stock15, Stock16_period1Price = self.player.period1Price_stock16, Stock17_period1Price = self.player.period1Price_stock17, Stock18_period1Price = self.player.period1Price_stock18, Stock19_period1Price = self.player.period1Price_stock19, Stock20_period1Price = self.player.period1Price_stock20, Stock1_period1Change = self.add_plus_to_pos_numbers(stock1_p4_1), Stock2_period1Change = self.add_plus_to_pos_numbers(stock2_p4_1), Stock3_period1Change = self.add_plus_to_pos_numbers(stock3_p4_1), Stock4_period1Change = self.add_plus_to_pos_numbers(stock4_p4_1), Stock5_period1Change = self.add_plus_to_pos_numbers(stock5_p4_1), Stock6_period1Change = self.add_plus_to_pos_numbers(stock6_p4_1), Stock7_period1Change = self.add_plus_to_pos_numbers(stock7_p4_1), Stock8_period1Change = self.add_plus_to_pos_numbers(stock8_p4_1), Stock9_period1Change = self.add_plus_to_pos_numbers(stock9_p4_1), Stock10_period1Change = self.add_plus_to_pos_numbers(stock10_p4_1), Stock11_period1Change = self.add_plus_to_pos_numbers(stock11_p4_1), Stock12_period1Change = self.add_plus_to_pos_numbers(stock12_p4_1), Stock13_period1Change = self.add_plus_to_pos_numbers(stock13_p4_1), Stock14_period1Change = self.add_plus_to_pos_numbers(stock14_p4_1), Stock15_period1Change = self.add_plus_to_pos_numbers(stock15_p4_1), Stock16_period1Change = self.add_plus_to_pos_numbers(stock16_p4_1), Stock17_period1Change = self.add_plus_to_pos_numbers(stock17_p4_1), Stock18_period1Change = self.add_plus_to_pos_numbers(stock18_p4_1), Stock19_period1Change = self.add_plus_to_pos_numbers(stock19_p4_1), Stock20_period1Change = self.add_plus_to_pos_numbers(stock20_p4_1), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period1Price_row1, PriceRow2 = self.player.period1Price_row2, PriceRow3 = self.player.period1Price_row3, PriceRow4 = self.player.period1Price_row4, PriceRow5 = self.player.period1Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row5), PrePriceRow1 = self.player.preperiod1Price_row1, PrePriceRow2 = self.player.preperiod1Price_row2, PrePriceRow3 = self.player.preperiod1Price_row3, PrePriceRow4 = self.player.preperiod1Price_row4, PrePriceRow5 = self.player.preperiod1Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 1 class Period1_choice(Page): form_model = 'player' form_fields = ['Select_choice2'] currentPeriod = 1 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice2 == 21: self.participant.vars['Portfolio'].append('Stock21') elif self.player.Select_choice2 == 22: self.participant.vars['Portfolio'].append('Stock22') elif self.player.Select_choice2 == 23: self.participant.vars['Portfolio'].append('Stock23') else: self.participant.vars['Portfolio'].append('Stock24') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold1) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock21_p1_1 = self.participant.vars["priceChangeDict"].get('Stock21_p1_1') stock21_p2_1 = self.participant.vars["priceChangeDict"].get('Stock21_p2_1') stock21_p3_1 = self.participant.vars["priceChangeDict"].get('Stock21_p3_1') stock22_p1_1 = self.participant.vars["priceChangeDict"].get('Stock22_p1_1') stock22_p2_1 = self.participant.vars["priceChangeDict"].get('Stock22_p2_1') stock22_p3_1 = self.participant.vars["priceChangeDict"].get('Stock22_p3_1') stock23_p1_1 = self.participant.vars["priceChangeDict"].get('Stock23_p1_1') stock23_p2_1 = self.participant.vars["priceChangeDict"].get('Stock23_p2_1') stock23_p3_1 = self.participant.vars["priceChangeDict"].get('Stock23_p3_1') stock24_p1_1 = self.participant.vars["priceChangeDict"].get('Stock24_p1_1') stock24_p2_1 = self.participant.vars["priceChangeDict"].get('Stock24_p2_1') stock24_p3_1 = self.participant.vars["priceChangeDict"].get('Stock24_p3_1') return dict( Stock21_p1 = 30 - stock21_p1_1, Stock21_p2 = 30 - stock21_p1_1 - stock21_p2_1, Stock21_p3 = 30 - stock21_p1_1 - stock21_p2_1 - stock21_p3_1, Stock22_p1 = 30 - stock22_p1_1, Stock22_p2 = 30 - stock22_p1_1 - stock22_p2_1, Stock22_p3 = 30 - stock22_p1_1 - stock22_p2_1 - stock22_p3_1, Stock23_p1 = 30 - stock23_p1_1, Stock23_p2 = 30 - stock23_p1_1 - stock23_p2_1, Stock23_p3 = 30 - stock23_p1_1 - stock23_p2_1 - stock23_p3_1, Stock24_p1 = 30 - stock24_p1_1, Stock24_p2 = 30 - stock24_p1_1 - stock24_p2_1, Stock24_p3 = 30 - stock24_p1_1 - stock24_p2_1 - stock24_p3_1, Stock1_period1Price = self.player.period1Price_stock1, Stock2_period1Price = self.player.period1Price_stock2, Stock3_period1Price = self.player.period1Price_stock3, Stock4_period1Price = self.player.period1Price_stock4, Stock5_period1Price = self.player.period1Price_stock5, Stock6_period1Price = self.player.period1Price_stock6, Stock7_period1Price = self.player.period1Price_stock7, Stock8_period1Price = self.player.period1Price_stock8, Stock9_period1Price = self.player.period1Price_stock9, Stock10_period1Price = self.player.period1Price_stock10, Stock11_period1Price = self.player.period1Price_stock11, Stock12_period1Price = self.player.period1Price_stock12, Stock13_period1Price = self.player.period1Price_stock13, Stock14_period1Price = self.player.period1Price_stock14, Stock15_period1Price = self.player.period1Price_stock15, Stock16_period1Price = self.player.period1Price_stock16, Stock17_period1Price = self.player.period1Price_stock17, Stock18_period1Price = self.player.period1Price_stock18, Stock19_period1Price = self.player.period1Price_stock19, Stock20_period1Price = self.player.period1Price_stock20, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period1Price_row1, PriceRow2 = self.player.period1Price_row2, PriceRow3 = self.player.period1Price_row3, PriceRow4 = self.player.period1Price_row4, PriceRow5 = self.player.period1Price_row5, StockSold1 = self.showName_nicely(self.player.stockSold1), SellingPriceSold1 = self.player.sellingPriceSold1, PriceDiff1 = self.player.sellingPriceSold1 -30, GAINLOSS1 = self.showDiff(), ) ###Show period 2 class Period2_show(Page): form_model = 'player' currentPeriod = 2 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period 2 price per row def showPrice(self): self.player.period2Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow1, self.currentPeriod)) self.player.period2Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow2, self.currentPeriod)) self.player.period2Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow3, self.currentPeriod)) self.player.period2Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow4, self.currentPeriod)) self.player.period2Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold2 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold2 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold2 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold2 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold2 = self.player.sellingPrice_T1(self.player.stockSold2, 2) #variables for template def vars_for_template(self): self.showStocks() self.player.period2PriceChange_row1 = self.player.showPriceChangeForStockName_T1(self.player.stockRow1,self.currentPeriod) self.player.period2PriceChange_row2 = self.player.showPriceChangeForStockName_T1(self.player.stockRow2,self.currentPeriod) self.player.period2PriceChange_row3 = self.player.showPriceChangeForStockName_T1(self.player.stockRow3,self.currentPeriod) self.player.period2PriceChange_row4 = self.player.showPriceChangeForStockName_T1(self.player.stockRow4,self.currentPeriod) self.player.period2PriceChange_row5 = self.player.showPriceChangeForStockName_T1(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod2Price_row1 = self.player.period2Price_row1 - self.player.period2PriceChange_row1 self.player.preperiod2Price_row2 = self.player.period2Price_row2 - self.player.period2PriceChange_row2 self.player.preperiod2Price_row3 = self.player.period2Price_row3 - self.player.period2PriceChange_row3 self.player.preperiod2Price_row4 = self.player.period2Price_row4 - self.player.period2PriceChange_row4 self.player.preperiod2Price_row5 = self.player.period2Price_row5 - self.player.period2PriceChange_row5 stock25_p1_1 = self.participant.vars["priceChangeDict"].get('Stock25_p1_1') stock25_p2_1 = self.participant.vars["priceChangeDict"].get('Stock25_p2_1') stock25_p3_1 = self.participant.vars["priceChangeDict"].get('Stock25_p3_1') stock26_p1_1 = self.participant.vars["priceChangeDict"].get('Stock26_p1_1') stock26_p2_1 = self.participant.vars["priceChangeDict"].get('Stock26_p2_1') stock26_p3_1 = self.participant.vars["priceChangeDict"].get('Stock26_p3_1') stock27_p1_1 = self.participant.vars["priceChangeDict"].get('Stock27_p1_1') stock27_p2_1 = self.participant.vars["priceChangeDict"].get('Stock27_p2_1') stock27_p3_1 = self.participant.vars["priceChangeDict"].get('Stock27_p3_1') stock28_p1_1 = self.participant.vars["priceChangeDict"].get('Stock28_p1_1') stock28_p2_1 = self.participant.vars["priceChangeDict"].get('Stock28_p2_1') stock28_p3_1 = self.participant.vars["priceChangeDict"].get('Stock28_p3_1') return dict( Stock25_p1 = self.add_plus_to_pos_numbers(stock25_p1_1), Stock25_p2 = self.add_plus_to_pos_numbers(stock25_p2_1), Stock25_p3 = self.add_plus_to_pos_numbers(stock25_p3_1), Stock26_p1 = self.add_plus_to_pos_numbers(stock26_p1_1), Stock26_p2 = self.add_plus_to_pos_numbers(stock26_p2_1), Stock26_p3 = self.add_plus_to_pos_numbers(stock26_p3_1), Stock27_p1 = self.add_plus_to_pos_numbers(stock27_p1_1), Stock27_p2 = self.add_plus_to_pos_numbers(stock27_p2_1), Stock27_p3 = self.add_plus_to_pos_numbers(stock27_p3_1), Stock28_p1 = self.add_plus_to_pos_numbers(stock28_p1_1), Stock28_p2 = self.add_plus_to_pos_numbers(stock28_p2_1), Stock28_p3 = self.add_plus_to_pos_numbers(stock28_p3_1), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period2Price_row1, PriceRow2 = self.player.period2Price_row2, PriceRow3 = self.player.period2Price_row3, PriceRow4 = self.player.period2Price_row4, PriceRow5 = self.player.period2Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row5), PrePriceRow1 = self.player.preperiod2Price_row1, PrePriceRow2 = self.player.preperiod2Price_row2, PrePriceRow3 = self.player.preperiod2Price_row3, PrePriceRow4 = self.player.preperiod2Price_row4, PrePriceRow5 = self.player.preperiod2Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ### Choice period 2 class Period2_choice(Page): form_model = 'player' form_fields = ['Select_choice3'] currentPeriod = 2 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice3 == 25: self.participant.vars['Portfolio'].append('Stock25') elif self.player.Select_choice3 == 26: self.participant.vars['Portfolio'].append('Stock26') elif self.player.Select_choice3 == 27: self.participant.vars['Portfolio'].append('Stock27') else: self.participant.vars['Portfolio'].append('Stock28') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold2) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock25_p1_1 = self.participant.vars["priceChangeDict"].get('Stock25_p1_1') stock25_p2_1 = self.participant.vars["priceChangeDict"].get('Stock25_p2_1') stock25_p3_1 = self.participant.vars["priceChangeDict"].get('Stock25_p3_1') stock26_p1_1 = self.participant.vars["priceChangeDict"].get('Stock26_p1_1') stock26_p2_1 = self.participant.vars["priceChangeDict"].get('Stock26_p2_1') stock26_p3_1 = self.participant.vars["priceChangeDict"].get('Stock26_p3_1') stock27_p1_1 = self.participant.vars["priceChangeDict"].get('Stock27_p1_1') stock27_p2_1 = self.participant.vars["priceChangeDict"].get('Stock27_p2_1') stock27_p3_1 = self.participant.vars["priceChangeDict"].get('Stock27_p3_1') stock28_p1_1 = self.participant.vars["priceChangeDict"].get('Stock28_p1_1') stock28_p2_1 = self.participant.vars["priceChangeDict"].get('Stock28_p2_1') stock28_p3_1 = self.participant.vars["priceChangeDict"].get('Stock28_p3_1') return dict( Stock25_p1 = 30 - stock25_p1_1, Stock25_p2 = 30 - stock25_p1_1 - stock25_p2_1, Stock25_p3 = 30 - stock25_p1_1 - stock25_p2_1 - stock25_p3_1, Stock26_p1 = 30 - stock26_p1_1, Stock26_p2 = 30 - stock26_p1_1 - stock26_p2_1, Stock26_p3 = 30 - stock26_p1_1 - stock26_p2_1 - stock26_p3_1, Stock27_p1 = 30 - stock27_p1_1, Stock27_p2 = 30 - stock27_p1_1 - stock27_p2_1, Stock27_p3 = 30 - stock27_p1_1 - stock27_p2_1 - stock27_p3_1, Stock28_p1 = 30 - stock28_p1_1, Stock28_p2 = 30 - stock28_p1_1 - stock28_p2_1, Stock28_p3 = 30 - stock28_p1_1 - stock28_p2_1 - stock28_p3_1, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period2Price_row1, PriceRow2 = self.player.period2Price_row2, PriceRow3 = self.player.period2Price_row3, PriceRow4 = self.player.period2Price_row4, PriceRow5 = self.player.period2Price_row5, StockSold1 = self.showName_nicely(self.player.stockSold1), StockSold2 = self.showName_nicely(self.player.stockSold2), SellingPriceSold2 = self.player.sellingPriceSold2, PriceDiff2 = self.player.sellingPriceSold2 -30, GAINLOSS2 = self.showDiff(), ) ###Show period 3 class Period3_show(Page): form_model = 'player' currentPeriod = 3 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period 3 price per row def showPrice(self): self.player.period3Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow1, self.currentPeriod)) self.player.period3Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow2, self.currentPeriod)) self.player.period3Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow3, self.currentPeriod)) self.player.period3Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow4, self.currentPeriod)) self.player.period3Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold3 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold3 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold3 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold3 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold3 = self.player.sellingPrice_T1(self.player.stockSold3, 3) #variables for template def vars_for_template(self): self.showStocks() self.player.period3PriceChange_row1 = self.player.showPriceChangeForStockName_T1(self.player.stockRow1,self.currentPeriod) self.player.period3PriceChange_row2 = self.player.showPriceChangeForStockName_T1(self.player.stockRow2,self.currentPeriod) self.player.period3PriceChange_row3 = self.player.showPriceChangeForStockName_T1(self.player.stockRow3,self.currentPeriod) self.player.period3PriceChange_row4 = self.player.showPriceChangeForStockName_T1(self.player.stockRow4,self.currentPeriod) self.player.period3PriceChange_row5 = self.player.showPriceChangeForStockName_T1(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod3Price_row1 = self.player.period3Price_row1 - self.player.period3PriceChange_row1 self.player.preperiod3Price_row2 = self.player.period3Price_row2 - self.player.period3PriceChange_row2 self.player.preperiod3Price_row3 = self.player.period3Price_row3 - self.player.period3PriceChange_row3 self.player.preperiod3Price_row4 = self.player.period3Price_row4 - self.player.period3PriceChange_row4 self.player.preperiod3Price_row5 = self.player.period3Price_row5 - self.player.period3PriceChange_row5 stock29_p1_1 = self.participant.vars["priceChangeDict"].get('Stock29_p1_1') stock29_p2_1 = self.participant.vars["priceChangeDict"].get('Stock29_p2_1') stock29_p3_1 = self.participant.vars["priceChangeDict"].get('Stock29_p3_1') stock30_p1_1 = self.participant.vars["priceChangeDict"].get('Stock30_p1_1') stock30_p2_1 = self.participant.vars["priceChangeDict"].get('Stock30_p2_1') stock30_p3_1 = self.participant.vars["priceChangeDict"].get('Stock30_p3_1') stock31_p1_1 = self.participant.vars["priceChangeDict"].get('Stock31_p1_1') stock31_p2_1 = self.participant.vars["priceChangeDict"].get('Stock31_p2_1') stock31_p3_1 = self.participant.vars["priceChangeDict"].get('Stock31_p3_1') stock32_p1_1 = self.participant.vars["priceChangeDict"].get('Stock32_p1_1') stock32_p2_1 = self.participant.vars["priceChangeDict"].get('Stock32_p2_1') stock32_p3_1 = self.participant.vars["priceChangeDict"].get('Stock32_p3_1') return dict( Stock29_p1 = self.add_plus_to_pos_numbers(stock29_p1_1), Stock29_p2 = self.add_plus_to_pos_numbers(stock29_p2_1), Stock29_p3 = self.add_plus_to_pos_numbers(stock29_p3_1), Stock30_p1 = self.add_plus_to_pos_numbers(stock30_p1_1), Stock30_p2 = self.add_plus_to_pos_numbers(stock30_p2_1), Stock30_p3 = self.add_plus_to_pos_numbers(stock30_p3_1), Stock31_p1 = self.add_plus_to_pos_numbers(stock31_p1_1), Stock31_p2 = self.add_plus_to_pos_numbers(stock31_p2_1), Stock31_p3 = self.add_plus_to_pos_numbers(stock31_p3_1), Stock32_p1 = self.add_plus_to_pos_numbers(stock32_p1_1), Stock32_p2 = self.add_plus_to_pos_numbers(stock32_p2_1), Stock32_p3 = self.add_plus_to_pos_numbers(stock32_p3_1), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period3Price_row1, PriceRow2 = self.player.period3Price_row2, PriceRow3 = self.player.period3Price_row3, PriceRow4 = self.player.period3Price_row4, PriceRow5 = self.player.period3Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row5), PrePriceRow1 = self.player.preperiod3Price_row1, PrePriceRow2 = self.player.preperiod3Price_row2, PrePriceRow3 = self.player.preperiod3Price_row3, PrePriceRow4 = self.player.preperiod3Price_row4, PrePriceRow5 = self.player.preperiod3Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 3 class Period3_choice(Page): form_model = 'player' form_fields = ['Select_choice4'] currentPeriod = 3 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice4 == 29: self.participant.vars['Portfolio'].append('Stock29') elif self.player.Select_choice4 == 30: self.participant.vars['Portfolio'].append('Stock30') elif self.player.Select_choice4 == 31: self.participant.vars['Portfolio'].append('Stock31') else: self.participant.vars['Portfolio'].append('Stock32') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold3) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock29_p1_1 = self.participant.vars["priceChangeDict"].get('Stock29_p1_1') stock29_p2_1 = self.participant.vars["priceChangeDict"].get('Stock29_p2_1') stock29_p3_1 = self.participant.vars["priceChangeDict"].get('Stock29_p3_1') stock30_p1_1 = self.participant.vars["priceChangeDict"].get('Stock30_p1_1') stock30_p2_1 = self.participant.vars["priceChangeDict"].get('Stock30_p2_1') stock30_p3_1 = self.participant.vars["priceChangeDict"].get('Stock30_p3_1') stock31_p1_1 = self.participant.vars["priceChangeDict"].get('Stock31_p1_1') stock31_p2_1 = self.participant.vars["priceChangeDict"].get('Stock31_p2_1') stock31_p3_1 = self.participant.vars["priceChangeDict"].get('Stock31_p3_1') stock32_p1_1 = self.participant.vars["priceChangeDict"].get('Stock32_p1_1') stock32_p2_1 = self.participant.vars["priceChangeDict"].get('Stock32_p2_1') stock32_p3_1 = self.participant.vars["priceChangeDict"].get('Stock32_p3_1') return dict( Stock29_p1 = 30 - stock29_p1_1, Stock29_p2 = 30 - stock29_p1_1 - stock29_p2_1, Stock29_p3 = 30 - stock29_p1_1 - stock29_p2_1 - stock29_p3_1, Stock30_p1 = 30 - stock30_p1_1, Stock30_p2 = 30 - stock30_p1_1 - stock30_p2_1, Stock30_p3 = 30 - stock30_p1_1 - stock30_p2_1 - stock30_p3_1, Stock31_p1 = 30 - stock31_p1_1, Stock31_p2 = 30 - stock31_p1_1 - stock31_p2_1, Stock31_p3 = 30 - stock31_p1_1 - stock31_p2_1 - stock31_p3_1, Stock32_p1 = 30 - stock32_p1_1, Stock32_p2 = 30 - stock32_p1_1 - stock32_p2_1, Stock32_p3 = 30 - stock32_p1_1 - stock32_p2_1 - stock32_p3_1, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period3Price_row1, PriceRow2 = self.player.period3Price_row2, PriceRow3 = self.player.period3Price_row3, PriceRow4 = self.player.period3Price_row4, PriceRow5 = self.player.period3Price_row5, StockSold1 = self.showName_nicely(self.player.stockSold1), StockSold2 = self.showName_nicely(self.player.stockSold2), StockSold3 = self.showName_nicely(self.player.stockSold3), SellingPriceSold3 = self.player.sellingPriceSold3, PriceDiff3 = self.player.sellingPriceSold3 -30, GAINLOSS3 = self.showDiff(), ) ###Show period 4 class Period4_show(Page): form_model = 'player' currentPeriod = 4 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period4Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow1, self.currentPeriod)) self.player.period4Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow2, self.currentPeriod)) self.player.period4Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow3, self.currentPeriod)) self.player.period4Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow4, self.currentPeriod)) self.player.period4Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold4 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold4 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold4 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold4 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold4 = self.player.sellingPrice_T1(self.player.stockSold4, 4) #variables for template def vars_for_template(self): self.showStocks() self.player.period4PriceChange_row1 = self.player.showPriceChangeForStockName_T1(self.player.stockRow1,self.currentPeriod) self.player.period4PriceChange_row2 = self.player.showPriceChangeForStockName_T1(self.player.stockRow2,self.currentPeriod) self.player.period4PriceChange_row3 = self.player.showPriceChangeForStockName_T1(self.player.stockRow3,self.currentPeriod) self.player.period4PriceChange_row4 = self.player.showPriceChangeForStockName_T1(self.player.stockRow4,self.currentPeriod) self.player.period4PriceChange_row5 = self.player.showPriceChangeForStockName_T1(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod4Price_row1 = self.player.period4Price_row1 - self.player.period4PriceChange_row1 self.player.preperiod4Price_row2 = self.player.period4Price_row2 - self.player.period4PriceChange_row2 self.player.preperiod4Price_row3 = self.player.period4Price_row3 - self.player.period4PriceChange_row3 self.player.preperiod4Price_row4 = self.player.period4Price_row4 - self.player.period4PriceChange_row4 self.player.preperiod4Price_row5 = self.player.period4Price_row5 - self.player.period4PriceChange_row5 stock33_p1_1 = self.participant.vars["priceChangeDict"].get('Stock33_p1_1') stock33_p2_1 = self.participant.vars["priceChangeDict"].get('Stock33_p2_1') stock33_p3_1 = self.participant.vars["priceChangeDict"].get('Stock33_p3_1') stock34_p1_1 = self.participant.vars["priceChangeDict"].get('Stock34_p1_1') stock34_p2_1 = self.participant.vars["priceChangeDict"].get('Stock34_p2_1') stock34_p3_1 = self.participant.vars["priceChangeDict"].get('Stock34_p3_1') stock35_p1_1 = self.participant.vars["priceChangeDict"].get('Stock35_p1_1') stock35_p2_1 = self.participant.vars["priceChangeDict"].get('Stock35_p2_1') stock35_p3_1 = self.participant.vars["priceChangeDict"].get('Stock35_p3_1') stock36_p1_1 = self.participant.vars["priceChangeDict"].get('Stock36_p1_1') stock36_p2_1 = self.participant.vars["priceChangeDict"].get('Stock36_p2_1') stock36_p3_1 = self.participant.vars["priceChangeDict"].get('Stock36_p3_1') return dict( Stock33_p1 = self.add_plus_to_pos_numbers(stock33_p1_1), Stock33_p2 = self.add_plus_to_pos_numbers(stock33_p2_1), Stock33_p3 = self.add_plus_to_pos_numbers(stock33_p3_1), Stock34_p1 = self.add_plus_to_pos_numbers(stock34_p1_1), Stock34_p2 = self.add_plus_to_pos_numbers(stock34_p2_1), Stock34_p3 = self.add_plus_to_pos_numbers(stock34_p3_1), Stock35_p1 = self.add_plus_to_pos_numbers(stock35_p1_1), Stock35_p2 = self.add_plus_to_pos_numbers(stock35_p2_1), Stock35_p3 = self.add_plus_to_pos_numbers(stock35_p3_1), Stock36_p1 = self.add_plus_to_pos_numbers(stock36_p1_1), Stock36_p2 = self.add_plus_to_pos_numbers(stock36_p2_1), Stock36_p3 = self.add_plus_to_pos_numbers(stock36_p3_1), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period4Price_row1, PriceRow2 = self.player.period4Price_row2, PriceRow3 = self.player.period4Price_row3, PriceRow4 = self.player.period4Price_row4, PriceRow5 = self.player.period4Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row5), PrePriceRow1 = self.player.preperiod4Price_row1, PrePriceRow2 = self.player.preperiod4Price_row2, PrePriceRow3 = self.player.preperiod4Price_row3, PrePriceRow4 = self.player.preperiod4Price_row4, PrePriceRow5 = self.player.preperiod4Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 4 class Period4_choice(Page): form_model = 'player' form_fields = ['Select_choice5'] currentPeriod = 4 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice5 == 33: self.participant.vars['Portfolio'].append('Stock33') elif self.player.Select_choice5 == 34: self.participant.vars['Portfolio'].append('Stock34') elif self.player.Select_choice5 == 35: self.participant.vars['Portfolio'].append('Stock35') else: self.participant.vars['Portfolio'].append('Stock36') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold4) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock33_p1_1 = self.participant.vars["priceChangeDict"].get('Stock33_p1_1') stock33_p2_1 = self.participant.vars["priceChangeDict"].get('Stock33_p2_1') stock33_p3_1 = self.participant.vars["priceChangeDict"].get('Stock33_p3_1') stock34_p1_1 = self.participant.vars["priceChangeDict"].get('Stock34_p1_1') stock34_p2_1 = self.participant.vars["priceChangeDict"].get('Stock34_p2_1') stock34_p3_1 = self.participant.vars["priceChangeDict"].get('Stock34_p3_1') stock35_p1_1 = self.participant.vars["priceChangeDict"].get('Stock35_p1_1') stock35_p2_1 = self.participant.vars["priceChangeDict"].get('Stock35_p2_1') stock35_p3_1 = self.participant.vars["priceChangeDict"].get('Stock35_p3_1') stock36_p1_1 = self.participant.vars["priceChangeDict"].get('Stock36_p1_1') stock36_p2_1 = self.participant.vars["priceChangeDict"].get('Stock36_p2_1') stock36_p3_1 = self.participant.vars["priceChangeDict"].get('Stock36_p3_1') return dict( Stock33_p1 = 30 - stock33_p1_1, Stock33_p2 = 30 - stock33_p1_1 - stock33_p2_1, Stock33_p3 = 30 - stock33_p1_1 - stock33_p2_1 - stock33_p3_1, Stock34_p1 = 30 - stock34_p1_1, Stock34_p2 = 30 - stock34_p1_1 - stock34_p2_1, Stock34_p3 = 30 - stock34_p1_1 - stock34_p2_1 - stock34_p3_1, Stock35_p1 = 30 - stock35_p1_1, Stock35_p2 = 30 - stock35_p1_1 - stock35_p2_1, Stock35_p3 = 30 - stock35_p1_1 - stock35_p2_1 - stock35_p3_1, Stock36_p1 = 30 - stock36_p1_1, Stock36_p2 = 30 - stock36_p1_1 - stock36_p2_1, Stock36_p3 = 30 - stock36_p1_1 - stock36_p2_1 - stock36_p3_1, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period4Price_row1, PriceRow2 = self.player.period4Price_row2, PriceRow3 = self.player.period4Price_row3, PriceRow4 = self.player.period4Price_row4, PriceRow5 = self.player.period4Price_row5, StockSold3 = self.showName_nicely(self.player.stockSold3), StockSold4 = self.showName_nicely(self.player.stockSold4), SellingPriceSold4 = self.player.sellingPriceSold4, PriceDiff4 = self.player.sellingPriceSold4 -30, GAINLOSS4 = self.showDiff(), ) ###Show period 5 class Period5_show(Page): form_model = 'player' currentPeriod = 5 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period5Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow1, self.currentPeriod)) self.player.period5Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow2, self.currentPeriod)) self.player.period5Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow3, self.currentPeriod)) self.player.period5Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow4, self.currentPeriod)) self.player.period5Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold5 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold5 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold5 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold5 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold5 = self.player.sellingPrice_T1(self.player.stockSold5, 5) #variables for template def vars_for_template(self): self.showStocks() self.player.period5PriceChange_row1 = self.player.showPriceChangeForStockName_T1(self.player.stockRow1,self.currentPeriod) self.player.period5PriceChange_row2 = self.player.showPriceChangeForStockName_T1(self.player.stockRow2,self.currentPeriod) self.player.period5PriceChange_row3 = self.player.showPriceChangeForStockName_T1(self.player.stockRow3,self.currentPeriod) self.player.period5PriceChange_row4 = self.player.showPriceChangeForStockName_T1(self.player.stockRow4,self.currentPeriod) self.player.period5PriceChange_row5 = self.player.showPriceChangeForStockName_T1(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod5Price_row1 = self.player.period5Price_row1 - self.player.period5PriceChange_row1 self.player.preperiod5Price_row2 = self.player.period5Price_row2 - self.player.period5PriceChange_row2 self.player.preperiod5Price_row3 = self.player.period5Price_row3 - self.player.period5PriceChange_row3 self.player.preperiod5Price_row4 = self.player.period5Price_row4 - self.player.period5PriceChange_row4 self.player.preperiod5Price_row5 = self.player.period5Price_row5 - self.player.period5PriceChange_row5 stock37_p1_1 = self.participant.vars["priceChangeDict"].get('Stock37_p1_1') stock37_p2_1 = self.participant.vars["priceChangeDict"].get('Stock37_p2_1') stock37_p3_1 = self.participant.vars["priceChangeDict"].get('Stock37_p3_1') stock38_p1_1 = self.participant.vars["priceChangeDict"].get('Stock38_p1_1') stock38_p2_1 = self.participant.vars["priceChangeDict"].get('Stock38_p2_1') stock38_p3_1 = self.participant.vars["priceChangeDict"].get('Stock38_p3_1') stock39_p1_1 = self.participant.vars["priceChangeDict"].get('Stock39_p1_1') stock39_p2_1 = self.participant.vars["priceChangeDict"].get('Stock39_p2_1') stock39_p3_1 = self.participant.vars["priceChangeDict"].get('Stock39_p3_1') stock40_p1_1 = self.participant.vars["priceChangeDict"].get('Stock40_p1_1') stock40_p2_1 = self.participant.vars["priceChangeDict"].get('Stock40_p2_1') stock40_p3_1 = self.participant.vars["priceChangeDict"].get('Stock40_p3_1') return dict( Stock37_p1 = self.add_plus_to_pos_numbers(stock37_p1_1), Stock37_p2 = self.add_plus_to_pos_numbers(stock37_p2_1), Stock37_p3 = self.add_plus_to_pos_numbers(stock37_p3_1), Stock38_p1 = self.add_plus_to_pos_numbers(stock38_p1_1), Stock38_p2 = self.add_plus_to_pos_numbers(stock38_p2_1), Stock38_p3 = self.add_plus_to_pos_numbers(stock38_p3_1), Stock39_p1 = self.add_plus_to_pos_numbers(stock39_p1_1), Stock39_p2 = self.add_plus_to_pos_numbers(stock39_p2_1), Stock39_p3 = self.add_plus_to_pos_numbers(stock39_p3_1), Stock40_p1 = self.add_plus_to_pos_numbers(stock40_p1_1), Stock40_p2 = self.add_plus_to_pos_numbers(stock40_p2_1), Stock40_p3 = self.add_plus_to_pos_numbers(stock40_p3_1), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period5Price_row1, PriceRow2 = self.player.period5Price_row2, PriceRow3 = self.player.period5Price_row3, PriceRow4 = self.player.period5Price_row4, PriceRow5 = self.player.period5Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row5), PrePriceRow1 = self.player.preperiod5Price_row1, PrePriceRow2 = self.player.preperiod5Price_row2, PrePriceRow3 = self.player.preperiod5Price_row3, PrePriceRow4 = self.player.preperiod5Price_row4, PrePriceRow5 = self.player.preperiod5Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 5 class Period5_choice(Page): form_model = 'player' form_fields = ['Select_choice6'] currentPeriod = 5 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice6 == 37: self.participant.vars['Portfolio'].append('Stock37') elif self.player.Select_choice6 == 38: self.participant.vars['Portfolio'].append('Stock38') elif self.player.Select_choice6 == 39: self.participant.vars['Portfolio'].append('Stock39') else: self.participant.vars['Portfolio'].append('Stock40') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold5) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock37_p1_1 = self.participant.vars["priceChangeDict"].get('Stock37_p1_1') stock37_p2_1 = self.participant.vars["priceChangeDict"].get('Stock37_p2_1') stock37_p3_1 = self.participant.vars["priceChangeDict"].get('Stock37_p3_1') stock38_p1_1 = self.participant.vars["priceChangeDict"].get('Stock38_p1_1') stock38_p2_1 = self.participant.vars["priceChangeDict"].get('Stock38_p2_1') stock38_p3_1 = self.participant.vars["priceChangeDict"].get('Stock38_p3_1') stock39_p1_1 = self.participant.vars["priceChangeDict"].get('Stock39_p1_1') stock39_p2_1 = self.participant.vars["priceChangeDict"].get('Stock39_p2_1') stock39_p3_1 = self.participant.vars["priceChangeDict"].get('Stock39_p3_1') stock40_p1_1 = self.participant.vars["priceChangeDict"].get('Stock40_p1_1') stock40_p2_1 = self.participant.vars["priceChangeDict"].get('Stock40_p2_1') stock40_p3_1 = self.participant.vars["priceChangeDict"].get('Stock40_p3_1') return dict( Stock37_p1 = 30 - stock37_p1_1, Stock37_p2 = 30 - stock37_p1_1 - stock37_p2_1, Stock37_p3 = 30 - stock37_p1_1 - stock37_p2_1 - stock37_p3_1, Stock38_p1 = 30 - stock38_p1_1, Stock38_p2 = 30 - stock38_p1_1 - stock38_p2_1, Stock38_p3 = 30 - stock38_p1_1 - stock38_p2_1 - stock38_p3_1, Stock39_p1 = 30 - stock39_p1_1, Stock39_p2 = 30 - stock39_p1_1 - stock39_p2_1, Stock39_p3 = 30 - stock39_p1_1 - stock39_p2_1 - stock39_p3_1, Stock40_p1 = 30 - stock40_p1_1, Stock40_p2 = 30 - stock40_p1_1 - stock40_p2_1, Stock40_p3 = 30 - stock40_p1_1 - stock40_p2_1 - stock40_p3_1, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period5Price_row1, PriceRow2 = self.player.period5Price_row2, PriceRow3 = self.player.period5Price_row3, PriceRow4 = self.player.period5Price_row4, PriceRow5 = self.player.period5Price_row5, StockSold4 = self.showName_nicely(self.player.stockSold4), StockSold5 = self.showName_nicely(self.player.stockSold5), SellingPriceSold5 = self.player.sellingPriceSold5, PriceDiff5 = self.player.sellingPriceSold5 -30, GAINLOSS5 = self.showDiff(), ) ###Show period 6 class Period6_show(Page): form_model = 'player' currentPeriod = 6 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period6Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow1, self.currentPeriod)) self.player.period6Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow2, self.currentPeriod)) self.player.period6Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow3, self.currentPeriod)) self.player.period6Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow4, self.currentPeriod)) self.player.period6Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold6 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold6 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T1(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold6 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold6 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold6 = self.player.sellingPrice_T1(self.player.stockSold6, 6) #variables for template def vars_for_template(self): self.showStocks() self.player.period6PriceChange_row1 = self.player.showPriceChangeForStockName_T1(self.player.stockRow1,self.currentPeriod) self.player.period6PriceChange_row2 = self.player.showPriceChangeForStockName_T1(self.player.stockRow2,self.currentPeriod) self.player.period6PriceChange_row3 = self.player.showPriceChangeForStockName_T1(self.player.stockRow3,self.currentPeriod) self.player.period6PriceChange_row4 = self.player.showPriceChangeForStockName_T1(self.player.stockRow4,self.currentPeriod) self.player.period6PriceChange_row5 = self.player.showPriceChangeForStockName_T1(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod6Price_row1 = self.player.period6Price_row1 - self.player.period6PriceChange_row1 self.player.preperiod6Price_row2 = self.player.period6Price_row2 - self.player.period6PriceChange_row2 self.player.preperiod6Price_row3 = self.player.period6Price_row3 - self.player.period6PriceChange_row3 self.player.preperiod6Price_row4 = self.player.period6Price_row4 - self.player.period6PriceChange_row4 self.player.preperiod6Price_row5 = self.player.period6Price_row5 - self.player.period6PriceChange_row5 stock41_p1_1 = self.participant.vars["priceChangeDict"].get('Stock41_p1_1') stock41_p2_1 = self.participant.vars["priceChangeDict"].get('Stock41_p2_1') stock41_p3_1 = self.participant.vars["priceChangeDict"].get('Stock41_p3_1') stock42_p1_1 = self.participant.vars["priceChangeDict"].get('Stock42_p1_1') stock42_p2_1 = self.participant.vars["priceChangeDict"].get('Stock42_p2_1') stock42_p3_1 = self.participant.vars["priceChangeDict"].get('Stock42_p3_1') stock43_p1_1 = self.participant.vars["priceChangeDict"].get('Stock43_p1_1') stock43_p2_1 = self.participant.vars["priceChangeDict"].get('Stock43_p2_1') stock43_p3_1 = self.participant.vars["priceChangeDict"].get('Stock43_p3_1') stock44_p1_1 = self.participant.vars["priceChangeDict"].get('Stock44_p1_1') stock44_p2_1 = self.participant.vars["priceChangeDict"].get('Stock44_p2_1') stock44_p3_1 = self.participant.vars["priceChangeDict"].get('Stock44_p3_1') return dict( Stock41_p1 = self.add_plus_to_pos_numbers(stock41_p1_1), Stock41_p2 = self.add_plus_to_pos_numbers(stock41_p2_1), Stock41_p3 = self.add_plus_to_pos_numbers(stock41_p3_1), Stock42_p1 = self.add_plus_to_pos_numbers(stock42_p1_1), Stock42_p2 = self.add_plus_to_pos_numbers(stock42_p2_1), Stock42_p3 = self.add_plus_to_pos_numbers(stock42_p3_1), Stock43_p1 = self.add_plus_to_pos_numbers(stock43_p1_1), Stock43_p2 = self.add_plus_to_pos_numbers(stock43_p2_1), Stock43_p3 = self.add_plus_to_pos_numbers(stock43_p3_1), Stock44_p1 = self.add_plus_to_pos_numbers(stock44_p1_1), Stock44_p2 = self.add_plus_to_pos_numbers(stock44_p2_1), Stock44_p3 = self.add_plus_to_pos_numbers(stock44_p3_1), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period6Price_row1, PriceRow2 = self.player.period6Price_row2, PriceRow3 = self.player.period6Price_row3, PriceRow4 = self.player.period6Price_row4, PriceRow5 = self.player.period6Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row5), PrePriceRow1 = self.player.preperiod6Price_row1, PrePriceRow2 = self.player.preperiod6Price_row2, PrePriceRow3 = self.player.preperiod6Price_row3, PrePriceRow4 = self.player.preperiod6Price_row4, PrePriceRow5 = self.player.preperiod6Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 6 class Period6_choice(Page): form_model = 'player' form_fields = ['Select_choice7'] currentPeriod = 6 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice7 == 41: self.participant.vars['Portfolio'].append('Stock41') elif self.player.Select_choice7 == 42: self.participant.vars['Portfolio'].append('Stock42') elif self.player.Select_choice7 == 43: self.participant.vars['Portfolio'].append('Stock43') else: self.participant.vars['Portfolio'].append('Stock44') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold6) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock41_p1_1 = self.participant.vars["priceChangeDict"].get('Stock41_p1_1') stock41_p2_1 = self.participant.vars["priceChangeDict"].get('Stock41_p2_1') stock41_p3_1 = self.participant.vars["priceChangeDict"].get('Stock41_p3_1') stock42_p1_1 = self.participant.vars["priceChangeDict"].get('Stock42_p1_1') stock42_p2_1 = self.participant.vars["priceChangeDict"].get('Stock42_p2_1') stock42_p3_1 = self.participant.vars["priceChangeDict"].get('Stock42_p3_1') stock43_p1_1 = self.participant.vars["priceChangeDict"].get('Stock43_p1_1') stock43_p2_1 = self.participant.vars["priceChangeDict"].get('Stock43_p2_1') stock43_p3_1 = self.participant.vars["priceChangeDict"].get('Stock43_p3_1') stock44_p1_1 = self.participant.vars["priceChangeDict"].get('Stock44_p1_1') stock44_p2_1 = self.participant.vars["priceChangeDict"].get('Stock44_p2_1') stock44_p3_1 = self.participant.vars["priceChangeDict"].get('Stock44_p3_1') return dict( Stock41_p1 = 30 - stock41_p1_1, Stock41_p2 = 30 - stock41_p1_1 - stock41_p2_1, Stock41_p3 = 30 - stock41_p1_1 - stock41_p2_1 - stock41_p3_1, Stock42_p1 = 30 - stock42_p1_1, Stock42_p2 = 30 - stock42_p1_1 - stock42_p2_1, Stock42_p3 = 30 - stock42_p1_1 - stock42_p2_1 - stock42_p3_1, Stock43_p1 = 30 - stock43_p1_1, Stock43_p2 = 30 - stock43_p1_1 - stock43_p2_1, Stock43_p3 = 30 - stock43_p1_1 - stock43_p2_1 - stock43_p3_1, Stock44_p1 = 30 - stock44_p1_1, Stock44_p2 = 30 - stock44_p1_1 - stock44_p2_1, Stock44_p3 = 30 - stock44_p1_1 - stock44_p2_1 - stock44_p3_1, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period6Price_row1, PriceRow2 = self.player.period6Price_row2, PriceRow3 = self.player.period6Price_row3, PriceRow4 = self.player.period6Price_row4, PriceRow5 = self.player.period6Price_row5, StockSold5 = self.showName_nicely(self.player.stockSold5), StockSold6 = self.showName_nicely(self.player.stockSold6), SellingPriceSold6 = self.player.sellingPriceSold6, PriceDiff6 = self.player.sellingPriceSold6 -30, GAINLOSS6 = self.showDiff(), ) ### period 7 class Period7_show(Page): form_model = 'player' currentPeriod = 7 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period7Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow1, self.currentPeriod)) self.player.period7Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow2, self.currentPeriod)) self.player.period7Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow3, self.currentPeriod)) self.player.period7Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow4, self.currentPeriod)) self.player.period7Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T1(self.player.stockRow5, self.currentPeriod)) #sell all stocks def sellStock(self): self.player.stockSold7 = self.player.stockRow1 self.player.stockSold8 = self.player.stockRow2 self.player.stockSold9 = self.player.stockRow3 self.player.stockSold10 = self.player.stockRow4 self.player.stockSold11 = self.player.stockRow5 #variables for template def vars_for_template(self): self.showStocks() self.player.period7PriceChange_row1 = self.player.showPriceChangeForStockName_T1(self.player.stockRow1,self.currentPeriod) self.player.period7PriceChange_row2 = self.player.showPriceChangeForStockName_T1(self.player.stockRow2,self.currentPeriod) self.player.period7PriceChange_row3 = self.player.showPriceChangeForStockName_T1(self.player.stockRow3,self.currentPeriod) self.player.period7PriceChange_row4 = self.player.showPriceChangeForStockName_T1(self.player.stockRow4,self.currentPeriod) self.player.period7PriceChange_row5 = self.player.showPriceChangeForStockName_T1(self.player.stockRow5,self.currentPeriod) self.showPrice() self.sellStock() self.player.sellingPriceSold7 = self.player.sellingPrice_T1(self.player.stockSold7, 7) self.player.sellingPriceSold8 = self.player.sellingPrice_T1(self.player.stockSold8, 7) self.player.sellingPriceSold9 = self.player.sellingPrice_T1(self.player.stockSold9, 7) self.player.sellingPriceSold10 = self.player.sellingPrice_T1(self.player.stockSold10, 7) self.player.sellingPriceSold11 = self.player.sellingPrice_T1(self.player.stockSold11, 7) #pre period price per row self.player.preperiod7Price_row1 = self.player.period7Price_row1 - self.player.period7PriceChange_row1 self.player.preperiod7Price_row2 = self.player.period7Price_row2 - self.player.period7PriceChange_row2 self.player.preperiod7Price_row3 = self.player.period7Price_row3 - self.player.period7PriceChange_row3 self.player.preperiod7Price_row4 = self.player.period7Price_row4 - self.player.period7PriceChange_row4 self.player.preperiod7Price_row5 = self.player.period7Price_row5 - self.player.period7PriceChange_row5 return dict( StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period7Price_row1, PriceRow2 = self.player.period7Price_row2, PriceRow3 = self.player.period7Price_row3, PriceRow4 = self.player.period7Price_row4, PriceRow5 = self.player.period7Price_row5, StockSold6 = self.showName_nicely(self.player.stockSold6), StockSold7 = self.showName_nicely(self.player.stockSold7), SellingPriceSold6 = self.player.sellingPriceSold6, PrePriceRow1 = self.player.preperiod7Price_row1, PrePriceRow2 = self.player.preperiod7Price_row2, PrePriceRow3 = self.player.preperiod7Price_row3, PrePriceRow4 = self.player.preperiod7Price_row4, PrePriceRow5 = self.player.preperiod7Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ValuePeriod7 = self.player.sellingPriceSold11 + self.player.sellingPriceSold10 + self.player.sellingPriceSold9 + self.player.sellingPriceSold8 + self.player.sellingPriceSold7, ) ### TRIAL TWO class Period0_2(Page): form_model = 'player' form_fields = ['Select_stock1_T2', 'Select_stock2_T2', 'Select_stock3_T2', 'Select_stock4_T2', 'Select_stock5_T2', 'Select_stock6_T2', 'Select_stock7_T2', 'Select_stock8_T2', 'Select_stock9_T2', 'Select_stock10_T2', 'Select_stock11_T2', 'Select_stock12_T2', 'Select_stock13_T2', 'Select_stock14_T2', 'Select_stock15_T2', 'Select_stock16_T2', 'Select_stock17_T2', 'Select_stock18_T2', 'Select_stock19_T2', 'Select_stock20_T2'] def error_message(self, values): print('values is', values) listSelectedStocks2 = [values['Select_stock1_T2'], values['Select_stock2_T2'], values['Select_stock3_T2'], values['Select_stock4_T2'], values['Select_stock5_T2'], values['Select_stock6_T2'], values['Select_stock7_T2'], values['Select_stock8_T2'], values['Select_stock9_T2'], values['Select_stock10_T2'], values['Select_stock11_T2'], values['Select_stock12_T2'], values['Select_stock13_T2'], values['Select_stock14_T2'], values['Select_stock15_T2'], values['Select_stock16_T2'], values['Select_stock17_T2'], values['Select_stock18_T2'], values['Select_stock19_T2'], values['Select_stock20_T2']] if listSelectedStocks2.count(None) < 15: return 'You can only choose 5 stocks.' elif listSelectedStocks2.count(None) > 15: return 'You have to choose 5 stocks.' def vars_for_template(self): stock1_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock1_p1_2') stock1_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock1_p2_2') stock1_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock1_p3_2') stock2_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock2_p1_2') stock2_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock2_p2_2') stock2_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock2_p3_2') stock3_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock3_p1_2') stock3_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock3_p2_2') stock3_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock3_p3_2') stock4_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock4_p1_2') stock4_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock4_p2_2') stock4_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock4_p3_2') stock5_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock5_p1_2') stock5_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock5_p2_2') stock5_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock5_p3_2') stock6_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock6_p1_2') stock6_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock6_p2_2') stock6_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock6_p3_2') stock7_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock7_p1_2') stock7_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock7_p2_2') stock7_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock7_p3_2') stock8_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock8_p1_2') stock8_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock8_p2_2') stock8_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock8_p3_2') stock9_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock9_p1_2') stock9_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock9_p2_2') stock9_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock9_p3_2') stock10_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock10_p1_2') stock10_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock10_p2_2') stock10_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock10_p3_2') stock11_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock11_p1_2') stock11_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock11_p2_2') stock11_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock11_p3_2') stock12_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock12_p1_2') stock12_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock12_p2_2') stock12_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock12_p3_2') stock13_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock13_p1_2') stock13_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock13_p2_2') stock13_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock13_p3_2') stock14_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock14_p1_2') stock14_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock14_p2_2') stock14_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock14_p3_2') stock15_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock15_p1_2') stock15_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock15_p2_2') stock15_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock15_p3_2') stock16_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock16_p1_2') stock16_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock16_p2_2') stock16_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock16_p3_2') stock17_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock17_p1_2') stock17_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock17_p2_2') stock17_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock17_p3_2') stock18_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock18_p1_2') stock18_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock18_p2_2') stock18_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock18_p3_2') stock19_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock19_p1_2') stock19_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock19_p2_2') stock19_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock19_p3_2') stock20_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock20_p1_2') stock20_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock20_p2_2') stock20_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock20_p3_2') return dict( Stock1_p1 = 30 - stock1_p1_2, Stock1_p2 = 30 - stock1_p1_2 - stock1_p2_2, Stock1_p3 = 30 - stock1_p1_2 - stock1_p2_2 - stock1_p3_2, Stock2_p1 = 30 - stock2_p1_2, Stock2_p2 = 30 - stock2_p1_2 - stock2_p2_2, Stock2_p3 = 30 - stock2_p1_2 - stock2_p2_2 - stock2_p3_2, Stock3_p1 = 30 - stock3_p1_2, Stock3_p2 = 30 - stock3_p1_2 - stock3_p2_2, Stock3_p3 = 30 - stock3_p1_2 - stock3_p2_2 - stock3_p3_2, Stock4_p1 = 30 - stock4_p1_2, Stock4_p2 = 30 - stock4_p1_2 - stock4_p2_2, Stock4_p3 = 30 - stock4_p1_2 - stock4_p2_2 - stock4_p3_2, Stock5_p1 = 30 - stock5_p1_2, Stock5_p2 = 30 - stock5_p1_2 - stock5_p2_2, Stock5_p3 = 30 - stock5_p1_2 - stock5_p2_2 - stock5_p3_2, Stock6_p1 = 30 - stock6_p1_2, Stock6_p2 = 30 - stock6_p1_2 - stock6_p2_2, Stock6_p3 = 30 - stock6_p1_2 - stock6_p2_2 - stock6_p3_2, Stock7_p1 = 30 - stock7_p1_2, Stock7_p2 = 30 - stock7_p1_2 - stock7_p2_2, Stock7_p3 = 30 - stock7_p1_2 - stock7_p2_2 - stock7_p3_2, Stock8_p1 = 30 - stock8_p1_2, Stock8_p2 = 30 - stock8_p1_2 - stock8_p2_2, Stock8_p3 = 30 - stock8_p1_2 - stock8_p2_2 - stock8_p3_2, Stock9_p1 = 30 - stock9_p1_2, Stock9_p2 = 30 - stock9_p1_2 - stock9_p2_2, Stock9_p3 = 30 - stock9_p1_2 - stock9_p2_2 - stock9_p3_2, Stock10_p1 = 30 - stock10_p1_2, Stock10_p2 = 30 - stock10_p1_2 - stock10_p2_2, Stock10_p3 = 30 - stock10_p1_2 - stock10_p2_2 - stock10_p3_2, Stock11_p1 = 30 - stock11_p1_2, Stock11_p2 = 30 - stock11_p1_2 - stock11_p2_2, Stock11_p3 = 30 - stock11_p1_2 - stock11_p2_2 - stock11_p3_2, Stock12_p1 = 30 - stock12_p1_2, Stock12_p2 = 30 - stock12_p1_2 - stock12_p2_2, Stock12_p3 = 30 - stock12_p1_2 - stock12_p2_2 - stock12_p3_2, Stock13_p1 = 30 - stock13_p1_2, Stock13_p2 = 30 - stock13_p1_2 - stock13_p2_2, Stock13_p3 = 30 - stock13_p1_2 - stock13_p2_2 - stock13_p3_2, Stock14_p1 = 30 - stock14_p1_2, Stock14_p2 = 30 - stock14_p1_2 - stock14_p2_2, Stock14_p3 = 30 - stock14_p1_2 - stock14_p2_2 - stock14_p3_2, Stock15_p1 = 30 - stock15_p1_2, Stock15_p2 = 30 - stock15_p1_2 - stock15_p2_2, Stock15_p3 = 30 - stock15_p1_2 - stock15_p2_2 - stock15_p3_2, Stock16_p1 = 30 - stock16_p1_2, Stock16_p2 = 30 - stock16_p1_2 - stock16_p2_2, Stock16_p3 = 30 - stock16_p1_2 - stock16_p2_2 - stock16_p3_2, Stock17_p1 = 30 - stock17_p1_2, Stock17_p2 = 30 - stock17_p1_2 - stock17_p2_2, Stock17_p3 = 30 - stock17_p1_2 - stock17_p2_2 - stock17_p3_2, Stock18_p1 = 30 - stock18_p1_2, Stock18_p2 = 30 - stock18_p1_2 - stock18_p2_2, Stock18_p3 = 30 - stock18_p1_2 - stock18_p2_2 - stock18_p3_2, Stock19_p1 = 30 - stock19_p1_2, Stock19_p2 = 30 - stock19_p1_2 - stock19_p2_2, Stock19_p3 = 30 - stock19_p1_2 - stock19_p2_2 - stock19_p3_2, Stock20_p1 = 30 - stock20_p1_2, Stock20_p2 = 30 - stock20_p1_2 - stock20_p2_2, Stock20_p3 = 30 - stock20_p1_2 - stock20_p2_2 - stock20_p3_2, ) #calculate period 1 stock price def before_next_page(self): self.player.period1Price_stock1 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock1_p4_2') self.player.period1Price_stock2 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock2_p4_2') self.player.period1Price_stock3 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock3_p4_2') self.player.period1Price_stock4 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock4_p4_2') self.player.period1Price_stock5 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock5_p4_2') self.player.period1Price_stock6 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock6_p4_2') self.player.period1Price_stock7 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock7_p4_2') self.player.period1Price_stock8 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock8_p4_2') self.player.period1Price_stock9 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock9_p4_2') self.player.period1Price_stock10 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock10_p4_2') self.player.period1Price_stock11 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock11_p4_2') self.player.period1Price_stock12 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock12_p4_2') self.player.period1Price_stock13 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock13_p4_2') self.player.period1Price_stock14 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock14_p4_2') self.player.period1Price_stock15 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock15_p4_2') self.player.period1Price_stock16 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock16_p4_2') self.player.period1Price_stock17 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock17_p4_2') self.player.period1Price_stock18 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock18_p4_2') self.player.period1Price_stock19 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock19_p4_2') self.player.period1Price_stock20 = 30 + self.participant.vars["priceChangeDict_2"].get('Stock20_p4_2') self.createInitialPortfolio() #create player's portfolio def createInitialPortfolio(self): self.participant.vars['Portfolio'] = [] if self.player.Select_stock1_T2 == 'on': self.participant.vars['Portfolio'].append('Stock1') if self.player.Select_stock2_T2 == 'on': self.participant.vars['Portfolio'].append('Stock2') if self.player.Select_stock3_T2 == 'on': self.participant.vars['Portfolio'].append('Stock3') if self.player.Select_stock4_T2 == 'on': self.participant.vars['Portfolio'].append('Stock4') if self.player.Select_stock5_T2 == 'on': self.participant.vars['Portfolio'].append('Stock5') if self.player.Select_stock6_T2 == 'on': self.participant.vars['Portfolio'].append('Stock6') if self.player.Select_stock7_T2 == 'on': self.participant.vars['Portfolio'].append('Stock7') if self.player.Select_stock8_T2 == 'on': self.participant.vars['Portfolio'].append('Stock8') if self.player.Select_stock9_T2 == 'on': self.participant.vars['Portfolio'].append('Stock9') if self.player.Select_stock10_T2 == 'on': self.participant.vars['Portfolio'].append('Stock10') if self.player.Select_stock11_T2 == 'on': self.participant.vars['Portfolio'].append('Stock11') if self.player.Select_stock12_T2 == 'on': self.participant.vars['Portfolio'].append('Stock12') if self.player.Select_stock13_T2 == 'on': self.participant.vars['Portfolio'].append('Stock13') if self.player.Select_stock14_T2 == 'on': self.participant.vars['Portfolio'].append('Stock14') if self.player.Select_stock15_T2 == 'on': self.participant.vars['Portfolio'].append('Stock15') if self.player.Select_stock16_T2 == 'on': self.participant.vars['Portfolio'].append('Stock16') if self.player.Select_stock17_T2 == 'on': self.participant.vars['Portfolio'].append('Stock17') if self.player.Select_stock18_T2 == 'on': self.participant.vars['Portfolio'].append('Stock18') if self.player.Select_stock19_T2 == 'on': self.participant.vars['Portfolio'].append('Stock19') if self.player.Select_stock20_T2 == 'on': self.participant.vars['Portfolio'].append('Stock20') ###Show period 1 class Period1_show_2(Page): form_model = 'player' currentPeriod = 1 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period 1 price per row def showPrice(self): self.player.period1Price_row1 = 30 + self.player.period1PriceChange_row1 self.player.period1Price_row2 = 30 + self.player.period1PriceChange_row2 self.player.period1Price_row3 = 30 + self.player.period1PriceChange_row3 self.player.period1Price_row4 = 30 + self.player.period1PriceChange_row4 self.player.period1Price_row5 = 30 + self.player.period1PriceChange_row5 #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.showPriceChangeForStockName_T2(currentStock,self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold1 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold1 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.showPriceChangeForStockName_T2(currentStock,self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold1 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold1 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold1 = self.player.sellingPrice_T2(self.player.stockSold1, 1) #variables for template def vars_for_template(self): self.showStocks() self.player.period1PriceChange_row1 = self.player.showPriceChangeForStockName_T2(self.player.stockRow1,self.currentPeriod) self.player.period1PriceChange_row2 = self.player.showPriceChangeForStockName_T2(self.player.stockRow2,self.currentPeriod) self.player.period1PriceChange_row3 = self.player.showPriceChangeForStockName_T2(self.player.stockRow3,self.currentPeriod) self.player.period1PriceChange_row4 = self.player.showPriceChangeForStockName_T2(self.player.stockRow4,self.currentPeriod) self.player.period1PriceChange_row5 = self.player.showPriceChangeForStockName_T2(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod1Price_row1 = self.player.period1Price_row1 - self.player.period1PriceChange_row1 self.player.preperiod1Price_row2 = self.player.period1Price_row2 - self.player.period1PriceChange_row2 self.player.preperiod1Price_row3 = self.player.period1Price_row3 - self.player.period1PriceChange_row3 self.player.preperiod1Price_row4 = self.player.period1Price_row4 - self.player.period1PriceChange_row4 self.player.preperiod1Price_row5 = self.player.period1Price_row5 - self.player.period1PriceChange_row5 stock21_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock21_p1_2') stock21_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock21_p2_2') stock21_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock21_p3_2') stock22_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock22_p1_2') stock22_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock22_p2_2') stock22_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock22_p3_2') stock23_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock23_p1_2') stock23_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock23_p2_2') stock23_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock23_p3_2') stock24_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock24_p1_2') stock24_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock24_p2_2') stock24_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock24_p3_2') stock1_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock1_p4_2') stock2_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock2_p4_2') stock3_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock3_p4_2') stock4_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock4_p4_2') stock5_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock5_p4_2') stock6_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock6_p4_2') stock7_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock7_p4_2') stock8_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock8_p4_2') stock9_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock9_p4_2') stock10_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock10_p4_2') stock11_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock11_p4_2') stock12_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock12_p4_2') stock13_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock13_p4_2') stock14_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock14_p4_2') stock15_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock15_p4_2') stock16_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock16_p4_2') stock17_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock17_p4_2') stock18_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock18_p4_2') stock19_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock19_p4_2') stock20_p4_2 = self.participant.vars["priceChangeDict_2"].get('Stock20_p4_2') return dict( Stock21_p1 = self.add_plus_to_pos_numbers(stock21_p1_2), Stock21_p2 = self.add_plus_to_pos_numbers(stock21_p3_2), Stock21_p3 = self.add_plus_to_pos_numbers(stock21_p3_2), Stock22_p1 = self.add_plus_to_pos_numbers(stock22_p1_2), Stock22_p2 = self.add_plus_to_pos_numbers(stock22_p2_2), Stock22_p3 = self.add_plus_to_pos_numbers(stock22_p3_2), Stock23_p1 = self.add_plus_to_pos_numbers(stock23_p1_2), Stock23_p2 = self.add_plus_to_pos_numbers(stock23_p2_2), Stock23_p3 = self.add_plus_to_pos_numbers(stock23_p3_2), Stock24_p1 = self.add_plus_to_pos_numbers(stock24_p1_2), Stock24_p2 = self.add_plus_to_pos_numbers(stock24_p2_2), Stock24_p3 = self.add_plus_to_pos_numbers(stock24_p3_2), Stock1_period1Price = self.player.period1Price_stock1, Stock2_period1Price = self.player.period1Price_stock2, Stock3_period1Price = self.player.period1Price_stock3, Stock4_period1Price = self.player.period1Price_stock4, Stock5_period1Price = self.player.period1Price_stock5, Stock6_period1Price = self.player.period1Price_stock6, Stock7_period1Price = self.player.period1Price_stock7, Stock8_period1Price = self.player.period1Price_stock8, Stock9_period1Price = self.player.period1Price_stock9, Stock10_period1Price = self.player.period1Price_stock10, Stock11_period1Price = self.player.period1Price_stock11, Stock12_period1Price = self.player.period1Price_stock12, Stock13_period1Price = self.player.period1Price_stock13, Stock14_period1Price = self.player.period1Price_stock14, Stock15_period1Price = self.player.period1Price_stock15, Stock16_period1Price = self.player.period1Price_stock16, Stock17_period1Price = self.player.period1Price_stock17, Stock18_period1Price = self.player.period1Price_stock18, Stock19_period1Price = self.player.period1Price_stock19, Stock20_period1Price = self.player.period1Price_stock20, Stock1_period1Change = self.add_plus_to_pos_numbers(stock1_p4_2), Stock2_period1Change = self.add_plus_to_pos_numbers(stock2_p4_2), Stock3_period1Change = self.add_plus_to_pos_numbers(stock3_p4_2), Stock4_period1Change = self.add_plus_to_pos_numbers(stock4_p4_2), Stock5_period1Change = self.add_plus_to_pos_numbers(stock5_p4_2), Stock6_period1Change = self.add_plus_to_pos_numbers(stock6_p4_2), Stock7_period1Change = self.add_plus_to_pos_numbers(stock7_p4_2), Stock8_period1Change = self.add_plus_to_pos_numbers(stock8_p4_2), Stock9_period1Change = self.add_plus_to_pos_numbers(stock9_p4_2), Stock10_period1Change = self.add_plus_to_pos_numbers(stock10_p4_2), Stock11_period1Change = self.add_plus_to_pos_numbers(stock11_p4_2), Stock12_period1Change = self.add_plus_to_pos_numbers(stock12_p4_2), Stock13_period1Change = self.add_plus_to_pos_numbers(stock13_p4_2), Stock14_period1Change = self.add_plus_to_pos_numbers(stock14_p4_2), Stock15_period1Change = self.add_plus_to_pos_numbers(stock15_p4_2), Stock16_period1Change = self.add_plus_to_pos_numbers(stock16_p4_2), Stock17_period1Change = self.add_plus_to_pos_numbers(stock17_p4_2), Stock18_period1Change = self.add_plus_to_pos_numbers(stock18_p4_2), Stock19_period1Change = self.add_plus_to_pos_numbers(stock19_p4_2), Stock20_period1Change = self.add_plus_to_pos_numbers(stock20_p4_2), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period1Price_row1, PriceRow2 = self.player.period1Price_row2, PriceRow3 = self.player.period1Price_row3, PriceRow4 = self.player.period1Price_row4, PriceRow5 = self.player.period1Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period1PriceChange_row5), PrePriceRow1 = self.player.preperiod1Price_row1, PrePriceRow2 = self.player.preperiod1Price_row2, PrePriceRow3 = self.player.preperiod1Price_row3, PrePriceRow4 = self.player.preperiod1Price_row4, PrePriceRow5 = self.player.preperiod1Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 1 class Period1_choice_2(Page): form_model = 'player' form_fields = ['Select_choice2_T2'] currentPeriod = 1 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice2_T2 == 21: self.participant.vars['Portfolio'].append('Stock21') elif self.player.Select_choice2_T2 == 22: self.participant.vars['Portfolio'].append('Stock22') elif self.player.Select_choice2_T2 == 23: self.participant.vars['Portfolio'].append('Stock23') else: self.participant.vars['Portfolio'].append('Stock24') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold1) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock21_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock21_p1_2') stock21_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock21_p2_2') stock21_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock21_p3_2') stock22_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock22_p1_2') stock22_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock22_p2_2') stock22_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock22_p3_2') stock23_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock23_p1_2') stock23_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock23_p2_2') stock23_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock23_p3_2') stock24_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock24_p1_2') stock24_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock24_p2_2') stock24_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock24_p3_2') return dict( Stock21_p1 = 30 - stock21_p1_2, Stock21_p2 = 30 - stock21_p1_2 - stock21_p2_2, Stock21_p3 = 30 - stock21_p1_2 - stock21_p2_2 - stock21_p3_2, Stock22_p1 = 30 - stock22_p1_2, Stock22_p2 = 30 - stock22_p1_2 - stock22_p2_2, Stock22_p3 = 30 - stock22_p1_2 - stock22_p2_2 - stock22_p3_2, Stock23_p1 = 30 - stock23_p1_2, Stock23_p2 = 30 - stock23_p1_2 - stock23_p2_2, Stock23_p3 = 30 - stock23_p1_2 - stock23_p2_2 - stock23_p3_2, Stock24_p1 = 30 - stock24_p1_2, Stock24_p2 = 30 - stock24_p1_2 - stock24_p2_2, Stock24_p3 = 30 - stock24_p1_2 - stock24_p2_2 - stock24_p3_2, Stock1_period1Price = self.player.period1Price_stock1, Stock2_period1Price = self.player.period1Price_stock2, Stock3_period1Price = self.player.period1Price_stock3, Stock4_period1Price = self.player.period1Price_stock4, Stock5_period1Price = self.player.period1Price_stock5, Stock6_period1Price = self.player.period1Price_stock6, Stock7_period1Price = self.player.period1Price_stock7, Stock8_period1Price = self.player.period1Price_stock8, Stock9_period1Price = self.player.period1Price_stock9, Stock10_period1Price = self.player.period1Price_stock10, Stock11_period1Price = self.player.period1Price_stock11, Stock12_period1Price = self.player.period1Price_stock12, Stock13_period1Price = self.player.period1Price_stock13, Stock14_period1Price = self.player.period1Price_stock14, Stock15_period1Price = self.player.period1Price_stock15, Stock16_period1Price = self.player.period1Price_stock16, Stock17_period1Price = self.player.period1Price_stock17, Stock18_period1Price = self.player.period1Price_stock18, Stock19_period1Price = self.player.period1Price_stock19, Stock20_period1Price = self.player.period1Price_stock20, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period1Price_row1, PriceRow2 = self.player.period1Price_row2, PriceRow3 = self.player.period1Price_row3, PriceRow4 = self.player.period1Price_row4, PriceRow5 = self.player.period1Price_row5, StockSold1 = self.showName_nicely(self.player.stockSold1), SellingPriceSold1 = self.player.sellingPriceSold1, PriceDiff1 = self.player.sellingPriceSold1 -30, GAINLOSS1 = self.showDiff(), ) ###Show period 2 class Period2_show_2(Page): form_model = 'player' currentPeriod = 2 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period 2 price per row def showPrice(self): self.player.period2Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow1, self.currentPeriod)) self.player.period2Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow2, self.currentPeriod)) self.player.period2Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow3, self.currentPeriod)) self.player.period2Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow4, self.currentPeriod)) self.player.period2Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold2 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold2 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold2 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold2 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold2 = self.player.sellingPrice_T2(self.player.stockSold2, 2) #variables for template def vars_for_template(self): self.showStocks() self.player.period2PriceChange_row1 = self.player.showPriceChangeForStockName_T2(self.player.stockRow1,self.currentPeriod) self.player.period2PriceChange_row2 = self.player.showPriceChangeForStockName_T2(self.player.stockRow2,self.currentPeriod) self.player.period2PriceChange_row3 = self.player.showPriceChangeForStockName_T2(self.player.stockRow3,self.currentPeriod) self.player.period2PriceChange_row4 = self.player.showPriceChangeForStockName_T2(self.player.stockRow4,self.currentPeriod) self.player.period2PriceChange_row5 = self.player.showPriceChangeForStockName_T2(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod2Price_row1 = self.player.period2Price_row1 - self.player.period2PriceChange_row1 self.player.preperiod2Price_row2 = self.player.period2Price_row2 - self.player.period2PriceChange_row2 self.player.preperiod2Price_row3 = self.player.period2Price_row3 - self.player.period2PriceChange_row3 self.player.preperiod2Price_row4 = self.player.period2Price_row4 - self.player.period2PriceChange_row4 self.player.preperiod2Price_row5 = self.player.period2Price_row5 - self.player.period2PriceChange_row5 stock25_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock25_p1_2') stock25_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock25_p2_2') stock25_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock25_p3_2') stock26_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock26_p1_2') stock26_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock26_p2_2') stock26_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock26_p3_2') stock27_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock27_p1_2') stock27_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock27_p2_2') stock27_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock27_p3_2') stock28_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock28_p1_2') stock28_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock28_p2_2') stock28_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock28_p3_2') return dict( Stock25_p1 = self.add_plus_to_pos_numbers(stock25_p1_2), Stock25_p2 = self.add_plus_to_pos_numbers(stock25_p2_2), Stock25_p3 = self.add_plus_to_pos_numbers(stock25_p3_2), Stock26_p1 = self.add_plus_to_pos_numbers(stock26_p1_2), Stock26_p2 = self.add_plus_to_pos_numbers(stock26_p2_2), Stock26_p3 = self.add_plus_to_pos_numbers(stock26_p3_2), Stock27_p1 = self.add_plus_to_pos_numbers(stock27_p1_2), Stock27_p2 = self.add_plus_to_pos_numbers(stock27_p2_2), Stock27_p3 = self.add_plus_to_pos_numbers(stock27_p3_2), Stock28_p1 = self.add_plus_to_pos_numbers(stock28_p1_2), Stock28_p2 = self.add_plus_to_pos_numbers(stock28_p2_2), Stock28_p3 = self.add_plus_to_pos_numbers(stock28_p3_2), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period2Price_row1, PriceRow2 = self.player.period2Price_row2, PriceRow3 = self.player.period2Price_row3, PriceRow4 = self.player.period2Price_row4, PriceRow5 = self.player.period2Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period2PriceChange_row5), PrePriceRow1 = self.player.preperiod2Price_row1, PrePriceRow2 = self.player.preperiod2Price_row2, PrePriceRow3 = self.player.preperiod2Price_row3, PrePriceRow4 = self.player.preperiod2Price_row4, PrePriceRow5 = self.player.preperiod2Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ### Choice period 2 class Period2_choice_2(Page): form_model = 'player' form_fields = ['Select_choice3_T2'] currentPeriod = 2 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice3_T2 == 25: self.participant.vars['Portfolio'].append('Stock25') elif self.player.Select_choice3_T2 == 26: self.participant.vars['Portfolio'].append('Stock26') elif self.player.Select_choice3_T2 == 27: self.participant.vars['Portfolio'].append('Stock27') else: self.participant.vars['Portfolio'].append('Stock28') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold2) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock25_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock25_p1_2') stock25_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock25_p2_2') stock25_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock25_p3_2') stock26_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock26_p1_2') stock26_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock26_p2_2') stock26_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock26_p3_2') stock27_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock27_p1_2') stock27_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock27_p2_2') stock27_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock27_p3_2') stock28_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock28_p1_2') stock28_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock28_p2_2') stock28_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock28_p3_2') return dict( Stock25_p1 = 30 - stock25_p1_2, Stock25_p2 = 30 - stock25_p1_2 - stock25_p2_2, Stock25_p3 = 30 - stock25_p1_2 - stock25_p2_2 - stock25_p3_2, Stock26_p1 = 30 - stock26_p1_2, Stock26_p2 = 30 - stock26_p1_2 - stock26_p2_2, Stock26_p3 = 30 - stock26_p1_2 - stock26_p2_2 - stock26_p3_2, Stock27_p1 = 30 - stock27_p1_2, Stock27_p2 = 30 - stock27_p1_2 - stock27_p2_2, Stock27_p3 = 30 - stock27_p1_2 - stock27_p2_2 - stock27_p3_2, Stock28_p1 = 30 - stock28_p1_2, Stock28_p2 = 30 - stock28_p1_2 - stock28_p2_2, Stock28_p3 = 30 - stock28_p1_2 - stock28_p2_2 - stock28_p3_2, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period2Price_row1, PriceRow2 = self.player.period2Price_row2, PriceRow3 = self.player.period2Price_row3, PriceRow4 = self.player.period2Price_row4, PriceRow5 = self.player.period2Price_row5, StockSold1 = self.showName_nicely(self.player.stockSold1), StockSold2 = self.showName_nicely(self.player.stockSold2), SellingPriceSold2 = self.player.sellingPriceSold2, PriceDiff2 = self.player.sellingPriceSold2 -30, GAINLOSS2 = self.showDiff(), ) ###Show period 3 class Period3_show_2(Page): form_model = 'player' currentPeriod = 3 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period 3 price per row def showPrice(self): self.player.period3Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow1, self.currentPeriod)) self.player.period3Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow2, self.currentPeriod)) self.player.period3Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow3, self.currentPeriod)) self.player.period3Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow4, self.currentPeriod)) self.player.period3Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold3 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold3 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold3 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold3 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold3 = self.player.sellingPrice_T2(self.player.stockSold3, 3) #variables for template def vars_for_template(self): self.showStocks() self.player.period3PriceChange_row1 = self.player.showPriceChangeForStockName_T2(self.player.stockRow1,self.currentPeriod) self.player.period3PriceChange_row2 = self.player.showPriceChangeForStockName_T2(self.player.stockRow2,self.currentPeriod) self.player.period3PriceChange_row3 = self.player.showPriceChangeForStockName_T2(self.player.stockRow3,self.currentPeriod) self.player.period3PriceChange_row4 = self.player.showPriceChangeForStockName_T2(self.player.stockRow4,self.currentPeriod) self.player.period3PriceChange_row5 = self.player.showPriceChangeForStockName_T2(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod3Price_row1 = self.player.period3Price_row1 - self.player.period3PriceChange_row1 self.player.preperiod3Price_row2 = self.player.period3Price_row2 - self.player.period3PriceChange_row2 self.player.preperiod3Price_row3 = self.player.period3Price_row3 - self.player.period3PriceChange_row3 self.player.preperiod3Price_row4 = self.player.period3Price_row4 - self.player.period3PriceChange_row4 self.player.preperiod3Price_row5 = self.player.period3Price_row5 - self.player.period3PriceChange_row5 stock29_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock29_p1_2') stock29_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock29_p2_2') stock29_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock29_p3_2') stock30_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock30_p1_2') stock30_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock30_p2_2') stock30_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock30_p3_2') stock31_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock31_p1_2') stock31_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock31_p2_2') stock31_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock31_p3_2') stock32_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock32_p1_2') stock32_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock32_p2_2') stock32_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock32_p3_2') return dict( Stock29_p1 = self.add_plus_to_pos_numbers(stock29_p1_2), Stock29_p2 = self.add_plus_to_pos_numbers(stock29_p2_2), Stock29_p3 = self.add_plus_to_pos_numbers(stock29_p3_2), Stock30_p1 = self.add_plus_to_pos_numbers(stock30_p1_2), Stock30_p2 = self.add_plus_to_pos_numbers(stock30_p2_2), Stock30_p3 = self.add_plus_to_pos_numbers(stock30_p3_2), Stock31_p1 = self.add_plus_to_pos_numbers(stock31_p1_2), Stock31_p2 = self.add_plus_to_pos_numbers(stock31_p2_2), Stock31_p3 = self.add_plus_to_pos_numbers(stock31_p3_2), Stock32_p1 = self.add_plus_to_pos_numbers(stock32_p1_2), Stock32_p2 = self.add_plus_to_pos_numbers(stock32_p2_2), Stock32_p3 = self.add_plus_to_pos_numbers(stock32_p3_2), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period3Price_row1, PriceRow2 = self.player.period3Price_row2, PriceRow3 = self.player.period3Price_row3, PriceRow4 = self.player.period3Price_row4, PriceRow5 = self.player.period3Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period3PriceChange_row5), PrePriceRow1 = self.player.preperiod3Price_row1, PrePriceRow2 = self.player.preperiod3Price_row2, PrePriceRow3 = self.player.preperiod3Price_row3, PrePriceRow4 = self.player.preperiod3Price_row4, PrePriceRow5 = self.player.preperiod3Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 3 class Period3_choice_2(Page): form_model = 'player' form_fields = ['Select_choice4_T2'] currentPeriod = 3 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice4_T2 == 29: self.participant.vars['Portfolio'].append('Stock29') elif self.player.Select_choice4_T2 == 30: self.participant.vars['Portfolio'].append('Stock30') elif self.player.Select_choice4_T2 == 31: self.participant.vars['Portfolio'].append('Stock31') else: self.participant.vars['Portfolio'].append('Stock32') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold3) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock29_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock29_p1_2') stock29_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock29_p2_2') stock29_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock29_p3_2') stock30_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock30_p1_2') stock30_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock30_p2_2') stock30_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock30_p3_2') stock31_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock31_p1_2') stock31_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock31_p2_2') stock31_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock31_p3_2') stock32_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock32_p1_2') stock32_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock32_p2_2') stock32_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock32_p3_2') return dict( Stock29_p1 = 30 - stock29_p1_2, Stock29_p2 = 30 - stock29_p1_2 - stock29_p2_2, Stock29_p3 = 30 - stock29_p1_2 - stock29_p2_2 - stock29_p3_2, Stock30_p1 = 30 - stock30_p1_2, Stock30_p2 = 30 - stock30_p1_2 - stock30_p2_2, Stock30_p3 = 30 - stock30_p1_2 - stock30_p2_2 - stock30_p3_2, Stock31_p1 = 30 - stock31_p1_2, Stock31_p2 = 30 - stock31_p1_2 - stock31_p2_2, Stock31_p3 = 30 - stock31_p1_2 - stock31_p2_2 - stock31_p3_2, Stock32_p1 = 30 - stock32_p1_2, Stock32_p2 = 30 - stock32_p1_2 - stock32_p2_2, Stock32_p3 = 30 - stock32_p1_2 - stock32_p2_2 - stock32_p3_2, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period3Price_row1, PriceRow2 = self.player.period3Price_row2, PriceRow3 = self.player.period3Price_row3, PriceRow4 = self.player.period3Price_row4, PriceRow5 = self.player.period3Price_row5, StockSold1 = self.showName_nicely(self.player.stockSold1), StockSold2 = self.showName_nicely(self.player.stockSold2), StockSold3 = self.showName_nicely(self.player.stockSold3), SellingPriceSold3 = self.player.sellingPriceSold3, PriceDiff3 = self.player.sellingPriceSold3 -30, GAINLOSS3 = self.showDiff(), ) ###Show period 4 class Period4_show_2(Page): form_model = 'player' currentPeriod = 4 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period4Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow1, self.currentPeriod)) self.player.period4Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow2, self.currentPeriod)) self.player.period4Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow3, self.currentPeriod)) self.player.period4Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow4, self.currentPeriod)) self.player.period4Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold4 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold4 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold4 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold4 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold4 = self.player.sellingPrice_T2(self.player.stockSold4, 4) #variables for template def vars_for_template(self): self.showStocks() self.player.period4PriceChange_row1 = self.player.showPriceChangeForStockName_T2(self.player.stockRow1,self.currentPeriod) self.player.period4PriceChange_row2 = self.player.showPriceChangeForStockName_T2(self.player.stockRow2,self.currentPeriod) self.player.period4PriceChange_row3 = self.player.showPriceChangeForStockName_T2(self.player.stockRow3,self.currentPeriod) self.player.period4PriceChange_row4 = self.player.showPriceChangeForStockName_T2(self.player.stockRow4,self.currentPeriod) self.player.period4PriceChange_row5 = self.player.showPriceChangeForStockName_T2(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod4Price_row1 = self.player.period4Price_row1 - self.player.period4PriceChange_row1 self.player.preperiod4Price_row2 = self.player.period4Price_row2 - self.player.period4PriceChange_row2 self.player.preperiod4Price_row3 = self.player.period4Price_row3 - self.player.period4PriceChange_row3 self.player.preperiod4Price_row4 = self.player.period4Price_row4 - self.player.period4PriceChange_row4 self.player.preperiod4Price_row5 = self.player.period4Price_row5 - self.player.period4PriceChange_row5 stock33_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock33_p1_2') stock33_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock33_p2_2') stock33_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock33_p3_2') stock34_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock34_p1_2') stock34_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock34_p2_2') stock34_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock34_p3_2') stock35_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock35_p1_2') stock35_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock35_p2_2') stock35_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock35_p3_2') stock36_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock36_p1_2') stock36_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock36_p2_2') stock36_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock36_p3_2') return dict( Stock33_p1 = self.add_plus_to_pos_numbers(stock33_p1_2), Stock33_p2 = self.add_plus_to_pos_numbers(stock33_p2_2), Stock33_p3 = self.add_plus_to_pos_numbers(stock33_p3_2), Stock34_p1 = self.add_plus_to_pos_numbers(stock34_p1_2), Stock34_p2 = self.add_plus_to_pos_numbers(stock34_p2_2), Stock34_p3 = self.add_plus_to_pos_numbers(stock34_p3_2), Stock35_p1 = self.add_plus_to_pos_numbers(stock35_p1_2), Stock35_p2 = self.add_plus_to_pos_numbers(stock35_p2_2), Stock35_p3 = self.add_plus_to_pos_numbers(stock35_p3_2), Stock36_p1 = self.add_plus_to_pos_numbers(stock36_p1_2), Stock36_p2 = self.add_plus_to_pos_numbers(stock36_p2_2), Stock36_p3 = self.add_plus_to_pos_numbers(stock36_p3_2), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period4Price_row1, PriceRow2 = self.player.period4Price_row2, PriceRow3 = self.player.period4Price_row3, PriceRow4 = self.player.period4Price_row4, PriceRow5 = self.player.period4Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period4PriceChange_row5), PrePriceRow1 = self.player.preperiod4Price_row1, PrePriceRow2 = self.player.preperiod4Price_row2, PrePriceRow3 = self.player.preperiod4Price_row3, PrePriceRow4 = self.player.preperiod4Price_row4, PrePriceRow5 = self.player.preperiod4Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 4 class Period4_choice_2(Page): form_model = 'player' form_fields = ['Select_choice5_T2'] currentPeriod = 4 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice5_T2 == 33: self.participant.vars['Portfolio'].append('Stock33') elif self.player.Select_choice5_T2 == 34: self.participant.vars['Portfolio'].append('Stock34') elif self.player.Select_choice5_T2 == 35: self.participant.vars['Portfolio'].append('Stock35') else: self.participant.vars['Portfolio'].append('Stock36') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold4) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock33_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock33_p1_2') stock33_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock33_p2_2') stock33_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock33_p3_2') stock34_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock34_p1_2') stock34_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock34_p2_2') stock34_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock34_p3_2') stock35_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock35_p1_2') stock35_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock35_p2_2') stock35_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock35_p3_2') stock36_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock36_p1_2') stock36_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock36_p2_2') stock36_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock36_p3_2') return dict( Stock33_p1 = 30 - stock33_p1_2, Stock33_p2 = 30 - stock33_p1_2 - stock33_p2_2, Stock33_p3 = 30 - stock33_p1_2 - stock33_p2_2 - stock33_p3_2, Stock34_p1 = 30 - stock34_p1_2, Stock34_p2 = 30 - stock34_p1_2 - stock34_p2_2, Stock34_p3 = 30 - stock34_p1_2 - stock34_p2_2 - stock34_p3_2, Stock35_p1 = 30 - stock35_p1_2, Stock35_p2 = 30 - stock35_p1_2 - stock35_p2_2, Stock35_p3 = 30 - stock35_p1_2 - stock35_p2_2 - stock35_p3_2, Stock36_p1 = 30 - stock36_p1_2, Stock36_p2 = 30 - stock36_p1_2 - stock36_p2_2, Stock36_p3 = 30 - stock36_p1_2 - stock36_p2_2 - stock36_p3_2, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period4Price_row1, PriceRow2 = self.player.period4Price_row2, PriceRow3 = self.player.period4Price_row3, PriceRow4 = self.player.period4Price_row4, PriceRow5 = self.player.period4Price_row5, StockSold3 = self.showName_nicely(self.player.stockSold3), StockSold4 = self.showName_nicely(self.player.stockSold4), SellingPriceSold4 = self.player.sellingPriceSold4, PriceDiff4 = self.player.sellingPriceSold4 -30, GAINLOSS4 = self.showDiff(), ) ###Show period 5 class Period5_show_2(Page): form_model = 'player' currentPeriod = 5 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period5Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow1, self.currentPeriod)) self.player.period5Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow2, self.currentPeriod)) self.player.period5Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow3, self.currentPeriod)) self.player.period5Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow4, self.currentPeriod)) self.player.period5Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold5 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold5 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold5 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold5 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold5 = self.player.sellingPrice_T2(self.player.stockSold5, 5) #variables for template def vars_for_template(self): self.showStocks() self.player.period5PriceChange_row1 = self.player.showPriceChangeForStockName_T2(self.player.stockRow1,self.currentPeriod) self.player.period5PriceChange_row2 = self.player.showPriceChangeForStockName_T2(self.player.stockRow2,self.currentPeriod) self.player.period5PriceChange_row3 = self.player.showPriceChangeForStockName_T2(self.player.stockRow3,self.currentPeriod) self.player.period5PriceChange_row4 = self.player.showPriceChangeForStockName_T2(self.player.stockRow4,self.currentPeriod) self.player.period5PriceChange_row5 = self.player.showPriceChangeForStockName_T2(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod5Price_row1 = self.player.period5Price_row1 - self.player.period5PriceChange_row1 self.player.preperiod5Price_row2 = self.player.period5Price_row2 - self.player.period5PriceChange_row2 self.player.preperiod5Price_row3 = self.player.period5Price_row3 - self.player.period5PriceChange_row3 self.player.preperiod5Price_row4 = self.player.period5Price_row4 - self.player.period5PriceChange_row4 self.player.preperiod5Price_row5 = self.player.period5Price_row5 - self.player.period5PriceChange_row5 stock37_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock37_p1_2') stock37_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock37_p2_2') stock37_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock37_p3_2') stock38_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock38_p1_2') stock38_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock38_p2_2') stock38_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock38_p3_2') stock39_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock39_p1_2') stock39_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock39_p2_2') stock39_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock39_p3_2') stock40_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock40_p1_2') stock40_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock40_p2_2') stock40_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock40_p3_2') return dict( Stock37_p1 = self.add_plus_to_pos_numbers(stock37_p1_2), Stock37_p2 = self.add_plus_to_pos_numbers(stock37_p2_2), Stock37_p3 = self.add_plus_to_pos_numbers(stock37_p3_2), Stock38_p1 = self.add_plus_to_pos_numbers(stock38_p1_2), Stock38_p2 = self.add_plus_to_pos_numbers(stock38_p2_2), Stock38_p3 = self.add_plus_to_pos_numbers(stock38_p3_2), Stock39_p1 = self.add_plus_to_pos_numbers(stock39_p1_2), Stock39_p2 = self.add_plus_to_pos_numbers(stock39_p2_2), Stock39_p3 = self.add_plus_to_pos_numbers(stock39_p3_2), Stock40_p1 = self.add_plus_to_pos_numbers(stock40_p1_2), Stock40_p2 = self.add_plus_to_pos_numbers(stock40_p2_2), Stock40_p3 = self.add_plus_to_pos_numbers(stock40_p3_2), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period5Price_row1, PriceRow2 = self.player.period5Price_row2, PriceRow3 = self.player.period5Price_row3, PriceRow4 = self.player.period5Price_row4, PriceRow5 = self.player.period5Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period5PriceChange_row5), PrePriceRow1 = self.player.preperiod5Price_row1, PrePriceRow2 = self.player.preperiod5Price_row2, PrePriceRow3 = self.player.preperiod5Price_row3, PrePriceRow4 = self.player.preperiod5Price_row4, PrePriceRow5 = self.player.preperiod5Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 5 class Period5_choice_2(Page): form_model = 'player' form_fields = ['Select_choice6_T2'] currentPeriod = 5 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice6_T2 == 37: self.participant.vars['Portfolio'].append('Stock37') elif self.player.Select_choice6_T2 == 38: self.participant.vars['Portfolio'].append('Stock38') elif self.player.Select_choice6_T2 == 39: self.participant.vars['Portfolio'].append('Stock39') else: self.participant.vars['Portfolio'].append('Stock40') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold5) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock37_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock37_p1_2') stock37_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock37_p2_2') stock37_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock37_p3_2') stock38_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock38_p1_2') stock38_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock38_p2_2') stock38_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock38_p3_2') stock39_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock39_p1_2') stock39_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock39_p2_2') stock39_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock39_p3_2') stock40_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock40_p1_2') stock40_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock40_p2_2') stock40_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock40_p3_2') return dict( Stock37_p1 = 30 - stock37_p1_2, Stock37_p2 = 30 - stock37_p1_2 - stock37_p2_2, Stock37_p3 = 30 - stock37_p1_2 - stock37_p2_2 - stock37_p3_2, Stock38_p1 = 30 - stock38_p1_2, Stock38_p2 = 30 - stock38_p1_2 - stock38_p2_2, Stock38_p3 = 30 - stock38_p1_2 - stock38_p2_2 - stock38_p3_2, Stock39_p1 = 30 - stock39_p1_2, Stock39_p2 = 30 - stock39_p1_2 - stock39_p2_2, Stock39_p3 = 30 - stock39_p1_2 - stock39_p2_2 - stock39_p3_2, Stock40_p1 = 30 - stock40_p1_2, Stock40_p2 = 30 - stock40_p1_2 - stock40_p2_2, Stock40_p3 = 30 - stock40_p1_2 - stock40_p2_2 - stock40_p3_2, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period5Price_row1, PriceRow2 = self.player.period5Price_row2, PriceRow3 = self.player.period5Price_row3, PriceRow4 = self.player.period5Price_row4, PriceRow5 = self.player.period5Price_row5, StockSold4 = self.showName_nicely(self.player.stockSold4), StockSold5 = self.showName_nicely(self.player.stockSold5), SellingPriceSold5 = self.player.sellingPriceSold5, PriceDiff5 = self.player.sellingPriceSold5 -30, GAINLOSS5 = self.showDiff(), ) ###Show period 6 class Period6_show_2(Page): form_model = 'player' currentPeriod = 6 def add_plus_to_pos_numbers(self, int_num): if int_num >= 0: return '%+g' % int_num else: return '%-g' % int_num #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period6Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow1, self.currentPeriod)) self.player.period6Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow2, self.currentPeriod)) self.player.period6Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow3, self.currentPeriod)) self.player.period6Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow4, self.currentPeriod)) self.player.period6Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow5, self.currentPeriod)) #sell a stock def sellStock(self): import random if (self.player.Treatment == 'SellingGains'): self.participant.vars['gainersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) <= 0: self.participant.vars['gainersPortfolio'].remove(currentStock) if len(self.participant.vars['gainersPortfolio']) > 0: pickGainerStockFromPortfolio = random.choice(self.participant.vars['gainersPortfolio']) self.participant.vars['Portfolio'].remove(pickGainerStockFromPortfolio) self.player.stockSold6 = pickGainerStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold6 = randomStockFromPortfolio else: self.participant.vars['losersPortfolio'] = self.participant.vars['Portfolio'].copy() for currentStock in self.participant.vars['Portfolio']: if self.player.sumPriceChangesInPeriod_T2(currentStock, self.currentPeriod) >= 0: self.participant.vars['losersPortfolio'].remove(currentStock) if len(self.participant.vars['losersPortfolio']) > 0: pickLoserStockFromPortfolio = random.choice(self.participant.vars['losersPortfolio']) self.participant.vars['Portfolio'].remove(pickLoserStockFromPortfolio) self.player.stockSold6 = pickLoserStockFromPortfolio else: randomStockFromPortfolio = random.choice(self.participant.vars['Portfolio']) self.participant.vars['Portfolio'].remove(randomStockFromPortfolio) self.player.stockSold6 = randomStockFromPortfolio def before_next_page(self): self.sellStock() self.player.sellingPriceSold6 = self.player.sellingPrice_T2(self.player.stockSold6, 6) #variables for template def vars_for_template(self): self.showStocks() self.player.period6PriceChange_row1 = self.player.showPriceChangeForStockName_T2(self.player.stockRow1,self.currentPeriod) self.player.period6PriceChange_row2 = self.player.showPriceChangeForStockName_T2(self.player.stockRow2,self.currentPeriod) self.player.period6PriceChange_row3 = self.player.showPriceChangeForStockName_T2(self.player.stockRow3,self.currentPeriod) self.player.period6PriceChange_row4 = self.player.showPriceChangeForStockName_T2(self.player.stockRow4,self.currentPeriod) self.player.period6PriceChange_row5 = self.player.showPriceChangeForStockName_T2(self.player.stockRow5,self.currentPeriod) self.showPrice() #pre period price per row self.player.preperiod6Price_row1 = self.player.period6Price_row1 - self.player.period6PriceChange_row1 self.player.preperiod6Price_row2 = self.player.period6Price_row2 - self.player.period6PriceChange_row2 self.player.preperiod6Price_row3 = self.player.period6Price_row3 - self.player.period6PriceChange_row3 self.player.preperiod6Price_row4 = self.player.period6Price_row4 - self.player.period6PriceChange_row4 self.player.preperiod6Price_row5 = self.player.period6Price_row5 - self.player.period6PriceChange_row5 stock41_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock41_p1_2') stock41_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock41_p2_2') stock41_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock41_p3_2') stock42_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock42_p1_2') stock42_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock42_p2_2') stock42_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock42_p3_2') stock43_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock43_p1_2') stock43_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock43_p2_2') stock43_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock43_p3_2') stock44_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock44_p1_2') stock44_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock44_p2_2') stock44_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock44_p3_2') return dict( Stock41_p1 = self.add_plus_to_pos_numbers(stock41_p1_2), Stock41_p2 = self.add_plus_to_pos_numbers(stock41_p2_2), Stock41_p3 = self.add_plus_to_pos_numbers(stock41_p3_2), Stock42_p1 = self.add_plus_to_pos_numbers(stock42_p1_2), Stock42_p2 = self.add_plus_to_pos_numbers(stock42_p2_2), Stock42_p3 = self.add_plus_to_pos_numbers(stock42_p3_2), Stock43_p1 = self.add_plus_to_pos_numbers(stock43_p1_2), Stock43_p2 = self.add_plus_to_pos_numbers(stock43_p2_2), Stock43_p3 = self.add_plus_to_pos_numbers(stock43_p3_2), Stock44_p1 = self.add_plus_to_pos_numbers(stock44_p1_2), Stock44_p2 = self.add_plus_to_pos_numbers(stock44_p2_2), Stock44_p3 = self.add_plus_to_pos_numbers(stock44_p3_2), StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period6Price_row1, PriceRow2 = self.player.period6Price_row2, PriceRow3 = self.player.period6Price_row3, PriceRow4 = self.player.period6Price_row4, PriceRow5 = self.player.period6Price_row5, PriceChangeRow1 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row1), PriceChangeRow2 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row2), PriceChangeRow3 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row3), PriceChangeRow4 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row4), PriceChangeRow5 = self.add_plus_to_pos_numbers(self.player.period6PriceChange_row5), PrePriceRow1 = self.player.preperiod6Price_row1, PrePriceRow2 = self.player.preperiod6Price_row2, PrePriceRow3 = self.player.preperiod6Price_row3, PrePriceRow4 = self.player.preperiod6Price_row4, PrePriceRow5 = self.player.preperiod6Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ) ###Choice period 6 class Period6_choice_2(Page): form_model = 'player' form_fields = ['Select_choice7_T2'] currentPeriod = 6 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #add new stock to portfolio def addStock(self): if self.player.Select_choice7_T2 == 41: self.participant.vars['Portfolio'].append('Stock41') elif self.player.Select_choice7_T2 == 42: self.participant.vars['Portfolio'].append('Stock42') elif self.player.Select_choice7_T2 == 43: self.participant.vars['Portfolio'].append('Stock43') else: self.participant.vars['Portfolio'].append('Stock44') def before_next_page(self): self.addStock() #gain and loss sentence def showDiff(self): if (self.player.sellingPriceSold6) - 30 >= 0: return "GAIN" else: return "LOSS" #variables for template def vars_for_template(self): stock41_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock41_p1_2') stock41_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock41_p2_2') stock41_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock41_p3_2') stock42_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock42_p1_2') stock42_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock42_p2_2') stock42_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock42_p3_2') stock43_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock43_p1_2') stock43_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock43_p2_2') stock43_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock43_p3_2') stock44_p1_2 = self.participant.vars["priceChangeDict_2"].get('Stock44_p1_2') stock44_p2_2 = self.participant.vars["priceChangeDict_2"].get('Stock44_p2_2') stock44_p3_2 = self.participant.vars["priceChangeDict_2"].get('Stock44_p3_2') return dict( Stock41_p1 = 30 - stock41_p1_2, Stock41_p2 = 30 - stock41_p1_2 - stock41_p2_2, Stock41_p3 = 30 - stock41_p1_2 - stock41_p2_2 - stock41_p3_2, Stock42_p1 = 30 - stock42_p1_2, Stock42_p2 = 30 - stock42_p1_2 - stock42_p2_2, Stock42_p3 = 30 - stock42_p1_2 - stock42_p2_2 - stock42_p3_2, Stock43_p1 = 30 - stock43_p1_2, Stock43_p2 = 30 - stock43_p1_2 - stock43_p2_2, Stock43_p3 = 30 - stock43_p1_2 - stock43_p2_2 - stock43_p3_2, Stock44_p1 = 30 - stock44_p1_2, Stock44_p2 = 30 - stock44_p1_2 - stock44_p2_2, Stock44_p3 = 30 - stock44_p1_2 - stock44_p2_2 - stock44_p3_2, StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period6Price_row1, PriceRow2 = self.player.period6Price_row2, PriceRow3 = self.player.period6Price_row3, PriceRow4 = self.player.period6Price_row4, PriceRow5 = self.player.period6Price_row5, StockSold5 = self.showName_nicely(self.player.stockSold5), StockSold6 = self.showName_nicely(self.player.stockSold6), SellingPriceSold6 = self.player.sellingPriceSold6, PriceDiff6 = self.player.sellingPriceSold6 -30, GAINLOSS6 = self.showDiff(), ) ### period 7 class Period7_show_2(Page): form_model = 'player' currentPeriod = 7 #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio']) self.player.stockRow1 = currentPortfolio[0] self.player.stockRow2 = currentPortfolio[1] self.player.stockRow3 = currentPortfolio[2] self.player.stockRow4 = currentPortfolio[3] self.player.stockRow5 = currentPortfolio[4] return #period price per row def showPrice(self): self.player.period7Price_row1 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow1, self.currentPeriod)) self.player.period7Price_row2 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow2, self.currentPeriod)) self.player.period7Price_row3 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow3, self.currentPeriod)) self.player.period7Price_row4 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow4, self.currentPeriod)) self.player.period7Price_row5 = (30 + self.player.sumPriceChangesInPeriod_T2(self.player.stockRow5, self.currentPeriod)) #sell all stocks def sellStock(self): self.player.stockSold7 = self.player.stockRow1 self.player.stockSold8 = self.player.stockRow2 self.player.stockSold9 = self.player.stockRow3 self.player.stockSold10 = self.player.stockRow4 self.player.stockSold11 = self.player.stockRow5 #variables for template def vars_for_template(self): self.showStocks() self.player.period7PriceChange_row1 = self.player.showPriceChangeForStockName_T2(self.player.stockRow1,self.currentPeriod) self.player.period7PriceChange_row2 = self.player.showPriceChangeForStockName_T2(self.player.stockRow2,self.currentPeriod) self.player.period7PriceChange_row3 = self.player.showPriceChangeForStockName_T2(self.player.stockRow3,self.currentPeriod) self.player.period7PriceChange_row4 = self.player.showPriceChangeForStockName_T2(self.player.stockRow4,self.currentPeriod) self.player.period7PriceChange_row5 = self.player.showPriceChangeForStockName_T2(self.player.stockRow5,self.currentPeriod) self.showPrice() self.sellStock() self.player.sellingPriceSold7 = self.player.sellingPrice_T2(self.player.stockSold7, 7) self.player.sellingPriceSold8 = self.player.sellingPrice_T2(self.player.stockSold8, 7) self.player.sellingPriceSold9 = self.player.sellingPrice_T2(self.player.stockSold9, 7) self.player.sellingPriceSold10 = self.player.sellingPrice_T2(self.player.stockSold10, 7) self.player.sellingPriceSold11 = self.player.sellingPrice_T2(self.player.stockSold11, 7) #pre period price per row self.player.preperiod7Price_row1 = self.player.period7Price_row1 - self.player.period7PriceChange_row1 self.player.preperiod7Price_row2 = self.player.period7Price_row2 - self.player.period7PriceChange_row2 self.player.preperiod7Price_row3 = self.player.period7Price_row3 - self.player.period7PriceChange_row3 self.player.preperiod7Price_row4 = self.player.period7Price_row4 - self.player.period7PriceChange_row4 self.player.preperiod7Price_row5 = self.player.period7Price_row5 - self.player.period7PriceChange_row5 return dict( StockRow1 = self.showName_nicely(self.player.stockRow1), StockRow2 = self.showName_nicely(self.player.stockRow2), StockRow3 = self.showName_nicely(self.player.stockRow3), StockRow4 = self.showName_nicely(self.player.stockRow4), StockRow5 = self.showName_nicely(self.player.stockRow5), PriceRow1 = self.player.period7Price_row1, PriceRow2 = self.player.period7Price_row2, PriceRow3 = self.player.period7Price_row3, PriceRow4 = self.player.period7Price_row4, PriceRow5 = self.player.period7Price_row5, StockSold6 = self.showName_nicely(self.player.stockSold6), StockSold7 = self.showName_nicely(self.player.stockSold7), SellingPriceSold6 = self.player.sellingPriceSold6, PrePriceRow1 = self.player.preperiod7Price_row1, PrePriceRow2 = self.player.preperiod7Price_row2, PrePriceRow3 = self.player.preperiod7Price_row3, PrePriceRow4 = self.player.preperiod7Price_row4, PrePriceRow5 = self.player.preperiod7Price_row5, CashPosition_choice = self.player.currentCash_choice(self.currentPeriod), ValuePeriod7 = self.player.sellingPriceSold11 + self.player.sellingPriceSold10 + self.player.sellingPriceSold9 + self.player.sellingPriceSold8 + self.player.sellingPriceSold7, ) ###IntroQuestions class Questions(Page): form_model = 'player' ###Confidence1 class Confidence1(Page): form_model = 'player' form_fields = ['confidence1'] ###Surprise class Surprise(Page): form_model = 'player' form_fields = ['surprise'] def calculateInterval(self, confidenceValue): self.player.surpriseValueAnte = confidenceValue -10 self.player.surpriseValuePost = confidenceValue +10 def createSurpriseSentence(self, value1, value2, confidenceValue): if confidenceValue == 11: self.player.surpriseSentence = 'less than ' + str(value1) elif confidenceValue == 0: self.player.surpriseSentence = 'more than ' + str(value2) else: self.player.surpriseSentence = 'less than ' + str(value1) + ' or more than ' + str(value2) def vars_for_template(self): self.calculateInterval(self.player.confidence3) self.createSurpriseSentence(self.player.surpriseValueAnte, self.player.surpriseValuePost, self.player.confidence1) return dict( SurpriseAnte = self.player.surpriseValueAnte, SurprisePost = self.player.surpriseValuePost, SurpriseSentence = self.player.surpriseSentence, ) ###Confidence2 class Confidence2(Page): form_model = 'player' form_fields = ['confidence2'] ###Confidence3 class Confidence3(Page): form_model = 'player' form_fields = ['confidence3'] ###Confidence4 class Confidence4(Page): form_model = 'player' form_fields = ['confidence4'] ###Memory class Memory(Page): form_model = 'player' form_fields = ['memory'] ###Task 2 class Task2(Page): form_model = 'player' form_fields = ['Select_stock50', 'Select_stock51', 'Select_stock52', 'Select_stock53', 'Select_stock54', 'Select_stock55', 'Select_stock56', 'Select_stock57', 'Select_stock58', 'Select_stock59', 'Select_stock60', 'Select_stock61', 'Increased_shares_stock50', 'Increased_shares_stock51', 'Increased_shares_stock52', 'Increased_shares_stock53', 'Increased_shares_stock54', 'Increased_shares_stock55', 'Increased_shares_stock56', 'Increased_shares_stock57', 'Increased_shares_stock58', 'Increased_shares_stock59', 'Increased_shares_stock60', 'Increased_shares_stock61', 'TimeFirstClick_stock50', 'TimeFirstClick_stock51', 'TimeFirstClick_stock52', 'TimeFirstClick_stock53', 'TimeFirstClick_stock54', 'TimeFirstClick_stock55', 'TimeFirstClick_stock56', 'TimeFirstClick_stock57', 'TimeFirstClick_stock58', 'TimeFirstClick_stock59', 'TimeFirstClick_stock60', 'TimeFirstClick_stock61','TimeSecondClick_stock50', 'TimeSecondClick_stock51', 'TimeSecondClick_stock52', 'TimeSecondClick_stock53', 'TimeSecondClick_stock54', 'TimeSecondClick_stock55', 'TimeSecondClick_stock56', 'TimeSecondClick_stock57', 'TimeSecondClick_stock58', 'TimeSecondClick_stock59', 'TimeSecondClick_stock60', 'TimeSecondClick_stock61'] def error_message(self, values): print('values is', values) listSelectedStocks = [values['Select_stock50'], values['Select_stock51'], values['Select_stock52'], values['Select_stock53'], values['Select_stock54'], values['Select_stock55'], values['Select_stock56'], values['Select_stock57'], values['Select_stock58'], values['Select_stock59'], values['Select_stock60'], values['Select_stock61']] if listSelectedStocks.count(None) < 9: return 'You can only choose 3 stocks.' elif listSelectedStocks.count(None) > 9: return 'You have to choose 3 stocks.' def vars_for_template(self): self.player.soldList() return dict( Stock50_p1 = 30 - self.player.Stock50_p1, Stock50_p2 = 30 - self.player.Stock50_p1 - self.player.Stock50_p2, Stock50_p3 = 30 - self.player.Stock50_p1 - self.player.Stock50_p2 - self.player.Stock50_p3, Stock51_p1 = 30 - self.player.Stock51_p1, Stock51_p2 = 30 - self.player.Stock51_p1 - self.player.Stock51_p2, Stock51_p3 = 30 - self.player.Stock51_p1 - self.player.Stock51_p2 - self.player.Stock51_p3, Stock52_p1 = 30 - self.player.Stock52_p1, Stock52_p2 = 30 - self.player.Stock52_p1 - self.player.Stock52_p2, Stock52_p3 = 30 - self.player.Stock52_p1 - self.player.Stock52_p2 - self.player.Stock52_p3, Stock53_p1 = 30 - self.player.Stock53_p1, Stock53_p2 = 30 - self.player.Stock53_p1 - self.player.Stock53_p2, Stock53_p3 = 30 - self.player.Stock53_p1 - self.player.Stock53_p2 - self.player.Stock53_p3, Stock54_p1 = 30 - self.player.Stock54_p1, Stock54_p2 = 30 - self.player.Stock54_p1 - self.player.Stock54_p2, Stock54_p3 = 30 - self.player.Stock54_p1 - self.player.Stock54_p2 - self.player.Stock54_p3, Stock55_p1 = 30 - self.player.Stock55_p1, Stock55_p2 = 30 - self.player.Stock55_p1 - self.player.Stock55_p2, Stock55_p3 = 30 - self.player.Stock55_p1 - self.player.Stock55_p2 - self.player.Stock55_p3, Stock56_p1 = 30 - self.player.Stock56_p1, Stock56_p2 = 30 - self.player.Stock56_p1 - self.player.Stock56_p2, Stock56_p3 = 30 - self.player.Stock56_p1 - self.player.Stock56_p2 - self.player.Stock56_p3, Stock57_p1 = 30 - self.player.Stock57_p1, Stock57_p2 = 30 - self.player.Stock57_p1 - self.player.Stock57_p2, Stock57_p3 = 30 - self.player.Stock57_p1 - self.player.Stock57_p2 - self.player.Stock57_p3, Stock58_p1 = 30 - self.player.Stock58_p1, Stock58_p2 = 30 - self.player.Stock58_p1 - self.player.Stock58_p2, Stock58_p3 = 30 - self.player.Stock58_p1 - self.player.Stock58_p2 - self.player.Stock58_p3, Stock59_p1 = 30 - self.player.Stock59_p1, Stock59_p2 = 30 - self.player.Stock59_p1 - self.player.Stock59_p2, Stock59_p3 = 30 - self.player.Stock59_p1 - self.player.Stock59_p2 - self.player.Stock59_p3, Stock60_p1 = 30 - self.player.Stock60_p1, Stock60_p2 = 30 - self.player.Stock60_p1 - self.player.Stock60_p2, Stock60_p3 = 30 - self.player.Stock60_p1 - self.player.Stock60_p2 - self.player.Stock60_p3, Stock61_p1 = 30 - self.player.Stock61_p1, Stock61_p2 = 30 - self.player.Stock61_p1 - self.player.Stock61_p2, Stock61_p3 = 30 - self.player.Stock61_p1 - self.player.Stock61_p2 - self.player.Stock61_p3, ) ###Task 2 show class Task2_show(Page): form_model = 'player' #create portfolio task 2 def createPortfolioTask2(self): self.participant.vars['Portfolio2'] = [] if self.player.Select_stock50 == 'on': self.participant.vars['Portfolio2'].append('Stock50') if self.player.Select_stock51 == 'on': self.participant.vars['Portfolio2'].append('Stock51') if self.player.Select_stock52 == 'on': self.participant.vars['Portfolio2'].append('Stock52') if self.player.Select_stock53 == 'on': self.participant.vars['Portfolio2'].append('Stock53') if self.player.Select_stock54 == 'on': self.participant.vars['Portfolio2'].append('Stock54') if self.player.Select_stock55 == 'on': self.participant.vars['Portfolio2'].append('Stock55') if self.player.Select_stock56 == 'on': self.participant.vars['Portfolio2'].append('Stock56') if self.player.Select_stock57 == 'on': self.participant.vars['Portfolio2'].append('Stock57') if self.player.Select_stock58 == 'on': self.participant.vars['Portfolio2'].append('Stock58') if self.player.Select_stock59 == 'on': self.participant.vars['Portfolio2'].append('Stock59') if self.player.Select_stock60 == 'on': self.participant.vars['Portfolio2'].append('Stock60') if self.player.Select_stock61 == 'on': self.participant.vars['Portfolio2'].append('Stock61') #stock name with space between string and integer def showName_nicely(self, stockName): niceStockname = "Stock " + stockName.split("Stock")[1] return niceStockname #allocate chosen stocks to table rows for display def sorted_nicely(self, l): import re """ Sort the given iterable in the way that humans expect.""" convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(l, key = alphanum_key) def showStocks(self): currentPortfolio = self.sorted_nicely(self.participant.vars['Portfolio2']) self.player.stockRow1_Task2 = currentPortfolio[0] self.player.stockRow2_Task2 = currentPortfolio[1] self.player.stockRow3_Task2 = currentPortfolio[2] return #price per period per row def showPriceFinal(self, name): if name == 'Stock50': price = 30 + self.player.Stock50_p4 + self.player.Stock50_p5 + self.player.Stock50_p6 if name == 'Stock51': price = 30 + self.player.Stock51_p4 + self.player.Stock51_p5 + self.player.Stock51_p6 if name == 'Stock52': price = 30 + self.player.Stock52_p4 + self.player.Stock52_p5 + self.player.Stock52_p6 if name == 'Stock53': price = 30 + self.player.Stock53_p4 + self.player.Stock53_p5 + self.player.Stock53_p6 if name == 'Stock54': price = 30 + self.player.Stock54_p4 + self.player.Stock54_p5 + self.player.Stock54_p6 if name == 'Stock55': price = 30 + self.player.Stock55_p4 + self.player.Stock55_p5 + self.player.Stock55_p6 if name == 'Stock56': price = 30 + self.player.Stock56_p4 + self.player.Stock56_p5 + self.player.Stock56_p6 if name == 'Stock57': price = 30 + self.player.Stock57_p4 + self.player.Stock57_p5 + self.player.Stock57_p6 if name == 'Stock58': price = 30 + self.player.Stock58_p4 + self.player.Stock58_p5 + self.player.Stock58_p6 if name == 'Stock59': price = 30 + self.player.Stock59_p4 + self.player.Stock59_p5 + self.player.Stock59_p6 if name == 'Stock60': price = 30 + self.player.Stock60_p4 + self.player.Stock60_p5 + self.player.Stock60_p6 if name == 'Stock61': price = 30 + self.player.Stock61_p4 + self.player.Stock61_p5 + self.player.Stock61_p6 return price def showPrePrice1(self, name): if name == 'Stock50': price = 30 + self.player.Stock50_p4 if name == 'Stock51': price = 30 + self.player.Stock51_p4 if name == 'Stock52': price = 30 + self.player.Stock52_p4 if name == 'Stock53': price = 30 + self.player.Stock53_p4 if name == 'Stock54': price = 30 + self.player.Stock54_p4 if name == 'Stock55': price = 30 + self.player.Stock55_p4 if name == 'Stock56': price = 30 + self.player.Stock56_p4 if name == 'Stock57': price = 30 + self.player.Stock57_p4 if name == 'Stock58': price = 30 + self.player.Stock58_p4 if name == 'Stock59': price = 30 + self.player.Stock59_p4 if name == 'Stock60': price = 30 + self.player.Stock60_p4 if name == 'Stock61': price = 30 + self.player.Stock61_p4 return price def showPrePrice2(self, name): if name == 'Stock50': price = 30 + self.player.Stock50_p4 + self.player.Stock50_p5 if name == 'Stock51': price = 30 + self.player.Stock51_p4 + self.player.Stock51_p5 if name == 'Stock52': price = 30 + self.player.Stock52_p4 + self.player.Stock52_p5 if name == 'Stock53': price = 30 + self.player.Stock53_p4 + self.player.Stock53_p5 if name == 'Stock54': price = 30 + self.player.Stock54_p4 + self.player.Stock54_p5 if name == 'Stock55': price = 30 + self.player.Stock55_p4 + self.player.Stock55_p5 if name == 'Stock56': price = 30 + self.player.Stock56_p4 + self.player.Stock56_p5 if name == 'Stock57': price = 30 + self.player.Stock57_p4 + self.player.Stock57_p5 if name == 'Stock58': price = 30 + self.player.Stock58_p4 + self.player.Stock58_p5 if name == 'Stock59': price = 30 + self.player.Stock59_p4 + self.player.Stock59_p5 if name == 'Stock60': price = 30 + self.player.Stock60_p4 + self.player.Stock60_p5 if name == 'Stock61': price = 30 + self.player.Stock61_p4 + self.player.Stock61_p5 return price def getNumberShares(self, name): number = 0 if name == 'Stock50' and self.player.Increased_shares_stock50 == None: number = 0 if name == 'Stock50' and self.player.Increased_shares_stock50 == 0: number = 0 if name == 'Stock50' and self.player.Increased_shares_stock50 == 1: number = 1 if name == 'Stock50' and self.player.Increased_shares_stock50 == 2: number = 2 if name == 'Stock51' and self.player.Increased_shares_stock51 == None: number = 0 if name == 'Stock51' and self.player.Increased_shares_stock51 == 0: number = 0 if name == 'Stock51' and self.player.Increased_shares_stock51 == 1: number = 1 if name == 'Stock51' and self.player.Increased_shares_stock51 == 2: number = 2 if name == 'Stock52' and self.player.Increased_shares_stock52 == None: number = 0 if name == 'Stock52' and self.player.Increased_shares_stock52 == 0: number = 0 if name == 'Stock52' and self.player.Increased_shares_stock52 == 1: number = 1 if name == 'Stock52' and self.player.Increased_shares_stock52 == 2: number = 2 if name == 'Stock53' and self.player.Increased_shares_stock53 == None: number = 0 if name == 'Stock53' and self.player.Increased_shares_stock53 == 0: number = 0 if name == 'Stock53' and self.player.Increased_shares_stock53 == 1: number = 1 if name == 'Stock53' and self.player.Increased_shares_stock53 == 2: number = 2 if name == 'Stock54' and self.player.Increased_shares_stock54 == None: number = 0 if name == 'Stock54' and self.player.Increased_shares_stock54 == 0: number = 0 if name == 'Stock54' and self.player.Increased_shares_stock54 == 1: number = 1 if name == 'Stock54' and self.player.Increased_shares_stock54 == 2: number = 2 if name == 'Stock55' and self.player.Increased_shares_stock55 == None: number = 0 if name == 'Stock55' and self.player.Increased_shares_stock55 == 0: number = 0 if name == 'Stock55' and self.player.Increased_shares_stock55 == 1: number = 1 if name == 'Stock55' and self.player.Increased_shares_stock55 == 2: number = 2 if name == 'Stock56' and self.player.Increased_shares_stock56 == None: number = 0 if name == 'Stock56' and self.player.Increased_shares_stock56 == 0: number = 0 if name == 'Stock56' and self.player.Increased_shares_stock56 == 1: number = 1 if name == 'Stock56' and self.player.Increased_shares_stock56 == 2: number = 2 if name == 'Stock57' and self.player.Increased_shares_stock57 == None: number = 0 if name == 'Stock57' and self.player.Increased_shares_stock57 == 0: number = 0 if name == 'Stock57' and self.player.Increased_shares_stock57 == 1: number = 1 if name == 'Stock57' and self.player.Increased_shares_stock57 == 2: number = 2 if name == 'Stock58' and self.player.Increased_shares_stock58 == None: number = 0 if name == 'Stock58' and self.player.Increased_shares_stock58 == 0: number = 0 if name == 'Stock58' and self.player.Increased_shares_stock58 == 1: number = 1 if name == 'Stock58' and self.player.Increased_shares_stock58 == 2: number = 2 if name == 'Stock59' and self.player.Increased_shares_stock59 == None: number = 0 if name == 'Stock59' and self.player.Increased_shares_stock59 == 0: number = 0 if name == 'Stock59' and self.player.Increased_shares_stock59 == 1: number = 1 if name == 'Stock59' and self.player.Increased_shares_stock59 == 2: number = 2 if name == 'Stock60' and self.player.Increased_shares_stock60 == None: number = 0 if name == 'Stock60' and self.player.Increased_shares_stock60 == 0: number = 0 if name == 'Stock60' and self.player.Increased_shares_stock60 == 1: number = 1 if name == 'Stock60' and self.player.Increased_shares_stock60 == 2: number = 2 if name == 'Stock61' and self.player.Increased_shares_stock61 == None: number = 0 if name == 'Stock61' and self.player.Increased_shares_stock61 == 0: number = 0 if name == 'Stock61' and self.player.Increased_shares_stock61 == 1: number = 1 if name == 'Stock61' and self.player.Increased_shares_stock61 == 2: number = 2 return number def getProceedsbyName(self, name): if name == 'Stock50': proceeds = 30 + self.player.Stock50_p4 + self.player.Stock50_p5 + self.player.Stock50_p6 if name == 'Stock51': proceeds = 30 + self.player.Stock51_p4 + self.player.Stock51_p5 + self.player.Stock51_p6 if name == 'Stock52': proceeds = 30 + self.player.Stock52_p4 + self.player.Stock52_p5 + self.player.Stock52_p6 if name == 'Stock53': proceeds = 30 + self.player.Stock53_p4 + self.player.Stock53_p5 + self.player.Stock53_p6 if name == 'Stock54': proceeds = 30 + self.player.Stock54_p4 + self.player.Stock54_p5 + self.player.Stock54_p6 if name == 'Stock55': proceeds = 30 + self.player.Stock55_p4 + self.player.Stock55_p5 + self.player.Stock55_p6 if name == 'Stock56': proceeds = 30 + self.player.Stock56_p4 + self.player.Stock56_p5 + self.player.Stock56_p6 if name == 'Stock57': proceeds = 30 + self.player.Stock57_p4 + self.player.Stock57_p5 + self.player.Stock57_p6 if name == 'Stock58': proceeds = 30 + self.player.Stock58_p4 + self.player.Stock58_p5 + self.player.Stock58_p6 if name == 'Stock59': proceeds = 30 + self.player.Stock59_p4 + self.player.Stock59_p5 + self.player.Stock59_p6 if name == 'Stock60': proceeds = 30 + self.player.Stock60_p4 + self.player.Stock60_p5 + self.player.Stock60_p6 if name == 'Stock61': proceeds = 30 + self.player.Stock61_p4 + self.player.Stock61_p5 + self.player.Stock61_p6 return proceeds def calculateTask2Totals(self): list2 = self.participant.vars['Portfolio2'] additionalShares = 0 for stock in list2: additionalShares = additionalShares + self.getNumberShares(stock) high_fee = 0 for stock in list2: if self.getNumberShares(stock) == 2: high_fee = high_fee +1 self.player.numberHighFeeTotal = high_fee self.player.numberAdditionalShares = additionalShares self.player.numberTotalShares = self.player.numberAdditionalShares + 3 self.player.costsPurchaseTask2 = self.player.numberTotalShares * Constants.stockPrice self.player.costsFeesTask2 = self.player.numberAdditionalShares * Constants.fee + self.player.numberHighFeeTotal * (2*Constants.fee_high_diff) self.player.task2Proceeds = 0 portfolio2 = self.participant.vars['Portfolio2'] for s in portfolio2: self.player.task2Proceeds = self.player.task2Proceeds + ((self.getNumberShares(s)+1) * self.getProceedsbyName(s)) self.player.task2Totals = Constants.endowment - (self.player.numberTotalShares * Constants.stockPrice) - (self.player.numberAdditionalShares * Constants.fee) - (self.player.numberHighFeeTotal * 1) + self.player.task2Proceeds def vars_for_template(self): self.createPortfolioTask2() self.showStocks() self.calculateTask2Totals() self.player.sharesRow1_Task2 = self.getNumberShares(self.player.stockRow1_Task2) + 1 self.player.sharesRow2_Task2 = self.getNumberShares(self.player.stockRow2_Task2) + 1 self.player.sharesRow3_Task2 = self.getNumberShares(self.player.stockRow3_Task2) + 1 self.player.priceRow1_Task2 = self.showPriceFinal(self.player.stockRow1_Task2) self.player.priceRow2_Task2 = self.showPriceFinal(self.player.stockRow2_Task2) self.player.priceRow3_Task2 = self.showPriceFinal(self.player.stockRow3_Task2) self.player.prePrice1Row1_Task2 = self.showPrePrice1(self.player.stockRow1_Task2) self.player.prePrice1Row2_Task2 = self.showPrePrice1(self.player.stockRow2_Task2) self.player.prePrice1Row3_Task2 = self.showPrePrice1(self.player.stockRow3_Task2) self.player.prePrice2Row1_Task2 = self.showPrePrice2(self.player.stockRow1_Task2) self.player.prePrice2Row2_Task2 = self.showPrePrice2(self.player.stockRow2_Task2) self.player.prePrice2Row3_Task2 = self.showPrePrice2(self.player.stockRow3_Task2) return dict( StockRow1_Task2 = self.showName_nicely(self.player.stockRow1_Task2), StockRow2_Task2 = self.showName_nicely(self.player.stockRow2_Task2), StockRow3_Task2 = self.showName_nicely(self.player.stockRow3_Task2), PrePrice1Row1_Task2 = self.player.prePrice1Row1_Task2, PrePrice1Row2_Task2 = self.player.prePrice1Row2_Task2, PrePrice1Row3_Task2 = self.player.prePrice1Row3_Task2, PrePrice2Row1_Task2 = self.player.prePrice2Row1_Task2, PrePrice2Row2_Task2 = self.player.prePrice2Row2_Task2, PrePrice2Row3_Task2 = self.player.prePrice2Row3_Task2, PriceRow1_Task2 = self.player.priceRow1_Task2, PriceRow2_Task2 = self.player.priceRow2_Task2, PriceRow3_Task2 = self.player.priceRow3_Task2, SharesRow1_Task2 = self.player.sharesRow1_Task2, SharesRow2_Task2 = self.player.sharesRow2_Task2, SharesRow3_Task2 = self.player.sharesRow3_Task2, Proceeds_Row1_Task2 = (self.player.sharesRow1_Task2 * self.player.priceRow1_Task2), Proceeds_Row2_Task2 = (self.player.sharesRow2_Task2 * self.player.priceRow2_Task2), Proceeds_Row3_Task2 = (self.player.sharesRow3_Task2 * self.player.priceRow3_Task2), Cash = self.player.task2Totals, ) ###Earnings class Earnings1(Page): form_model = 'player' def getProceedsbyName(self, name): if name == 'Stock50': proceeds = 30 + self.player.Stock50_p4 + self.player.Stock50_p5 + self.player.Stock50_p6 if name == 'Stock51': proceeds = 30 + self.player.Stock51_p4 + self.player.Stock51_p5 + self.player.Stock51_p6 if name == 'Stock52': proceeds = 30 + self.player.Stock52_p4 + self.player.Stock52_p5 + self.player.Stock52_p6 if name == 'Stock53': proceeds = 30 + self.player.Stock53_p4 + self.player.Stock53_p5 + self.player.Stock53_p6 if name == 'Stock54': proceeds = 30 + self.player.Stock54_p4 + self.player.Stock54_p5 + self.player.Stock54_p6 if name == 'Stock55': proceeds = 30 + self.player.Stock55_p4 + self.player.Stock55_p5 + self.player.Stock55_p6 if name == 'Stock56': proceeds = 30 + self.player.Stock56_p4 + self.player.Stock56_p5 + self.player.Stock56_p6 if name == 'Stock57': proceeds = 30 + self.player.Stock57_p4 + self.player.Stock57_p5 + self.player.Stock57_p6 if name == 'Stock58': proceeds = 30 + self.player.Stock58_p4 + self.player.Stock58_p5 + self.player.Stock58_p6 if name == 'Stock59': proceeds = 30 + self.player.Stock59_p4 + self.player.Stock59_p5 + self.player.Stock59_p6 if name == 'Stock60': proceeds = 30 + self.player.Stock60_p4 + self.player.Stock60_p5 + self.player.Stock60_p6 if name == 'Stock61': proceeds = 30 + self.player.Stock61_p4 + self.player.Stock61_p5 + self.player.Stock61_p6 return proceeds def getNumberShares(self, name): number = 0 if name == 'Stock50' and self.player.Increased_shares_stock50 == None: number = 0 if name == 'Stock50' and self.player.Increased_shares_stock50 == 0: number = 0 if name == 'Stock50' and self.player.Increased_shares_stock50 == 1: number = 1 if name == 'Stock50' and self.player.Increased_shares_stock50 == 2: number = 2 if name == 'Stock51' and self.player.Increased_shares_stock51 == None: number = 0 if name == 'Stock51' and self.player.Increased_shares_stock51 == 0: number = 0 if name == 'Stock51' and self.player.Increased_shares_stock51 == 1: number = 1 if name == 'Stock51' and self.player.Increased_shares_stock51 == 2: number = 2 if name == 'Stock52' and self.player.Increased_shares_stock52 == None: number = 0 if name == 'Stock52' and self.player.Increased_shares_stock52 == 0: number = 0 if name == 'Stock52' and self.player.Increased_shares_stock52 == 1: number = 1 if name == 'Stock52' and self.player.Increased_shares_stock52 == 2: number = 2 if name == 'Stock53' and self.player.Increased_shares_stock53 == None: number = 0 if name == 'Stock53' and self.player.Increased_shares_stock53 == 0: number = 0 if name == 'Stock53' and self.player.Increased_shares_stock53 == 1: number = 1 if name == 'Stock53' and self.player.Increased_shares_stock53 == 2: number = 2 if name == 'Stock54' and self.player.Increased_shares_stock54 == None: number = 0 if name == 'Stock54' and self.player.Increased_shares_stock54 == 0: number = 0 if name == 'Stock54' and self.player.Increased_shares_stock54 == 1: number = 1 if name == 'Stock54' and self.player.Increased_shares_stock54 == 2: number = 2 if name == 'Stock55' and self.player.Increased_shares_stock55 == None: number = 0 if name == 'Stock55' and self.player.Increased_shares_stock55 == 0: number = 0 if name == 'Stock55' and self.player.Increased_shares_stock55 == 1: number = 1 if name == 'Stock55' and self.player.Increased_shares_stock55 == 2: number = 2 if name == 'Stock56' and self.player.Increased_shares_stock56 == None: number = 0 if name == 'Stock56' and self.player.Increased_shares_stock56 == 0: number = 0 if name == 'Stock56' and self.player.Increased_shares_stock56 == 1: number = 1 if name == 'Stock56' and self.player.Increased_shares_stock56 == 2: number = 2 if name == 'Stock57' and self.player.Increased_shares_stock57 == None: number = 0 if name == 'Stock57' and self.player.Increased_shares_stock57 == 0: number = 0 if name == 'Stock57' and self.player.Increased_shares_stock57 == 1: number = 1 if name == 'Stock57' and self.player.Increased_shares_stock57 == 2: number = 2 if name == 'Stock58' and self.player.Increased_shares_stock58 == None: number = 0 if name == 'Stock58' and self.player.Increased_shares_stock58 == 0: number = 0 if name == 'Stock58' and self.player.Increased_shares_stock58 == 1: number = 1 if name == 'Stock58' and self.player.Increased_shares_stock58 == 2: number = 2 if name == 'Stock59' and self.player.Increased_shares_stock59 == None: number = 0 if name == 'Stock59' and self.player.Increased_shares_stock59 == 0: number = 0 if name == 'Stock59' and self.player.Increased_shares_stock59 == 1: number = 1 if name == 'Stock59' and self.player.Increased_shares_stock59 == 2: number = 2 if name == 'Stock60' and self.player.Increased_shares_stock60 == None: number = 0 if name == 'Stock60' and self.player.Increased_shares_stock60 == 0: number = 0 if name == 'Stock60' and self.player.Increased_shares_stock60 == 1: number = 1 if name == 'Stock60' and self.player.Increased_shares_stock60 == 2: number = 2 if name == 'Stock61' and self.player.Increased_shares_stock61 == None: number = 0 if name == 'Stock61' and self.player.Increased_shares_stock61 == 0: number = 0 if name == 'Stock61' and self.player.Increased_shares_stock61 == 1: number = 1 if name == 'Stock61' and self.player.Increased_shares_stock61 == 2: number = 2 return number def calculateTask1Earnings(self): self.player.task1Earnings = (self.player.sellingPriceSold1 + self.player.sellingPriceSold2 + self.player.sellingPriceSold3 + self.player.sellingPriceSold4 + self.player.sellingPriceSold5 + self.player.sellingPriceSold6 + self.player.sellingPriceSold7 + self.player.sellingPriceSold8 + self.player.sellingPriceSold9 + self.player.sellingPriceSold10 + self.player.sellingPriceSold11 - 30)/100 def calculateTask2Earnings(self): list2 = self.participant.vars['Portfolio2'] additionalShares = 0 for stock in list2: additionalShares = additionalShares + self.getNumberShares(stock) high_fee = 0 for stock in list2: if self.getNumberShares(stock) == 2: high_fee = high_fee +1 self.player.numberHighFeeTotal = high_fee self.player.numberAdditionalShares = additionalShares self.player.numberTotalShares = self.player.numberAdditionalShares + 3 self.player.costsPurchaseTask2 = self.player.numberTotalShares * Constants.stockPrice self.player.costsFeesTask2 = self.player.numberAdditionalShares * Constants.fee + self.player.numberHighFeeTotal * (2*Constants.fee_high_diff) self.player.task2Proceeds = 0 portfolio2 = self.participant.vars['Portfolio2'] for s in portfolio2: self.player.task2Proceeds = self.player.task2Proceeds + ((self.getNumberShares(s)+1) * self.getProceedsbyName(s)) self.player.task2Totals = Constants.endowment - (self.player.numberTotalShares * Constants.stockPrice) - (self.player.numberAdditionalShares * Constants.fee) - (self.player.numberHighFeeTotal * 1) + self.player.task2Proceeds self.player.task2Earnings = self.player.task2Totals / 100 def drawPaymentTask(self): import random self.player.paymentTask = random.choice(['Investment Task 1', 'Investment Task 2']) def calculateBonusPayment(self, task): if task == 'Investment Task 1': self.player.bonusPayment = self.player.task1Earnings elif task == 'Investment Task 2': self.player.bonusPayment = self.player.task2Earnings else: self.player.bonusPayment = -99 #oTree payoff variable def payoff(self): self.participant.payoff = 4 + self.player.bonusPayment def task1Proceeds(self): self.player.task1Proceeds = (self.player.task1Earnings * 100) + 30 def task1Totals(self): self.player.task1Totals = self.player.task1Earnings * 100 def vars_for_template(self): self.calculateTask1Earnings() self.calculateTask2Earnings() self.drawPaymentTask() self.calculateBonusPayment(self.player.paymentTask) self.payoff() self.task1Proceeds() self.task1Totals() return dict( Task1Earnings = str(f'{self.player.task1Earnings:.2f}'), Task2Earnings = str(f'{self.player.task2Earnings:.2f}'), PaymentTask = self.player.paymentTask, BonusPayment = str(f'{self.player.bonusPayment:.2f}'), Task1Proceeds = str(f'{self.player.task1Proceeds:.0f}'), Task1Totals = str(f'{self.player.task1Totals:.0f}'), NumberHighTypes = self.player.number_highStocks, Task2Proceeds = str(f'{self.player.task2Proceeds:.0f}'), Task2Totals = str(f'{self.player.task2Totals:.0f}'), CostsPurchaseTask2 = str(f'{self.player.costsPurchaseTask2:.0f}'), CostsFeesTask2 = str(f'{self.player.costsFeesTask2:.0f}'), ) class Question_understanding(Page): form_model = 'player' form_fields = ['Understanding'] class Question_comments(Page): form_model = 'player' form_fields = ['Comments'] class EndPage(Page): form_model = 'player' def getLink(self): if self.session.label == 'to': self.player.Link = 'https://app.prolific.co/submissions/complete?cc=44196AE8' elif self.session.label == 'lj': self.player.Link = 'https://app.prolific.co/submissions/complete?cc=36A94B3E' else: self.player.Link = '' def vars_for_template(self): self.getLink() return dict( Link = self.player.Link, ) page_sequence = [Introduction, Consent, Instructions_invest1_1, Instructions_invest1_2, Instructions_payment, StartTask1, Period0, Period1_show, Period1_choice, Period2_show, Period2_choice, Period3_show, Period3_choice, Period4_show, Period4_choice, Period5_show, Period5_choice, Period6_show, Period6_choice, Period7_show, StartTask1_2, Period0_2, Period1_show_2, Period1_choice_2, Period2_show_2, Period2_choice_2, Period3_show_2, Period3_choice_2, Period4_show_2, Period4_choice_2, Period5_show_2, Period5_choice_2, Period6_show_2, Period6_choice_2, Period7_show_2, Confidence3, Confidence4, Confidence1, Question_understanding, Question_comments, EndPage]