from redwood import Session, Controller
import random
import sys
import time

controller = Controller("Brownian Asset")
controller.set_config("brownian_asset.csv")
session = Session("Brownian Asset")
db = session.db

def start_period():
	db['asset'] = session.config['initial_asset'][db['_period']]

def tick():
	if random.random() < 0.501:
		db['asset'] *= 1.001
	else:
		db['asset'] *= 0.999
	if not session.config["autoend"][db["_period"]] and random.random() < 0.005:
		db['_ready_to_end_period'] = True

def end_period():
	pass

#session.read_config('brownian_asset.csv')
session.start_period(start_period)
session.tick(tick, millis=50)
#session.end_period(end_period)

"""
session.required_subjects = 12
#session.start()
session.join(float('inf'))
"""

#session.config_changed(config_changed)

#Here the script waits for the client.py file to connect
while 1:
    if len(controller.list_subjects()) >= 1:
        controller.start_session()
        break
    time.sleep(1)

#Wait for the session to finish
#wait_for_session_end(session.db)