public static void SetTickPriceValue(this Level1MarketData level1MarketData, int field, double price)
        {
            switch (field)
            {
            case TickType.BID:
                level1MarketData.Bid = price;
                break;

            case TickType.ASK:
                level1MarketData.Ask = price;
                break;

            case TickType.LAST:
                level1MarketData.Last = price;
                break;

            case TickType.OPEN:
                level1MarketData.Open = price;
                break;

            case TickType.HIGH:
                level1MarketData.High = price;
                break;

            case TickType.LOW:
                level1MarketData.Low = price;
                break;

            case TickType.CLOSE:
                level1MarketData.Close = price;
                break;
            }
        }
        public static void SetTickSizeValue(this Level1MarketData level1MarketData, int field, int size)
        {
            switch (field)
            {
            case TickType.BID_SIZE:
                level1MarketData.BidSize = size;
                break;

            case TickType.ASK_SIZE:
                level1MarketData.AskSize = size;
                break;

            case TickType.LAST_SIZE:
                level1MarketData.LastSize = size;
                break;

            case TickType.VOLUME:
                level1MarketData.Volume = size;
                break;

            case TickType.OPTION_CALL_VOLUME:
                level1MarketData.CallVolume = size;
                break;

            case TickType.OPTION_PUT_VOLUME:
                level1MarketData.PutVolume = size;
                break;
            }
        }
 public static bool Completed(this Level1MarketData data)
 {
     return(data.BidSize.HasValue &&
            data.Bid.HasValue &&
            data.AskSize.HasValue &&
            data.Ask.HasValue &&
            data.LastSize.HasValue &&
            data.Last.HasValue &&
            data.Open.HasValue &&
            data.High.HasValue &&
            data.Low.HasValue &&
            data.Close.HasValue &&
            data.Volume.HasValue &&
            data.PutVolume.HasValue &&
            data.CallVolume.HasValue);
 }
        public void PersistLevel1MarketData(Level1MarketData level1MarketData)
        {
            using (var unitOfWork = _unitOfWorkFactory.Create())
            {
                try
                {
                    var level1MarketDataRepository = unitOfWork.GetRepository <IRepository <Level1MarketData> >();
                    level1MarketDataRepository.Add(level1MarketData);

                    unitOfWork.Complete();
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                }
            }
        }
        public Task <Level1MarketData> GetLevel1MarketDataAsync(string symbol)
        {
            var reqId            = new Random(DateTime.Now.Millisecond).Next();
            var level1MarketData = new Level1MarketData()
            {
                Symbol    = symbol.ToUpper(),
                Timestamp = DateTime.Now
            };

            var ct  = new CancellationTokenSource(15 * 1000);
            var res = new TaskCompletionSource <Level1MarketData>();

            ct.Token.Register(() => res.TrySetResult(level1MarketData), false);

            EventHandler <TickSizeEventArgs> tickSize = (sender, args) =>
            {
                if (args.RequestId != reqId)
                {
                    return;
                }

                level1MarketData.SetTickSizeValue(args.Field, args.Size);

                if (level1MarketData.Completed())
                {
                    res.TrySetResult(level1MarketData);
                }
            };

            EventHandler <TickPriceEventArgs> tickPrice = (sender, args) =>
            {
                if (args.RequestId != reqId)
                {
                    return;
                }

                level1MarketData.SetTickPriceValue(args.Field, args.Price);

                if (level1MarketData.Completed())
                {
                    res.TrySetResult(level1MarketData);
                }
            };

            EventDispatcher.TickSize  += tickSize;
            EventDispatcher.TickPrice += tickPrice;

            var contract = new Contract
            {
                Symbol   = symbol.ToUpper(),
                SecType  = "STK",
                Currency = "USD",
                Exchange = "SMART"
            };

            ClientSocket.reqMktData(reqId, contract, GenericTick.OPTION_VOLUME.ToString(), false, false, new List <TagValue>());

            res.Task.ContinueWith(x =>
            {
                ClientSocket.cancelMktData(reqId);
                EventDispatcher.TickSize  -= tickSize;
                EventDispatcher.TickPrice -= tickPrice;
            }, TaskContinuationOptions.None);

            return(res.Task);
        }