Inheritance: IStockTicker
Esempio n. 1
0
        private static void Main(string[] args)
        {
            var shoppy = new Shoppy();
            shoppy.TestShoppy();
            return;
            ;

            var stockTicker = new StockTicker();
            var stockListner = new RxStockMonitor(stockTicker);
            ThreadPool.QueueUserWorkItem((_)=>stockTicker.Notify(new StockTick() { Price = 100, QuoteSymbol = "MSFT" }));
            ThreadPool.QueueUserWorkItem((_)=>stockTicker.Notify(new StockTick() { Price = 150, QuoteSymbol = "INTC" }));
            ThreadPool.QueueUserWorkItem((_)=>stockTicker.Notify(new StockTick() { Price = 170, QuoteSymbol = "MSFT" }));
            ThreadPool.QueueUserWorkItem((_)=>stockTicker.Notify(new StockTick() { Price = 195.5M, QuoteSymbol = "MSFT" }));

            while (true)
            {
                Console.Write("enter symbol: ");
                var symbol = Console.ReadLine();
                if (symbol.ToLower() == "x")
                {
                    break;
                }
                Console.WriteLine("enter value: ");
                var val = decimal.Parse(Console.ReadLine());
                for (int i = 0; i < 30; i++)
                {
                    int i1 = i;
                    Task.Run(() => stockTicker.Notify(new StockTick() { Price = val, QuoteSymbol = symbol + i1 }));
                }
            }

            Console.WriteLine("Bye Bye");
        }
Esempio n. 2
0
 private static void ManualSimulator(StockTicker stockTicker)
 {
     //////////////////////////////////////////////////////
     // A small program to let you enter the Ticks info. //
     // Symbol X will exit the program                   //
     //////////////////////////////////////////////////////
     while (true)
     {
         Console.Write("enter symbol (or x to exit): ");
         var symbol = Console.ReadLine();
         if (symbol.ToLower() == "x")
         {
             break;
         }
         Console.WriteLine("enter price: ");
         decimal price;
         if (decimal.TryParse(Console.ReadLine(), out price))
         {
             stockTicker.Notify(new StockTick() { Price = price, QuoteSymbol = symbol });
         }
         else
         {
             Console.WriteLine("price should be decimal");
         }
     }
 }
Esempio n. 3
0
 private static void TestConcurrentTicks(StockTicker stockTicker)
 {
     ThreadPool.QueueUserWorkItem((_) => stockTicker.Notify(new StockTick() {Price = 100, QuoteSymbol = "MSFT"}));
     ThreadPool.QueueUserWorkItem((_) => stockTicker.Notify(new StockTick() {Price = 150, QuoteSymbol = "INTC"}));
     ThreadPool.QueueUserWorkItem((_) => stockTicker.Notify(new StockTick() {Price = 170, QuoteSymbol = "MSFT"}));
     ThreadPool.QueueUserWorkItem((_) => stockTicker.Notify(new StockTick() {Price = 195.5M, QuoteSymbol = "MSFT"}));
 }
Esempio n. 4
0
        private static void Main(string[] args)
        {
            //
            // Uncomment the StockTicker version you wish to test
            //
            var stockTicker = new StockTicker();
            //var stockTicker = new RxStockMonitor(stockTicker);

            //
            // Uncomment to test the case of Concurrent Ticks
            //
            //TestConcurrentTicks(stockTicker);

            // A small program to let you enter the Ticks info.
            // Symbol X will exit the program
            while (true)
            {
                Console.Write("enter symbol: ");
                var symbol = Console.ReadLine();
                if (symbol.ToLower() == "x")
                {
                    break;
                }
                Console.WriteLine("enter value: ");
                var val = decimal.Parse(Console.ReadLine());
                for (int i = 0; i < 30; i++)
                {
                    int i1 = i;
                    Task.Run(() => stockTicker.Notify(new StockTick() { Price = val, QuoteSymbol = symbol + i1 }));
                }
            }

            Console.WriteLine("Bye Bye");
        }
Esempio n. 5
0
 public StockSimulator(StockTicker ticker)
 {
     _ticker = ticker;
     _ticks = new[]
     {
         new StockTick() {QuoteSymbol = "MSFT", Price = 53.49M},
         new StockTick() {QuoteSymbol = "INTC", Price = 32.68M},
         new StockTick() {QuoteSymbol = "ORCL", Price = 41.48M},
         new StockTick() {QuoteSymbol = "CSCO", Price = 28.33M},
     };
 }
