public void SimulateMarket() { try { AxChartSpace chartSpace = this.axChartSpace; // It is possible for this foreground thread to be called after the thread has been terminated. This will prevent // the COM control from being accessed during a shutdown sequence. Also, if there is nothing to display there's no // need to continue trying to paint the chart. if (!this.isTickerRunning || chartSpace.Charts.Count == 0) { return; } ChChart chartMain = chartSpace.Charts[0]; int side = random.Next(0, 3); if (side == 0) { int range = this.market.Bid.Rows.Count; if (range == 0) { return; } int bidIndex = random.Next(0, range - 1); Market.BidRow bidRow = this.market.Bid[bidIndex]; int down = random.Next(0, 2); if (down == 0) { int largeBlock = random.Next(0, 3); double quantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); bidRow.Quantity -= quantity; if (bidRow.Quantity <= 0.0) { bidRow.Quantity = 0.0; } } else { int largeBlock = random.Next(0, 3); double quantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); bidRow.Quantity += quantity; } bidRow.AcceptChanges(); } else { if (side == 1) { int range = this.market.Ask.Rows.Count; if (range == 0) { return; } int askIndex = random.Next(0, range - 1); Market.AskRow askRow = this.market.Ask[askIndex]; int down = random.Next(0, 2); if (down == 0) { int largeBlock = random.Next(0, 3); double quantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); askRow.Quantity -= quantity; if (askRow.Quantity <= 0.0) { askRow.Quantity = 0.0; } } else { int largeBlock = random.Next(0, 3); double quantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); askRow.Quantity += quantity; } askRow.AcceptChanges(); } else { int largeBlock = random.Next(0, 3); int direction = random.Next(0, 3); double newPrice = this.lastPrice + (direction == 0 ? -0.01 : direction == 1 ? 0.0 : 0.01); Market.BidRow highestBid = null; foreach (Market.BidRow bidLoop in market.Bid) { if (highestBid == null || bidLoop.Price > highestBid.Price) { highestBid = bidLoop; } } Market.AskRow lowestAsk = null; foreach (Market.AskRow askLoop in market.Ask) { if (lowestAsk == null || askLoop.Price < lowestAsk.Price) { lowestAsk = askLoop; } } if (newPrice < highestBid.Price || lowestAsk.Price < newPrice) { return; } this.lastPrice = newPrice; this.lastQuantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); } } Series series = GenerateSeries(); ChSeries chSeries = chartMain.SeriesCollection[0]; chSeries.SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, series.values); chSeries.SetData(ChartDimensionsEnum.chDimFormatValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, series.format); } catch (Exception exception) { Console.WriteLine(exception.Message); } }
private void MoveMarket(double targetPrice) { lock (this) { // Select four random bids to delete or reduce. for (int movement = 0; movement < 2; movement++) { int range = this.market.Bid.Rows.Count; int bidIndex = random.Next(0, range - 1); Market.BidRow bidRow = this.market.Bid[bidIndex]; int deleteBidChance = random.Next(0, 3); if (deleteBidChance == 0) { bidRow = null; foreach (Market.BidRow bidLoop in market.Bid) { if (bidRow == null || bidLoop.Price > bidRow.Price) { bidRow = bidLoop; } } if (bidRow != null) { bidRow.Delete(); } } else { bidRow = this.market.Bid[bidIndex]; int largeBlock = random.Next(0, 3); double quantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); bidRow.Quantity -= quantity; if (bidRow.Quantity <= 0) { bidRow.Delete(); } } bidRow.AcceptChanges(); } for (int movement = 0; movement < 2; movement++) { Market.AskRow askRow = null; int newAskChance = random.Next(0, 3); if (newAskChance == 0) { Market.AskRow lowestAsk = null; foreach (Market.AskRow lowestAskLoop in market.Ask) { if (lowestAsk == null || lowestAskLoop.Price < lowestAsk.Price) { lowestAsk = lowestAskLoop; } } if (lowestAsk != null) { int largeBlock = random.Next(0, 3); double askPrice = lowestAsk.Price - 0.01 - Math.Round(random.NextDouble() * 0.02, 2); double quantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); askRow = market.Ask.NewAskRow(); askRow.Price = Convert.ToDouble(askPrice); askRow.Quantity = quantity; askRow.Time = DateTime.Now; market.Ask.AddAskRow(askRow); } } else { int range = this.market.Ask.Rows.Count; int askIndex = random.Next(0, range - 1); askRow = this.market.Ask[askIndex]; int largeBlock = random.Next(0, 3); double quantity = Convert.ToDouble(random.Next(1, largeBlock == 0 ? 20 : 5) * 100); askRow.Quantity += quantity; } askRow.AcceptChanges(); } } }