Ejemplo n.º 1
0
        private void OrderFilledHandler(OrderFilledData data)
        {
            TraderInfo traderInfo = TraderStatistic.Find(x => x.Symbol.Equals(data.Symbol));

            if (traderInfo != null)
            {
                traderInfo.Update(data);
                TraderInfoChanged?.Invoke(traderInfo);
            }

            // Code below is usefull?
            List <Trade> updatedPositions = new List <Trade>();
            List <Trade> allTrades        = new List <Trade>();

            foreach (Trade trade in data.Trades)
            {
                trade.Symbol = data.Symbol;
                updatedPositions.Add(trade);
            }

            foreach (Trade trade in data.RawTrades)
            {
                trade.Symbol = data.Symbol;
                allTrades.Add(trade);
            }
            NewTrades?.Invoke(updatedPositions, allTrades);

            _receiver.OrderFilledHandle(data);
        }
Ejemplo n.º 2
0
        public void NotifyOrderLegMatched(OrderLeg orderLegRecord, OrderFilledData orderFilledDataRecord)
        {
            orderFilledDataRecord.TransactionId = orderFilledCount++;
            OrderData         orderData;
            ClientFilledOrder clientFilledOrderRecord;
            ClientOrder       clientOrderRecord;


            Console.WriteLine("Order Matched:  Order Id {0} , Client {1}  Transaction Id {2} Token Pair {3}-{4} type {5} Amount filled {6} at Price {7}",
                              orderLegRecord.OrderLegId, orderLegRecord.Order.ClientId, orderFilledDataRecord.TransactionId,
                              orderLegRecord.Token1Id, orderLegRecord.Token2Id, orderLegRecord.OrderLegType.ToString(),
                              orderFilledDataRecord.Token1Amount, orderFilledDataRecord.PriceFilled);

            // Update Position
            addFilledOrderToPosition(orderLegRecord, orderFilledDataRecord);

            lock (ClientOrderLock)
            {
                // This should always be true
                if (ClientOrders.TryGetValue(orderLegRecord.Order.ClientId, out clientOrderRecord) == true)
                {
                    if (clientOrderRecord.ClientOrders.TryGetValue(orderLegRecord.OrderLegId, out orderData) == true)
                    {
                        orderData.OrderFillStatus     = OrderLegFillStatusEnum.Partial;
                        orderData.Token1AmountFilled += orderFilledDataRecord.Token1Amount;
                        UpdateBlockChain(orderLegRecord, orderFilledDataRecord);


                        lock (ClientFilledOrderLock)
                        {
                            if (ClientFilledOrders.TryGetValue(orderLegRecord.Order.ClientId, out clientFilledOrderRecord) == false)
                            {
                                clientFilledOrderRecord = new ClientFilledOrder(orderLegRecord.Order.ClientId);
                                clientFilledOrderRecord.ClientTransactions.TryAdd(orderFilledDataRecord.TransactionId, orderFilledDataRecord);

                                ClientFilledOrders.TryAdd(orderLegRecord.Order.ClientId, clientFilledOrderRecord);
                            }
                            else
                            {
                                clientFilledOrderRecord.ClientTransactions.TryAdd(orderFilledDataRecord.TransactionId, orderFilledDataRecord);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception(String.Format("Error:  Unable to  locate client order {0} in NotifyOrderLegMatched", orderLegRecord.OrderLegId));
                    }
                }
                else
                {
                    throw new Exception(String.Format("Error: Unable to locate client orders for clientId {0}", orderLegRecord.Order.ClientId));
                }
            }
        }
Ejemplo n.º 3
0
 internal void Update(OrderFilledData data)
 {
     Update(data as CommonFields);
     LastTradePrice            = data.LastTradePrice;
     LastTradeQuantity         = data.LastTradeQuantity;
     PositionContracts         = data.PositionContracts;
     PositionLiquidationVolume = data.PositionLiquidationVolume;
     PositionType   = data.PositionType;
     PositionVolume = data.PositionVolume;
     TraderBalance2 = data.TraderBalance2;
 }
Ejemplo n.º 4
0
        public void SaveFilledOrders(WatchOrder buyOrder, WatchOrder sellOrder, decimal AmountOffset, decimal OrderPrice)
        {
            OrderFilledData orderFilledDataRecord = new OrderFilledData();

            orderFilledDataRecord.OrderLegId   = buyOrder.orderLegRecord.OrderLegId;
            orderFilledDataRecord.Token1Amount = AmountOffset;
            orderFilledDataRecord.Token1Id     = buyOrder.orderLegRecord.Token1Id;
            orderFilledDataRecord.Token2Id     = buyOrder.orderLegRecord.Token2Id;
            orderFilledDataRecord.Token2Amount = AmountOffset * (buyOrder.orderLegRecord.OrderPriceTerms == OrderPriceTermsEnum.Token2PerToken1 ?
                                                                 OrderPrice : 1.0M / OrderPrice);
            orderFilledDataRecord.OrderPriceTerms = buyOrder.orderLegRecord.OrderPriceTerms;
            orderFilledDataRecord.FilledDateTime  = System.DateTime.Now;

            _exchangeBook.NotifyOrderLegMatched(buyOrder.orderLegRecord, orderFilledDataRecord);
            _exchangeBook.NotifyOrderLegMatched(sellOrder.orderLegRecord, orderFilledDataRecord);

            //Console.WriteLine("Updating Order Book for Order {0}/{1} offset amount {2} at price {3}", buyOrder.orderLegRecord.OrderId, sellOrder.orderLegRecord.OrderId, AmountOffset, OrderPrice);
        }
Ejemplo n.º 5
0
        private void UpdateBlockChain(OrderLeg orderLegRecord, OrderFilledData orderFilledData)
        {
            BlockChainData blockChainRecord = new BlockChainData();
            KeyPairData    KeyPair          = new KeyPairData();

            //Console.WriteLine("Writing Order Flows to blockchain");

            lock (BlockChainTransactionsLock)
            {
                if (orderLegRecord.BuySellType == OrderLegBuySellEnum.Buy)
                {
                    blockChainRecord = new BlockChainData();

                    blockChainRecord.clientId = orderLegRecord.Order.ClientId;

                    blockChainRecord.Date        = orderFilledData.FilledDateTime.ToShortDateString();
                    blockChainRecord.BlockNumber = blockCount++;
                    blockChainRecord.OrderId     = orderLegRecord.OrderId;
                    blockChainRecord.PayReceive  = "Receive";
                    blockChainRecord.TokenAmount = orderFilledData.Token1Amount;
                    blockChainRecord.TokenId     = orderFilledData.Token1Id;

                    if (KeyPairs.TryGetValue(1, out KeyPair) == true)
                    {
                        blockChainRecord.Address = KeyPair.publicKey;
                    }
                    else
                    {
                        //blockChainRecord.Address = GenerateNewClientKey(orderLegRecord.Order.ClientId);
                    }

                    blockChainRecord.TransactionHash = HashOrder(blockChainRecord);
                    BlockChainTransactions.TryAdd(blockChainRecord.BlockNumber, blockChainRecord);


                    blockChainRecord             = new BlockChainData();
                    blockChainRecord.clientId    = orderLegRecord.Order.ClientId;
                    blockChainRecord.Date        = DateTime.Now.ToShortDateString();
                    blockChainRecord.BlockNumber = blockCount++;
                    blockChainRecord.OrderId     = orderFilledData.OrderLegId;
                    blockChainRecord.PayReceive  = "Pay";
                    blockChainRecord.TokenAmount = orderFilledData.Token2Amount;
                    blockChainRecord.TokenId     = orderFilledData.Token2Id;

                    if (KeyPairs.TryGetValue(0, out KeyPair) == true)
                    {
                        blockChainRecord.Address = KeyPair.publicKey;
                    }
                    else
                    {
                        //blockChainRecord.Address = GenerateNewClientKey(orderLegRecord.Order.ClientId);
                    }

                    blockChainRecord.TransactionHash = HashOrder(blockChainRecord);
                    BlockChainTransactions.TryAdd(blockChainRecord.BlockNumber, blockChainRecord);
                }
                else
                {
                    blockChainRecord      = new BlockChainData();
                    blockChainRecord.Date = DateTime.Now.ToShortDateString();

                    blockChainRecord.clientId    = orderLegRecord.Order.ClientId;
                    blockChainRecord.BlockNumber = blockCount++;
                    blockChainRecord.OrderId     = orderLegRecord.OrderId;
                    blockChainRecord.PayReceive  = "Pay";
                    blockChainRecord.TokenAmount = orderFilledData.Token1Amount;
                    blockChainRecord.TokenId     = orderFilledData.Token1Id;

                    if (KeyPairs.TryGetValue(0, out KeyPair) == true)
                    {
                        blockChainRecord.Address = KeyPair.publicKey;
                    }
                    else
                    {
                    }

                    blockChainRecord.TransactionHash = HashOrder(blockChainRecord);
                    BlockChainTransactions.TryAdd(blockChainRecord.BlockNumber, blockChainRecord);

                    blockChainRecord      = new BlockChainData();
                    blockChainRecord.Date = DateTime.Now.ToShortDateString();

                    blockChainRecord.clientId    = orderLegRecord.Order.ClientId;
                    blockChainRecord.BlockNumber = blockCount++;
                    blockChainRecord.OrderId     = orderLegRecord.OrderId;
                    blockChainRecord.PayReceive  = "Receive";
                    blockChainRecord.TokenAmount = orderFilledData.Token2Amount;
                    blockChainRecord.TokenId     = orderFilledData.Token2Id;
                    if (KeyPairs.TryGetValue(1, out KeyPair) == true)
                    {
                        blockChainRecord.Address = KeyPair.publicKey;
                    }
                    else
                    {
                    }

                    blockChainRecord.TransactionHash = HashOrder(blockChainRecord);
                    BlockChainTransactions.TryAdd(blockChainRecord.BlockNumber, blockChainRecord);
                }
            }
        }
Ejemplo n.º 6
0
        private void addFilledOrderToPosition(OrderLeg orderLegRecord, OrderFilledData orderFilledDataRecord)
        {
            ClientPosition clientPositionRecord;

            ClientPositionData ClientPosition = new ClientPositionData();
            TokenRateData      TokenRate      = new TokenRateData();
            int clientId = orderLegRecord.Order.ClientId;


            if (ClientPositions.TryGetValue(clientId, out clientPositionRecord) == false)
            {
                clientPositionRecord = new ClientPosition(clientId);
                ClientPositions.TryAdd(clientId, clientPositionRecord);
            }


            if (clientPositionRecord == null)
            {
                throw new Exception("Unable to find/generate client position record");
            }


            if (orderLegRecord.BuySellType == OrderLegBuySellEnum.Buy)
            {
                if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token1Id, out ClientPosition) == true)
                {
                    ClientPosition.TokenAmount += orderFilledDataRecord.Token1Amount;
                    ClientPosition.TokenValue   = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                }
                else
                {
                    ClientPosition = new ClientPositionData();

                    ClientPosition.TokenId     = orderFilledDataRecord.Token1Id;
                    ClientPosition.TokenAmount = orderFilledDataRecord.Token1Amount;

                    ClientPosition.TokenRate   = 0.0m;
                    ClientPosition.TokenRateIn = "USD";

                    if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true)
                    {
                        ClientPosition.TokenRate   = (decimal)TokenRate.AskRate;
                        ClientPosition.TokenRateIn = TokenRate.PriceCurrency;
                    }

                    ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                    clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition);
                }


                if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token2Id, out ClientPosition) == true)
                {
                    ClientPosition.TokenAmount -= orderFilledDataRecord.Token2Amount;
                    ClientPosition.TokenValue   = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                }
                else
                {
                    ClientPosition = new ClientPositionData();

                    ClientPosition.TokenId      = orderFilledDataRecord.Token2Id;
                    ClientPosition.TokenAmount -= orderFilledDataRecord.Token2Amount;

                    ClientPosition.TokenRate   = 0.0m;
                    ClientPosition.TokenRateIn = "USD";

                    if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true)
                    {
                        ClientPosition.TokenRate   = (decimal)TokenRate.AskRate;
                        ClientPosition.TokenRateIn = TokenRate.PriceCurrency;
                    }

                    ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                    clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition);
                }
            }
            else
            {
                if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token2Id, out ClientPosition) == true)
                {
                    ClientPosition.TokenAmount += orderFilledDataRecord.Token2Amount;
                    ClientPosition.TokenValue   = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                }
                else
                {
                    ClientPosition             = new ClientPositionData();
                    ClientPosition.TokenId     = orderFilledDataRecord.Token2Id;
                    ClientPosition.TokenAmount = orderFilledDataRecord.Token2Amount;

                    ClientPosition.TokenRate   = 0.0m;
                    ClientPosition.TokenRateIn = "USD";

                    if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true)
                    {
                        ClientPosition.TokenRate   = (decimal)TokenRate.AskRate;
                        ClientPosition.TokenRateIn = TokenRate.PriceCurrency;
                    }

                    ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                    clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition);
                }

                if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token1Id, out ClientPosition) == true)
                {
                    ClientPosition.TokenAmount -= orderFilledDataRecord.Token1Amount;
                    ClientPosition.TokenValue   = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                }
                else
                {
                    ClientPosition              = new ClientPositionData();
                    ClientPosition.TokenId      = orderFilledDataRecord.Token1Id;
                    ClientPosition.TokenAmount -= orderFilledDataRecord.Token1Amount;

                    ClientPosition.TokenRate   = 0.0m;
                    ClientPosition.TokenRateIn = "USD";

                    if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true)
                    {
                        ClientPosition.TokenRate   = (decimal)TokenRate.AskRate;
                        ClientPosition.TokenRateIn = TokenRate.PriceCurrency;
                    }

                    ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate;
                    clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition);
                }
            }
        }
