public static MarketDataRequest MakeMarketDataRequest()
        {
            //let ms = System.DateTimeOffset.Now.ToUnixTimeMilliseconds()
            //let mdr = MkMarketDataRequest(
            //            MDReqID(sprintf "MDRQ-%d" ms),
            //            SubscriptionRequestType.SnapshotPlusUpdates,
            //            MarketDepth 1,
            //            [MDEntryType.Bid; MDEntryType.Offer] |> List.map MkNoMDEntryTypesGrp,
            //            [MkInstrument(Fix44.Fields.Symbol "EUR/USD")] |> List.map MkMarketDataRequestNoRelatedSymGrp
            //            )

            var msg = new MarketDataRequest();
            //var ms = System.DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var ms = 1489416392996;

            msg.MDReqID = new MDReqID($"MDRQ-{ms}");
            msg.SubscriptionRequestType = new SubscriptionRequestType(SubscriptionRequestType.SNAPSHOT_PLUS_UPDATES);
            msg.MarketDepth             = new MarketDepth(1);

            var entryTypesGroup = new MarketDataRequest.NoMDEntryTypesGroup();

            entryTypesGroup.MDEntryType = new MDEntryType(MDEntryType.BID);
            msg.AddGroup(entryTypesGroup);
            entryTypesGroup.MDEntryType = new MDEntryType(MDEntryType.OFFER);
            msg.AddGroup(entryTypesGroup);

            var symGroup = new MarketDataRequest.NoRelatedSymGroup();

            symGroup.Symbol = new Symbol("EUR/USD");
            msg.AddGroup(symGroup);
            return(msg);
        }
Example #2
0
        protected override CMState ProcessMarketDataOrderBookRequest(Wrapper wrapper)
        {
            try
            {
                MarketDataRequest mdr = MarketDataRequestConverter.GetMarketDataRequest(wrapper);

                if (mdr.SubscriptionRequestType == SubscriptionRequestType.Snapshot)
                {
                    throw new Exception(string.Format("@{0}: Market Data Order Book snaphsot not implemented for symbol {1}", BitmexConfiguration.Name, mdr.Security.Symbol));
                }
                else if (mdr.SubscriptionRequestType == SubscriptionRequestType.SnapshotAndUpdates)
                {
                    return(ProcessMarketDataRequestOrderBook(wrapper));
                }
                else if (mdr.SubscriptionRequestType == SubscriptionRequestType.Unsuscribe)
                {
                    CancelMarketDataOrderBook(mdr.Security);

                    return(CMState.BuildSuccess());
                }
                else
                {
                    throw new Exception(string.Format("@{0}: Value not recognized for subscription type {1} for symbol {2}", BitmexConfiguration.Name, mdr.SubscriptionRequestType.ToString(), mdr.Security.Symbol));
                }
            }
            catch (Exception ex)
            {
                return(CMState.BuildFail(ex));
            }
        }
Example #3
0
        public MarketDataRequest unsubscribeIncrementalRequest(string symbol)
        {
            MarketDataRequest tickerRequest = new MarketDataRequest();

            MarketDataRequest.NoRelatedSymGroup symbolGroup = new MarketDataRequest.NoRelatedSymGroup();
            symbolGroup.SetField(new Symbol(symbol));
            tickerRequest.AddGroup(symbolGroup);

            tickerRequest.Set(new MDReqID("123"));
            tickerRequest.Set(new SubscriptionRequestType('2'));
            tickerRequest.Set(new MarketDepth(0));

            addMDType(tickerRequest, '0');
            addMDType(tickerRequest, '1');
            addMDType(tickerRequest, '2');
            addMDType(tickerRequest, '3');
            addMDType(tickerRequest, '4');
            addMDType(tickerRequest, '5');
            addMDType(tickerRequest, '6');
            addMDType(tickerRequest, '7');
            addMDType(tickerRequest, '8');
            addMDType(tickerRequest, '9');
            addMDType(tickerRequest, 'A');
            addMDType(tickerRequest, 'B');
            addMDType(tickerRequest, 'C');

            return(tickerRequest);
        }
Example #4
0
        public void RequestMarketData(object state)
        {
            try
            {
                // So pra teste, aguarda 5 segundo antes de enviar request
                Thread.Sleep(15000);

                if (_marketDataRequested < _channelUmdfConfig.Markets.Count)
                {
                    QuickFix.SessionID session = (QuickFix.SessionID)state;

                    ConflatedSecurityList secList = _channelUmdfConfig.Markets[_marketDataRequested];

                    logger.Info("Requisitando snapshot MD [" + secList.Product + "] Type [" + secList.SecurityType + "]");

                    QuickFix.FIX44.MarketDataRequest mktDataReq = new MarketDataRequest();

                    mktDataReq.Product = new QuickFix.Fields.Product(Convert.ToInt32(secList.Product));    //EQUITIES
                    mktDataReq.SubscriptionRequestType = new QuickFix.Fields.SubscriptionRequestType('1'); // Subscribe + update
                    mktDataReq.MDReqID      = new QuickFix.Fields.MDReqID(secList.SecurityReqIDPrefix + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                    mktDataReq.SecurityType = new QuickFix.Fields.SecurityType(secList.SecurityType);
                    mktDataReq.MDBookType   = new QuickFix.Fields.MDBookType(3); //MBO

                    Session.SendToTarget(mktDataReq, session);

                    _marketDataRequested++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("RequestMarketData: " + ex.Message, ex);
            }
        }
Example #5
0
        /// <summary>
        /// Gets the quotes for the symbol
        /// </summary>
        private List <MarketDataSnapshot> GetQuotes(List <string> fxcmSymbols)
        {
            // get current quotes for the instrument
            var request = new MarketDataRequest();

            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SNAPSHOT);
            foreach (var fxcmSymbol in fxcmSymbols)
            {
                request.addRelatedSymbol(_fxcmInstruments[fxcmSymbol]);
            }

            AutoResetEvent autoResetEvent;

            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(request);
                autoResetEvent  = new AutoResetEvent(false);
                _mapRequestsToAutoResetEvents[_currentRequest] = autoResetEvent;
            }
            if (!autoResetEvent.WaitOne(ResponseTimeout))
            {
                throw new TimeoutException("FxcmBrokerage.GetQuotes(): Operation took longer than " +
                                           $"{((decimal)ResponseTimeout / 1000).ToStringInvariant()} seconds."
                                           );
            }

            return(_rates.Where(x => fxcmSymbols.Contains(x.Key)).Select(x => x.Value).ToList());
        }
Example #6
0
        /// <summary>
        /// Creates and returns a new <see cref="MarketDataRequest"/> FIX message.
        /// </summary>
        /// <param name="symbol">The symbol.</param>
        /// <param name="marketDepth">The market depth.</param>
        /// <param name="timeNow">The time now.</param>
        /// <returns>The FIX message.</returns>
        public static MarketDataRequest Create(string symbol, int marketDepth, ZonedDateTime timeNow)
        {
            Debug.NotEmptyOrWhiteSpace(symbol, nameof(symbol));
            Debug.NotNegativeInt32(marketDepth, nameof(marketDepth));
            Debug.NotDefault(timeNow, nameof(timeNow));

            var marketDataEntryGroup1 = new MarketDataRequest.NoMDEntryTypesGroup();

            marketDataEntryGroup1.Set(new MDEntryType(MDEntryType.BID));

            var marketDataEntryGroup2 = new MarketDataRequest.NoMDEntryTypesGroup();

            marketDataEntryGroup2.Set(new MDEntryType(MDEntryType.OFFER));

            var symbolGroup = new MarketDataRequest.NoRelatedSymGroup();

            symbolGroup.SetField(new Symbol(symbol));

            var message = new MarketDataRequest(
                new MDReqID($"MD_{timeNow.TickOfDay.ToString()}"),
                new SubscriptionRequestType(SubscriptionRequestType.SNAPSHOT_PLUS_UPDATES),
                new MarketDepth(marketDepth));

            message.Set(new MDUpdateType(0));  // val:1 to receive shortened message
            message.AddGroup(marketDataEntryGroup1);
            message.AddGroup(marketDataEntryGroup2);
            message.Set(new NoRelatedSym(1));
            message.AddGroup(symbolGroup);

            return(message);
        }
Example #7
0
        /// <summary>
        /// Removes the specified symbols to the subscription
        /// </summary>
        /// <param name="job">Job we're processing.</param>
        /// <param name="symbols">The symbols to be removed keyed by SecurityType</param>
        public void Unsubscribe(LiveNodePacket job, IDictionary <SecurityType, List <string> > symbols)
        {
            var symbolsToUnsubscribe = (from secType in symbols
                                        from symbol in secType.Value
                                        where _subscribedSymbols.Contains(symbol)
                                        select symbol).ToList();

            if (symbolsToUnsubscribe.Count == 0)
            {
                return;
            }

            Log.Trace("FxcmBrokerage.Unsubscribe(): {0}", string.Join(",", symbolsToUnsubscribe));

            var request = new MarketDataRequest();

            foreach (var symbol in symbolsToUnsubscribe)
            {
                request.addRelatedSymbol(_fxcmInstruments[ConvertSymbolToFxcmSymbol(symbol)]);
            }
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.UNSUBSCRIBE);
            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

            lock (_locker)
            {
                _gateway.sendMessage(request);
            }

            foreach (var symbol in symbolsToUnsubscribe)
            {
                _subscribedSymbols.Remove(symbol);
            }
        }
        /// <summary>
        /// The retrieve market traded volume.
        /// </summary>
        /// <param name="mostRecentTrade">
        /// The most recent trade.
        /// </param>
        /// <param name="tradingHours">
        /// The trading hours.
        /// </param>
        /// <param name="activeHistory">
        /// The active history.
        /// </param>
        /// <returns>
        /// The <see cref="long?"/>.
        /// </returns>
        private long?RetrieveMarketTradedVolume(Order mostRecentTrade, ITradingHours tradingHours, Stack <Order> activeHistory)
        {
            var closeTime =
                this.source == DataSource.AnyIntraday
                    ? UniverseDateTime
                    : tradingHours.ClosingInUtcForDay(UniverseDateTime);

            var marketDataRequest =
                new MarketDataRequest(
                    mostRecentTrade.Market?.MarketIdentifierCode,
                    mostRecentTrade.Instrument.Cfi,
                    mostRecentTrade.Instrument.Identifiers,
                    tradingHours.OpeningInUtcForDay(UniverseDateTime.Subtract(this.TradeBackwardWindowSize)),
                    closeTime,
                    RuleCtx.Id(),
                    this.source);

            var  hadMissingData     = false;
            long?marketTradedVolume = null;

            switch (this.source)
            {
            case DataSource.AnyInterday:
                var securityResultInterday = UniverseEquityInterdayCache.GetMarkets(marketDataRequest);
                hadMissingData = securityResultInterday.HadMissingData;

                if (!hadMissingData)
                {
                    marketTradedVolume = this.InterdayMarketTradedVolume(securityResultInterday);
                }

                break;

            case DataSource.AnyIntraday:
                var securityResultIntraday = UniverseEquityIntradayCache.GetMarkets(marketDataRequest);
                hadMissingData = securityResultIntraday.HadMissingData;

                if (!hadMissingData)
                {
                    marketTradedVolume = this.IntradayMarketTradedVolume(securityResultIntraday);
                }

                break;
            }

            if (hadMissingData && RunMode == RuleRunMode.ForceRun)
            {
                this.UpdatePassedFilterWithOrders(activeHistory);
                return(null);
            }

            if (hadMissingData && RunMode == RuleRunMode.ValidationRun)
            {
                this.logger.LogInformation($"market traded volume was not calculable for {mostRecentTrade.Instrument.Identifiers} due to missing data");
                this.hadMissingData = true;
                return(null);
            }

            return(marketTradedVolume);
        }
        /// <summary>
        /// Adds the specified symbols to the subscription
        /// </summary>
        /// <param name="job">Job we're subscribing for:</param>
        /// <param name="symbols">The symbols to be added keyed by SecurityType</param>
        public void Subscribe(LiveNodePacket job, IDictionary<SecurityType, List<Symbol>> symbols)
        {
            var symbolsToSubscribe = (from secType in symbols
                                      from symbol in secType.Value
                                      where !_subscribedSymbols.Contains(symbol)
                                      select symbol).ToList();
            if (symbolsToSubscribe.Count == 0)
                return;

            Log.Trace("FxcmBrokerage.Subscribe(): {0}", string.Join(",", symbolsToSubscribe));

            var request = new MarketDataRequest();
            foreach (var symbol in symbolsToSubscribe)
            {
                request.addRelatedSymbol(_fxcmInstruments[ConvertSymbolToFxcmSymbol(symbol)]);
            }
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SUBSCRIBE);
            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

            lock (_locker)
            {
                _gateway.sendMessage(request);
            }

            foreach (var symbol in symbolsToSubscribe)
            {
                _subscribedSymbols.Add(symbol);
            }
        }
