Skip to content

Commit

Permalink
Add 2FA debug message
Browse files Browse the repository at this point in the history
- Add 2FA debug message
- Minor ib data downloader cleanup
  • Loading branch information
Martin-Molinero committed Apr 12, 2024
1 parent b133b55 commit c68ecb5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace QuantConnect.Tests.Brokerages.InteractiveBrokers
{
[TestFixture]
[Ignore("These tests require the IBGateway to be installed.")]
[Explicit("These tests require the IBGateway to be installed.")]
public class IBDataDownloaderTests
{
[TestCase("ES", Resolution.Daily, 15)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
using QuantConnect.Data;
using QuantConnect.Util;
using QuantConnect.Securities;
using QuantConnect.Interfaces;
using QuantConnect.Data.Market;
using System.Collections.Generic;
using QuantConnect.Configuration;
using QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Brokerages.InteractiveBrokers;

namespace QuantConnect.ToolBox.IBDownloader
Expand All @@ -33,18 +31,28 @@ namespace QuantConnect.ToolBox.IBDownloader
/// </summary>
public class IBDataDownloader : IDataDownloader, IDisposable
{
private readonly MarketHoursDatabase _marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
private readonly InteractiveBrokersBrokerage _brokerage;

/// <summary>
/// Initializes a new instance of the <see cref="IBDataDownloader"/> class
/// </summary>
public IBDataDownloader()
{
_brokerage = new InteractiveBrokersBrokerage(null, null, null);
_brokerage = new InteractiveBrokersBrokerage(null, null, null,
Config.Get("ib-account"),
Config.Get("ib-host", "LOCALHOST"),
Config.GetInt("ib-port", 4001),
Config.Get("ib-tws-dir"),
Config.Get("ib-version", InteractiveBrokersBrokerage.DefaultVersion),
Config.Get("ib-user-name"),
Config.Get("ib-password"),
Config.Get("ib-trading-mode"),
Config.GetValue("ib-agent-description", Brokerages.InteractiveBrokers.Client.AgentDescription.Individual),
loadExistingHoldings: false);
_brokerage.Connect();
}


/// <summary>
/// Get historical data enumerable for a single symbol, type and resolution given this start and end time (in UTC).
/// </summary>
Expand Down Expand Up @@ -74,8 +82,8 @@ public IEnumerable<BaseData> Get(DataDownloaderGetParameters dataDownloaderGetPa
symbols = GetChainSymbols(symbol, true).ToList();
}

var exchangeHours = MarketHoursDatabase.FromDataFolder().GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType);
var dataTimeZone = MarketHoursDatabase.FromDataFolder().GetDataTimeZone(symbol.ID.Market, symbol, symbol.SecurityType);
var exchangeHours = _marketHoursDatabase.GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType);
var dataTimeZone = _marketHoursDatabase.GetDataTimeZone(symbol.ID.Market, symbol, symbol.SecurityType);

return symbols
.Select(symbol =>
Expand Down Expand Up @@ -131,51 +139,6 @@ public void DownloadAndSave(List<Symbol> symbols, Resolution resolution, Securit
writer.DownloadAndSave(_brokerage, symbols, startTimeUtc, endTimeUtc);
}

/// <summary>
/// Groups a list of bars into a dictionary keyed by date
/// </summary>
/// <param name="bars"></param>
/// <returns></returns>
private static SortedDictionary<DateTime, List<QuoteBar>> GroupBarsByDate(IList<QuoteBar> bars)
{
var groupedBars = new SortedDictionary<DateTime, List<QuoteBar>>();

foreach (var bar in bars)
{
var date = bar.Time.Date;

if (!groupedBars.ContainsKey(date))
groupedBars[date] = new List<QuoteBar>();

groupedBars[date].Add(bar);
}

return groupedBars;
}

#region Console Helper

/// <summary>
/// Draw a progress bar
/// </summary>
/// <param name="complete"></param>
/// <param name="maxVal"></param>
/// <param name="barSize"></param>
/// <param name="progressCharacter"></param>
private static void ProgressBar(long complete, long maxVal, long barSize, char progressCharacter)
{

decimal p = (decimal)complete / (decimal)maxVal;
int chars = (int)Math.Floor(p / ((decimal)1 / (decimal)barSize));
string bar = string.Empty;
bar = bar.PadLeft(chars, progressCharacter);
bar = bar.PadRight(Convert.ToInt32(barSize) - 1);

Console.Write($"\r[{bar}] {(p * 100).ToStringInvariant("N2")}%");
}

#endregion

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ public InteractiveBrokersBrokerage(
userName,
password,
tradingMode,
agentDescription = IB.AgentDescription.Individual,
loadExistingHoldings = true,
agentDescription,
loadExistingHoldings,
weeklyRestartUtcTime: weeklyRestartUtcTime);
}

Expand Down Expand Up @@ -4047,7 +4047,7 @@ public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, str
symbol.ID.Market,
symbol,
symbol.SecurityType,
_algorithm.Portfolio.CashBook.AccountCurrency);
_algorithm != null ? _algorithm.Portfolio.CashBook.AccountCurrency : Currencies.USD);

// setting up lookup request
var contract = new Contract
Expand All @@ -4069,7 +4069,14 @@ public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, str
// IB requests for full option chains are rate limited and responses can be delayed up to a minute for each underlying,
// so we fetch them from the OCC website instead of using the IB API.
// For futures options, we fetch the option chain from CME.
symbols.AddRange(_algorithm.OptionChainProvider.GetOptionContractList(symbol, DateTime.Today));
if (_algorithm != null)
{
symbols.AddRange(_algorithm.OptionChainProvider.GetOptionContractList(symbol, DateTime.Today));
}
else
{
symbols.AddRange(Composer.Instance.GetPart<IOptionChainProvider>().GetOptionContractList(symbol, DateTime.Today));
}
}
else if (symbol.SecurityType == SecurityType.Future)
{
Expand Down Expand Up @@ -4600,6 +4607,12 @@ private void OnIbAutomaterOutputDataReceived(object sender, OutputDataReceivedEv
return;
}

if (e.Data.Contains("Waiting for 2FA confirmation", StringComparison.InvariantCultureIgnoreCase))
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Information, "2FA",
"Logging into account. Check phone for two-factor authentication verification..."));
}

Log.Trace($"InteractiveBrokersBrokerage.OnIbAutomaterOutputDataReceived(): {e.Data}");
}

Expand Down

0 comments on commit c68ecb5

Please sign in to comment.