How to use otree - 10 common examples

To help you get started, we’ve selected a few otree examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oTree-org / otree-core / otree / bots / browser_launcher.py View on Github external
def websocket_listen(self, session_code, num_participants):
        # seems that urljoin doesn't work with ws:// urls
        # so do the ws replace after URLjoin
        websocket_url = urljoin(
            self.server_url,
            channel_utils.browser_bots_launcher_path(session_code)
        )
        websocket_url = websocket_url.replace(
            'http://', 'ws://').replace('https://', 'wss://')

        ws_client = OtreeWebSocketClient(
            websocket_url,
            session_size=num_participants,
        )
        ws_client.connect()
        ws_client.run_forever()
github oTree-org / oTree / public_goods_simple / tests.py View on Github external
def play_round(self):
        yield (pages.Contribute, {'contribution': c(1)})
        yield (pages.Results)
github oTree-org / oTree / trust_simple / tests.py View on Github external
from otree.api import Currency as c, currency_range, SubmissionMustFail
from . import pages
from ._builtin import Bot
from .models import Constants


class PlayerBot(Bot):
    cases = [
        {'offer': c(0), 'return': c(0), 'p1_payoff': c(10), 'p2_payoff': c(0)},
        {'offer': c(5), 'return': c(10), 'p1_payoff': c(15), 'p2_payoff': c(5)},
        {'offer': c(10), 'return': c(30), 'p1_payoff': c(30), 'p2_payoff': c(0)},
    ]

    def play_round(self):
        case = self.case
        if self.player.id_in_group == 1:
            yield (pages.Send, {"sent_amount": case['offer']})

        else:
            for invalid_return in [-1, case['offer'] * Constants.multiplier + 1]:
                yield SubmissionMustFail(
                    pages.SendBack, {'sent_back_amount': invalid_return}
                )
            yield (pages.SendBack, {'sent_back_amount': case['return']})

        if self.player.id_in_group == 1:
github oTree-org / oTree / bargaining / tests.py View on Github external
def play_round(self):

        # start
        yield (pages.Introduction)

        if self.case == 'success':
            request = c(10)
            yield (pages.Request, {"request": request})
            yield (pages.Results)
            assert self.player.payoff == request

        if self.case == 'greedy':
            yield (pages.Request, {"request": Constants.amount_shared})
            yield (pages.Results)
            assert self.player.payoff == 0
github oTree-org / otree-core / otree / management / commands / zip.py View on Github external
# (using __temp prefix makes it show up in the middle, because it's a file)
    archive_name = f'{project_path.name}.otreezip'

    settings_file = project_path / 'settings.py'
    if not settings_file.exists():
        msg = (
            "Cannot find oTree settings. "
            "You must run this command from the folder that contains your "
            "settings.py file."
        )
        logger.error(msg)
        sys.exit(1)

    try:
        check_requirements_files(project_path)
    except RequirementsError as exc:
        logger.error(str(exc))
        sys.exit(1)

    # once Heroku uses py 3.7 by default, we can remove this runtime stuff.
    runtime_txt = project_path / 'runtime.txt'
    runtime_existed = runtime_txt.exists()
    if not runtime_existed:
        # don't use sys.version_info because it might be newer than what
        # heroku supports
        runtime_txt.write_text(f'python-3.7.3')
    try:
        with tarfile.open(archive_name, 'w:gz') as tar:
            # if i omit arcname, it nests the project 2 levels deep.
            # if i say arcname=proj, it puts the whole project in a folder.
            # if i say arcname='', it has 0 levels of nesting.
            tar.add(project_path, arcname='', filter=filter_func)
github Leeps-Lab / high_frequency_trading / hft / models.py View on Github external
market_id = models.CharField()
    id_in_market = models.IntegerField()
    speed_unit_cost = models.IntegerField()
    net_worth = models.IntegerField()
    cash = models.IntegerField()
    cost = models.IntegerField()
    speed_cost = models.IntegerField()
    tax_paid = models.IntegerField()
    reference_price = models.IntegerField()
    inventory = models.IntegerField()
    bid = models.IntegerField()
    offer = models.IntegerField()
    staged_bid = models.IntegerField()
    staged_offer = models.IntegerField()
    implied_bid = models.IntegerField()
    implied_offer = models.IntegerField()
    best_bid = models.IntegerField()
    best_offer = models.IntegerField()
    e_best_bid = models.IntegerField()
    e_best_offer = models.IntegerField()
    slider_a_x = models.FloatField()
    slider_a_y = models.FloatField()
    slider_a_z = models.FloatField()
    signed_volume = models.FloatField()
    e_signed_volume = models.FloatField()

    # fields for this player's initial strategy decisions
    # these are set from the InitialDecisionSelection form
    initial_slider_a_x = models.FloatField()
    initial_slider_a_y = models.FloatField()
    initial_slider_a_z = models.FloatField()
    initial_role = models.CharField()