Example #10
0
        public async Task CreateDataRequest(MarketDataRequest request)
        {
            this._logger.LogInformation(
                $"repository recording request for {request.MarketIdentifierCode} from {request.UniverseEventTimeFrom} to {request.UniverseEventTimeTo} for {request.Identifiers}.");

            if (string.IsNullOrWhiteSpace(request.Identifiers.Id) ||
                string.IsNullOrWhiteSpace(request.SystemProcessOperationRuleRunId))
            {
                this._logger.LogError(
                    $"CreateDataRequest had a null or empty id for identifiers for financial instrument. {request.Identifiers.Id} - {request.SystemProcessOperationRuleRunId}");
                return;
            }

            try
            {
                this._logger.LogTrace(
                    $"CreateDataRequest about to save request for {request.Identifiers} at {request.UniverseEventTimeFrom} to {request.UniverseEventTimeTo}");
                var dtoRequest = new MarketDataRequestDto(request);

                using (var dbConnection = this._dbConnectionFactory.BuildConn())
                    using (var conn = dbConnection.ExecuteAsync(CreateDataRequestSql, dtoRequest))
                    {
                        await conn;
                        this._logger.LogTrace(
                            $"CreateDataRequest has saved request for {request.Identifiers} at {request.UniverseEventTimeFrom} to {request.UniverseEventTimeTo}");
                    }
            }
            catch (Exception e)
            {
                this._logger.LogError(e, $"repository error for CreateDataRequest");
            }
        }
        public async Task DataRequestsForSystemOperation_StoredOperationId_ReturnsDataRequests()
        {
            var repo = new RuleRunDataRequestRepository(new ConnectionStringFactory(this._configuration), this._logger);

            var id = "1";

            var marketDataRequest = new MarketDataRequest(
                null,
                "XLON",
                "entsbp",
                new InstrumentIdentifiers {
                Id = "1"
            },
                DateTime.UtcNow,
                DateTime.UtcNow.AddHours(1),
                id,
                true,
                DataSource.Bmll);

            await repo.CreateDataRequest(marketDataRequest);

            var dataRequests = await repo.DataRequestsForSystemOperation("11");

            Assert.IsNotNull(dataRequests);
            Assert.AreEqual(dataRequests.Count, 1);
        }
        /// <summary>
        /// Adds the specified symbols to the subscription
        /// </summary>
        /// <param name="symbols">The symbols to be added keyed by SecurityType</param>
        private bool Subscribe(IEnumerable <Symbol> symbols)
        {
            var request = new MarketDataRequest();

            foreach (var symbol in symbols)
            {
                TradingSecurity fxcmSecurity;
                if (_fxcmInstruments.TryGetValue(_symbolMapper.GetBrokerageSymbol(symbol), out fxcmSecurity))
                {
                    request.addRelatedSymbol(fxcmSecurity);

                    // cache exchange time zone for symbol
                    DateTimeZone exchangeTimeZone;
                    if (!_symbolExchangeTimeZones.TryGetValue(symbol, out exchangeTimeZone))
                    {
                        exchangeTimeZone = MarketHoursDatabase.FromDataFolder().GetExchangeHours(Market.FXCM, symbol, symbol.SecurityType).TimeZone;
                        _symbolExchangeTimeZones.Add(symbol, exchangeTimeZone);
                    }
                }
            }
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SUBSCRIBE);
            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

            lock (_locker)
            {
                _gateway.sendMessage(request);
            }

            return(true);
        }
        /// <summary>
        /// Adds the specified symbols to the subscription
        /// </summary>
        /// <param name="job">Job we're subscribing for:</param>
        /// <param name="symbols">The symbols to be added keyed by SecurityType</param>
        public void Subscribe(LiveNodePacket job, IEnumerable<Symbol> symbols)
        {
            var symbolsToSubscribe = (from symbol in symbols 
                                      where !_subscribedSymbols.Contains(symbol) && CanSubscribe(symbol)
                                      select symbol).ToList();
            if (symbolsToSubscribe.Count == 0)
                return;

            Log.Trace("FxcmBrokerage.Subscribe(): {0}", string.Join(",", symbolsToSubscribe));

            var request = new MarketDataRequest();
            foreach (var symbol in symbolsToSubscribe)
            {
                TradingSecurity fxcmSecurity;
                if (_fxcmInstruments.TryGetValue(_symbolMapper.GetBrokerageSymbol(symbol), out fxcmSecurity))
                {
                    request.addRelatedSymbol(fxcmSecurity);
                }
            }
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SUBSCRIBE);
            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

            lock (_locker)
            {
                _gateway.sendMessage(request);
            }

            foreach (var symbol in symbolsToSubscribe)
            {
                _subscribedSymbols.Add(symbol);
            }
        }
        public MarketDataResponse GetData(MarketDataRequest request)
        {
            _client.QueryString.Clear();
            _client.QueryString.Add("typeid", request.TypeId);
            _client.QueryString.Add("sethours", request.Duration);

            if (request.SystemId != null)
            {
                _client.QueryString.Add("usesystem", request.SystemId);
            }

            if (request.MinimumQuantity != null)
            {
                _client.QueryString.Add("setminQ", request.MinimumQuantity);
            }

            string result = _client.DownloadString(_host);

            XDocument data = XDocument.Parse(result);

            var responseData = (from item in data.Root.Descendants("quicklook")
                                let sellOrders = item.Descendants("sell_orders").Descendants("order").OrderBy(arg => arg.Element("price").Value)
                                                 let buyOrders = item.Descendants("buy_orders").Descendants("order").OrderByDescending(arg => arg.Element("price").Value)
                                                                 select new MarketDataResponse
            {
                Commodity = item.Element("itemname").Value,
                BuyOrders = buyOrders.Select(order => MarketOrder.Load(order)).ToList(),
                SellOrders = sellOrders.Select(order => MarketOrder.Load(order)).ToList()
            }).FirstOrDefault();

            return(responseData);
        }
Example #15
0
        private static void ResolveMarketDataRequest(MarketDataRequest message, out Symbol symbol, out char bidAskObj, out string currencyCodeObj)
        {
            MDReqID mdReqId = new MDReqID();

            message.Get(mdReqId);

            SubscriptionRequestType subType = new SubscriptionRequestType();

            message.Get(subType);

            MarketDepth marketDepth = new MarketDepth();

            message.Get(marketDepth);

            var symbolGroup = new FixSpec.MarketDataRequest.NoRelatedSymGroup();

            message.GetGroup(1, symbolGroup);
            symbol = new Symbol();
            symbolGroup.Get(symbol);

            FixSpec.MarketDataRequest.NoMDEntryTypesGroup marketDataEntryGroup = new FixSpec.MarketDataRequest.NoMDEntryTypesGroup();
            message.GetGroup(1, marketDataEntryGroup);
            var mDEntryType = new MDEntryType();

            marketDataEntryGroup.Get(mDEntryType);

            bidAskObj       = mDEntryType.Obj;
            currencyCodeObj = symbol.Obj;
            var marketDepthObj = marketDepth.Obj;
        }
        private GetMinuteBarsRequest GetMinuteBarsRequest(MarketDataRequest request)
        {
            if (request == null || (!request?.IsValid() ?? true))
            {
                this._logger.LogError(
                    $"{nameof(BmllDataRequestsManager)} had a null request or a request that did not pass data request validation for {request?.Identifiers}");

                return(null);
            }

            if (string.IsNullOrWhiteSpace(request.Identifiers.Figi))
            {
                this._logger.LogError($"{nameof(BmllDataRequestsManager)} asked to process a security without a figi");

                return(null);
            }

            return(new GetMinuteBarsRequest
            {
                Figi = request.Identifiers.Figi,
                From = request.UniverseEventTimeFrom.Value.Date,
                To = request.UniverseEventTimeTo.Value.Date.AddDays(1).AddMilliseconds(-1),
                Interval = "1min"
            });
        }
        /// <summary>
        /// Adds the specified symbols to the subscription
        /// </summary>
        /// <param name="job">Job we're subscribing for:</param>
        /// <param name="symbols">The symbols to be added keyed by SecurityType</param>
        public void Subscribe(LiveNodePacket job, IEnumerable <Symbol> symbols)
        {
            var symbolsToSubscribe = (from symbol in symbols
                                      where !_subscribedSymbols.Contains(symbol)
                                      select symbol).ToList();

            if (symbolsToSubscribe.Count == 0)
            {
                return;
            }

            Log.Trace("FxcmBrokerage.Subscribe(): {0}", string.Join(",", symbolsToSubscribe));

            var request = new MarketDataRequest();

            foreach (var symbol in symbolsToSubscribe)
            {
                request.addRelatedSymbol(_fxcmInstruments[_symbolMapper.GetBrokerageSymbol(symbol)]);
            }
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SUBSCRIBE);
            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

            lock (_locker)
            {
                _gateway.sendMessage(request);
            }

            foreach (var symbol in symbolsToSubscribe)
            {
                _subscribedSymbols.Add(symbol);
            }
        }
        void OnMarketDataRequest(int reqId, IBContract contract)
        {
            Server?.OnMarketDataRequest(this, reqId, contract);

            MarketDataRequest?.Invoke(this, new TWSMarketDataRequestEventArgs(this, reqId, contract));

            _marketDataSubscriptions.Add(reqId, contract);
        }
        public IObservable <HistoricalDataMessage> Request(MarketDataRequest request)
        {
            Logger.LogDebug("Request: {0}", RequestId);
            var stream = Construct();

            IbClient.ClientSocket.ReqHistoricalData(RequestId, request);
            return(stream);
        }
