Skip to content

Commit

Permalink
Add implicit conversion from FuturesContract to Symbol
Browse files Browse the repository at this point in the history
Modified algorithms to use futures contract objects directly instead of accessing their Symbol property.
Removed unnecessary import statements and redundant lines in various files.
  • Loading branch information
jhonabreul committed Dec 27, 2024
1 parent a8259ac commit 77d383f
Show file tree
Hide file tree
Showing 17 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override void Initialize()
contractDepthOffset: 0
);

_futureContract = AddFutureContract(FuturesChain(_continuousContract.Symbol).First().Symbol);
_futureContract = AddFutureContract(FuturesChain(_continuousContract.Symbol).First());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Initialize()
SetEndDate(2013, 10, 10);

var SP500 = QuantConnect.Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME);
_symbol = FuturesChain(SP500).First().Symbol;
_symbol = FuturesChain(SP500).First();

// Test case: custom IndicatorBase<QuoteBar> indicator using Future unsubscribed symbol
var indicator1 = new CustomIndicator();
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.CSharp/ConsolidateRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void Initialize()
SetEndDate(2020, 01, 20);

var SP500 = QuantConnect.Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME);
var symbol = FuturesChain(SP500).First().Symbol;
var symbol = FuturesChain(SP500).First();
_future = AddFutureContract(symbol);

var tradableDatesCount = QuantConnect.Time.EachTradeableDayInTimeZone(_future.Exchange.Hours,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override void Initialize()

}

return universe.Select(x => x.Symbol);
return universe;
});

_milk.SetFilter(universe =>
Expand All @@ -94,7 +94,7 @@ public override void Initialize()
}
}

return universe.Select(x => x.Symbol);
return universe;
});
}

Expand Down
3 changes: 1 addition & 2 deletions Algorithm.CSharp/FuturesChainFullDataRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public override void Initialize()
// Get the contract with the latest expiration date, and lowest price
.OrderByDescending(x => x.Expiry)
.ThenBy(x => x.LastPrice)
.First()
.Symbol;
.First();

AddFutureContract(_futureContract);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ private Symbol GetContract(FuturesChains chains, Symbol canonical)
// Get the contract with the latest expiration date, and lowest price
.OrderByDescending(x => x.Expiry)
.ThenBy(x => x.LastPrice)
.First()
.Symbol;
.First();
}

public override void OnData(Slice slice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void Initialize()

var future = QuantConnect.Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME);

_contractSymbol = FuturesChain(future).OrderBy(x => x.ID.Date).FirstOrDefault().Symbol;
_contractSymbol = FuturesChain(future).OrderBy(x => x.ID.Date).First();
_future = AddFutureContract(_contractSymbol);

_future.Holdings.SetHoldings(1600, 1 * OrderSide);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void Initialize()
contractDepthOffset: 0,
extendedMarketHours: true
);
_futureContract = AddFutureContract(FuturesChain(_continuousContract.Symbol).First().Symbol, extendedMarketHours: true);
_futureContract = AddFutureContract(FuturesChain(_continuousContract.Symbol).First(), extendedMarketHours: true);
}

public override void OnWarmupFinished()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void Initialize()
contractDepthOffset: 0,
extendedMarketHours: true
);
_futureContract = AddFutureContract(FuturesChain(_continuousContract.Symbol).First().Symbol,
_futureContract = AddFutureContract(FuturesChain(_continuousContract.Symbol).First(),
extendedMarketHours: true);
}
public override void OnData(Slice slice)
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.CSharp/StatisticsResultsAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ private void CheckMostTradedSecurityStatistic(Dictionary<string, string> statist
{"Estimated Strategy Capacity", "$1100000.00"},
{"Lowest Capacity Asset", "IBM R735QTJ8XC9X"},
{"Portfolio Turnover", "549.26%"},
{"Most Traded Security", "IBM"},
{"Most Traded Security Trade Count", "63"},
{"Most Traded Security", "IBM"},
{"OrderListHash", "8dd77e35338a81410a5b68dc8345f402"}
};
}
Expand Down
1 change: 0 additions & 1 deletion Algorithm.Python/BasicTemplateFuturesDailyAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# limitations under the License.

from AlgorithmImports import *
import QuantConnect.Data.UniverseSelection

### <summary>
### This example demonstrates how to add futures with daily resolution.
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.Python/ConsolidateRegressionAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(self):
self.set_end_date(2020, 1, 20)

SP500 = Symbol.create(Futures.Indices.SP_500_E_MINI, SecurityType.FUTURE, Market.CME)
symbol = list(self.futures_chain(SP500))[0].symbol
symbol = list(self.futures_chain(SP500))[0]
self._future = self.add_future_contract(symbol)

tradable_dates_count = len(list(Time.each_tradeable_day_in_time_zone(self._future.exchange.hours,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# limitations under the License.

from AlgorithmImports import *
from datetime import timedelta

### <summary>
### Regression algorithm illustrating the usage of the <see cref="QCAlgorithm.OptionChain(Symbol)"/> method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# limitations under the License.

from AlgorithmImports import *
from datetime import timedelta

### <summary>
### Regression algorithm illustrating the usage of the <see cref="QCAlgorithm.OptionChains(IEnumerable{Symbol})"/> method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# limitations under the License.

from AlgorithmImports import *
from datetime import timedelta

### <summary>
### Regression algorithm illustrating the usage of the <see cref="QCAlgorithm.FuturesChain(Symbol, bool)"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# limitations under the License.

from AlgorithmImports import *
from datetime import timedelta

### <summary>
### Regression algorithm illustrating the usage of the <see cref="QCAlgorithm.FuturesChains(IEnumerable{Symbol}, bool)"/>
Expand Down
9 changes: 9 additions & 0 deletions Common/Data/Market/FuturesContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ public FuturesContract(FutureUniverse contractData)
/// </returns>
public override string ToString() => Symbol.Value;

/// <summary>
/// Implicit conversion into <see cref="Symbol"/>
/// </summary>
/// <param name="contract">The option contract to be converted</param>
public static implicit operator Symbol(FuturesContract contract)
{
return contract.Symbol;
}

/// <summary>
/// Updates the future contract with the new data, which can be a <see cref="Tick"/> or <see cref="TradeBar"/> or <see cref="QuoteBar"/>
/// </summary>
Expand Down

0 comments on commit 77d383f

Please sign in to comment.