/* * 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 QuantConnect.Data.UniverseSelection; using QuantConnect.Orders; using QuantConnect.Orders.Fees; using QuantConnect.Securities; using System.Collections.Generic; using System.Linq; namespace QuantConnect.Algorithm.CSharp { /// /// In this algorithm we demonstrate how to use the coarse fundamental data to /// define a universe as the top dollar volume and set the algorithm to use /// raw prices /// /// /// /// /// public class RawPricesCoarseUniverseAlgorithm : QCAlgorithm { private const int NumberOfSymbols = 5; public override void Initialize() { // what resolution should the data *added* to the universe be? UniverseSettings.Resolution = Resolution.Daily; SetStartDate(2014, 01, 01); SetEndDate(2015, 01, 01); SetCash(50000); // Set the security initializer with the characteristics defined in CustomSecurityInitializer SetSecurityInitializer(CustomSecurityInitializer); // this add universe method accepts a single parameter that is a function that // accepts an IEnumerable and returns IEnumerable AddUniverse(CoarseSelectionFunction); } /// /// Initialize the security with raw prices and zero fees /// /// Security which characteristics we want to change private void CustomSecurityInitializer(Security security) { security.SetDataNormalizationMode(DataNormalizationMode.Raw); security.SetFeeModel(new ConstantFeeModel(0)); } // sort the data by daily dollar volume and take the top 'NumberOfSymbols' public static IEnumerable CoarseSelectionFunction(IEnumerable coarse) { // sort descending by daily dollar volume var sortedByDollarVolume = coarse.OrderByDescending(x => x.DollarVolume); // take the top entries from our sorted collection var top5 = sortedByDollarVolume.Take(NumberOfSymbols); // we need to return only the symbol objects return top5.Select(x => x.Symbol); } // this event fires whenever we have changes to our universe public override void OnSecuritiesChanged(SecurityChanges changes) { foreach (var security in changes.RemovedSecurities) { if (security.Invested) { Liquidate(security.Symbol); } } // we want 20% allocation in each security in our universe foreach (var security in changes.AddedSecurities) { SetHoldings(security.Symbol, 0.2m); } } public override void OnOrderEvent(OrderEvent orderEvent) { if (orderEvent.Status == OrderStatus.Filled) { Log($"OnOrderEvent({UtcTime:o}):: {orderEvent}"); } } } }