Example #20
0
        /// <summary>
        /// The check daily volume breach.
        /// </summary>
        /// <param name="opposingPosition">
        /// The opposing position.
        /// </param>
        /// <param name="mostRecentTrade">
        /// The most recent trade.
        /// </param>
        /// <returns>
        /// The <see cref="RuleBreachDescription"/>.
        /// </returns>
        private RuleBreachDescription CheckDailyVolumeBreach(
            ITradePosition opposingPosition,
            Order mostRecentTrade)
        {
            var tradingHoursManager = this.tradingHoursService.GetTradingHoursForMic(mostRecentTrade.Market.MarketIdentifierCode);

            if (!tradingHoursManager.IsValid)
            {
                this.logger.LogInformation($"unable to fetch market data for ({mostRecentTrade.Market.MarketIdentifierCode}) for the most recent trade {mostRecentTrade?.Instrument?.Identifiers} the market data did not contain the security indicated as trading in that market");

                this.hadMissingData = true;
                return(RuleBreachDescription.False());
            }

            var marketRequest =
                new MarketDataRequest(
                    mostRecentTrade.Market.MarketIdentifierCode,
                    mostRecentTrade.Instrument.Cfi,
                    mostRecentTrade.Instrument.Identifiers,
                    tradingHoursManager.OpeningInUtcForDay(UniverseDateTime.Subtract(this.TradeBackwardWindowSize)),
                    tradingHoursManager.ClosingInUtcForDay(UniverseDateTime),
                    this.ruleContext?.Id(),
                    DataSource.AnyInterday);

            var marketResult = UniverseEquityInterdayCache.Get(marketRequest);

            if (marketResult.HadMissingData)
            {
                this.logger.LogInformation($"unable to fetch market data for ({mostRecentTrade.Market.MarketIdentifierCode}) for the most recent trade {mostRecentTrade?.Instrument?.Identifiers} the market data did not contain the security indicated as trading in that market");

                this.hadMissingData = true;
                return(RuleBreachDescription.False());
            }

            var marketSecurityData = marketResult.Response;

            if (marketSecurityData?.DailySummaryTimeBar?.DailyVolume.Traded <= 0 ||
                opposingPosition.TotalVolumeOrderedOrFilled() <= 0)
            {
                this.logger.LogInformation($"unable to evaluate for {mostRecentTrade?.Instrument?.Identifiers} either the market daily volume data was not available or the opposing position had a bad total volume value (daily volume){marketSecurityData?.DailySummaryTimeBar?.DailyVolume.Traded} - (opposing position){opposingPosition.TotalVolumeOrderedOrFilled()}");

                this.hadMissingData = true;
                return(RuleBreachDescription.False());
            }

            var percentageDailyVolume = (decimal)opposingPosition.TotalVolumeOrderedOrFilled() / (decimal)marketSecurityData?.DailySummaryTimeBar?.DailyVolume.Traded;

            if (percentageDailyVolume >= this.equitiesParameters.PercentageOfMarketDailyVolume)
            {
                return(new RuleBreachDescription
                {
                    RuleBreached = true,
                    Description = $" Percentage of market daily volume traded within a {this.equitiesParameters.Windows.BackwardWindowSize.TotalSeconds} second window exceeded the layering window threshold of {this.equitiesParameters.PercentageOfMarketDailyVolume * 100}%."
                });
            }

            return(RuleBreachDescription.False());
        }
Example #21
0
        public string EnviarAssinaturaMarketDataConflated(string instrumento, string securityType, string product, string cfiCode)
        {
            string mdReqID = "MDREQ";

            try
            {
                logger.InfoFormat("Requisitando snapshot MD Instrumento[{0}] Produto [{1}] Type [{2}] CFI[{3}]",
                                  instrumento,
                                  product,
                                  securityType,
                                  cfiCode);

                QuickFix.FIX44.MarketDataRequest mktDataReq = new MarketDataRequest();

                if (!String.IsNullOrEmpty(instrumento))
                {
                    QuickFix.FIX44.MarketDataRequest.NoRelatedSymGroup noRelatedSym = new MarketDataRequest.NoRelatedSymGroup();

                    noRelatedSym.SecurityExchange = new QuickFix.Fields.SecurityExchange("BVMF");
                    noRelatedSym.SecurityIDSource = new QuickFix.Fields.SecurityIDSource("8");
                    noRelatedSym.SecurityID       = new QuickFix.Fields.SecurityID(instrumento);

                    mktDataReq.AddGroup(noRelatedSym);

                    mdReqID += "-" + instrumento;
                }

                if (!String.IsNullOrEmpty(product) && !product.Equals("0"))
                {
                    mktDataReq.Product = new QuickFix.Fields.Product(Convert.ToInt32(product));
                    mdReqID           += "-" + product;
                }

                if (!String.IsNullOrEmpty(securityType))
                {
                    mktDataReq.SecurityType = new QuickFix.Fields.SecurityType(securityType);
                }

                if (!String.IsNullOrEmpty(cfiCode))
                {
                    mktDataReq.CFICode = new QuickFix.Fields.CFICode(cfiCode);
                }

                mdReqID += "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff");

                mktDataReq.MDReqID = new QuickFix.Fields.MDReqID(mdReqID);
                mktDataReq.SubscriptionRequestType = new QuickFix.Fields.SubscriptionRequestType(QuickFix.Fields.SubscriptionRequestType.SNAPSHOT_PLUS_UPDATES); // Subscribe + update
                mktDataReq.MDBookType = new QuickFix.Fields.MDBookType(QuickFix.Fields.MDBookType.ORDER_DEPTH);                                                  //MBO

                Session.SendToTarget(mktDataReq, _session);
            }
            catch (Exception ex)
            {
                logger.Error("RequestMarketData: " + ex.Message, ex);
            }

            return(mdReqID);
        }
        public async Task ShouldReceiveOrderBooks()
        {
            await _connector.ConnectAsync(CancellationToken.None);

            WaitForState(FixConnectorState.Connected, 30);
            var symbols = new[] { "USDCHF", "EURUSD" };
            var request = new MarketDataRequest()
            {
                MDReqID = new MDReqID(DateTime.UtcNow.Ticks.ToString()),
                SubscriptionRequestType = new SubscriptionRequestType(SubscriptionRequestType.SNAPSHOT_PLUS_UPDATES),
                MarketDepth             = new MarketDepth(50),
                MDUpdateType            = new MDUpdateType(MDUpdateType.FULL_REFRESH),
                NoMDEntryTypes          = new NoMDEntryTypes(2),
                NoRelatedSym            = new NoRelatedSym(symbols.Length)
            };
            var bidGroup = new MarketDataRequest.NoMDEntryTypesGroup
            {
                MDEntryType = new MDEntryType(MDEntryType.BID)
            };
            var askGroup = new MarketDataRequest.NoMDEntryTypesGroup
            {
                MDEntryType = new MDEntryType(MDEntryType.OFFER)
            };

            foreach (var symb in symbols)
            {
                var noRelatedSymGroup = new MarketDataRequest.NoRelatedSymGroup
                {
                    Symbol = new Symbol(symb)
                };
                request.AddGroup(noRelatedSymGroup);
            }

            request.AddGroup(bidGroup);
            request.AddGroup(askGroup);

            await _connector.SendRequestAsync(request, CancellationToken.None);

            for (int i = 0; i < symbols.Length; i++)
            {
                var resp = await _connector.GetResponseAsync(CancellationToken.None);

                if (resp is MarketDataSnapshotFullRefresh snapshot)
                {
                    var symbol = snapshot.Symbol.Obj;
                    for (int j = 1; j <= snapshot.NoMDEntries.Obj; j++)
                    {
                        var ob    = resp.GetGroup(j, new MarketDataSnapshotFullRefresh.NoMDEntriesGroup());
                        var dir   = ob.GetField(new MDEntryType()).Obj;
                        var price = ob.GetField(new MDEntryPx()).Obj;
                        var size  = ob.GetField(new MDEntrySize()).Obj;

                        await _output.WriteInfoAsync("", "", $"Sym:{symbol} side:{dir} price:{price} size:{size}");
                    }
                }
            }
        }
Example #23
0
        /// <summary>
        /// The check window volume traded.
        /// </summary>
        /// <param name="securities">
        /// The securities.
        /// </param>
        /// <returns>
        /// The <see cref="VolumeBreach"/>.
        /// </returns>
        private VolumeBreach CheckWindowVolumeTraded(Stack <Order> securities)
        {
            if (!securities.Any())
            {
                return(new VolumeBreach());
            }

            var tradingHours = this.tradingHoursService.GetTradingHoursForMic(securities.Peek().Market?.MarketIdentifierCode);

            if (!tradingHours.IsValid)
            {
                this.logger.LogError($"Request for trading hours was invalid. MIC - {securities.Peek().Market?.MarketIdentifierCode}");
                return(new VolumeBreach());
            }

            var tradingDates = this.tradingHoursService.GetTradingDaysWithinRangeAdjustedToTime(
                tradingHours.OpeningInUtcForDay(UniverseDateTime.Subtract(this.TradeBackwardWindowSize)),
                tradingHours.ClosingInUtcForDay(UniverseDateTime),
                securities.Peek().Market?.MarketIdentifierCode);

            var marketDataRequest =
                new MarketDataRequest(
                    securities.Peek().Market.MarketIdentifierCode,
                    securities.Peek().Instrument.Cfi,
                    securities.Peek().Instrument.Identifiers,
                    UniverseDateTime.Subtract(this.TradeBackwardWindowSize), // implicitly correct (market closure event trigger)
                    UniverseDateTime,
                    this.ruleContext?.Id(),
                    DataSource.AnyIntraday);

            // marking the close should not have windows exceeding a few hours
            var marketResult = UniverseEquityIntradayCache.GetMarketsForRange(marketDataRequest, tradingDates, RunMode);

            if (marketResult.HadMissingData)
            {
                this.hadMissingData = true;
                return(new VolumeBreach());
            }

            var securityUpdates       = marketResult.Response;
            var securityVolume        = securityUpdates.Sum(su => su.SpreadTimeBar.Volume.Traded);
            var thresholdVolumeTraded = securityVolume * this.equitiesParameters.PercentageThresholdWindowVolume;

            if (thresholdVolumeTraded == null)
            {
                this.hadMissingData = true;
                return(new VolumeBreach());
            }

            var result =
                this.CalculateVolumeBreaches(
                    securities,
                    thresholdVolumeTraded.GetValueOrDefault(0),
                    securityVolume);

            return(result);
        }
