Exemplo n.º 1
0
        /// <summary>
        /// This object will simulate an auction market on equities.
        /// </summary>
        static Price()
        {
            // Install the Server Market Data in this container.
            Price.components       = new System.ComponentModel.Container();
            Price.serverMarketData = new Shadows.Quasar.Server.ServerMarketData(Price.components);

            // One or more simulators can be running at a time to simulate different market feeds.  This counter keeps track of how
            // many are running at a time.  The counter is also used in an implicit name created for new threads.  If a name isn't
            // provided when the thread is started, this counter will be used to create a default name.
            Price.simulatorCounter = 0;

            // This hash table is used to manage the multiple simulated threads.
            Price.marketThreadTable = new Hashtable();

            // This signal is used for pausing -- without terminating -- a simulation thread.
            Price.pausePrice = new ManualResetEvent(false);

            // This is the initial time to sleep between generating simulated ticks. The value is in terms of milliseconds.
            Price.timer = Convert.ToInt32(1000.0M / DefaultTickerRate);

            try
            {
                // Lock the tables.
                Debug.Assert(!ServerMarketData.AreLocksHeld);
                ServerMarketData.PriceLock.AcquireWriterLock(Timeout.Infinite);

                // Initialize the bid and ask prices around the last known price.
                foreach (ServerMarketData.PriceRow priceRow in ServerMarketData.Price)
                {
                    // Initialize the bid price.
                    decimal bidChange = (decimal)(Math.Round(Random.NextDouble() * 0.25, 2));
                    priceRow.BidPrice = priceRow.LastPrice - bidChange;
                    priceRow.BidSize  = (decimal)(Math.Round(Random.NextDouble() * 10.0, 0) * 100.0 + 100);

                    // Initialize the ask price.
                    decimal askChange = (decimal)(Math.Round(Random.NextDouble(), 2) * 0.25);
                    priceRow.AskPrice = priceRow.LastPrice + askChange;
                    priceRow.AskSize  = (decimal)(Math.Round(Random.NextDouble() * 10.0, 0) * 100.0 + 100);
                }
            }
            catch (Exception exception)
            {
                // Write the error and stack trace out to the debug listener
                Debug.WriteLine(String.Format("{0}, {1}", exception.Message, exception.StackTrace));
            }
            finally
            {
                // Release the global tables.
                if (ServerMarketData.PriceLock.IsWriterLockHeld)
                {
                    ServerMarketData.PriceLock.ReleaseWriterLock();
                }
                Debug.Assert(!ServerMarketData.AreLocksHeld);
            }
        }
Exemplo n.º 2
0
        static Broker()
        {
            // Install the Server Market Data in this container.
            Broker.components       = new System.ComponentModel.Container();
            Broker.serverMarketData = new Shadows.Quasar.Server.ServerMarketData(Broker.components);

            // Create an order book to hold the orders.
            orderBookLock = new ReaderWriterLock();
            orderBook     = new OrderBook();

            // This is a list of brokers that service the simulated orders.
            Broker.brokerList = new ArrayList();

            // This queue is used to pass along a changed placement record to a thread that will create an order in the order book.
            Broker.placementQueue = new PlacementQueue();

            // This event is used to signal that there are orders in the order book that need simulated executions.
            Broker.orderBookEvent = new ManualResetEvent(false);

            try
            {
                // Lock the tables
                Debug.Assert(!ServerMarketData.AreLocksHeld);
                ServerMarketData.PlacementLock.AcquireReaderLock(CommonTimeout.LockWait);

                // Install an event handler that will pump proposed orders into a thread that makes them into simulated orders
                // serviced by an imaginary broker.
                ServerMarketData.Placement.PlacementRowChanged +=
                    new ServerMarketData.PlacementRowChangeEventHandler(Broker.PlacementRowChangeEvent);
            }
            finally
            {
                // Release the table locks.
                if (ServerMarketData.PlacementLock.IsReaderLockHeld)
                {
                    ServerMarketData.PlacementLock.ReleaseReaderLock();
                }
                Debug.Assert(!ServerMarketData.AreLocksHeld);
            }
        }