Esempio n. 1
0
        public void Sell(string Symbol, decimal Shares)
        {
            lock (_updateStockPricesLock)
            {
                if (!_updatingStockPrices)
                {
                    _updatingStockPrices = true;

                    if (MarketState == MarketState.Open)
                    {
                        Stock thisStock = null;
                        foreach (var stock in _stocks.Values)
                        {
                            if (stock.Symbol.Equals(Symbol, StringComparison.Ordinal))
                            {
                                thisStock = stock;
                            }
                        }

                        Account thisAccount = null;
                        foreach (var account in _account.Values)
                        {
                            thisAccount = account;
                        }

                        var positionValue = thisStock.Price * Shares;

                        if (positionValue <= thisAccount.Balance)
                        {
                            var transactionNumber = GetLastTransactionNumber() + 1;
                            var negativeShares = Shares * -1;
                            var positionValueNegative = positionValue * -1;
                            var order = new Transaction { TransactionNumber = transactionNumber, Symbol = thisStock.Symbol, Price = thisStock.Price, Shares = negativeShares, BuyOrSell = "Sell", TimeStamp = DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), PositionValue = positionValueNegative };
                            _transactions.TryAdd(order.TransactionNumber, order);

                            thisAccount.Balance = thisAccount.Balance - positionValue;
                            thisAccount.ShortExposure += positionValue;
                            _account.AddOrUpdate(thisAccount.BrokerID, thisAccount, (key, oldValue) => thisAccount);
                        }
                        else
                        {
                            Clients.All.alertInsufficientBalance();
                        }
                    }
                    else
                    {
                        Clients.All.alertMarketClosed();
                    }

                    _updatingStockPrices = false;
                }
            }
        }
Esempio n. 2
0
 private void BroadcastTransaction(Transaction transaction)
 {
     Clients.All.updateTransaction(transaction);
 }