Example #24
0
        public void Query_Calls_Cache()
        {
            var cacheStrategy = new EquityInterDayMarketCacheStrategy(this._cache);
            var dataRequest   = MarketDataRequest.Null();

            cacheStrategy.Query(dataRequest);

            A.CallTo(() => this._cache.Get(dataRequest)).MustHaveHappenedOnceExactly();
        }
Example #25
0
 private FactsetSecurityRequestItem Project(MarketDataRequest req)
 {
     return(new FactsetSecurityRequestItem
     {
         Figi = req?.Identifiers.Figi,
         From = req?.UniverseEventTimeFrom?.AddDays(-1) ?? DateTime.UtcNow,
         To = req?.UniverseEventTimeTo?.AddDays(1) ?? DateTime.UtcNow
     });
 }
Example #26
0
        /// <summary>
        /// The window volume check.
        /// </summary>
        /// <param name="mostRecentTrade">
        /// The most recent trade.
        /// </param>
        /// <param name="tradedVolume">
        /// The traded volume.
        /// </param>
        /// <returns>
        /// The <see cref="BreachDetails"/>.
        /// </returns>
        private HighVolumeRuleBreach.BreachDetails WindowVolumeCheck(Order mostRecentTrade, decimal tradedVolume)
        {
            var tradingHours = this.TradingHoursService.GetTradingHoursForMic(mostRecentTrade.Market?.MarketIdentifierCode);

            if (!tradingHours.IsValid)
            {
                this.Logger.LogError($"Request for trading hours was invalid. MIC - {mostRecentTrade.Market?.MarketIdentifierCode}");
            }

            var tradingDates = this.TradingHoursService.GetTradingDaysWithinRangeAdjustedToTime(
                tradingHours.OpeningInUtcForDay(UniverseDateTime.Subtract(this.TradeBackwardWindowSize)),
                tradingHours.ClosingInUtcForDay(UniverseDateTime),
                mostRecentTrade.Market?.MarketIdentifierCode);

            var marketRequest =
                new MarketDataRequest(
                    mostRecentTrade.Market?.MarketIdentifierCode,
                    mostRecentTrade.Instrument.Cfi,
                    mostRecentTrade.Instrument.Identifiers,
                    tradingHours.OpeningInUtcForDay(UniverseDateTime.Subtract(this.TradeBackwardWindowSize)),
                    tradingHours.ClosingInUtcForDay(UniverseDateTime),
                    this.RuleCtx?.Id(),
                    DataSource.AnyIntraday);

            var marketResult = this.UniverseEquityIntradayCache.GetMarketsForRange(marketRequest, tradingDates, RunMode);

            if (marketResult.HadMissingData)
            {
                this.Logger.LogTrace($"Unable to fetch market data frames for {mostRecentTrade.Market.MarketIdentifierCode} at {UniverseDateTime}.");

                this.HadMissingData = true;
                return(HighVolumeRuleBreach.BreachDetails.None());
            }

            var securityDataTicks = marketResult.Response;
            var windowVolume      = securityDataTicks.Sum(sdt => sdt.SpreadTimeBar.Volume.Traded);
            var threshold         = (long)Math.Ceiling(this.EquitiesParameters.HighVolumePercentageWindow.GetValueOrDefault(0) * windowVolume);

            var breachPercentage =
                windowVolume != 0 && tradedVolume != 0
                    ? (decimal)tradedVolume / (decimal)windowVolume
                    : 0;

            if (threshold <= 0)
            {
                this.HadMissingData = true;
                this.Logger.LogInformation($"Window volume threshold of {threshold} was recorded.");
                return(HighVolumeRuleBreach.BreachDetails.None());
            }

            if (tradedVolume >= threshold)
            {
                return(new HighVolumeRuleBreach.BreachDetails(true, breachPercentage, threshold, mostRecentTrade.Market));
            }

            return(HighVolumeRuleBreach.BreachDetails.None());
        }
Example #27
0
        public static void Main2(string[] args)
        {
            BTCCFIXClientApp     app          = new BTCCFIXClientApp();
            string               sessionFile  = Environment.CurrentDirectory + "/session_client.txt";
            SessionSettings      settings     = new SessionSettings(sessionFile);
            IMessageStoreFactory storeFactory = new FileStoreFactory(settings);
            ILogFactory          logFactory   = new FileLogFactory(settings);

            QuickFix.Transport.SocketInitiator initiator = new QuickFix.Transport.SocketInitiator(app, storeFactory, settings, logFactory);
            initiator.Start();

            BTCCMarketDataRequest btccDataRequest = new BTCCMarketDataRequest();

            System.Threading.Thread.Sleep(5000);
            //request full snapshot
            MarketDataRequest dataRequest = btccDataRequest.marketDataFullSnapRequest("BTCCNY");
            bool ret = Session.SendToTarget(dataRequest, app.m_sessionID);

            Console.WriteLine("SendToTarget ret={0}", ret);

            dataRequest = btccDataRequest.marketDataFullSnapRequest("LTCCNY");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);

            dataRequest = btccDataRequest.marketDataFullSnapRequest("LTCBTC");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);

            System.Threading.Thread.Sleep(15000);
            //request incremental request
            dataRequest = btccDataRequest.marketDataIncrementalRequest("BTCCNY");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);

            dataRequest = btccDataRequest.marketDataIncrementalRequest("LTCCNY");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);

            dataRequest = btccDataRequest.marketDataIncrementalRequest("LTCBTC");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);

            System.Threading.Thread.Sleep(40000);
            //unsubscribe incremental request
            dataRequest = btccDataRequest.unsubscribeIncrementalRequest("BTCCNY");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);

            dataRequest = btccDataRequest.unsubscribeIncrementalRequest("LTCCNY");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);

            dataRequest = btccDataRequest.unsubscribeIncrementalRequest("LTCBTC");
            ret         = Session.SendToTarget(dataRequest, app.m_sessionID);
            Console.WriteLine("SendToTarget ret={0}", ret);
        }
Example #28
0
        public void Query_Returns_Interday_Market_Data_Response()
        {
            var cacheStrategy = new EquityInterDayMarketCacheStrategy(this._cache);
            var dataRequest   = MarketDataRequest.Null();

            var response = cacheStrategy.Query(dataRequest);

            Assert.IsNotNull(response);
            Assert.IsInstanceOf <EquityInterDayMarketDataResponse>(response);
        }
Example #29
0
    private IEnumerator MarketDataRequest(Request r)
    {
        yield return(new WaitForSeconds(_loadTime));

        MarketDataRequest m = r as MarketDataRequest;

        m.Datas = _marketManager.GetBuyOrders(m.ResourceType);

        FinishRequest(m);
    }