github Leeps-Lab / high_frequency_trading / hft / output.py View on Github external
cost = models.IntegerField()
    speed_cost = models.IntegerField()
    tax_paid = models.IntegerField()
    reference_price = models.IntegerField()
    inventory = models.IntegerField()
    bid = models.IntegerField()
    offer = models.IntegerField()
    staged_bid = models.IntegerField()
    staged_offer = models.IntegerField()
    implied_bid = models.IntegerField()
    implied_offer = models.IntegerField()
    best_bid = models.IntegerField()
    best_offer = models.IntegerField()
    best_bid_except_me = models.IntegerField()
    best_offer_except_me = models.IntegerField()
    next_bid = models.IntegerField()
    next_offer = models.IntegerField()
    volume_at_best_bid = models.IntegerField()
    volume_at_best_offer = models.IntegerField()
    e_best_bid = models.IntegerField()
    e_best_offer = models.IntegerField()
    slider_a_x = models.FloatField()
    slider_a_y = models.FloatField()
    slider_a_z = models.FloatField()
    signed_volume = models.FloatField()
    e_signed_volume = models.FloatField()
    midpoint_peg = models.BooleanField()
    peg_price = models.IntegerField()
    peg_state = models.IntegerField()
 

class MarketRecord(TimeAwareInSessionRecord):
github Leeps-Lab / high_frequency_trading / hft / output.py View on Github external
from otree.api import models
from otree.db.models import Model, ForeignKey
from . import market_environments
from django.utils import timezone
import logging

log = logging.getLogger(__name__)



class TimeAwareInSessionRecord(Model):
    timestamp = models.DateTimeField(default=timezone.now)
    trigger_event_type = models.CharField()
    event_no = models.IntegerField()
    subsession_id = models.IntegerField()
    market_id = models.IntegerField()


class TraderRecord(TimeAwareInSessionRecord):
    
    csv_meta = (
    'timestamp', 'subsession_id', 'market_id', 'player_id', 'trigger_event_type',
    'event_no',  'trader_model_name', 'inventory', 'bid', 'offer', 
    'best_bid_except_me', 'best_offer_except_me',
    'delay', 'staged_bid', 'staged_offer', 'implied_bid', 
    'implied_offer', 'slider_a_x','slider_a_y', 'slider_a_z',
    'net_worth', 'cash', 'tax_paid', 'speed_cost', 'midpoint_peg', 'peg_price', 'peg_state')

    # timestamp = models.DateTimeField(default=timezone.now)
    # trigger_event_type = models.CharField()
    # event_no = models.IntegerField()
    # subsession_id = models.IntegerField()
github Leeps-Lab / high_frequency_trading / hft / models.py View on Github external
from .dispatcher import DispatcherFactory

log = logging.getLogger(__name__)

class Constants(BaseConstants):
    name_in_url = 'hft'
    players_per_group = None
    num_rounds = 3


class Subsession(BaseSubsession):
    model_name = models.StringField(initial='subsession')
    auction_format = models.StringField()
    session_duration = models.IntegerField()
    batch_length = models.IntegerField(initial=0)
    code = models.CharField(default=random_chars_8)

    def creating_session(self):
        def create_trade_session(session_format):
            trade_session_cls = TradeSessionFactory.get_session(session_format)
            dispatcher = DispatcherFactory.get_dispatcher(session_format)
            trade_session = trade_session_cls(self, session_format, dispatcher)
            environment = market_environments.environments[session_format]
            for event_type in environment.exogenous_events:
                event_filename = self.session.config[event_type].pop(0)
                trade_session.register_exogenous_event(
                    event_type, event_filename)
            return trade_session
        session_format = self.session.config['environment']    
        if self.round_number == 1:
            self.session.config = utility.process_configs(
github Leeps-Lab / high_frequency_trading / hft / models.py View on Github external
exchange_port = models.IntegerField()
    # subsession_id = models.CharField()
    market_id = models.CharField()
    id_in_market = models.IntegerField()
    speed_unit_cost = models.IntegerField()
    net_worth = models.IntegerField()
    cash = models.IntegerField()
    cost = models.IntegerField()
    speed_cost = models.IntegerField()
    tax_paid = models.IntegerField()
    reference_price = models.IntegerField()
    inventory = models.IntegerField()
    bid = models.IntegerField()
    offer = models.IntegerField()
    staged_bid = models.IntegerField()
    staged_offer = models.IntegerField()
    implied_bid = models.IntegerField()
    implied_offer = models.IntegerField()
    best_bid = models.IntegerField()
    best_offer = models.IntegerField()
    e_best_bid = models.IntegerField()
    e_best_offer = models.IntegerField()
    slider_a_x = models.FloatField()
    slider_a_y = models.FloatField()
    slider_a_z = models.FloatField()
    signed_volume = models.FloatField()
    e_signed_volume = models.FloatField()

    # fields for this player's initial strategy decisions
    # these are set from the InitialDecisionSelection form
    initial_slider_a_x = models.FloatField()
    initial_slider_a_y = models.FloatField()