Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new IBrokerage instance and set ups the environment for the brokerage
        /// </summary>
        /// <param name="brokerageData">Brokerage data containing necessary parameters for initialization</param>
        /// <param name="orderMapping">IOrderMapping instance to maintain IB -> QC order id mapping</param>
        /// <returns>A new brokerage instance</returns>
        public IBrokerage CreateBrokerage(Dictionary <string, string> brokerageData, IOrderMapping orderMapping)
        {
            var errors = new List <string>();

            // read values from the brokerage datas
            var useTws                = Config.GetBool("ib-use-tws");
            var port                  = Config.GetInt("ib-port", 4001);
            var host                  = Config.Get("ib-host", "127.0.0.1");
            var twsDirectory          = Config.Get("ib-tws-dir", "C:\\Jts");
            var ibControllerDirectory = Config.Get("ib-controller-dir", "C:\\IBController");

            var account          = Read <string>(brokerageData, "ib-account", errors);
            var userID           = Read <string>(brokerageData, "ib-user-name", errors);
            var password         = Read <string>(brokerageData, "ib-password", errors);
            var agentDescription = Read <AgentDescription>(brokerageData, "ib-agent-description", errors);

            if (errors.Count != 0)
            {
                // if we had errors then we can't create the instance
                throw new Exception(string.Join(Environment.NewLine, errors));
            }

            // launch the IB gateway
            InteractiveBrokersGatewayRunner.Start(ibControllerDirectory, twsDirectory, userID, password, useTws);

            return(new InteractiveBrokersBrokerage(orderMapping, account, host, port, agentDescription));
        }
Ejemplo n.º 2
0
 protected override IBrokerage CreateBrokerage(IOrderMapping orderMapping, IHoldingsProvider holdingsProvider)
 {
     if (!_gatewayLaunched)
     {
         _gatewayLaunched = true;
         InteractiveBrokersGatewayRunner.Start(Config.Get("ib-controller-dir"),
                                               Config.Get("ib-tws-dir"),
                                               Config.Get("ib-user-name"),
                                               Config.Get("ib-password"),
                                               Config.GetBool("ib-use-tws")
                                               );
     }
     return(new InteractiveBrokersBrokerage(orderMapping));
 }