Example #30
0
        /// <summary>
        /// The daily volume check.
        /// </summary>
        /// <param name="mostRecentTrade">
        /// The most recent trade.
        /// </param>
        /// <param name="tradedVolume">
        /// The traded volume.
        /// </param>
        /// <returns>
        /// The <see cref="BreachDetails"/>.
        /// </returns>
        private HighVolumeRuleBreach.BreachDetails DailyVolumeCheck(Order mostRecentTrade, decimal tradedVolume)
        {
            if (mostRecentTrade == null)
            {
                return(HighVolumeRuleBreach.BreachDetails.None());
            }

            var tradingHours = this.TradingHoursService.GetTradingHoursForMic(mostRecentTrade.Market?.MarketIdentifierCode);

            if (!tradingHours.IsValid)
            {
                this.Logger.LogError($"Request for trading hours was invalid. MIC - {mostRecentTrade.Market?.MarketIdentifierCode}");
            }

            var marketDataRequest = new MarketDataRequest(
                mostRecentTrade.Market?.MarketIdentifierCode,
                mostRecentTrade.Instrument.Cfi,
                mostRecentTrade.Instrument.Identifiers,
                tradingHours.OpeningInUtcForDay(UniverseDateTime.Subtract(this.TradeBackwardWindowSize)),
                tradingHours.ClosingInUtcForDay(UniverseDateTime),
                this.RuleCtx?.Id(),
                DataSource.AnyInterday);

            var securityResult = UniverseEquityInterdayCache.Get(marketDataRequest);

            if (securityResult.HadMissingData)
            {
                this.HadMissingData = true;
                this.Logger.LogWarning($"Missing data for {marketDataRequest}.");
                return(HighVolumeRuleBreach.BreachDetails.None());
            }

            var security  = securityResult.Response;
            var threshold = (long)Math.Ceiling(
                this.EquitiesParameters.HighVolumePercentageDaily.GetValueOrDefault(0) * security.DailySummaryTimeBar.DailyVolume.Traded);

            if (threshold <= 0)
            {
                this.HadMissingData = true;
                this.Logger.LogInformation($"Daily volume threshold of {threshold} was recorded.");
                return(HighVolumeRuleBreach.BreachDetails.None());
            }

            var breachPercentage =
                security.DailySummaryTimeBar.DailyVolume.Traded != 0 && tradedVolume != 0
                    ? (decimal)tradedVolume / (decimal)security.DailySummaryTimeBar.DailyVolume.Traded
                    : 0;

            if (tradedVolume >= threshold)
            {
                return(new HighVolumeRuleBreach.BreachDetails(true, breachPercentage, threshold, mostRecentTrade.Market));
            }

            return(HighVolumeRuleBreach.BreachDetails.None());
        }
        public MarketDataResponse <EquityInstrumentInterDayTimeBar> Get(MarketDataRequest request)
        {
            if (request == null || !request.IsValid())
            {
                this._logger.LogError("UniverseEquityInterDayCache received either a null or invalid request");
                return(MarketDataResponse <EquityInstrumentInterDayTimeBar> .MissingData());
            }

            this._logger.LogInformation(
                $"UniverseEquityInterDayCache fetching for market {request?.MarketIdentifierCode} from {request?.UniverseEventTimeFrom} to {request?.UniverseEventTimeTo} as part of rule run {request?.SystemProcessOperationRuleRunId}");

            if (!this._latestExchangeFrameBook.ContainsKey(request.MarketIdentifierCode))
            {
                this._dataRequestRepository.CreateDataRequest(request);
                this._logger.LogInformation(
                    $"UniverseEquityInterDayCache was not able to find the MIC {request.MarketIdentifierCode} in the latest exchange frame book. Recording missing data.");
                return(MarketDataResponse <EquityInstrumentInterDayTimeBar> .MissingData());
            }

            this._latestExchangeFrameBook.TryGetValue(request.MarketIdentifierCode, out var exchangeFrame);

            if (exchangeFrame == null)
            {
                this._dataRequestRepository.CreateDataRequest(request);
                this._logger.LogInformation(
                    $"UniverseEquityInterDayCache was not able to find the MIC {request.MarketIdentifierCode} in the latest exchange frame book. Recording missing data.");
                return(MarketDataResponse <EquityInstrumentInterDayTimeBar> .MissingData());
            }

            var security =
                exchangeFrame.Securities.FirstOrDefault(sec => Equals(sec.Security.Identifiers, request.Identifiers));

            if (security == null)
            {
                this._dataRequestRepository.CreateDataRequest(request);
                this._logger.LogInformation(
                    $"UniverseEquityInterDayCache was not able to find the security {request.Identifiers} for MIC {request.MarketIdentifierCode} in the latest exchange frame book. Recording missing data.");
                return(MarketDataResponse <EquityInstrumentInterDayTimeBar> .MissingData());
            }

            if (exchangeFrame.Epoch.Date >= request.UniverseEventTimeTo.GetValueOrDefault() &&
                exchangeFrame.Epoch.Date <= request.UniverseEventTimeFrom.GetValueOrDefault())
            {
                this._dataRequestRepository.CreateDataRequest(request);

                this._logger.LogInformation(
                    $"UniverseEquityInterDayCache was not able to find the security {request.Identifiers} for MIC {request.MarketIdentifierCode} in the latest exchange frame book within a suitable data range to {request.UniverseEventTimeTo} from {request.UniverseEventTimeFrom}. Recording missing data.");

                return(MarketDataResponse <EquityInstrumentInterDayTimeBar> .MissingData());
            }

            this._logger.LogInformation(
                $"UniverseEquityInterDayCache was able to find a match for {request.Identifiers} returning data.");
            return(new MarketDataResponse <EquityInstrumentInterDayTimeBar>(security, false, false));
        }
        public async Task DataRequestsForSystemOperation_UpdateToCompleteWithDuplicates_UpdatesRelevantRequests()
        {
            var repo = new RuleRunDataRequestRepository(new ConnectionStringFactory(this._configuration), this._logger);

            var id = "11";

            var start = DateTime.UtcNow;
            var end   = DateTime.UtcNow.AddHours(1);

            var marketDataRequest1 = new MarketDataRequest(
                null,
                "TESTME",
                "entsbp",
                new InstrumentIdentifiers {
                Id = "1"
            },
                start,
                end,
                "1",
                false,
                DataSource.Bmll);

            await repo.CreateDataRequest(marketDataRequest1);

            var marketDataRequest2 = new MarketDataRequest(
                null,
                "TESTME",
                "entsbp",
                new InstrumentIdentifiers {
                Id = "1"
            },
                start,
                end,
                "2",
                false,
                DataSource.Bmll);

            await repo.CreateDataRequest(marketDataRequest2);

            var dataRequests = (await repo.DataRequestsForSystemOperation(id))
                               .Where(_ => _.MarketIdentifierCode == "TESTME").ToList();

            Assert.IsNotNull(dataRequests);
            Assert.AreEqual(dataRequests.Count, 2);
            Assert.IsFalse(dataRequests.All(_ => _.IsCompleted));

            var oneDataRequest = new[] { dataRequests.FirstOrDefault() };
            await repo.UpdateToCompleteWithDuplicates(oneDataRequest);

            var dataRequests2 = (await repo.DataRequestsForSystemOperation(id))
                                .Where(_ => _.MarketIdentifierCode == "TESTME").ToList();

            Assert.IsTrue(dataRequests2.All(_ => _.IsCompleted));
        }
        /// <summary>
        /// Gets the quotes for the symbol
        /// </summary>
        public List<MarketDataSnapshot> GetQuotes(List<string> fxcmSymbols)
        {
            // get current quotes for the instrument
            var request = new MarketDataRequest();
            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SNAPSHOT);
            foreach (var fxcmSymbol in fxcmSymbols)
            {
                request.addRelatedSymbol(_fxcmInstruments[fxcmSymbol]);
            }

            AutoResetEvent autoResetEvent;
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(request);
                autoResetEvent = new AutoResetEvent(false);
                _mapRequestsToAutoResetEvents[_currentRequest] = autoResetEvent;
            }
            if (!autoResetEvent.WaitOne(ResponseTimeout))
                throw new TimeoutException(string.Format("FxcmBrokerage.GetQuotes(): Operation took longer than {0} seconds.", (decimal)ResponseTimeout / 1000));

            return _rates.Where(x => fxcmSymbols.Contains(x.Key)).Select(x => x.Value).ToList();
        }
        /// <summary>
        /// Gets the history for the requested securities
        /// </summary>
        /// <param name="requests">The historical data requests</param>
        /// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param>
        /// <returns>An enumerable of the slices of data covering the span specified in each request</returns>
        public IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
        {
            foreach (var request in requests)
            {
                var interval = ToFxcmInterval(request.Resolution);

                // download data
                var history = new List<BaseData>();

                var end = request.EndTimeUtc;

                var attempt = 1;
                while (end > request.StartTimeUtc)
                {
                    _lastHistoryChunk.Clear();

                    var mdr = new MarketDataRequest();
                    mdr.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SNAPSHOT);
                    mdr.setResponseFormat(IFixMsgTypeDefs.__Fields.MSGTYPE_FXCMRESPONSE);
                    mdr.setFXCMTimingInterval(interval);
                    mdr.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

                    mdr.setFXCMStartDate(new UTCDate(ToJavaDateUtc(request.StartTimeUtc)));
                    mdr.setFXCMStartTime(new UTCTimeOnly(ToJavaDateUtc(request.StartTimeUtc)));
                    mdr.setFXCMEndDate(new UTCDate(ToJavaDateUtc(end)));
                    mdr.setFXCMEndTime(new UTCTimeOnly(ToJavaDateUtc(end)));
                    mdr.addRelatedSymbol(_fxcmInstruments[_symbolMapper.GetBrokerageSymbol(request.Symbol)]);

                    AutoResetEvent autoResetEvent;
                    lock (_locker)
                    {
                        _currentRequest = _gateway.sendMessage(mdr);
                        autoResetEvent = new AutoResetEvent(false);
                        _mapRequestsToAutoResetEvents[_currentRequest] = autoResetEvent;
                        _pendingHistoryRequests.Add(_currentRequest);
                    }
                    if (!autoResetEvent.WaitOne(HistoryResponseTimeout))
                    {
                        // no response
                        if (++attempt > MaximumHistoryRetryAttempts)
                        {
                            break;
                        }
                        continue;
                    }

                    // Add data
                    lock (_locker)
                    {
                        history.InsertRange(0, _lastHistoryChunk);
                    }

                    var firstDateUtc = _lastHistoryChunk[0].Time.ConvertToUtc(_configTimeZone);
                    if (end != firstDateUtc)
                    {
                        // new end date = first datapoint date.
                        end = request.Resolution == Resolution.Tick ? firstDateUtc.AddMilliseconds(-1) : firstDateUtc.AddSeconds(-1);

                        if (request.StartTimeUtc.AddSeconds(1) >= end)
                            break;
                    }
                    else
                    {
                        break;
                    }
                }

                DataPointCount += history.Count;

                foreach (var data in history)
                {
                    yield return new Slice(data.EndTime, new[] { data });
                }
            }
        }
        public void Subscribe(string symbol, SessionID sessionId)
        {
            var marketDataRequest = new MarketDataRequest
                                        {
                                            MDReqID = new MDReqID(symbol),
                                            SubscriptionRequestType = new SubscriptionRequestType('1'),
                                            //incremental refresh
                                            MarketDepth = new MarketDepth(1), //yes market depth need
                                            MDUpdateType = new MDUpdateType(1) //
                                        };

            var relatedSymbol = new MarketDataRequest.NoRelatedSymGroup { Symbol = new Symbol(symbol) };

            marketDataRequest.AddGroup(relatedSymbol);

            var noMdEntryTypes = new MarketDataRequest.NoMDEntryTypesGroup();

            var mdEntryTypeBid = new MDEntryType('0');

            noMdEntryTypes.MDEntryType = mdEntryTypeBid;

            marketDataRequest.AddGroup(noMdEntryTypes);

            noMdEntryTypes = new MarketDataRequest.NoMDEntryTypesGroup();

            var mdEntryTypeOffer = new MDEntryType('1');

            noMdEntryTypes.MDEntryType = mdEntryTypeOffer;

            marketDataRequest.AddGroup(noMdEntryTypes);

            //Send message
            Session.SendToTarget(marketDataRequest, sessionId);
        }
