Hi Everyone,
I have implemented several Futures contract in my code,
I have different conditions for each Futures to place orders. In this case i have to check which contract must be selected.
I tried that :
public override void OnData(Slice slice)
{
if (!Portfolio.Invested)
{
foreach(var chain in slice.FutureChains)
{
// find the front contract expiring no earlier than in 90 days
var contract = (from futuresContract in chain.Value.OrderBy(x => x.Expiry) where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault();
// if found, perform logic
if (contract != null && contract.LastPrice != 0)
{
if (contract.StartsWith("ES"))
{
But the line :
if (contract.StartsWith("ES"))
return an access violation due to its protection level.
How can i do ?
Gmamuze Cht
Hi Everyone,
Does the code below seem correct to you?var contractSP500 = (from futuresContract in chain.Value.OrderBy(Symbol => Symbol).ThenBy(x => x.Expiry) where futuresContract.Symbol.Contains("ES") where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault(); var contractDOW30 = (from futuresContract in chain.Value.OrderBy(Symbol => Symbol).ThenBy(x => x.Expiry) where futuresContract.Symbol.Contains("YM") where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault();
I get this RunTime Error below, but i don't know if it's these var contract in trouble or an other block of code.System.InvalidOperationException: Failed to compare two elements in the array. ---> System.ArgumentException: At least one object must implement IComparable. at System.Collections.Comparer.Compare (System.Object a, System.Object b) [0x00069] in :0 at System.Collections.Generic.ObjectComparer`1[T].Compare (T x, T y) [0x00000] in :0 at System.Linq.EnumerableSorter`2[TElement, TKey].CompareAnyKeys (System.Int32 index1, System.Int32 index2) [0x00000] in <92922d9bda2f4e1cba9242d052be6d43>:0 at System.Collections.Generic.ComparisonComparer`1[T].Compare (T x, T y) [0x00000] in :0 at System.Collections.Generic.ArraySortHelper`1[T].SwapIfGreater (T[] keys, System.Comparison`1[T] comparer, System.Int32 a, System.Int32 b) [0x00004] in :0 at System.Collections.Generic.ArraySortHelper`1[T].IntroSort (T[] keys, System.Int32 lo, System.Int32 hi, System.Int32 depthLimit, System.Comparison`1[T] comparer) [0x00019] in :0 at System.Collections.Generic.ArraySortHelper`1[T].IntrospectiveSort (T[] keys, System.Int32 left, System.Int32 length, System.Comparison`1[T] comparer) [0x00015] in :0 at System.Collections.Generic.ArraySortHelper`1[T].Sort (T[] keys, System.Int32 index, System.Int32 length, System.Collections.Generic.IComparer`1[T] comparer) [0x0000a] in :0 --- End of inner exception stack trace --- at System.Collections.Generic.ArraySortHelper`1[T].Sort (T[] keys, System.Int32 index, System.Int32 length, System.Collections.Generic.IComparer`1[T] comparer) [0x00036] in :0 at System.Array.Sort[T] (T[] array, System.Int32 index, System.Int32 length, System.Collections.Generic.IComparer`1[T] comparer) [0x00048] in :0 at System.Linq.EnumerableSorter`2[TElement, TKey].QuickSort (System.Int32[] keys, System.Int32 lo, System.Int32 hi) [0x00019] in <92922d9bda2f4e1cba9242d052be6d43>:0 at System.Linq.EnumerableSorter`1[TElement].Sort (TElement[] elements, System.Int32 count) [0x00009] in <92922d9bda2f4e1cba9242d052be6d43>:0 at System.Linq.OrderedEnumerable`1[TElement].SortedMap (System.Linq.Buffer`1[TElement] buffer) [0x00006] in <92922d9bda2f4e1cba9242d052be6d43>:0 at System.Linq.OrderedEnumerable`1+d__3[TElement].MoveNext () [0x0003d] in <92922d9bda2f4e1cba9242d052be6d43>:0 at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].MoveNext () [0x0004e] in <92922d9bda2f4e1cba9242d052be6d43>:0 at System.Linq.Enumerable.TryGetFirst[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Boolean& found) [0x00045] in <92922d9bda2f4e1cba9242d052be6d43>:0 at System.Linq.Enumerable.FirstOrDefault[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00000] in <92922d9bda2f4e1cba9242d052be6d43>:0 at QuantConnect.Algorithm.CSharp.TestAlgo.OnData (QuantConnect.Data.Slice slice) [0x00021] in Test.cs:84 at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.ISynchronizer synchronizer, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Lean.Engine.Server.ILeanManager leanManager, QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x0139a] in Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x0139a] in :0
Gmamuze Cht
I am finding it ! :D
var contractSP500 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "ES").ThenBy(x => x.Expiry) where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault(); var contractDOW30 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "YM").ThenBy(x => x.Expiry) where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault();
Gmamuze Cht
It seems they are no effect to apply
OrderBy(x => x.Symbol == "ES").
But :
if (contractSP500.Symbol.StartsWith("ES")) MarketOrder(contractDOW30.Symbol, 10); Debug("contract en cours : " + chain.Value.Symbol); Debug("contract en cours : " + contractDOW30);
Have one
Alethea Lin
Hi Gmamuze,
Thank you for your effort trying to solve the problem yourself. Could you please provide your full algorithm for further assistance?
Gmamuze Cht
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!