Ejemplo n.º 3
0
 protected override IBrokerage CreateBrokerage(IOrderMapping orderMapping, IHoldingsProvider holdingsProvider)
 {
     if (!_gatewayLaunched)
     {
         _gatewayLaunched = true;
         InteractiveBrokersGatewayRunner.Start(Config.Get("ib-controller-dir"),
             Config.Get("ib-tws-dir"),
             Config.Get("ib-user-name"),
             Config.Get("ib-password"),
             Config.GetBool("ib-use-tws")
             );
     }
     return new InteractiveBrokersBrokerage(orderMapping);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates the brokerage under test
        /// </summary>
        /// <returns>A connected brokerage instance</returns>
        protected override IBrokerage CreateBrokerage(IOrderMapping orderMapping, IHoldingsProvider holdingsProvider)
        {
            var accountID = TradierBrokerageFactory.Configuration.AccountID;
            var tradier   = new TradierBrokerage(orderMapping, holdingsProvider, 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);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates the brokerage under test and connects it
 /// </summary>
 /// <returns>A connected brokerage instance</returns>
 protected abstract IBrokerage CreateBrokerage(IOrderMapping orderMapping, IHoldingsProvider holdingsProvider);
Ejemplo n.º 6
0
        /// <summary>
        /// Creates the brokerage under test
        /// </summary>
        /// <returns>A connected brokerage instance</returns>
        protected override IBrokerage CreateBrokerage(IOrderMapping orderMapping, IHoldingsProvider holdingsProvider)
        {
            var accountID = TradierBrokerageFactory.Configuration.AccountID;
            var tradier = new TradierBrokerage(orderMapping, holdingsProvider, 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 and set ups the environment for the brokerage
        /// </summary>
        /// <param name="brokerageData">Brokerage data containing necessary parameters for initialization</param>
        /// <param name="orderMapping">IOrderMapping instance to maintain IB -> QC order id mapping</param>
        /// <returns>A new brokerage instance</returns>
        public IBrokerage CreateBrokerage(Dictionary<string, string> brokerageData, IOrderMapping orderMapping)
        {
            var errors = new List<string>();

            // read values from the brokerage datas
            var useTws = Config.GetBool("ib-use-tws");
            var port = Config.GetInt("ib-port", 4001);
            var host = Config.Get("ib-host", "127.0.0.1");
            var twsDirectory = Config.Get("ib-tws-dir", "C:\\Jts");
            var ibControllerDirectory = Config.Get("ib-controller-dir", "C:\\IBController");

            var account = Read<string>(brokerageData, "ib-account", errors);
            var userID = Read<string>(brokerageData, "ib-user-name", errors);
            var password = Read<string>(brokerageData, "ib-password", errors);
            var agentDescription = Read<AgentDescription>(brokerageData, "ib-agent-description", errors);

            if (errors.Count != 0)
            {
                // if we had errors then we can't create the instance
                throw new Exception(string.Join(Environment.NewLine, errors));
            }

            // launch the IB gateway
            InteractiveBrokersGatewayRunner.Start(ibControllerDirectory, twsDirectory, userID, password, useTws);

            return new InteractiveBrokersBrokerage(orderMapping, account, host, port, agentDescription);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates the brokerage under test and connects it
 /// </summary>
 /// <returns>A connected brokerage instance</returns>
 protected abstract IBrokerage CreateBrokerage(IOrderMapping orderMapping, IHoldingsProvider holdingsProvider);
Ejemplo n.º 9
0
        /// <summary>
        /// Creates a new InteractiveBrokersBrokerage from the specified values
        /// </summary>
        /// <param name="orderMapping">An instance of IOrderMapping used to fetch Order objects by brokerage ID</param>
        /// <param name="account">The Interactive Brokers account name</param>
        /// <param name="host">host name or IP address of the machine where TWS is running. Leave blank to connect to the local host.</param>
        /// <param name="port">must match the port specified in TWS on the Configure&gt;API&gt;Socket Port field.</param>
        /// <param name="agentDescription">Used for Rule 80A describes the type of trader.</param>
        public InteractiveBrokersBrokerage(IOrderMapping orderMapping, string account, string host, int port, IB.AgentDescription agentDescription = IB.AgentDescription.Individual)
            : base("Interactive Brokers Brokerage")
        {
            _orderMapping = orderMapping;
            _account = account;
            _host = host;
            _port = port;
            _clientID = IncrementClientID();
            _agentDescription = agentDescription;
            _client = new IB.IBClient();

            // set up event handlers
            _client.UpdatePortfolio += HandlePortfolioUpdates;
            _client.OrderStatus += HandleOrderStatusUpdates;
            _client.UpdateAccountValue += HandleUpdateAccountValue;
            _client.Error += HandleError;
            _client.TickPrice += HandleTickPrice;
            _client.TickSize += HandleTickSize;
            _client.CurrentTime += HandleBrokerTime;

            // we need to wait until we receive the next valid id from the server
            _client.NextValidId += (sender, e) =>
            {
                // only grab this id when we initialize, and we'll manually increment it here to avoid threading issues
                if (_nextValidID == 0)
                {
                    _nextValidID = e.OrderId;
                    _waitForNextValidID.Set();
                }
                Log.Trace("InteractiveBrokersBrokerage.HandleNextValidID(): " + e.OrderId);
            };
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Creates a new InteractiveBrokeragBrokerage for the specified account
 /// </summary>
 /// <param name="orderMapping">An instance of IOrderMapping used to fetch Order objects by brokerage ID</param>
 /// <param name="account">The account used to connect to IB</param>
 public InteractiveBrokersBrokerage(IOrderMapping orderMapping, string account)
     : this(orderMapping,
         account,
         Config.Get("ib-host", "LOCALHOST"),
         Config.GetInt("ib-port", 4001),
         Config.GetValue("ib-agent-description", IB.AgentDescription.Individual))
 {
 }