Example #36
0
        public static void HistTradeRequest(string traderName, string companyName, string brokerName,
                                            string instrumentName, string commodityName, string firstSeqName, 
                                            DateTime fromDate, DateTime toDate)
        {
            int nrFilter = 0;
            MarketDataRequest request;
            request = new MarketDataRequest();

            // @@@ filter inputs for reasonable values, i.e. warning for many days etc...

            request.DataType = MarketDataTypeEnum.Deals;
            request.FilterRules = new FilterRule[
                                        (traderName == "All"? 0 : 1) +
                                        (companyName == "All"? 0 : 1) +
                                        (brokerName == "All"? 1 : 1) +
                                        (instrumentName == "All" && commodityName == "All"? 0 : 1) +
                                        (firstSeqName == "All" ? 0 : 1) +
                                        + 1]; // +1 Dates// @@@ how to filter for gas as commodity

            // Date Filter
            DateFilterRule dateFilter = new DateFilterRule();
            dateFilter.StartDate = fromDate;
            dateFilter.EndDate = toDate;
            request.FilterRules[nrFilter] = dateFilter; nrFilter+=1;

            // Trader Filter
            if(traderName != "All")
            {
                TraderNameFilterRule traderFilter = new TraderNameFilterRule();
                traderFilter.Action = FilterRuleActionEnum.Add;
                traderFilter.TraderName = traderName;
                request.FilterRules[nrFilter] = traderFilter; nrFilter += 1;
            }

            // Instrument Filter == Product, e.g. NBP, not tenor!
            // We introduced Commodity which is a collection of intruments; setting an Instrument Type will supersede the Commodity
            if (instrumentName != "All")
            {
                InstrumentsFilterRule instrumentsFilter = new InstrumentsFilterRule();
                InstrumentFilterRule[] iGroup = new InstrumentFilterRule[1]; // @@@ extend >1 later

                InstrumentFilterRule iRule = new InstrumentFilterRule();
                iRule.Action = FilterRuleActionEnum.Add;
                iRule.InstrumentName = instrumentName;
                iGroup[0] = iRule;

                instrumentsFilter.InstrumentRules = iGroup;
                request.FilterRules[nrFilter] = instrumentsFilter; nrFilter += 1;
            }
            else if (commodityName != "All")
            {
                List<string> _instNames;
                if (CommodityGrouper.CommodityClassList.Contains(commodityName))
                {
                    _instNames = TP_SetupData.Commodities.Values.Where(k => k.CommodityClass == commodityName).Select(k => k.InstName).ToList();
                }
                else if (CommodityGrouper.CommodityList.Contains(commodityName))
                {
                    _instNames = TP_SetupData.Commodities.Values.Where(k => k.Commodity == commodityName).Select(k => k.InstName).ToList();
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Commodity = " + commodityName + " unknown.");
                }

                InstrumentsFilterRule instrumentsFilter = new InstrumentsFilterRule();
                InstrumentFilterRule[] iGroup = new InstrumentFilterRule[_instNames.Count];

                InstrumentFilterRule iRule;
                for (int i = 0; i < _instNames.Count; i++)
                {
                    iRule = new InstrumentFilterRule();
                    iRule.Action = FilterRuleActionEnum.Add;
                    iRule.InstrumentName = _instNames[i];
                    iGroup[i] = iRule;
                }

                instrumentsFilter.InstrumentRules = iGroup;
                request.FilterRules[nrFilter] = instrumentsFilter; nrFilter += 1;
            }

            // FirstSqeuenceName Filter, e.g. Sep-16, Win 16-17, ...
            if (firstSeqName != "All")
            {
                SequenceFilterRule iRule = new SequenceFilterRule(); // multiple not possible
                iRule.Action = FilterRuleActionEnum.Add;
                iRule.SequenceItemName = firstSeqName;

                request.FilterRules[nrFilter] = iRule; nrFilter += 1;
            }

            // Company Filter
            if (companyName != "All")
            {
                CompaniesFilterRule companiesFilter = new CompaniesFilterRule();
                CompanyFilterRule[] cGroup = new CompanyFilterRule[1]; // @@@ extend >1 later (possible?)

                CompanyFilterRule cRule = new CompanyFilterRule();
                cRule.Action = FilterRuleActionEnum.Add;
                cRule.CompanyName = companyName;
                cGroup[0] = cRule;

                companiesFilter.CompanyRules = cGroup;
                request.FilterRules[nrFilter] = companiesFilter; nrFilter += 1;
            }

            // Broker Filter
            BrokersFilterRule brokersFilter= new BrokersFilterRule();
            BrokerFilterRule[] bGroup;
            BrokerFilterRule bRule;

            if (brokerName == "All")
            {
                //char[] firstLetters = "SPI".ToCharArray();
                //bGroup = new BrokerFilterRule[firstLetters.Length];
                //for (int i = 0; i < firstLetters.Length; i++)
                //{
                //    bRule = new BrokerFilterRule();
                //    bRule.Action = FilterRuleActionEnum.Add;
                //    bRule.CombineBrokers = true;
                //    bRule.BrokerName = firstLetters[i].ToString();
                //    bGroup[i] = bRule;
                //}

                bGroup = new BrokerFilterRule[1];
                bRule = new BrokerFilterRule();
                bRule.Action = FilterRuleActionEnum.Add;
                bRule.CombineBrokers = true;
                bRule.BrokerName = "";
                bGroup[0] = bRule;
            }
            else
            {
                bGroup = new BrokerFilterRule[1];
                bRule = new BrokerFilterRule();
                bRule.Action = FilterRuleActionEnum.Add;
                bRule.CombineBrokers = true;
                bRule.BrokerName = brokerName;
                bGroup[0] = bRule;
            }

            brokersFilter.BrokerRules = bGroup;
            request.FilterRules[nrFilter] = brokersFilter; nrFilter += 1;

            DealsServiceSoapClient api = new DealsServiceSoapClient();
            MarketDataResponse response = api.GetDeals(request);
            Debugger.AddDebugLine("Query executed.");

            // convert data into our structure
            Deal[] deals =  response.Deals.Deals;

            GV8APIDATA data = new GV8APIDATA();
            data.TRADE = new TradeData[deals.Length];
            for(int i = 0; i < deals.Length; i++)
            {
                TradeData tpDeal = new TradeData();
                TrayportDeal srcDeal = (TrayportDeal)deals[i];

                tpDeal.TradeID = srcDeal.DealId;
                tpDeal.DateTime = srcDeal.DealDate; tpDeal._DateTime = tpDeal.DateTime.ToString("s", System.Globalization.CultureInfo.InvariantCulture);
                tpDeal.Price = srcDeal.Price;
                tpDeal.Volume = srcDeal.Quantity;
                tpDeal.INSTSPECIFIER = new InstspecifierData();
                tpDeal.INSTSPECIFIER.InstID = srcDeal.InstrumentId;
                tpDeal.INSTSPECIFIER.InstName = srcDeal.InstrumentName;
                tpDeal.INSTSPECIFIER.FirstSequenceID = srcDeal.FirstSequenceId;
                tpDeal.INSTSPECIFIER.FirstSequenceItemName = srcDeal.FirstSequenceItemName;
                tpDeal.INSTSPECIFIER.SecondSequenceItemName = srcDeal.SecondSequenceItemName;
                tpDeal.InitiatorCompany = srcDeal.InitiatorCompany; //@@@ name or ID? check
                tpDeal.InitiatorBroker = srcDeal.InitiatorBroker; // @@@
                tpDeal.InitiatorTrader= srcDeal.InitiatorTrader; // @@@
                tpDeal.InitiatorAction = srcDeal.InitiatorAction; // @@@
                tpDeal.AggressorCompany = srcDeal.AggressorCompany; //@@@
                tpDeal.AggressorBroker = srcDeal.AggressorBroker; // @@@
                tpDeal.AggressorTrader = srcDeal.AggressorTrader; // @@@
                tpDeal.AggressorAction = srcDeal.AggressorAction; // @@@
                tpDeal.LastUpdate = srcDeal.LastUpdate; tpDeal._LastUpdate = tpDeal.LastUpdate.ToString("s", System.Globalization.CultureInfo.InvariantCulture);
                tpDeal.InitiatorUser = srcDeal.InitiatorUser; //@@@
                tpDeal.AggressorUser = srcDeal.AggressorUser; //@@@

                tpDeal.ManualDeal = srcDeal.ManualDeal.HasValue? srcDeal.ManualDeal.Value : false;
                tpDeal._ManualDeal = srcDeal.ManualDeal.HasValue ? srcDeal.ManualDeal.Value.ToString() : null;
                tpDeal.VoiceDeal = srcDeal.VoiceDeal.HasValue ? srcDeal.VoiceDeal.Value : false;
                tpDeal._VoiceDeal = srcDeal.VoiceDeal.HasValue ? srcDeal.VoiceDeal.Value.ToString() : null;
                tpDeal.InitSleeve = srcDeal.InitSleeve.HasValue ? srcDeal.InitSleeve.Value : false;
                tpDeal._InitSleeve = srcDeal.InitSleeve.HasValue ? srcDeal.InitSleeve.Value.ToString() : null;
                tpDeal.AggSleeve = srcDeal.AggSleeve.HasValue ? srcDeal.AggSleeve.Value : false;
                tpDeal._AggSleeve = srcDeal.AggSleeve.HasValue ? srcDeal.AggSleeve.Value.ToString() : null;
                tpDeal.PNC = srcDeal.PNC.HasValue ? srcDeal.PNC.Value : false;
                tpDeal._PNC = srcDeal.PNC.HasValue ? srcDeal.PNC.Value.ToString() : null;

                /* 3 more  I cannot find in the normal TP responses for current trades:
                * tpDeal.INSTSPECIFIER.e = srcDeal.ExchangeDealId.HasValue ? srcDeal.ExchangeDealId.Value : 0;
                * private string sCoTAOriginField;
                * private string sCoTADeliveryPointField;
                */
                data.TRADE[i] = tpDeal;
            }

            Adaptor.HistTrades = data;
            string name = "HistTrades";
            Trayport_API.XmlHandler.Save2Data(XmlHandler.SerializeToXml(request).OuterXml, name + ".query");

            // @@@skip writing? excessive ....
            //Trayport_API.XmlHandler.Save2Data(XmlHandler.SerializeToXml(data).OuterXml, name + ".data");
        }
        /// <summary>
        /// Removes the specified symbols to the subscription
        /// </summary>
        /// <param name="job">Job we're processing.</param>
        /// <param name="symbols">The symbols to be removed keyed by SecurityType</param>
        public void Unsubscribe(LiveNodePacket job, IEnumerable<Symbol> symbols)
        {
            var symbolsToUnsubscribe = (from symbol in symbols 
                                        where _subscribedSymbols.Contains(symbol) 
                                        select symbol).ToList();
            if (symbolsToUnsubscribe.Count == 0)
                return;

            Log.Trace("FxcmBrokerage.Unsubscribe(): {0}", string.Join(",", symbolsToUnsubscribe));

            var request = new MarketDataRequest();
            foreach (var symbol in symbolsToUnsubscribe)
            {
                request.addRelatedSymbol(_fxcmInstruments[_symbolMapper.GetBrokerageSymbol(symbol)]);
            }
            request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.UNSUBSCRIBE);
            request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

            lock (_locker)
            {
                _gateway.sendMessage(request);
            }

            foreach (var symbol in symbolsToUnsubscribe)
            {
                _subscribedSymbols.Remove(symbol);
            }
        }
