Пример #1
0
        protected double determinePurchaseQuantity(Market bazaar, String commodity_)
        {
            var mean          = bazaar.getAverageHistoricalPrice(commodity_, _lookback); //double
            var trading_range = observeTradingRange(commodity_, 10);                     //Point

            if (trading_range != null)
            {
                var favorability = Quick.positionInRange(mean, trading_range.x, trading_range.y);            //double
                favorability = 1 - favorability;
                //do 1 - favorability to see how close we are to the low end

                double amount_to_buy = Math.Round(favorability * _inventory.shortage(commodity_));            //double
                if (amount_to_buy < 1)
                {
                    amount_to_buy = 1;
                }
                return(amount_to_buy);
            }
            return(0);
        }
Пример #2
0
        protected double determineSaleQuantity(Market bazaar, String commodity_)
        {
            var mean          = bazaar.getAverageHistoricalPrice(commodity_, _lookback); //double
            var trading_range = observeTradingRange(commodity_, 10);                     //point

            if (trading_range != null && mean > 0)
            {
                var favorability = Quick.positionInRange(mean, trading_range.x, trading_range.y);           //double
                //position_in_range: high means price is at a high point

                double amount_to_sell = Math.Round(favorability * _inventory.surplus(commodity_));             //double
                amount_to_sell = _inventory.query(commodity_);
                if (amount_to_sell < 1)
                {
                    amount_to_sell = 1;
                }
                return(amount_to_sell);
            }
            return(0);
        }
Пример #3
0
        //TODO: do we need the default ""?
        private void resolveOffers(string good = "")
        {
            var bids = book.bids[good];
            var asks = book.asks[good];

            bids = Quick.Shuffle(bids);
            asks = Quick.Shuffle(asks);

            //bids.Sort(Quick.sortOfferDecending); //highest buying price first
            asks.Sort(Quick.SortOfferAcending); //lowest selling price first

            int    successfulTrades = 0;        //# of successful trades this round
            double moneyTraded      = 0;        //amount of money traded this round
            double unitsTraded      = 0;        //amount of goods traded this round
            double avgPrice         = 0;        //avg clearing price this round
            double numAsks          = 0;
            double numBids          = 0;

            int failsafe = 0;

            for (int i = 0; i < bids.Count; i++)
            {
                numBids += bids[i].units;
            }

            for (int i = 0; i < asks.Count; i++)
            {
                numAsks += asks[i].units;
            }

            //march through and try to clear orders
            while (bids.Count > 0 && asks.Count > 0)                    //while both books are non-empty
            {
                var buyer  = bids[0];
                var seller = asks[0];

                var quantity_traded = (double)Math.Min(seller.units, buyer.units);
                var clearing_price  = seller.unitPrice; //Quick.avgf(seller.unit_price, buyer.unit_price);

                //if (buyer.unit_price < seller.unit_price)
                //    break;

                if (quantity_traded > 0)
                {
                    //transfer the goods for the agreed price
                    seller.units -= quantity_traded;
                    buyer.units  -= quantity_traded;

                    transferGood(good, quantity_traded, seller.agentId, buyer.agentId, clearing_price);
                    transferMoney(quantity_traded * clearing_price, seller.agentId, buyer.agentId);

                    //update agent price beliefs based on successful transaction
                    var buyer_a  = agents[buyer.agentId];
                    var seller_a = agents[seller.agentId];
                    buyer_a.UpdatePriceModel(this, "buy", good, true, clearing_price);
                    seller_a.UpdatePriceModel(this, "sell", good, true, clearing_price);

                    //log the stats
                    moneyTraded += (quantity_traded * clearing_price);
                    unitsTraded += quantity_traded;
                    successfulTrades++;
                }

                if (seller.units == 0)                //seller is out of offered good
                {
                    asks.RemoveAt(0);                 //.splice(0, 1);		//remove ask
                    failsafe = 0;
                }
                if (buyer.units == 0)                //buyer is out of offered good
                {
                    bids.RemoveAt(0);                //.splice(0, 1);		//remove bid
                    failsafe = 0;
                }

                failsafe++;

                if (failsafe > 1000)             //lol, ok
                {
                    Console.WriteLine("BOINK!");
                }
            }

            //reject all remaining offers,
            //update price belief models based on unsuccessful transaction
            while (bids.Count > 0)
            {
                var buyer   = bids[0];
                var buyer_a = agents[buyer.agentId];
                buyer_a.UpdatePriceModel(this, "buy", good, false);
                bids.RemoveAt(0);            //.splice(0, 1);
            }
            while (asks.Count > 0)
            {
                var seller   = asks[0];
                var seller_a = agents[seller.agentId];
                seller_a.UpdatePriceModel(this, "sell", good, false);
                asks.RemoveAt(0);// splice(0, 1);
            }

            //update history

            History.Asks.Add(good, numAsks);
            History.Bids.Add(good, numBids);
            History.Trades.Add(good, unitsTraded);

            if (unitsTraded > 0)
            {
                avgPrice = moneyTraded / (double)unitsTraded;
                History.Prices.Add(good, avgPrice);
            }
            else
            {
                //special case: none were traded this round, use last round's average price
                History.Prices.Add(good, History.Prices.Average(good, 1));
                avgPrice = History.Prices.Average(good, 1);
            }

            List <BasicAgent> agentsList = agents.ToList <BasicAgent>();

            agentsList.Sort(Quick.SortAgentAlpha);

            string        currClass = "";
            string        lastClass = "";
            List <double> list      = null;

            for (int i = 0; i < agentsList.Count; i++)
            {
                var a = agentsList[i];                          //get current agent
                currClass = a.ClassName;                        //check its class
                if (currClass != lastClass)                     //new class?
                {
                    if (list != null)                           //do we have a list built up?
                    {
                        //log last class' profit
                        History.Profit.Add(lastClass, Quick.AvgList(list));
                    }
                    list      = new List <double>();                    //make a new list
                    lastClass = currClass;
                }
                list.Add(a.GetProfit());                                //push profit onto list
            }

            //add the last class too
            History.Profit.Add(lastClass, Quick.AvgList(list));

            //sort by id so everything works again
            //_agents.Sort(Quick.sortAgentId);
        }
