Exemplo n.º 1
0
        /// <summary>
        /// Creates a new IBrokerage instance
        /// </summary>
        /// <param name="job">The job packet to create the brokerage for</param>
        /// <param name="algorithm">The algorithm instance</param>
        /// <returns>A new brokerage instance</returns>
        public override IBrokerage CreateBrokerage(LiveNodePacket job, IAlgorithm algorithm)
        {
            var errors       = new List <string>();
            var accountID    = Read <string>(job.BrokerageData, "tradier-account-id", errors);
            var accessToken  = Read <string>(job.BrokerageData, "tradier-access-token", errors);
            var refreshToken = Read <string>(job.BrokerageData, "tradier-refresh-token", errors);
            var issuedAt     = Read <DateTime>(job.BrokerageData, "tradier-issued-at", errors);
            var lifeSpan     = TimeSpan.FromSeconds(Read <double>(job.BrokerageData, "tradier-lifespan", errors));

            var brokerage = new TradierBrokerage(
                algorithm.Transactions,
                algorithm.Portfolio,
                Composer.Instance.GetExportedValueByTypeName <IDataAggregator>(Config.Get("data-aggregator", "QuantConnect.Lean.Engine.DataFeeds.AggregationManager")),
                accountID);

            // if we're running live locally we'll want to save any new tokens generated so that they can easily be retrieved
            if (Config.GetBool("tradier-save-tokens"))
            {
                brokerage.SessionRefreshed += (sender, args) =>
                {
                    File.WriteAllText(TokensFile, JsonConvert.SerializeObject(args, Formatting.Indented));
                };
            }

            brokerage.SetTokens(job.UserId, accessToken, refreshToken, issuedAt, lifeSpan);

            //Add the brokerage to the composer to ensure its accessible to the live data feed.
            Composer.Instance.AddPart <IDataQueueHandler>(brokerage);
            Composer.Instance.AddPart <IHistoryProvider>(brokerage);
            return(brokerage);
        }
        public void GetsHistory(Symbol symbol, Resolution resolution, TimeSpan period, bool throwsException)
        {
            TestDelegate test = () =>
            {
                var accessToken = Config.Get("tradier-access-token");

                var brokerage = new TradierBrokerage(null, null, "");
                brokerage.SetTokens(0, accessToken, "", DateTime.Now, Time.OneDay);

                var now = DateTime.UtcNow;

                var requests = new[]
                {
                    new HistoryRequest
                    {
                        Symbol = symbol,
                        Resolution = resolution,
                        StartTimeUtc = now.Add(-period),
                        EndTimeUtc = now
                    }
                };

                var history = brokerage.GetHistory(requests, TimeZones.Utc);

                foreach (var slice in history)
                {
                    if (resolution == Resolution.Tick)
                    {
                        foreach (var tick in slice.Ticks[symbol])
                        {
                            Log.Trace("{0}: {1} - {2} / {3}", tick.Time, tick.Symbol, tick.BidPrice, tick.AskPrice);
                        }
                    }
                    else
                    {
                        var bar = slice.Bars[symbol];

                        Log.Trace("{0}: {1} - O={2}, H={3}, L={4}, C={5}, V={6}", bar.Time, bar.Symbol, bar.Open, bar.High, bar.Low, bar.Close, bar.Volume);
                    }
                }

                Log.Trace("Data points retrieved: " + brokerage.DataPointCount);
            };

            if (throwsException)
            {
                Assert.Throws<ArgumentException>(test);
            }
            else
            {
                Assert.DoesNotThrow(test);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates the brokerage under test
        /// </summary>
        /// <returns>A connected brokerage instance</returns>
        protected override IBrokerage CreateBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider)
        {
            var accountID = TradierBrokerageFactory.Configuration.AccountID;
            var tradier = new TradierBrokerage(orderProvider, securityProvider, accountID);

            var qcUserID = TradierBrokerageFactory.Configuration.QuantConnectUserID;
            var tokens = TradierBrokerageFactory.GetTokens();
            tradier.SetTokens(qcUserID, tokens.AccessToken, tokens.RefreshToken, tokens.IssuedAt, TimeSpan.FromSeconds(tokens.ExpiresIn));

            // keep the tokens up to date in the event of a refresh
            tradier.SessionRefreshed += (sender, args) =>
            {
                File.WriteAllText(TradierBrokerageFactory.TokensFile, JsonConvert.SerializeObject(args, Formatting.Indented));
            };

            return tradier;
        }
        /// <summary>
        /// Creates a new IBrokerage instance
        /// </summary>
        /// <param name="job">The job packet to create the brokerage for</param>
        /// <param name="algorithm">The algorithm instance</param>
        /// <returns>A new brokerage instance</returns>
        public override IBrokerage CreateBrokerage(LiveNodePacket job, IAlgorithm algorithm)
        {
            var errors      = new List <string>();
            var useSandbox  = Read <bool>(job.BrokerageData, "tradier-use-sandbox", errors);
            var accountId   = Read <string>(job.BrokerageData, "tradier-account-id", errors);
            var accessToken = Read <string>(job.BrokerageData, "tradier-access-token", errors);

            var brokerage = new TradierBrokerage(
                algorithm.Transactions,
                algorithm.Portfolio,
                Composer.Instance.GetExportedValueByTypeName <IDataAggregator>(Config.Get("data-aggregator", "QuantConnect.Lean.Engine.DataFeeds.AggregationManager")),
                useSandbox,
                accountId,
                accessToken);

            // Add the brokerage to the composer to ensure its accessible to the live data feed.
            Composer.Instance.AddPart <IDataQueueHandler>(brokerage);
            Composer.Instance.AddPart <IHistoryProvider>(brokerage);

            return(brokerage);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new IBrokerage instance
        /// </summary>
        /// <param name="job">The job packet to create the brokerage for</param>
        /// <param name="algorithm">The algorithm instance</param>
        /// <returns>A new brokerage instance</returns>
        public override IBrokerage CreateBrokerage(LiveNodePacket job, IAlgorithm algorithm)
        {
            var errors       = new List <string>();
            var accountID    = Read <long>(job.BrokerageData, "tradier-account-id", errors);
            var accessToken  = Read <string>(job.BrokerageData, "tradier-access-token", errors);
            var refreshToken = Read <string>(job.BrokerageData, "tradier-refresh-token", errors);
            var issuedAt     = Read <DateTime>(job.BrokerageData, "tradier-issued-at", errors);
            var lifeSpan     = TimeSpan.FromSeconds(Read <double>(job.BrokerageData, "tradier-lifespan", errors));

            var brokerage = new TradierBrokerage(algorithm.Transactions, algorithm.Portfolio, accountID);

            // if we're running live locally we'll want to save any new tokens generated so that they can easily be retrieved
            if (Config.GetBool("tradier-save-tokens"))
            {
                brokerage.SessionRefreshed += (sender, args) =>
                {
                    File.WriteAllText(TokensFile, JsonConvert.SerializeObject(args, Formatting.Indented));
                };
            }

            brokerage.SetTokens(job.UserId, accessToken, refreshToken, issuedAt, lifeSpan);

            return(brokerage);
        }
        /// <summary>
        /// Creates a new IBrokerage instance
        /// </summary>
        /// <param name="job">The job packet to create the brokerage for</param>
        /// <param name="algorithm">The algorithm instance</param>
        /// <returns>A new brokerage instance</returns>
        public override IBrokerage CreateBrokerage(LiveNodePacket job, IAlgorithm algorithm)
        {
            var errors = new List<string>();
            var accountID = Read<long>(job.BrokerageData, "tradier-account-id", errors);
            var accessToken = Read<string>(job.BrokerageData, "tradier-access-token", errors);
            var refreshToken = Read<string>(job.BrokerageData, "tradier-refresh-token", errors);
            var issuedAt = Read<DateTime>(job.BrokerageData, "tradier-issued-at", errors);
            var lifeSpan = TimeSpan.FromSeconds(Read<double>(job.BrokerageData, "tradier-lifespan", errors));

            var brokerage = new TradierBrokerage(algorithm.Transactions, algorithm.Portfolio, accountID);

            // if we're running live locally we'll want to save any new tokens generated so that they can easily be retrieved
            if (Config.GetBool("tradier-save-tokens"))
            {
                brokerage.SessionRefreshed += (sender, args) =>
                {
                    File.WriteAllText(TokensFile, JsonConvert.SerializeObject(args, Formatting.Indented));
                };
            }

            brokerage.SetTokens(job.UserId, accessToken, refreshToken, issuedAt, lifeSpan);

            return brokerage;
        }