Example #38
0
        public void Run()
        {
            _gateway = GatewayFactory.createGateway();

            // register the generic message listener with the gateway
            _gateway.registerGenericMessageListener(this);

            // register the status message listener with the gateway
            _gateway.registerStatusMessageListener(this);

            // attempt to login with the local login properties
            Console.WriteLine("Logging in...");
            var loginProperties = new FXCMLoginProperties(UserName, Password, Terminal, Server);
            // disable the streaming rates (default automatic subscriptions)
            loginProperties.addProperty(IConnectionManager.__Fields.MSG_FLAGS, IFixDefs.__Fields.CHANNEL_MARKET_DATA.ToString());
            _gateway.login(loginProperties);
            Console.WriteLine("Logged in.\n");

            // get instrument list
            Console.WriteLine("Requesting trading session status...");
            lock (_locker)
            {
                _currentRequest = _gateway.requestTradingSessionStatus();
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestTradingSessionStatusEvent.WaitOne();
            Console.WriteLine("Instruments: {0}\n", _securities.Count);
            foreach (var pair in _securities)
            {
                var security = pair.Value;

                Console.WriteLine(security.getSymbol() + " " +
                                  security.getCurrency() + " " +
                                  security.getFXCMSubscriptionStatus() + " " +
                                  security.getRoundLot() + " " +
                                  security.getContractMultiplier() + " " +
                                  security.getFXCMMinQuantity() + " " +
                                  security.getFXCMMaxQuantity() + " " +
                                  security.getFXCMSymPointSize() + " " +
                                  security.getFactor() + " " +
                                  security.getFXCMSymPrecision() + " " +
                                  security.getProduct() + " " +
                                  security.getFXCMProductID() + " " +
                                  (security.getFXCMProductID() == IFixValueDefs.__Fields.FXCMPRODUCTID_FOREX ? "Forex" : "CFD"));
            }
            Console.WriteLine();

            // get account list
            Console.WriteLine("Requesting account list...");
            lock (_locker)
            {
                _currentRequest = _gateway.requestAccounts();
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestAccountListEvent.WaitOne();
            Console.WriteLine("Accounts: {0}\n", _accounts.Count);

            // use first account
            var accountId = _accounts.Keys.First();

            // we are unable to continue if Hedging enabled
            if (_accounts[accountId].getParties().getFXCMPositionMaintenance() != "N")
                throw new NotSupportedException("The Lean engine does not support accounts with Hedging enabled. Please contact FXCM support to disable Hedging and try again.");

            // get open order list
            Console.WriteLine("Requesting open order list...");
            lock (_locker)
            {
                _currentRequest = _gateway.requestOpenOrders();
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Open orders: {0}\n", _orders.Keys.Count);

            // cancel all open orders
            if (_orders.Keys.Count > 0)
            {
                Console.WriteLine("Cancelling open orders...");
                foreach (var order in _orders.Values.ToList())
                {
                    Console.WriteLine("Cancelling order {0}...", order.getOrderID());
                    var request = MessageGenerator.generateOrderCancelRequest("", order.getOrderID(), order.getSide(), order.getAccount());
                    lock (_locker)
                    {
                        _currentRequest = _gateway.sendMessage(request);
                        Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
                    }
                    _requestOrderEvent.WaitOne();
                    Console.WriteLine("Order {0} cancelled.", order.getOrderID());
                }
                _orders.Clear();
                Console.WriteLine("Open orders cancelled.\n");
            }

            // get open position list
            Console.WriteLine("Requesting open position list...");
            lock (_locker)
            {
                _currentRequest = _gateway.requestOpenPositions();
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestPositionListEvent.WaitOne();
            Console.WriteLine("Open position list complete.\n");

            // get closed position list
            Console.WriteLine("Requesting closed position list...");
            lock (_currentRequest)
            {
                _currentRequest = _gateway.requestClosedPositions();
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestPositionListEvent.WaitOne();
            Console.WriteLine("Closed position list complete.\n");

            // get current quotes for the instrument
            if (_securities.ContainsKey(Symbol))
            {
                Console.WriteLine("Requesting market data for " + Symbol + "...");
                var request = new MarketDataRequest();
                request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);
                request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SNAPSHOT);
                request.addRelatedSymbol(_securities[Symbol]);
                lock (_locker)
                {
                    _currentRequest = _gateway.sendMessage(request);
                    Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
                }
                _requestMarketDataEvent.WaitOne();
                Console.WriteLine(Symbol + " - Bid: {0} Ask: {1}\n", _rates[Symbol].getBidClose(), _rates[Symbol].getAskClose());
            }

            // submit limit order
            Console.WriteLine("Submitting limit order...");
            var limitOrderRequest = MessageGenerator.generateOpenOrder(0.7, accountId, 10000, SideFactory.BUY, Symbol, "");
            limitOrderRequest.setOrdType(OrdTypeFactory.LIMIT);
            limitOrderRequest.setTimeInForce(TimeInForceFactory.GOOD_TILL_CANCEL);
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(limitOrderRequest);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Limit order submitted.\n");

            var limitOrder = _orderRequests[_currentRequest];

            // update limit order
            Console.WriteLine("Updating limit order...");
            var orderReplaceRequest = MessageGenerator.generateOrderReplaceRequest("", limitOrder.getOrderID(), limitOrder.getSide(), limitOrder.getOrdType(), 0.8, limitOrder.getAccount());
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(orderReplaceRequest);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Limit order updated.\n");

            // cancel limit order
            Console.WriteLine("Cancelling limit order...");
            var orderCancelRequest = MessageGenerator.generateOrderCancelRequest("", limitOrder.getOrderID(), limitOrder.getSide(), limitOrder.getAccount());
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(orderCancelRequest);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Limit order cancelled.\n");

            // submit stop market order
            Console.WriteLine("Submitting stop market order...");
            var stopMarketOrderRequest = MessageGenerator.generateOpenOrder(0.7, accountId, 10000, SideFactory.SELL, Symbol, "");
            stopMarketOrderRequest.setOrdType(OrdTypeFactory.STOP);
            stopMarketOrderRequest.setTimeInForce(TimeInForceFactory.GOOD_TILL_CANCEL);
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(stopMarketOrderRequest);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Stop market order submitted.\n");

            var stopMarketOrder = _orderRequests[_currentRequest];

            // cancel stop market order
            Console.WriteLine("Cancelling stop market order...");
            orderCancelRequest = MessageGenerator.generateOrderCancelRequest("", stopMarketOrder.getOrderID(), stopMarketOrder.getSide(), stopMarketOrder.getAccount());
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(orderCancelRequest);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Stop market order cancelled.\n");

            // submit stop limit order
            Console.WriteLine("Submitting stop limit order...");
            var stopLimitOrderRequest = MessageGenerator.generateOpenOrder(0.7, accountId, 10000, SideFactory.SELL, Symbol, "");
            stopLimitOrderRequest.setOrdType(OrdTypeFactory.STOP_LIMIT);
            stopLimitOrderRequest.setStopPx(0.695);
            stopLimitOrderRequest.setTimeInForce(TimeInForceFactory.GOOD_TILL_CANCEL);
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(stopLimitOrderRequest);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Stop limit order submitted.\n");

            var stopLimitOrder = _orderRequests[_currentRequest];

            // cancel stop limit order
            Console.WriteLine("Cancelling stop limit order...");
            orderCancelRequest = MessageGenerator.generateOrderCancelRequest("", stopLimitOrder.getOrderID(), stopLimitOrder.getSide(), stopLimitOrder.getAccount());
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(orderCancelRequest);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Stop limit order cancelled.\n");

            // submit market order (buy)
            Console.WriteLine("Submitting buy market order...");
            var buyOrder = MessageGenerator.generateMarketOrder(accountId, 10000, SideFactory.BUY, Symbol, "");
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(buyOrder);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Buy market order submitted.\n");

            // submit market order (sell)
            Console.WriteLine("Submitting sell market order...");
            var sellOrder = MessageGenerator.generateMarketOrder(accountId, 10000, SideFactory.SELL, Symbol, "");
            lock (_locker)
            {
                _currentRequest = _gateway.sendMessage(sellOrder);
                Console.WriteLine("\tRequestId = {0}\n", _currentRequest);
            }
            _requestOrderEvent.WaitOne();
            Console.WriteLine("Sell market order submitted.\n");

            Console.WriteLine();
            Console.WriteLine("Press Return to Logout and exit.\n");
            Console.ReadKey();

            // log out
            Console.WriteLine("Logging out...");
            _gateway.logout();
            Console.WriteLine("Logged out.");

            // remove the generic message listener, stop listening to updates
            _gateway.removeGenericMessageListener(this);

            // remove the status message listener, stop listening to status changes
            _gateway.removeStatusMessageListener(this);
        }
Example #39
0
        public MarketDataRequest unsubscribeIncrementalRequest(string symbol)
        {
            MarketDataRequest tickerRequest = new MarketDataRequest();

            MarketDataRequest.NoRelatedSymGroup symbolGroup = new MarketDataRequest.NoRelatedSymGroup();
            symbolGroup.SetField(new Symbol(symbol));
            tickerRequest.AddGroup(symbolGroup);

            tickerRequest.Set(new MDReqID("123"));
            tickerRequest.Set(new SubscriptionRequestType('2'));
            tickerRequest.Set(new MarketDepth(0));

            addMDType(tickerRequest, '0');
            addMDType(tickerRequest, '1');
            addMDType(tickerRequest, '2');
            addMDType(tickerRequest, '3');
            addMDType(tickerRequest, '4');
            addMDType(tickerRequest, '5');
            addMDType(tickerRequest, '6');
            addMDType(tickerRequest, '7');
            addMDType(tickerRequest, '8');
            addMDType(tickerRequest, '9');
            addMDType(tickerRequest, 'A');
            addMDType(tickerRequest, 'B');
            addMDType(tickerRequest, 'C');

            return tickerRequest;
        }
Example #40
0
		public virtual void Send(FIXMarketDataRequest Request)
		{
			Console.WriteLine(BeAEwTZGlZaeOmY5cm.J00weU3cM6(992));
			MarketDataRequest marketDataRequest = new MarketDataRequest(new MDReqID(Request.MDReqID), new SubscriptionRequestType(Request.SubscriptionRequestType), new MarketDepth(Request.MarketDepth));
			if (Request.ContainsField(265))
				marketDataRequest.set(new MDUpdateType(Request.MDUpdateType));
			MarketDataRequest.NoMDEntryTypes noMdEntryTypes = new MarketDataRequest.NoMDEntryTypes();
			for (int i = 0; i < Request.NoMDEntryTypes; ++i)
			{
				noMdEntryTypes.set(new MDEntryType(Request.GetMDEntryTypesGroup(i).MDEntryType));
				marketDataRequest.addGroup((Group)noMdEntryTypes);
			}
			noMdEntryTypes.Dispose();
			MarketDataRequest.NoRelatedSym noRelatedSym = new MarketDataRequest.NoRelatedSym();
			for (int i = 0; i < Request.NoRelatedSym; ++i)
			{
				FIXRelatedSymGroup relatedSymGroup = Request.GetRelatedSymGroup(i);
				noRelatedSym.set(new Symbol(relatedSymGroup.Symbol));
				if (relatedSymGroup.ContainsField(65))
					noRelatedSym.set(new SymbolSfx(relatedSymGroup.SymbolSfx));
				if (relatedSymGroup.ContainsField(48))
					noRelatedSym.set(new SecurityID(relatedSymGroup.SecurityID));
				if (relatedSymGroup.ContainsField(22))
					noRelatedSym.set(new IDSource(relatedSymGroup.SecurityIDSource));
				if (relatedSymGroup.ContainsField(167))
					noRelatedSym.set(new QuickFix.SecurityType(relatedSymGroup.SecurityType));
				if (relatedSymGroup.ContainsField(200))
					noRelatedSym.set(new MaturityMonthYear(relatedSymGroup.MaturityMonthYear));
				if (relatedSymGroup.ContainsField(202))
					noRelatedSym.set(new StrikePrice(relatedSymGroup.StrikePrice));
				if (relatedSymGroup.ContainsField(206))
					noRelatedSym.set(new OptAttribute(relatedSymGroup.OptAttribute));
				if (relatedSymGroup.ContainsField(231))
					noRelatedSym.set(new ContractMultiplier(relatedSymGroup.ContractMultiplier));
				if (relatedSymGroup.ContainsField(223))
					noRelatedSym.set(new CouponRate(relatedSymGroup.CouponRate));
				if (relatedSymGroup.ContainsField(207))
					noRelatedSym.set(new SecurityExchange(relatedSymGroup.SecurityExchange));
				if (relatedSymGroup.ContainsField(106))
					noRelatedSym.set(new Issuer(relatedSymGroup.Issuer));
				if (relatedSymGroup.ContainsField(348))
					noRelatedSym.set(new EncodedIssuerLen(relatedSymGroup.EncodedIssuerLen));
				if (relatedSymGroup.ContainsField(349))
					noRelatedSym.set(new EncodedIssuer(relatedSymGroup.EncodedIssuer));
				if (relatedSymGroup.ContainsField(107))
					noRelatedSym.set(new SecurityDesc(relatedSymGroup.SecurityDesc));
				if (relatedSymGroup.ContainsField(350))
					noRelatedSym.set(new EncodedSecurityDescLen(relatedSymGroup.EncodedSecurityDescLen));
				if (relatedSymGroup.ContainsField(351))
					noRelatedSym.set(new EncodedSecurityDesc(relatedSymGroup.EncodedSecurityDesc));
				marketDataRequest.addGroup((Group)noRelatedSym);
			}
			noRelatedSym.Dispose();
			try
			{
				Session.sendToTarget((QuickFix.Message)marketDataRequest, this.fSessionID);
			}
			catch (Exception ex)
			{
				Console.WriteLine(BeAEwTZGlZaeOmY5cm.J00weU3cM6(1096) + ex.Message);
			}
		}
        /// <summary>
        /// TradingSessionStatus message handler
        /// </summary>
        private void OnTradingSessionStatus(TradingSessionStatus message)
        {
            if (message.getRequestID() == _currentRequest)
            {
                // load instrument list into a dictionary
                var securities = message.getSecurities();
                while (securities.hasMoreElements())
                {
                    var security = (TradingSecurity)securities.nextElement();
                    _fxcmInstruments[security.getSymbol()] = security;
                }

                // create map from QuantConnect symbols to FXCM symbols
                foreach (var fxcmSymbol in _fxcmInstruments.Keys)
                {
                    var symbol = ConvertFxcmSymbolToSymbol(fxcmSymbol);
                    _mapInstrumentSymbols[symbol] = fxcmSymbol;
                }

                // get account base currency
                _fxcmAccountCurrency = message.getParameter("BASE_CRNCY").getValue();

                _mapRequestsToAutoResetEvents[_currentRequest].Set();
                _mapRequestsToAutoResetEvents.Remove(_currentRequest);

                // unsubscribe all instruments
                var request = new MarketDataRequest();
                foreach (var fxcmSymbol in _fxcmInstruments.Keys)
                {
                    request.addRelatedSymbol(_fxcmInstruments[fxcmSymbol]);
                }
                request.setSubscriptionRequestType(SubscriptionRequestTypeFactory.UNSUBSCRIBE);
                request.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);
                _gateway.sendMessage(request);
            }
        }
Example #42
0
        /// <summary>
        /// Get historical data enumerable for a single symbol, type and resolution given this start and end time (in UTC).
        /// </summary>
        /// <param name="symbol">Symbol for the data we're looking for.</param>
        /// <param name="resolution">Resolution of the data request</param>
        /// <param name="startUtc">Start time of the data in UTC</param>
        /// <param name="endUtc">End time of the data in UTC</param>
        /// <returns>Enumerable of base data for this symbol</returns>
        public IEnumerable<BaseData> Get(Symbol symbol, Resolution resolution, DateTime startUtc, DateTime endUtc)
        {
            if (!_symbolMapper.IsKnownLeanSymbol(symbol))
                throw new ArgumentException("Invalid symbol requested: " + symbol.Value);

            if (symbol.ID.SecurityType != SecurityType.Forex && symbol.ID.SecurityType != SecurityType.Cfd)
                throw new NotSupportedException("SecurityType not available: " + symbol.ID.SecurityType);

            if (endUtc <= startUtc)
                throw new ArgumentException("The end date must be greater than the start date.");

            Console.WriteLine("Logging in...");

            // create the gateway
            _gateway = GatewayFactory.createGateway();

            // register the message listeners with the gateway
            _gateway.registerGenericMessageListener(this);
            _gateway.registerStatusMessageListener(this);

            // create local login properties
            var loginProperties = new FXCMLoginProperties(_userName, _password, _terminal, _server);

            // log in
            _gateway.login(loginProperties);

            // initialize session
            RequestTradingSessionStatus();

            Console.WriteLine("Downloading {0} data from {1} to {2}...", resolution, startUtc.ToString("yyyyMMdd HH:mm:ss"), endUtc.ToString("yyyyMMdd HH:mm:ss"));

            // Find best FXCM parameters
            var interval = FxcmBrokerage.ToFxcmInterval(resolution);

            var totalTicks = (endUtc - startUtc).Ticks;

            // download data
            var totalBaseData = new List<BaseData>();

            var end = endUtc;

            do // 
            {
                //show progress
                progressBar(Math.Abs((end - endUtc).Ticks), totalTicks, Console.WindowWidth / 2,'█');
                _currentBaseData.Clear();

                var mdr = new MarketDataRequest();
                mdr.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SNAPSHOT);
                mdr.setResponseFormat(IFixMsgTypeDefs.__Fields.MSGTYPE_FXCMRESPONSE);
                mdr.setFXCMTimingInterval(interval);
                mdr.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

                mdr.setFXCMStartDate(new UTCDate(FxcmBrokerage.ToJavaDateUtc(startUtc)));
                mdr.setFXCMStartTime(new UTCTimeOnly(FxcmBrokerage.ToJavaDateUtc(startUtc)));
                mdr.setFXCMEndDate(new UTCDate(FxcmBrokerage.ToJavaDateUtc(end)));
                mdr.setFXCMEndTime(new UTCTimeOnly(FxcmBrokerage.ToJavaDateUtc(end)));
                mdr.addRelatedSymbol(_fxcmInstruments[_symbolMapper.GetBrokerageSymbol(symbol)]);


                AutoResetEvent autoResetEvent;
                lock (_locker)
                {
                    _currentRequest = _gateway.sendMessage(mdr);
                    autoResetEvent = new AutoResetEvent(false);
                    _mapRequestsToAutoResetEvents[_currentRequest] = autoResetEvent;
                }
                if (!autoResetEvent.WaitOne(1000 * 5))
                {
                    // no response, exit
                    break;
                }

                // Add data
                totalBaseData.InsertRange(0, _currentBaseData.Where(x => x.Time.Date >= startUtc.Date));
                
                if (end != _currentBaseData[0].Time)
                {
                    // new end date = first datapoint date.
                    end = _currentBaseData[0].Time;
                }
                else
                {
                    break;
                }
               
              

            } while (end > startUtc);


            Console.WriteLine("\nLogging out...");

            // log out
            _gateway.logout();

            // remove the message listeners
            _gateway.removeGenericMessageListener(this);
            _gateway.removeStatusMessageListener(this);

            return totalBaseData.ToList();

        }
Example #43
0
        /// <summary>
        /// Get historical data enumerable for a single symbol, type and resolution given this start and end time (in UTC).
        /// </summary>
        /// <param name="symbol">Symbol for the data we're looking for.</param>
        /// <param name="resolution">Resolution of the data request</param>
        /// <param name="startUtc">Start time of the data in UTC</param>
        /// <param name="endUtc">End time of the data in UTC</param>
        /// <returns>Enumerable of base data for this symbol</returns>
        public IEnumerable<BaseData> Get(Symbol symbol, Resolution resolution, DateTime startUtc, DateTime endUtc)
        {
            if (!_symbolMapper.IsKnownLeanSymbol(symbol))
                throw new ArgumentException("Invalid symbol requested: " + symbol.Value);

            if (resolution == Resolution.Tick)
                throw new NotSupportedException("Resolution not available: " + resolution);

            if (symbol.ID.SecurityType != SecurityType.Forex && symbol.ID.SecurityType != SecurityType.Cfd)
                throw new NotSupportedException("SecurityType not available: " + symbol.ID.SecurityType);

            if (endUtc < startUtc)
                throw new ArgumentException("The end date must be greater or equal than the start date.");

            Console.WriteLine("Logging in...");

            // create the gateway
            _gateway = GatewayFactory.createGateway();

            // register the message listeners with the gateway
            _gateway.registerGenericMessageListener(this);
            _gateway.registerStatusMessageListener(this);

            // create local login properties
            var loginProperties = new FXCMLoginProperties(_userName, _password, _terminal, _server);

            // log in
            _gateway.login(loginProperties);

            // initialize session
            RequestTradingSessionStatus();

            Console.WriteLine("Downloading data from {0} to {1}...", startUtc.ToShortDateString(), endUtc.ToShortDateString());

            // download bars
            var totalBars = new List<TradeBar>();

            // calculate the maximum time span for one request (using 10-second bars)
            const int maxBarsPerRequest = 300;
            var timeSpanPerRequest = TimeSpan.FromSeconds(maxBarsPerRequest * 10);

            var start = startUtc;
            var end = startUtc + timeSpanPerRequest;

            // request loop
            while (start < endUtc.AddDays(1))
            {
                _currentBars.Clear();

                var mdr = new MarketDataRequest();
                mdr.setSubscriptionRequestType(SubscriptionRequestTypeFactory.SNAPSHOT);
                mdr.setResponseFormat(IFixMsgTypeDefs.__Fields.MSGTYPE_FXCMRESPONSE);
                mdr.setFXCMTimingInterval(FXCMTimingIntervalFactory.SEC10);
                mdr.setMDEntryTypeSet(MarketDataRequest.MDENTRYTYPESET_ALL);

                mdr.setFXCMStartDate(new UTCDate(ToJavaDateUtc(start)));
                mdr.setFXCMStartTime(new UTCTimeOnly(ToJavaDateUtc(start)));
                mdr.setFXCMEndDate(new UTCDate(ToJavaDateUtc(end)));
                mdr.setFXCMEndTime(new UTCTimeOnly(ToJavaDateUtc(end)));
                mdr.addRelatedSymbol(_fxcmInstruments[_symbolMapper.GetBrokerageSymbol(symbol)]);

                AutoResetEvent autoResetEvent;
                lock (_locker)
                {
                    _currentRequest = _gateway.sendMessage(mdr);
                    autoResetEvent = new AutoResetEvent(false);
                    _mapRequestsToAutoResetEvents[_currentRequest] = autoResetEvent;
                }
                if (!autoResetEvent.WaitOne(1000))
                {
                    // no response, continue loop
                    start = end.AddSeconds(10);
                    // if saturday, fast-forward to sunday
                    if (start.DayOfWeek == DayOfWeek.Saturday) start = start.AddDays(1);
                    end = start + timeSpanPerRequest;
                    continue;
                }

                var lastBarTime = _currentBars[_currentBars.Count - 1].Time;
                if (lastBarTime < start)
                {
                    // no more data available, exit loop
                    break;
                }

                // add bars received
                totalBars.AddRange(_currentBars.Where(x => x.Time.Date <= endUtc.Date));

                // calculate time span for next request
                start = lastBarTime.AddSeconds(10);
                end = start + timeSpanPerRequest;

                if (start >= DateTime.UtcNow)
                {
                    // data in the future not available, exit loop
                    break;
                }
            }

            Console.WriteLine("Logging out...");

            // log out
            _gateway.logout();

            // remove the message listeners
            _gateway.removeGenericMessageListener(this);
            _gateway.removeStatusMessageListener(this);

            switch (resolution)
            {
                case Resolution.Second:
                    foreach (var bar in totalBars) 
                        yield return bar;
                    break;

                case Resolution.Minute:
                case Resolution.Hour:
                case Resolution.Daily:
                    foreach (var bar in AggregateBars(symbol, totalBars, resolution.ToTimeSpan())) 
                        yield return bar;
                    break;
            }
        }
Example #44
0
 private static void addMDType(MarketDataRequest tickerRequest, char type)
 {
     MarketDataRequest.NoMDEntryTypesGroup g0 = new MarketDataRequest.NoMDEntryTypesGroup();
     g0.Set(new MDEntryType(type));
     tickerRequest.AddGroup(g0);
 }