Files
quantconnect--lean/Algorithm.CSharp/CoarseFineFundamentalRegressionAlgorithm.cs
T
Michael Handschuh 891ab365ab Reverse a day when reading fine data
The fine enumerator factor accepts a 'date'. This date is that day
that the data is produced, so the emit time on that data is midnight
the following day. When we go to read the fine data we can't use the
current time because that's the 'emit time' -- instead we need to back
the time up a full day to find the correct start time. In addition,
the fine data was being read using UTC time stamps which led to even
more confusion here -- this change resolves both issues.

In addition to the above, the security.Fundamentals property wasn't being
properly set for the first time step when a security is selected due to
a bug in the subscription synchronizer not clearing out the universeData
in preparation for another loop (in the event changes != None).
2018-04-27 14:54:07 -04:00

132 lines
5.2 KiB
C#

/*
* 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.Fundamental;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Demonstration of how to define a universe
/// as a combination of use the coarse fundamental data and fine fundamental data
/// </summary>
/// <meta name="tag" content="using data" />
/// <meta name="tag" content="universes" />
/// <meta name="tag" content="coarse universes" />
/// <meta name="tag" content="regression test" />
public class CoarseFineFundamentalRegressionAlgorithm : QCAlgorithm
{
private const int NumberOfSymbolsFine = 2;
// initialize our changes to nothing
private SecurityChanges _changes = SecurityChanges.None;
public override void Initialize()
{
UniverseSettings.Resolution = Resolution.Daily;
SetStartDate(2014, 03, 24);
SetEndDate(2014, 04, 07);
SetCash(50000);
// this add universe method accepts two parameters:
// - coarse selection function: accepts an IEnumerable<CoarseFundamental> and returns an IEnumerable<Symbol>
// - fine selection function: accepts an IEnumerable<FineFundamental> and returns an IEnumerable<Symbol>
AddUniverse(CoarseSelectionFunction, FineSelectionFunction);
}
// return a list of three fixed symbol objects
public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
{
if (Time.Date < new DateTime(2014, 4, 1))
{
return new List<Symbol>
{
QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA),
QuantConnect.Symbol.Create("AIG", SecurityType.Equity, Market.USA),
QuantConnect.Symbol.Create("IBM", SecurityType.Equity, Market.USA)
};
}
return new List<Symbol>
{
QuantConnect.Symbol.Create("BAC", SecurityType.Equity, Market.USA),
QuantConnect.Symbol.Create("GOOG", SecurityType.Equity, Market.USA),
QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA)
};
}
// sort the data by P/E ratio and take the top 'NumberOfSymbolsFine'
public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
{
// sort descending by P/E ratio
var sortedByPeRatio = fine.OrderByDescending(x => x.ValuationRatios.PERatio);
// take the top entries from our sorted collection
var topFine = sortedByPeRatio.Take(NumberOfSymbolsFine);
// we need to return only the symbol objects
return topFine.Select(x => x.Symbol);
}
//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
public void OnData(TradeBars data)
{
// if we have no changes, do nothing
if (_changes == SecurityChanges.None) return;
// liquidate removed securities
foreach (var security in _changes.RemovedSecurities)
{
if (security.Invested)
{
Liquidate(security.Symbol);
Debug("Liquidated Stock: " + security.Symbol.Value);
}
}
// we want 50% allocation in each security in our universe
foreach (var security in _changes.AddedSecurities)
{
if (security.Fundamentals.EarningRatios.EquityPerShareGrowth.OneYear > 0.25m)
{
SetHoldings(security.Symbol, 0.5m);
Debug("Purchased Stock: " + security.Symbol.Value);
}
}
_changes = SecurityChanges.None;
}
// this event fires whenever we have changes to our universe
public override void OnSecuritiesChanged(SecurityChanges changes)
{
_changes = changes;
if (changes.AddedSecurities.Count > 0)
{
Debug("Securities added: " + string.Join(",", changes.AddedSecurities.Select(x => x.Symbol.Value)));
}
if (changes.RemovedSecurities.Count > 0)
{
Debug("Securities removed: " + string.Join(",", changes.RemovedSecurities.Select(x => x.Symbol.Value)));
}
}
}
}