/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Securities.Future;
using QuantConnect.Util;
namespace QuantConnect.Securities
{
///
/// Represents futures symbols universe used in filtering.
///
public class FutureFilterUniverse : ContractSecurityFilterUniverse
{
///
/// Constructs FutureFilterUniverse
///
public FutureFilterUniverse(IEnumerable allData, DateTime localTime)
: base(allData, localTime)
{
}
///
/// Determine if the given Future contract symbol is standard
///
/// True if contract is standard
protected override bool IsStandard(Symbol symbol)
{
return FutureSymbol.IsStandard(symbol);
}
///
/// Creates a new instance of the data type for the given symbol
///
/// A data instance for the given symbol, which is just the symbol itself
protected override FutureUniverse CreateDataInstance(Symbol symbol)
{
return new FutureUniverse()
{
Symbol = symbol,
Time = LocalTime
};
}
///
/// Applies filter selecting futures contracts based on expiration cycles. See for details
///
/// Months to select contracts from
/// Universe with filter applied
public FutureFilterUniverse ExpirationCycle(int[] months)
{
var monthHashSet = months.ToHashSet();
return this.Where(x => monthHashSet.Contains(x.ID.Date.Month));
}
}
///
/// Extensions for Linq support
///
public static class FutureFilterUniverseEx
{
///
/// Filters universe
///
/// Universe to apply the filter too
/// Bool function to determine which Symbol are filtered
/// with filter applied
public static FutureFilterUniverse Where(this FutureFilterUniverse universe, Func predicate)
{
universe.Data = universe.Data.Where(predicate).ToList();
return universe;
}
///
/// Maps universe
///
/// Universe to apply the filter too
/// Symbol function to determine which Symbols are filtered
/// with filter applied
public static FutureFilterUniverse Select(this FutureFilterUniverse universe, Func mapFunc)
{
universe.AllSymbols = universe.Data.Select(mapFunc).ToList();
return universe;
}
///
/// Binds universe
///
/// Universe to apply the filter too
/// Symbols function to determine which Symbols are filtered
/// with filter applied
public static FutureFilterUniverse SelectMany(this FutureFilterUniverse universe, Func> mapFunc)
{
universe.AllSymbols = universe.Data.SelectMany(mapFunc).ToList();
return universe;
}
}
}