Example #1
0
        private static void ParseStartConditions(BazaarBot bazaar, JSONNode node)
        {
            bazaar.Agents = new List <Agent>();
            var starts      = node["agents"].ToString().RemoveAll("\"{}").Split(',');
            var agent_index = 0;

            //Make given number of each agent type
            foreach (var item in starts)
            {
                var split       = item.Split(':');
                var name        = split[0].Trim();
                var val         = int.Parse(split[1]);
                var agent_class = bazaar.AgentClasses[name];
                var inv         = agent_class.GetStartInventory();
                var money       = agent_class.money;

                for (int i = 0; i < val; i++)
                {
                    var a = new Agent(agent_index, name, inv.Copy(), money);
                    a.init(bazaar);
                    bazaar.Agents.Add(a);
                    agent_index++;
                }
            }
        }
Example #2
0
        public static void LoadJsonSettings(BazaarBot bazaar, string fileName)
        {
            var node = JSON.Parse(File.ReadAllText(fileName));

            bazaar.Agents           = new List <Agent>();
            bazaar.CommodityClasses = new List <string>();
            bazaar.AgentClasses     = new Dictionary <string, AgentClass>();

            ParseCommodities(bazaar, node["commodities"]);
            ParseAgents(bazaar, node["agents"]);
            ParseStartConditions(bazaar, node["start_conditions"]);
        }
Example #3
0
        public Offer create_ask(BazaarBot bazaar, string commodity_, float limit_)
        {
            var ask_price = determine_price_of(commodity_);
            var ideal     = determine_sale_quantity(bazaar, commodity_);

            //can't sell less than limit
            var quantity_to_sell = ideal < limit_ ? limit_ : ideal;

            if (quantity_to_sell > 0)
            {
                return(new Offer(Id, commodity_, quantity_to_sell, ask_price));
            }
            return(null);
        }
Example #4
0
        public Offer create_bid(BazaarBot bazaar, string commodity, float limit)
        {
            var bid_price = determine_price_of(commodity);
            var ideal     = determine_purchase_quantity(bazaar, commodity);

            //can't buy more than limit
            var quantity_to_buy = ideal > limit ? limit : ideal;

            if (quantity_to_buy > 0)
            {
                return(new Offer(Id, commodity, quantity_to_buy, bid_price));
            }
            return(null);
        }
Example #5
0
 private static void ParseAgents(BazaarBot bazaar, JSONNode node)
 {
     bazaar.AgentClasses = new Dictionary <string, AgentClass>();
     foreach (var agent in node.AsArray.Children)
     {
         //a.inventory.size = { };
         //foreach (var key in _map_commodities.Keys) {
         //    var c = _map_commodities[key];
         //    Reflect.setField(a.inventory.size, c.id, c.size);
         //}
         var agentClass = JSONParser.ParseAgentClass(agent);
         bazaar.AgentClasses[agentClass.id]  = agentClass;
         bazaar.ProfitHistory[agentClass.id] = new List <float>();
     }
 }
Example #6
0
        public void init(BazaarBot bazaar)
        {
            var list_commodities = bazaar.get_commodities_unsafe();

            foreach (var str in list_commodities)
            {
                var trades = new List <float>();

                var price = 6;
                trades.Add(price * 0.5f);
                trades.Add(price * 1.5f);       //push two fake trades to generate a range

                //set initial price belief & observed trading range
                _observedTradingRange.Add(str, trades);
                _priceBeliefs.Add(str, new Point(price * 0.5f, price * 1.5f));
            }
        }
