Skip to content

Commit

Permalink
Fix Composer type fetching
Browse files Browse the repository at this point in the history
- Fix fetching the map file provider and data aggregator instance for
  the composer
  • Loading branch information
Martin-Molinero committed Mar 5, 2024
1 parent 455d2e7 commit 6d41dcc
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void SendComboOrder(OrderType orderType, decimal comboLimitPrice, decimal
var orderProvider = new OrderProvider();
// wait for the previous run to finish, avoid any race condition
Thread.Sleep(2000);
using var brokerage = new InteractiveBrokersBrokerage(algo, orderProvider, algo.Portfolio, new AggregationManager(), TestGlobals.MapFileProvider);
using var brokerage = new InteractiveBrokersBrokerage(algo, orderProvider, algo.Portfolio);
brokerage.Connect();

var openOrders = brokerage.GetOpenOrders();
Expand Down Expand Up @@ -147,7 +147,7 @@ public void SendComboOrderWithUnderlying(OrderType orderType, decimal comboLimit
var orderProvider = new OrderProvider();
// wait for the previous run to finish, avoid any race condition
Thread.Sleep(2000);
using var brokerage = new InteractiveBrokersBrokerage(algo, orderProvider, algo.Portfolio, new AggregationManager(), TestGlobals.MapFileProvider);
using var brokerage = new InteractiveBrokersBrokerage(algo, orderProvider, algo.Portfolio);
brokerage.Connect();

var openOrders = brokerage.GetOpenOrders();
Expand Down Expand Up @@ -252,7 +252,7 @@ public void UpdateComboOrder(OrderType orderType)
var orderProvider = new OrderProvider();
// wait for the previous run to finish, avoid any race condition
Thread.Sleep(2000);
using var brokerage = new InteractiveBrokersBrokerage(algo, orderProvider, algo.Portfolio, new AggregationManager(), TestGlobals.MapFileProvider);
using var brokerage = new InteractiveBrokersBrokerage(algo, orderProvider, algo.Portfolio);
brokerage.Connect();

var openOrders = brokerage.GetOpenOrders();
Expand Down Expand Up @@ -384,8 +384,7 @@ public void SendTrailingStopOrder(decimal quantity, decimal stopPrice, decimal t
Thread.Sleep(2000);

var algorithm = new AlgorithmStub();
using var brokerage = new InteractiveBrokersBrokerage(algorithm, algorithm.Transactions, algorithm.Portfolio, new AggregationManager(),
TestGlobals.MapFileProvider);
using var brokerage = new InteractiveBrokersBrokerage(algorithm, algorithm.Transactions, algorithm.Portfolio);

var orderProcesor = new BrokerageTransactionHandler();
orderProcesor.Initialize(algorithm, brokerage, new TestResultHandler());
Expand Down Expand Up @@ -468,8 +467,7 @@ public void SendUpdateAndCancelTrailingStopOrder()
Thread.Sleep(2000);

var algorithm = new AlgorithmStub();
using var brokerage = new InteractiveBrokersBrokerage(algorithm, algorithm.Transactions, algorithm.Portfolio, new AggregationManager(),
TestGlobals.MapFileProvider);
using var brokerage = new InteractiveBrokersBrokerage(algorithm, algorithm.Transactions, algorithm.Portfolio);

var orderProcesor = new BrokerageTransactionHandler();
orderProcesor.Initialize(algorithm, brokerage, new TestResultHandler());
Expand Down Expand Up @@ -559,8 +557,7 @@ public void SendStopLimitOrder(decimal quantity, decimal stopPrice, decimal limi
Thread.Sleep(2000);

var algorithm = new AlgorithmStub();
using var brokerage = new InteractiveBrokersBrokerage(algorithm, algorithm.Transactions, algorithm.Portfolio, new AggregationManager(),
TestGlobals.MapFileProvider);
using var brokerage = new InteractiveBrokersBrokerage(algorithm, algorithm.Transactions, algorithm.Portfolio);

var orderProcesor = new BrokerageTransactionHandler();
orderProcesor.Initialize(algorithm, brokerage, new TestResultHandler());
Expand Down Expand Up @@ -1039,9 +1036,7 @@ private InteractiveBrokersBrokerage GetBrokerage()
var brokerage = new InteractiveBrokersBrokerage(
new QCAlgorithm(),
new OrderProvider(_orders),
securityProvider,
new AggregationManager(),
TestGlobals.MapFileProvider);
securityProvider);
brokerage.Connect();

return brokerage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class InteractiveBrokersBrokerageDataQueueHandlerTest
[Test]
public void FutureSubscriptions()
{
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider(), new AggregationManager(), TestGlobals.MapFileProvider))
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider()))
{
ib.Connect();
var gotEsData = false;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void FutureSubscriptions()
[Test]
public void GetsTickData()
{
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider(), new AggregationManager(), TestGlobals.MapFileProvider))
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider()))
{
ib.Connect();
var gotUsdData = false;
Expand Down Expand Up @@ -101,7 +101,7 @@ public void GetsTickData()
[Test]
public void GetsTickDataAfterDisconnectionConnectionCycle()
{
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider(), new AggregationManager(), TestGlobals.MapFileProvider))
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider()))
{
ib.Connect();
var cancelationToken = new CancellationTokenSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public void InitializeBrokerage()
_interactiveBrokersBrokerage = new InteractiveBrokersBrokerage(
new QCAlgorithm(),
new OrderProvider(_orders),
securityProvider,
new AggregationManager(),
TestGlobals.MapFileProvider);
securityProvider);
_interactiveBrokersBrokerage.Connect();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void CreatesExpectedFuturesContracts()
{
var symbolMapper = new InteractiveBrokersSymbolMapper(TestGlobals.MapFileProvider);

using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider(), new AggregationManager(), TestGlobals.MapFileProvider))
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider()))
{
ib.Connect();
Assert.IsTrue(ib.IsConnected);
Expand Down Expand Up @@ -310,7 +310,7 @@ public void CreatesExpectedFuturesContracts()
[Test]
public void CreateExpectedFutureContractsWithDifferentCurrencies()
{
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider(), new AggregationManager(), TestGlobals.MapFileProvider))
using (var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider()))
{
ib.Connect();
Assert.IsTrue(ib.IsConnected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected override decimal GetAskPrice(Symbol symbol)

protected override IBrokerage CreateBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider)
{
return new InteractiveBrokersBrokerage(new QCAlgorithm(), orderProvider, securityProvider, new AggregationManager(), TestGlobals.MapFileProvider);
return new InteractiveBrokersBrokerage(new QCAlgorithm(), orderProvider, securityProvider);
}

protected override void DisposeBrokerage(IBrokerage brokerage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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 @@ -39,10 +40,7 @@ public class IBDataDownloader : IDataDownloader, IDisposable
/// </summary>
public IBDataDownloader()
{
var mapFileProvider = Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(
Config.Get("map-file-provider", "LocalDiskMapFileProvider"));

_brokerage = new InteractiveBrokersBrokerage(null, null, null, null, mapFileProvider);
_brokerage = new InteractiveBrokersBrokerage(null, null, null);
_brokerage.Connect();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using QuantConnect.Logging;
using QuantConnect.Data.Market;
using System.Collections.Generic;
using QuantConnect.Configuration;

namespace QuantConnect.ToolBox.IBDownloader
{
Expand Down Expand Up @@ -67,7 +66,7 @@ public static void IBDownloader(IList<string> tickers, string resolution, DateTi
}

// Load settings from config.json
var dataDirectory = Config.Get("data-folder", "../../../Data");
var dataDirectory = Globals.DataFolder;

// Only FOREX for now
SecurityType securityType = SecurityType.Forex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,8 @@ public InteractiveBrokersBrokerage() : base("Interactive Brokers Brokerage")
/// <param name="algorithm">The algorithm instance</param>
/// <param name="orderProvider">An instance of IOrderProvider used to fetch Order objects by brokerage ID</param>
/// <param name="securityProvider">The security provider used to give access to algorithm securities</param>
/// <param name="aggregator">consolidate ticks</param>
/// <param name="mapFileProvider">representing all the map files</param>
public InteractiveBrokersBrokerage(IAlgorithm algorithm, IOrderProvider orderProvider, ISecurityProvider securityProvider, IDataAggregator aggregator, IMapFileProvider mapFileProvider)
: this(algorithm, orderProvider, securityProvider, aggregator, mapFileProvider, Config.Get("ib-account"))
public InteractiveBrokersBrokerage(IAlgorithm algorithm, IOrderProvider orderProvider, ISecurityProvider securityProvider)
: this(algorithm, orderProvider, securityProvider, Config.Get("ib-account"))
{
}

Expand All @@ -274,16 +272,12 @@ public InteractiveBrokersBrokerage(IAlgorithm algorithm, IOrderProvider orderPro
/// <param name="algorithm">The algorithm instance</param>
/// <param name="orderProvider">An instance of IOrderProvider used to fetch Order objects by brokerage ID</param>
/// <param name="securityProvider">The security provider used to give access to algorithm securities</param>
/// <param name="aggregator">consolidate ticks</param>
/// <param name="mapFileProvider">representing all the map files</param>
/// <param name="account">The account used to connect to IB</param>
public InteractiveBrokersBrokerage(IAlgorithm algorithm, IOrderProvider orderProvider, ISecurityProvider securityProvider, IDataAggregator aggregator, IMapFileProvider mapFileProvider, string account)
public InteractiveBrokersBrokerage(IAlgorithm algorithm, IOrderProvider orderProvider, ISecurityProvider securityProvider, string account)
: this(
algorithm,
orderProvider,
securityProvider,
aggregator,
mapFileProvider,
account,
Config.Get("ib-host", "LOCALHOST"),
Config.GetInt("ib-port", 4001),
Expand Down Expand Up @@ -319,8 +313,6 @@ public InteractiveBrokersBrokerage(
IAlgorithm algorithm,
IOrderProvider orderProvider,
ISecurityProvider securityProvider,
IDataAggregator aggregator,
IMapFileProvider mapFileProvider,
string account,
string host,
int port,
Expand All @@ -338,8 +330,6 @@ public InteractiveBrokersBrokerage(
algorithm,
orderProvider,
securityProvider,
aggregator,
mapFileProvider,
account,
host,
port,
Expand Down Expand Up @@ -1192,8 +1182,6 @@ public override void Dispose()
/// <param name="algorithm">The algorithm instance</param>
/// <param name="orderProvider">An instance of IOrderProvider used to fetch Order objects by brokerage ID</param>
/// <param name="securityProvider">The security provider used to give access to algorithm securities</param>
/// <param name="aggregator">consolidate ticks</param>
/// <param name="mapFileProvider">representing all the map files</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>
Expand All @@ -1208,8 +1196,6 @@ private void Initialize(
IAlgorithm algorithm,
IOrderProvider orderProvider,
ISecurityProvider securityProvider,
IDataAggregator aggregator,
IMapFileProvider mapFileProvider,
string account,
string host,
int port,
Expand All @@ -1233,15 +1219,31 @@ private void Initialize(
_loadExistingHoldings = loadExistingHoldings;
_algorithm = algorithm;
_orderProvider = orderProvider;
_mapFileProvider = mapFileProvider;
_aggregator = aggregator;

_mapFileProvider = Composer.Instance.GetPart<IMapFileProvider>();
if (_mapFileProvider == null)
{
// toolbox downloader case
var mapFileProviderName = Config.Get("map-file-provider", "QuantConnect.Data.Auxiliary.LocalDiskMapFileProvider");
Log.Trace($"InteractiveBrokersBrokerage.InteractiveBrokersBrokerage(): found no map file provider instance, creating {mapFileProviderName}");
_mapFileProvider = Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(mapFileProviderName);
_mapFileProvider.Initialize(Composer.Instance.GetExportedValueByTypeName<IDataProvider>(Config.Get("data-provider", "DefaultDataProvider")));
}
_aggregator = Composer.Instance.GetPart<IDataAggregator>();
if (_aggregator == null)
{
// toolbox downloader case
var aggregatorName = Config.Get("data-aggregator", "QuantConnect.Lean.Engine.DataFeeds.AggregationManager");
Log.Trace($"InteractiveBrokersBrokerage.InteractiveBrokersBrokerage(): found no data aggregator instance, creating {aggregatorName}");
_aggregator = Composer.Instance.GetExportedValueByTypeName<IDataAggregator>(aggregatorName);
}
_account = account;
_host = host;
_port = port;
_ibVersion = Convert.ToInt32(ibVersion, CultureInfo.InvariantCulture);
_agentDescription = agentDescription;

_symbolMapper = new InteractiveBrokersSymbolMapper(mapFileProvider);
_symbolMapper = new InteractiveBrokersSymbolMapper(_mapFileProvider);

_subscriptionManager = new EventBasedDataQueueHandlerSubscriptionManager();
_subscriptionManager.SubscribeImpl += (s, t) => Subscribe(s);
Expand Down Expand Up @@ -3460,8 +3462,6 @@ public void SetJob(LiveNodePacket job)
Initialize(null,
null,
null,
Composer.Instance.GetExportedValueByTypeName<IDataAggregator>(Config.Get("data-aggregator", "QuantConnect.Lean.Engine.DataFeeds.AggregationManager"), forceTypeNameOnExisting: false),
Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(Config.Get("map-file-provider", "QuantConnect.Data.Auxiliary.LocalDiskMapFileProvider")),
account,
host,
port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public override IBrokerage CreateBrokerage(LiveNodePacket job, IAlgorithm algori
algorithm,
algorithm.Transactions,
algorithm.Portfolio,
Composer.Instance.GetExportedValueByTypeName<IDataAggregator>(Config.Get("data-aggregator", "QuantConnect.Lean.Engine.DataFeeds.AggregationManager"), forceTypeNameOnExisting: false),
Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(Config.Get("map-file-provider", "QuantConnect.Data.Auxiliary.LocalDiskMapFileProvider")),
account,
host,
port,
Expand Down

0 comments on commit 6d41dcc

Please sign in to comment.