Пример #4
0
        /********REPORT**********/
        public MarketReport GetMarketReport(int rounds)
        {
            var mr = new MarketReport();

            mr.strListGood       = "Commod\n\n";
            mr.strListGoodPrices = "Price\n\n";
            mr.strListGoodTrades = "Trades\n\n";
            mr.strListGoodAsks   = "Supply\n\n";
            mr.strListGoodBids   = "Demand\n\n";

            mr.strListAgent       = "Classes\n\n";
            mr.strListAgentCount  = "Count\n\n";
            mr.strListAgentProfit = "Profit\n\n";
            mr.strListAgentMoney  = "Money\n\n";

            mr.arrStrListInventory = new List <string>();

            foreach (var commodity in goodTypes)
            {
                mr.strListGood += commodity + "\n";

                var price = History.Prices.Average(commodity, rounds);
                mr.strListGoodPrices += Quick.NumToStr(price, 2) + "\n";

                var asks = History.Asks.Average(commodity, rounds);
                mr.strListGoodAsks += (int)(asks) + "\n";

                var bids = History.Bids.Average(commodity, rounds);
                mr.strListGoodBids += (int)(bids) + "\n";

                var trades = History.Trades.Average(commodity, rounds);
                mr.strListGoodTrades += (int)(trades) + "\n";

                mr.arrStrListInventory.Add(commodity + "\n\n");
            }
            foreach (var key in mapAgents.Keys)
            {
                var inventory = new List <double>();
                foreach (var str in goodTypes)
                {
                    inventory.Add(0);
                }
                mr.strListAgent += key + "\n";
                var profit = History.Profit.Average(key, rounds);
                mr.strListAgentProfit += Quick.NumToStr(profit, 2) + "\n";

                var    list  = agents;         //var list = _agents.filter(function(a:BasicAgent):Bool { return a.className == key; } );  dfs stub wtf
                int    count = 0;
                double money = 0;

                foreach (var a in list)
                {
                    if (a.ClassName == key)
                    {
                        count++;
                        money += a.Money;
                        for (int lic = 0; lic < goodTypes.Count; lic++)
                        {
                            inventory[lic] += a.QueryInventory(goodTypes[lic]);
                        }
                    }
                }

                money /= count;
                for (int lic = 0; lic < goodTypes.Count; lic++)
                {
                    inventory[lic] /= count;
                    mr.arrStrListInventory[lic] += Quick.NumToStr(inventory[lic], 1) + "\n";
                }

                mr.strListAgentCount += Quick.NumToStr(count, 0) + "\n";
                mr.strListAgentMoney += Quick.NumToStr(money, 0) + "\n";
            }
            return(mr);
        }