Example #7
0
 private static void ParseCommodities(BazaarBot bazaar, JSONNode node)
 {
     foreach (var commodity in node.AsArray.Children)
     {
         var id = commodity["id"].Value;
         bazaar.CommodityClasses.Add(id);
         bazaar.PriceHistory[id] = new List <float>();
         bazaar.AskHistory[id]   = new List <float>();
         bazaar.BidHistory[id]   = new List <float>();
         bazaar.VarHistory[id]   = new List <float>();
         bazaar.TradeHistory[id] = new List <float>();
         bazaar.PriceHistory[id].Add(1);    //start the bidding at $1!
         bazaar.AskHistory[id].Add(1);      //start history charts with 1 fake buy/sell bid
         bazaar.BidHistory[id].Add(1);
         bazaar.TradeHistory[id].Add(1);
         bazaar.Asks[id] = new List <Offer>();
         bazaar.Bids[id] = new List <Offer>();
     }
 }
Example #8
0
        private float determine_sale_quantity(BazaarBot bazaar, string commodity_)
        {
            var mean          = bazaar.GetPriceAverage(commodity_, _lookback);
            var trading_range = observe_trading_range(commodity_);

            if (trading_range != null)
            {
                var favorability = position_in_range(mean, trading_range.X, trading_range.Y);
                //position_in_range: high means price is at a high point

                var amount_to_sell = System.Math.Round(favorability * Inventory.Surplus(commodity_));
                if (amount_to_sell < 1)
                {
                    amount_to_sell = 1;
                }
                return((float)amount_to_sell);
            }
            return(0);
        }
Example #9
0
        public void generate_offers(BazaarBot bazaar, string commodity)
        {
            Offer offer;
            float surplus = Inventory.Surplus(commodity);

            if (surplus >= 1)
            {
                offer = create_ask(bazaar, commodity, 1);
                if (offer != null)
                {
                    bazaar.ask(offer);
                }
            }
            else
            {
                float shortage  = Inventory.Shortage(commodity);
                float space     = Inventory.SpaceEmpty(commodity);
                float unit_size = Inventory.Size(commodity);

                if (shortage > 0 && space >= unit_size)
                {
                    float limit;
                    if ((shortage * unit_size) <= space)
                    { //enough space for ideal order
                        limit = shortage;
                    }
                    else
                    {                                                              //not enough space for ideal order
                        limit = (float)System.Math.Floor(space / shortage);
                    }

                    if (limit > 0)
                    {
                        offer = create_bid(bazaar, commodity, limit);
                        if (offer != null)
                        {
                            bazaar.bid(offer);
                        }
                    }
                }
            }
        }
Example #10
0
        private float determine_purchase_quantity(BazaarBot bazaar, string commodity)
        {
            var mean          = bazaar.GetPriceAverage(commodity, _lookback);
            var trading_range = observe_trading_range(commodity);

            if (trading_range != null)
            {
                var favorability = position_in_range(mean, trading_range.X, trading_range.Y);
                favorability = 1 - favorability;
                //do 1 - favorability to see how close we are to the low end

                var amount_to_buy = System.Math.Round(favorability * Inventory.Shortage(commodity));
                if (amount_to_buy < 1)
                {
                    amount_to_buy = 1;
                }
                return((float)amount_to_buy);
            }
            return(0);
        }
Example #11
0
        public MarketReport(BazaarBot bazaar)
        {
            _commodities = new List <string> {
                "R: " + bazaar.TotalRounds.ToString()
            };
            var rounds = 1;

            foreach (var commodity in bazaar.CommodityClasses)
            {
                _commodities.Add(commodity);
                _commodityPrices.Add(bazaar.GetPriceAverage(commodity, rounds).ToString("N2"));
                _commodityAsks.Add(bazaar.GetAskAverage(commodity, rounds).ToString());
                _commodityBids.Add(bazaar.GetBidAverage(commodity, rounds).ToString());
                _commodityTrades.Add(bazaar.GetTradeAverage(commodity, rounds).ToString());
                _commodityProduction.Add(BazaarBot.Production.ContainsKey(commodity) ? BazaarBot.Production[commodity].ToString() : string.Empty);
            }

            foreach (var agentKey in bazaar.AgentClasses.Keys)
            {
                _agents.Add(agentKey);
                _agentProfit.Add(bazaar.GetProfitAverage(agentKey, rounds).ToString("N2"));

                var agents = bazaar.Agents.Where(p => p.ClassId == agentKey);

                _agentCount.Add(agents.Count().ToString("N0"));
                _agentMoney.Add(BazaarBot.Average(agents.Select(p => p.Money)).ToString("N0"));


                var inventory = Enumerable.Repeat(0f, bazaar.CommodityClasses.Count).ToArray();
                foreach (var a in agents)
                {
                    for (int i = 0; i < bazaar.CommodityClasses.Count; i++)
                    {
                        inventory[i] += a.QueryInventory(bazaar.CommodityClasses[i]);
                    }
                }
                _inventory.AddRange(inventory.Select(p => (p > 0 ? p / agents.Count() : 0)));
            }
        }
