Beispiel #1
0
 public System.Object ClientCommunication(Version serverversion, ICommunicateable communicator, bool full, System.Object obj)
 {
     try
     {
         if (serverversion.Major > 0)
         {
             if (full)
             {
                 IsComputer = communicator.ReceiveBool();
                 IsYou      = communicator.ReceiveBool();
                 ValueHistory.ClientCommunication(serverversion, communicator, full, obj);
             }
             if (IsYou && !full)
             {
                 communicator.SendString(Company);
                 communicator.SendString(Name);
             }
             else
             {
                 Company = communicator.ReceiveString();
                 Name    = communicator.ReceiveString();
             }
             Deposit.ClientCommunication(serverversion, communicator, full, obj);
             Capital = communicator.ReceiveDouble();
             ValueHistory.AddHistory(Value);
         }
         return(this);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Beispiel #2
0
        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;
            }
        }