public override Order actBeforeOpening(Stock currentStock)
 {
     Order tempOrder = new Order();
     //never act before opening, place random order in act()
     tempOrder.OrderAgentPriceOfOrder = 0;
     return tempOrder;
 }
        public List<Stock> ReadInStocksFromFile(string path, int numberOfStocks = 0)
        {
            List<Stock> stocks = new List<Stock>();
            string[] lines = System.IO.File.ReadAllLines(path);
            Stock currentStock = new Stock(lines[0].Substring(1));
            for (int i = 1; i < lines.Length; i++)
            {
                if (lines[i].Contains('#'))
                {
                    if (currentStock != null)
                    {
                        stocks.Add(currentStock);
                        if (numberOfStocks > 0 && stocks.Count >= numberOfStocks)
                        {
                            return stocks;
                        }
                    }

                    currentStock = new Stock(lines[i].Substring(1));
                }
                else
                {
                    currentStock.AddPrice(double.Parse(lines[i], CultureInfo.GetCultureInfo("en-US")));
                }

            }

            return stocks;
        }
 //private double calculateNewPrice()
 //{
 //    return stockPriceBook.getEndofDayPrice(StockMarketSimulation.simDay);
 //}
 private void createStocks()
 {
     allStocks = new List<Stock>();
     for (int i = 0; i < defaultValues.stockNumber; i++)
     {
         Stock stock = new Stock(i.ToString());
         allStocks.Add(stock);
     }
 }
        public override Order act(Stock currentStock)
        {
            Order tempOrder = new Order();
            List<double> lastPrices = currentStock.GetPricesSince(this.meanPriceHistoryLength);
            double LastPrice = currentStock.stockPriceBook.getEndofDayPrice(StockMarketSimulation.simDay - 1);

            if (lastPrices == null)
            {
                lastPrices = new List<double>();
                lastPrices.Add(1.0);
            }

            int tempHistory = this.meanPriceHistoryLength;

            while (tempHistory > lastPrices.Count)
                tempHistory--;

            if (tempHistory == 0)
                return tempOrder;

            double r = 0;

            // MA
            for (int i = 0; i < tempHistory; i++)
            {
                r += LastPrice - lastPrices.ElementAt(i);
            }

            double tempBudget = 0;

            // risk seeking approach
            if (r <= 0)
            {
                tempOrder.OrderAgentSizeOrder = random.Next(1, Convert.ToInt32(Math.Ceiling((double)(maxOrderNumber + 1) * minInvest)));
                tempBudget = this.budget * minInvest;
            }
            else
            {
                tempOrder.OrderAgentSizeOrder = random.Next(1, Convert.ToInt32((maxOrderNumber + 1) * maxInvest));
                tempBudget = this.budget * maxInvest;
            }
            int choice = 1;
            if (r < 0)
                choice = -1;

            double price = LastPrice * getRandomDouble(this.minCorrectingCoefficient, this.maxCorrectingCoefficient);

            tempOrder.OrderAgentPriceOfOrder = (float)price * choice;
            tempOrder.OrderAgentNumber = this.number;
            tempOrder.simulationDay = StockMarketSimulation.simDay;
            tempOrder.OrderAgentSizeOrder = random.Next(1, maxOrderNumber + 1);

            account(ref tempOrder, ref currentStock, tempBudget);

            return tempOrder;
        }
        public override Order act(Stock currentStock)
        {
            Order tempOrder = new Order();

            //1 is buy and -1 is sell
            int buyOrSell = random.NextDouble() < 0.5 ? 1 : -1;
            double LastPrice = currentStock.stockPriceBook.getEndofDayPrice(StockMarketSimulation.simDay - 1);
            double price = LastPrice * getRandomDouble(this.minCorrectingCoefficient, this.maxCorrectingCoefficient);

            tempOrder.OrderAgentPriceOfOrder = (float)price * buyOrSell;
            tempOrder.OrderAgentNumber = this.number;
            tempOrder.simulationDay = StockMarketSimulation.simDay;
            tempOrder.OrderAgentSizeOrder = random.Next(1, maxOrderNumber + 1);

            account(ref tempOrder, ref currentStock);
            return tempOrder;
        }
        protected void account(ref Order tempOrder, ref Stock currentStock, double tempBudget)
        {
            //buy
            if (tempOrder.OrderAgentPriceOfOrder > 0)
            {
                while (!isBudgetHighEnough(tempOrder, tempBudget) && tempOrder.OrderAgentSizeOrder > 0)
                    tempOrder.OrderAgentSizeOrder--;
                addToOwnedStocks(currentStock, tempOrder.OrderAgentSizeOrder);
            }
            //sell
            else if (tempOrder.OrderAgentPriceOfOrder < 0)
            {
                while (!ownsEnoughStocks(Convert.ToInt32(currentStock.Name), tempOrder.OrderAgentSizeOrder) && tempOrder.OrderAgentSizeOrder > 0)
                {
                    tempOrder.OrderAgentSizeOrder--;
                }
                removeFromOwnedStocks(currentStock, tempOrder.OrderAgentSizeOrder);
            }

            this.budget -= tempOrder.OrderAgentSizeOrder * tempOrder.OrderAgentPriceOfOrder;
        }
 public int getDecisionsOfLastAgents(int length, Stock stock)
 {
     return stock.stockPriceBook.getLocalHistory(length);
 }
 public virtual Order actBeforeOpening(Stock currentStock)
 {
     return new Order();
 }
 public virtual Order act(Stock currentStock)
 {
     return new Order();
 }
 protected void removeFromOwnedStocks(Stock stock, int amount)
 {
     this.ownedStocks[Convert.ToInt32(stock.Name)] -= amount;
 }
 protected double getMeanPriceOfTwoDaysBefore(Stock stock)
 {
     return stock.stockPriceBook.getEndofDayPrice(StockMarketSimulation.simDay - 2);
 }
 protected void addToOwnedStocks(Stock stock, int amount)
 {
     this.ownedStocks[Convert.ToInt32(stock.Name)] += amount;
 }
        public override Order act(Stock currentStock)
        {
            Order tempOrder = new Order();

            //if agent has acted before opening
            //return 0 price order that is ignored in the stockpricebook
            if (actedBeforeOpening)
            {
                tempOrder.OrderAgentPriceOfOrder = 0;
                return tempOrder;
            }

            double LastPrice = currentStock.stockPriceBook.getEndofDayPrice(StockMarketSimulation.simDay - 1);

            double buysellswitch = 0.5;

            if (this.probOfImitatingTheMarket > getRandomDouble())
            {
                if (getMeanPriceOfTwoDaysBefore(currentStock) < getMeanPriceOfDayBefore(currentStock))
                {
                    buysellswitch = this.asymmetricBuySellProb;
                }
                if (getMeanPriceOfTwoDaysBefore(currentStock) > getMeanPriceOfDayBefore(currentStock))
                {
                    buysellswitch = 1 - this.asymmetricBuySellProb;
                }
            }

            int choice = 1;

            if (this.probOfLocalImitation > getRandomDouble())
            {
                int tempDecision = getDecisionsOfLastAgents(this.localHistoryLength, currentStock);
                if (tempDecision > 0)
                {
                    buysellswitch = 1 - this.asymmetricBuySellProb;
                }
                if (tempDecision < 0)
                {
                    buysellswitch = this.asymmetricBuySellProb;
                }

            }

            if (buysellswitch < getRandomDouble())
            {
                choice = -1;
            }

            double price = LastPrice * getRandomDouble(this.minCorrectingCoefficient, this.maxCorrectingCoefficient);

            if (LastPrice < this.floorP)
            {
                if (this.agentProbToActBelowFloorPrice > getRandomDouble())
                {
                    price = LastPrice;
                    choice = 1;
                }
            }

            int stopLossChoice = 0;
            double stopLossMeanPrice = currentStock.stockPriceBook.getMeanPrice(this.stopLossInterval);

            if (LastPrice >= stopLossMeanPrice * (1 + this.maxLossRate))
                stopLossChoice = 1;
            if (LastPrice <= stopLossMeanPrice * (1 - this.maxLossRate))
                stopLossChoice = -1;

            if (stopLossChoice != 0)
            {
                if (this.agentProbToAdoptStopLoss > getRandomDouble())
                {
                    price = LastPrice;
                    choice = stopLossChoice;
                }
            }

            tempOrder.OrderAgentPriceOfOrder = (float)price * choice;
            tempOrder.OrderAgentNumber = this.number;
            tempOrder.simulationDay = StockMarketSimulation.simDay;
            tempOrder.OrderAgentSizeOrder = random.Next(1, maxOrderNumber + 1);

            account(ref tempOrder, ref currentStock);

            return tempOrder;
        }
 private Series createSerieForSimulatedDataChart(Stock stock, int length)
 {
     double[] prices = stock.GetPricesFromTo(0, length).ToArray();
     Series serie = new Series();
     serie.ChartType = SeriesChartType.FastLine;
     serie.Name = stock.Name;
     serie.Color = getRandomColor();
     serie.XValueType = ChartValueType.Int32;
     serie.XValueMember = "Day";
     serie.YValueType = ChartValueType.Double;
     serie.YValueMembers = "Price";
     for (int i = 0; i < prices.Length; i++)
     {
         serie.Points.AddXY(i + 1, prices[i]);
     }
     return serie;
 }