Esempio n. 6
0
        private static void Main(string[] args)
        {
            var stockTicker = new StockTicker();

            /////////////////////////////////////////////////////////////
            //                                                         //
            // 1. Uncomment the StockMonitor version you wish to test  //
            //                                                         //
            /////////////////////////////////////////////////////////////

            // Regular events StockMonitor
            var stockMonitor = new StockMonitor(stockTicker);

            // Rx StockMonitor
            //var stockMonitor = new RxStockMonitor(stockTicker);

            //////////////////////////////////////////////////////////////////
            //                                                              //
            // 2. (optional) Uncomment to test the case of Concurrent Ticks //
            //                                                              //
            //////////////////////////////////////////////////////////////////

            //TestConcurrentTicks(stockTicker);

            //////////////////////////////////////////////////////
            // A small program to let you enter the Ticks info. //
            // Symbol X will exit the program                   //
            //////////////////////////////////////////////////////
            while (true)
            {
                Console.Write("enter symbol (or x to exit): ");
                var symbol = Console.ReadLine();
                if (symbol.ToLower() == "x")
                {
                    break;
                }
                Console.WriteLine("enter price: ");
                decimal price;
                if (decimal.TryParse(Console.ReadLine(), out price))
                {
                    stockTicker.Notify(new StockTick() {Price = price, QuoteSymbol = symbol});
                }
                else
                {
                    Console.WriteLine("price should be decimal");
                }
            }

            GC.KeepAlive(stockMonitor);
            Console.WriteLine("Bye Bye");
        }
Esempio n. 7
0
        public RxStockMonitor(StockTicker ticker)
        {
            const decimal maxChangeRatio = 0.1m;

            IObservable<StockTick> ticks =
            Observable.FromEventPattern<EventHandler<StockTick>, StockTick>(
            h => ticker.StockTick += h, //#A
            h => ticker.StockTick -= h) //#B
            .Select(tickEvent => tickEvent.EventArgs)
            .Synchronize();

            IObservable<IGroupedObservable<string, StockTick>> groupedTicks =
            ticks.GroupBy(tick => tick.QuoteSymbol);
            var drasticChanges =
            from tick in ticks
            group tick by tick.QuoteSymbol
            into company
            from tickPair in company.Buffer(2, 1)
            let changeRatio = Math.Abs((tickPair[1].Price - tickPair[0].Price)/tickPair[0].Price)
            where changeRatio > maxChangeRatio
            select new DrasticChange()
            {
                Symbol = company.Key,
                ChangeRatio = changeRatio,
                OldPrice = tickPair[0].Price,
                NewPrice = tickPair[1].Price
            };

            _subscription =
            drasticChanges.Subscribe(change =>
                {
                    Console.WriteLine("Stock:{0} has changed with {1} ratio, Old Price:{2} New Price:{3}", change.Symbol,
                        change.ChangeRatio,
                        change.OldPrice,
                        change.NewPrice);
                },
                ex => { /* code that handles erros */}, //#C
                () =>{/* code that handles the observable completenss */}); //#C
        }
Esempio n. 8
0
        private static void Main(string[] args)
        {
            _stockTicker = new StockTicker();

            /////////////////////////////////////////////////////////////
            //                                                         //
            // 1. Uncomment the StockMonitor version you wish to test  //
            //                                                         //
            /////////////////////////////////////////////////////////////

            // Regular events StockMonitor
            //var stockMonitor = new StockMonitor(_stockTicker);

            // Rx StockMonitor - uses Rx to consume and process the stock ticks
            var stockMonitor = new RxStockMonitor(_stockTicker);

            ShowMenu();

            GC.KeepAlive(stockMonitor);
            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
            Console.WriteLine("Bye Bye");
        }
Esempio n. 9
0
 private static void AutomaticSimulator(StockTicker stockTicker)
 {
     var simulator = new StockSimulator(stockTicker);
     simulator.Run();
 }
Esempio n. 10
0
            object _stockTickLocker = new object(); //#A

            #endregion Fields

            #region Constructors

            public StockMonitor(StockTicker ticker)
            {
                _ticker = ticker;
                //var stockTicker = new StockTicker();
                ticker.StockTick += OnStockTick;
            }
Esempio n. 11
0
 public StockMonitor(StockTicker ticker)
 {
     _ticker = ticker;
     ticker.StockTick += OnStockTick;
 }
Esempio n. 12
0
 public StockMonitor(StockTicker ticker)
 {
     _ticker = ticker;
     //var stockTicker = new StockTicker();
     ticker.StockTick += OnStockTick;
 }
Esempio n. 13
0
 public StockMonitor(StockTicker ticker)
 {
     _ticker            = ticker;
     _ticker.StockTick += OnStockTick;
 }
Esempio n. 14
0
        private static void AutomaticSimulator(StockTicker stockTicker)
        {
            var simulator = new StockSimulator(stockTicker);

            simulator.Run();
        }