public System.Object ClientCommunication(Version serverversion, ICommunicateable communicator, bool full, System.Object obj) { try { if (serverversion.Major > 0) { if (full) { Id = communicator.ReceiveString(); Name = communicator.ReceiveString(); int c = communicator.ReceiveInt(); if (c > 0) { StockIndexes stockindexes = (StockIndexes) obj; for (int i = 0; i < c; i++) { string id = communicator.ReceiveString(); StockIndex stockindex = null; if (stockindexes.TryGetValue(id, out stockindex)) { stockindex.Stocks.Add(Id, this); StockIndexes.Add(stockindex.Id, stockindex); } } } PriceHistory.ClientCommunication(serverversion, communicator, full, obj); } if (serverversion.Major > 0 && serverversion.Minor >= 1) { MinPrice = communicator.ReceiveDouble(); MaxPrice = communicator.ReceiveDouble(); } else { MinPrice = MIN_PRICE; MaxPrice = MAX_PRICE; } Price = communicator.ReceiveDouble(); if (full && PriceHistory.Count > 0) PriceHistory.RemoveAt(PriceHistory.Count - 1); Available = communicator.ReceiveInt(); OwnedByPlayers = communicator.ReceiveInt(); } return this; } catch (System.Exception ex) { throw ex; } }
public void AddStockIndex(StockIndex stockindex) { try { if (!StockIndexes.ContainsKey(stockindex.Id)) { StockIndexes.Add(stockindex.Id, stockindex); if (!stockindex.Stocks.ContainsKey(this.Id)) stockindex.Stocks.Add(this.Id, this); } } catch (System.Exception ex) { throw ex; } }
public Stock(string id, string name, StockIndex stockindex, System.Random random) : base() { try { Id = id; Name = name; StockIndexes = new StockIndexes(); PriceHistory = new DoubleHistory(); AddStockIndex(stockindex); Reset(random); } catch (System.Exception ex) { throw ex; } }
public System.Object Load(Version fv, WsgFileStream fs, System.Object obj) { try { if (fv.Major > 0) { Id = fs.ReadString(); Name = fs.ReadString(); int c = fs.ReadInt(); if (c > 0) { StockIndexes stockindexes = (StockIndexes) obj; for (int i = 0; i < c; i++) { string id = fs.ReadString(); StockIndex stockindex = null; if (stockindexes.TryGetValue(id, out stockindex)) { stockindex.Stocks.Add(Id, this); StockIndexes.Add(stockindex.Id, stockindex); } } } PriceHistory.Load(fv, fs, obj); if (fv.Major > 0 && fv.Minor >= 1) { MinPrice = fs.ReadDouble(); MaxPrice = fs.ReadDouble(); } else { MinPrice = MIN_PRICE; MaxPrice = MAX_PRICE; } Price = fs.ReadDouble(); if (PriceHistory.Count > 0) PriceHistory.RemoveAt(PriceHistory.Count - 1); Available = fs.ReadInt(); OwnedByPlayers = fs.ReadInt(); } return this; } catch (System.Exception ex) { throw ex; } }
public void Load(System.Random random) { try { while (StockIndexes.Count > 0) { StockIndexes.Clear(); } while (Stocks.Count > 0) { Stocks.Clear(); } if (XmlDocument == null) { XmlDocument = new System.Xml.XmlDocument(); } XmlDocument.Load(SetupFileName); AllStocksXmlNode = XmlDocument.DocumentElement.SelectSingleNode("AllStocks"); if (AllStocksXmlNode == null) { throw new System.Exception("No node named 'AllStocks' in the file named '" + SetupFileName + "'."); } else if (AllStocksXmlNode.Attributes["indexid"] == null) { throw new System.Exception("No attribute named 'indexid' on a node named '" + AllStocksXmlNode.LocalName + "' in the file named '" + SetupFileName + "'."); } System.Xml.XmlNodeList xmlnodes = XmlDocument.DocumentElement.SelectNodes("StockIndex"); if (xmlnodes == null) { throw new System.Exception("No nodes named 'StockIndex' in the file named '" + SetupFileName + "'."); } else if (xmlnodes.Count == 0) { throw new System.Exception("No nodes named 'StockIndex' in the file named '" + SetupFileName + "'."); } foreach (System.Xml.XmlNode xmlnode in xmlnodes) { if (xmlnode.Attributes["indexid"] == null) { throw new System.Exception("No attribute named 'indexid' on a node named '" + xmlnode.LocalName + "' in the file named '" + SetupFileName + "'."); } if (xmlnode.Attributes["name"] == null) { throw new System.Exception("No attribute named 'named' on a node named '" + xmlnode.LocalName + "' in the file named '" + SetupFileName + "'."); } StockIndex stockindex = null; if (!StockIndexes.TryGetValue(xmlnode.Attributes["indexid"].Value, out stockindex)) { stockindex = new StockIndex(xmlnode.Attributes["indexid"].Value, xmlnode.Attributes["name"].Value); StockIndexes.Add(stockindex.Id, stockindex); } System.Xml.XmlNodeList xmlchildnodes = xmlnode.SelectNodes("Stock"); if (xmlchildnodes == null) { throw new System.Exception("No nodes named 'Stock' under the node named '" + xmlnode.LocalName + "' where indexid is '" + xmlnode.Attributes["indexid"].Value + "' in the file named '" + SetupFileName + "'."); } if (xmlchildnodes.Count == 0) { throw new System.Exception("No nodes named 'Stock' under the node named '" + xmlnode.LocalName + "' where indexid is '" + xmlnode.Attributes["indexid"].Value + "' in the file named '" + SetupFileName + "'."); } foreach (System.Xml.XmlNode xmlchildnode in xmlchildnodes) { if (xmlchildnode.Attributes["stockid"] == null) { throw new System.Exception("No attribute named 'stockid' on a node named '" + xmlchildnode.LocalName + "' under the node named '" + xmlnode.LocalName + "' where indexid is '" + xmlnode.Attributes["indexid"].Value + "' in the file named '" + SetupFileName + "'."); } Stock stock = null; if (!Stocks.TryGetValue(xmlchildnode.Attributes["stockid"].Value, out stock)) { if (xmlchildnode.HasChildNodes) { if (xmlchildnode.HasChildNodes) { stock = new Stock(xmlchildnode.Attributes["stockid"].Value, xmlchildnode.FirstChild.Value, stockindex, random); Stocks.Add(stock.Id, stock); } } } else if (xmlchildnode.HasChildNodes) { stock.AddStockIndex(stockindex); } } } if (AllStocksIndexId.Length > 0 && AllStocksIndexName.Length > 0) { if (!StockIndexes.ContainsKey(AllStocksIndexId)) { StockIndex stockindex = new StockIndex(AllStocksIndexId, AllStocksIndexName); foreach (Stock stock in Stocks.Values) { stock.StockIndexes.Add(stockindex.Id, stockindex); stockindex.Stocks.Add(stock.Id, stock); } StockIndexes.Add(stockindex.Id, stockindex); } } xmlnodes = XmlDocument.DocumentElement.SelectNodes("Player"); if (xmlnodes == null) { throw new System.Exception("No nodes named 'Player' in the file named '" + SetupFileName + "'."); } else if (xmlnodes.Count == 0) { throw new System.Exception("No nodes named 'Player' in the file named '" + SetupFileName + "'."); } foreach (System.Xml.XmlNode xmlnode in xmlnodes) { if (xmlnode.Attributes["company"] == null) { throw new System.Exception("No attribute named 'company' on a node named '" + xmlnode.LocalName + "' in the file named '" + SetupFileName + "'."); } if (xmlnode.HasChildNodes) { Players.Add(new Player(Players.NextPlayerId, xmlnode.Attributes["company"].Value, xmlnode.FirstChild.Value, Stocks)); } } } catch (System.Exception ex) { throw ex; } }
public void Play(MarketState marketstate, System.Random random) { try { if (IsComputer) { if (Deposit.Count > 0) { StockIndex lastindex = null; double lastpriceaverage = 0D; double savecapital = 0D; foreach (DepositContent content in Deposit.Values) { if (content.Stock.StockIndexes.Count > 0) { System.Collections.Generic.Dictionary <string, StockIndex> .Enumerator e = content.Stock.StockIndexes.GetEnumerator(); if (e.MoveNext()) { if (lastindex == null) { lastindex = e.Current.Value; lastpriceaverage = lastindex.PriceAverage; savecapital = (lastindex.PriceAverage * 15); } else if (e.Current.Value.Id != lastindex.Id) { lastindex = e.Current.Value; lastpriceaverage = lastindex.PriceAverage; savecapital = (lastindex.PriceAverage * 15); } // Handle computer players deposit. int buyprocentlow = 0, buyprocentmiddle = 0, buyprocenthigh = 0, maxbuyprepoch = MAX_BUY_PR_EPOCH, stockstobuy = 0; int sellprocentlow = 0, sellprocentmiddle = 0, sellprocenthigh = 0, maxsellprepoch = MAX_SELL_PR_EPOCH, stockstosell = 0; switch (marketstate.State) { case MarketStateType.Normal: buyprocentlow = 75; buyprocentmiddle = 85; buyprocenthigh = 95; sellprocentlow = 50; sellprocentmiddle = 70; sellprocenthigh = 90; break; case MarketStateType.Depression: buyprocentlow = 60; buyprocentmiddle = 70; buyprocenthigh = 80; sellprocentlow = 30; sellprocentmiddle = 50; sellprocenthigh = 70; break; case MarketStateType.Boom: buyprocentlow = 70; buyprocentmiddle = 80; buyprocenthigh = 90; sellprocentlow = 70; sellprocentmiddle = 80; sellprocenthigh = 90; break; default: buyprocentlow = 100; buyprocentmiddle = 100; buyprocenthigh = 100; sellprocentlow = 100; sellprocentmiddle = 100; sellprocenthigh = 100; break; } // Try to buy stocks. if (content.Stock.Price < content.Stock.MinPrice * 4 && content.Stock.Available > 0) { if (random.Next(100) > buyprocentlow / 1.75 && maxbuyprepoch > 0) { stockstobuy = MAX_STOCKS_TO_BUY; if (stockstobuy > content.Stock.Available) { stockstobuy = content.Stock.Available; } while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital) { stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10)); } if (stockstobuy > 5) { content.Buy(marketstate, random.Next(stockstobuy)); maxbuyprepoch -= 1; } else if (stockstobuy > 0) { content.Buy(marketstate, stockstobuy); maxbuyprepoch -= 1; } } } else if (content.Stock.Price < lastpriceaverage / 8 && (content.LastSellPrice == 0 || content.Stock.Price < content.LastSellPrice || content.Stock.Price > content.LastSellPrice * 1.5) && content.Stock.Available > 0) { // Try to buy stocks. if (random.Next(100) > buyprocentlow && maxbuyprepoch > 0) { stockstobuy = MAX_STOCKS_TO_BUY; if (stockstobuy > content.Stock.Available) { stockstobuy = content.Stock.Available; } while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital) { stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10)); } if (stockstobuy > 5) { content.Buy(marketstate, random.Next(stockstobuy)); maxbuyprepoch -= 1; } else if (stockstobuy > 0) { content.Buy(marketstate, stockstobuy); maxbuyprepoch -= 1; } } } else if (content.Stock.Price < lastpriceaverage / 4 && (content.LastSellPrice == 0 || content.Stock.Price < content.LastSellPrice || content.Stock.Price > content.LastSellPrice * 1.5) && content.Stock.Available > 0) { // Try to buy stocks. if (random.Next(100) > buyprocentmiddle && maxbuyprepoch > 0) { stockstobuy = MAX_STOCKS_TO_BUY; if (stockstobuy > content.Stock.Available) { stockstobuy = content.Stock.Available; } while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital) { stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10)); } if (stockstobuy >= 10) { stockstobuy = stockstobuy / 2; if (stockstobuy > 5) { content.Buy(marketstate, random.Next(stockstobuy)); maxbuyprepoch -= 1; } else if (stockstobuy > 0) { content.Buy(marketstate, stockstobuy); maxbuyprepoch -= 1; } } } } else if (content.Stock.Price < lastpriceaverage && (content.LastSellPrice == 0 || content.Stock.Price < content.LastSellPrice || content.Stock.Price > content.LastSellPrice * 1.5) && content.Stock.Available > 0) { // Try to buy stocks. if (random.Next(100) > buyprocenthigh && maxbuyprepoch > 0) { stockstobuy = MAX_STOCKS_TO_BUY; if (stockstobuy > content.Stock.Available) { stockstobuy = content.Stock.Available; } while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital) { stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10)); } if (stockstobuy >= 20) { stockstobuy = stockstobuy / 4; if (stockstobuy > 5) { content.Buy(marketstate, random.Next(stockstobuy)); maxbuyprepoch -= 1; } else if (stockstobuy > 0) { content.Buy(marketstate, stockstobuy); maxbuyprepoch -= 1; } } } } // Try to sell stocks. if (marketstate.State == MarketStateType.Depression && content.Stock.Price < content.LastBuyPrice / 4 && content.LastBuyPrice > content.Stock.MinPrice * 50) { // No profit expected. stockstosell = content.Count; if (stockstosell > 5) { content.Sell(marketstate, random.Next(stockstosell)); maxsellprepoch -= 1; } else { content.Sell(marketstate, stockstosell); maxsellprepoch -= 1; } } else if (content.Count > 0 && content.Stock.Price > content.LastBuyPrice * 2 && content.Stock.Price > content.Stock.MinPrice * 5) { // Try to sell stocks. if (random.Next(100) > sellprocentlow && maxsellprepoch > 0) { stockstosell = content.Count; if (stockstosell > 5) { content.Sell(marketstate, random.Next(stockstosell)); maxsellprepoch -= 1; } else { content.Sell(marketstate, stockstosell); maxsellprepoch -= 1; } } } else if (content.Count > 0 && content.Stock.Price > content.LastBuyPrice * 1.5 && content.Stock.Price > content.Stock.MinPrice * 5) { // Try to sell stocks. if (random.Next(100) > sellprocentmiddle && maxsellprepoch > 0) { stockstosell = content.Count; if (stockstosell >= 10) { stockstosell = stockstosell / 2; if (stockstosell > 5) { content.Sell(marketstate, random.Next(stockstosell)); maxsellprepoch -= 1; } else { content.Sell(marketstate, stockstosell); maxsellprepoch -= 1; } } else { content.Sell(marketstate, stockstosell); maxsellprepoch -= 1; } } } else if (content.Count > 0 && content.Stock.Price > content.LastBuyPrice * 1.25 && content.Stock.Price > content.Stock.MinPrice * 5) { // Try to sell stocks. if (random.Next(100) > sellprocenthigh && maxsellprepoch > 0) { stockstosell = content.Count; if (stockstosell >= 20) { stockstosell = stockstosell / 4; if (stockstosell > 5) { content.Sell(marketstate, random.Next(stockstosell)); maxsellprepoch -= 1; } else { content.Sell(marketstate, stockstosell); maxsellprepoch -= 1; } } else { content.Sell(marketstate, stockstosell); maxsellprepoch -= 1; } } } } } } } } ValueHistory.AddHistory(Value); } catch (System.Exception ex) { throw ex; } }