Example #12
0
        public void update_price_model(BazaarBot bazaar, string act, string commodity, bool success, float unit_price_ = 0)
        {
            if (success)
            {
                //Add this to my list of observed trades
                var observed_trades = _observedTradingRange[commodity];
                observed_trades.Add(unit_price_);
            }

            var public_mean_price = bazaar.GetPriceAverage(commodity, 1);

            var belief = _priceBeliefs[commodity];
            var mean   = (belief.X + belief.Y) / 2;
            var wobble = 0.05;

            var delta_to_mean = mean - public_mean_price;

            if (success)
            {
                if (act == "buy" && delta_to_mean > SIGNIFICANT)
                {                                  //overpaid
                    belief.X -= delta_to_mean / 2; //SHIFT towards mean
                    belief.Y -= delta_to_mean / 2;
                }
                else if (act == "sell" && delta_to_mean < -SIGNIFICANT)
                {                                  //undersold
                    belief.X -= delta_to_mean / 2; //SHIFT towards mean
                    belief.Y -= delta_to_mean / 2;
                }

                belief.X += (float)wobble * mean;       //increase the belief's certainty
                belief.Y -= (float)wobble * mean;
            }
            else
            {
                belief.X -= delta_to_mean / 2;  //SHIFT towards the mean
                belief.Y -= delta_to_mean / 2;

                var special_case = false;
                var stocks       = QueryInventory(commodity);
                var ideal        = Inventory.Ideal(commodity);

                if (act == "buy" && stocks < LOW_INVENTORY * ideal)
                {
                    //very low on inventory AND can't buy
                    wobble      *= 2;                   //bid more liberally
                    special_case = true;
                }
                else if (act == "sell" && stocks > HIGH_INVENTORY * ideal)
                {
                    //very high on inventory AND can't sell
                    wobble      *= 2;                   //ask more liberally
                    special_case = true;
                }

                if (!special_case)
                {
                    //Don't know what else to do? Check supply vs. demand
                    var asks = bazaar.GetAskAverage(commodity, 1);
                    var bids = bazaar.GetBidAverage(commodity, 1);

                    //supply_vs_demand: 0=balance, 1=all supply, -1=all demand
                    var supply_vs_demand = (asks - bids) / (asks + bids);

                    //too much supply, or too much demand
                    if (supply_vs_demand > SIG_IMBALANCE || supply_vs_demand < -SIG_IMBALANCE)
                    {
                        //too much supply: lower price
                        //too much demand: raise price

                        var new_mean = public_mean_price * (1 - supply_vs_demand);
                        delta_to_mean = mean - new_mean;

                        belief.X -= delta_to_mean / 2;  //SHIFT towards anticipated new mean
                        belief.Y -= delta_to_mean / 2;
                    }
                }

                belief.X -= (float)wobble * mean;       //decrease the belief's certainty
                belief.Y += (float)wobble * mean;
            }

            if (belief.X < MIN_PRICE)
            {
                belief.X = MIN_PRICE;
            }
            if (belief.Y < MIN_PRICE)
            {
                belief.Y = MIN_PRICE;
            }
            if (belief.Y > MAX_PRICE)
            {
                belief.Y = MAX_PRICE;
            }
            if (belief.X > MAX_PRICE)
            {
                belief.X = MAX_PRICE;
            }
        }