Example #1
0
        public void Valid_Ask_With_Wrong_StockID_Order_Should_NOT_Place()
        {
            //arrange
            var ask = Ask.New(NewId(), "Wrong", units: 4, price: 100);

            //act
            _matcher.Tell(ask);

            //assert
            var askResult = ExpectMsg <AskResult>(r => Assert.False(r.Success, r.Reason));

            Assert.Equal("StockId doesn't match", askResult.Reason);
        }
Example #2
0
        static void Main(string[] args)
        {
            using var system = ActorSystem.Create("trade");

            var matcher = system.ActorOf(Props.Create(() => new Matcher("MSFT")));

            // Place a few orders
            matcher.Tell(Ask.New("1", "MSFT", units: 50, price: 99.00m), ActorRefs.Nobody);
            matcher.Tell(Bid.New("2", "MSFT", units: 100, price: 100.00m), ActorRefs.Nobody);
            matcher.Tell(Ask.New("3", "MSFT", units: 50, price: 100.00m), ActorRefs.Nobody);

            // Get the current price
            matcher.Ask <GetPriceResult>(Ask.New("3", "MSFT", units: 50, price: 100.00m))
            .ContinueWith(r => Console.WriteLine($"MSFT bid:{r.Result.Bid:c2} ask:{r.Result.Ask:c2}"));

            // Listen for trade settlement and price changes
            var logger = system.ActorOf(dsl =>
            {
                dsl.Receive <TradeSettled>((evt, ctx) =>
                                           Console.WriteLine($"! Settled bid {evt.BidOrderId} with ask {evt.AskOrderId}: {evt.Units} @ {evt.Price:c2}"));
                dsl.Receive <PriceChanged>((evt, ctx) =>
                                           Console.WriteLine($"! {evt.StockId} now at bid: {evt.Bid:c2} ask: {evt.Ask:c2}"));
                dsl.Receive <OrderPlaced>((evt, ctx) =>
                                          Console.WriteLine($"! Order placed: {evt.Order}"));
            });

            // Subscribe to trade settlement, price change, and order placed events
            system.EventStream.Subscribe(logger, typeof(TradeSettled));
            system.EventStream.Subscribe(logger, typeof(PriceChanged));
            system.EventStream.Subscribe(logger, typeof(OrderPlaced));

            // Get the full list of successful trades
            var trades = matcher.Ask <GetTradesResult>(new GetTrades())
                         .ContinueWith(r => Console.WriteLine(string.Join(Environment.NewLine, r.Result.Orders)));

            Console.WriteLine("Press enter to stop trading engine");
            Console.ReadLine();
        }
Example #3
0
 private static Ask NewAsk(int units, decimal price) => Ask.New(NewId(), StockId, units: units, price: price);