Esempio n. 1
0
def simulation(redemptions, costs, info_traders, simulations, periods, time_steps):

    # sort valuations from highest to lowest, costs from lowest to highest
    redemptions.sort(reverse=True)
    costs.sort()

    # calculates maximum surplus
    maximum_surplus = max_surplus(redemptions, costs, info_traders["traders"])

    # run certain amount of simulations
    for sim in range(simulations):

        # divide the number of agents participating in auction
        agents_in_auction = divide_traders(info_traders)

        # creates transaction period object
        trans_period = Period(agents_in_auction, time_steps, info_traders["min_price"],
                              info_traders["max_price"])

        # starts loop for the total amount of transaction periods
        for period in range(periods):

            trans_period.period = period

            # loop for amount of time steps per period
            for time_step in range(time_steps):

                # set activity agents
                trans_period.set_activity_traders(time_step)

                # checks if agents that are willing to shout are found
                if trans_period.active_agents:

                    # random agent to shout bid or ask
                    trader = trans_period.shout_offer()

                    # checks if transactions is possible
                    if trans_period.check_transaction():

                        # chooses radomly a traders from the opposite market side to exchange
                        second_trader = trans_period.pick_agents_transactions(
                            trader)

                        # checks if traders that shouted is buyer
                        if trader.type == "buyer":

                            # check if buyer has enough budget
                            if trans_period.check_buyer(trader, second_trader.price):

                                # procces transactions between the two traders
                                trans_period.procces_transaction(trader, second_trader,
                                                                 second_trader.price,
                                                                 period, time_step)

                                # check if traders can still participate in auction,
                                trans_period.check_competing_agents(trader,
                                                                    second_trader,
                                                                    redemptions,
                                                                    costs)

                        # checks if trader that shouted is seller
                        else:

                            # checks if the buyer has enough budget
                            if trans_period.check_buyer(second_trader,
                                                        second_trader.price):

                                # procces transaction between the two traders
                                trans_period.procces_transaction(second_trader, trader,
                                                                 second_trader.price,
                                                                 period, time_step)

                                # check if traders can still participate in auction
                                trans_period.check_competing_agents(second_trader,
                                                                    trader,
                                                                    redemptions,
                                                                    costs)

                    # check if trades are still possible
                    if trans_period.check_end_period():
                        break

            # set all agents "reseted" in auction and keep track of min max trade price
            trans_period.update_min_max_trade(period)
            trans_period.reset_agents_new_round(redemptions, costs)

        all_data_simulation(sim, trans_period.data_surplus,
                            trans_period.data_transactions,
                            trans_period.agents_in_auction)