Ejemplo n.º 7
0
        internal void OrderFilledHandle(OrderFilledData data)
        {
            Guid guid = data.OrigClientId;

            _ordersLock.EnterUpgradeableReadLock();
            try
            {
                if (_orders.ContainsKey(guid) && data.Status == OrderStatus.Partial)
                {
                    //_orders[data.ClientId].FilledQuantity += data.Quantity;
                    //Guid newClientId = data.NewClientId;
                    ////_orders[newClientId] = _orders[data.ClientId];
                    //_orders.Add(newClientId, _orders[data.ClientId]);
                    _orders[guid].PaidPrice = data.PaidPrice;
                    _orders[guid].Type      = data.OrderType;
                    _orders[guid].Side      = data.Side;
                    _orders[guid].Leverage  = data.Leverage;
                    _orders[guid].Duration  = data.Duration;
                    _orders[guid].Price     = data.Price;
                    _orders[guid].Quantity  = data.Quantity;
                    //OrderBase tmpOrder = _orders[data.ClientId];
                    //tmpOrder.Status = OrderStatus.Accepted;
                    //_orders[newClientId].ClientId = newClientId;
                    //_orders[newClientId].OrigClientId = data.OrigClientId;
                    _orders[guid].OrigQuantity = data.OrigQuantity;
                    _orders[guid].OpenTime     = data.OpenTime;
                    foreach (var trade in data.RawTrades)
                    {
                        _orders[guid].Trades.Add(trade);
                    }
                    _orders[guid].Status = data.Status;

                    _orders[guid].OrderChanged();


                    //_ordersLock.EnterWriteLock();
                    //try
                    //{
                    //    _orders.Remove(data.ClientId);
                    //}
                    //finally
                    //{ _ordersLock.ExitWriteLock(); }
                }
                else if (_orders.ContainsKey(guid) && data.Status == OrderStatus.Filled)
                {
                    foreach (var trade in data.RawTrades)
                    {
                        _orders[guid].Trades.Add(trade);
                    }

                    _orders[guid].Status = data.Status;

                    _orders[guid].OrderChanged();

                    _ordersLock.EnterWriteLock();
                    try
                    {
                        _orders[guid].Dispose();
                        _orders.Remove(guid);
                    }
                    finally
                    { _ordersLock.ExitWriteLock(); }
                }
            }
            finally
            { _ordersLock.ExitUpgradeableReadLock(); }
        }