Skip to content

Commit

Permalink
Merge pull request QuantConnect#3970 from AlexCatarino/feature-3969-a…
Browse files Browse the repository at this point in the history
…dds-marketcap

Adds MarketCap Member to FineFundamental Class
  • Loading branch information
jaredbroad authored Jan 7, 2020
2 parents 936a0c9 + dc2cac9 commit 14d0b47
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Algorithm.CSharp/CoarseFineFundamentalRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental
};
}

// sort the data by P/E ratio and take the top 'NumberOfSymbolsFine'
// sort the data by market capitalization 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);
// sort descending by market capitalization
var sortedByMarketCap = fine.OrderByDescending(x => x.MarketCap);

// take the top entries from our sorted collection
var topFine = sortedByPeRatio.Take(NumberOfSymbolsFine);
var topFine = sortedByMarketCap.Take(NumberOfSymbolsFine);

// we need to return only the symbol objects
return topFine.Select(x => x.Symbol);
Expand Down
8 changes: 4 additions & 4 deletions Algorithm.Python/CoarseFineFundamentalRegressionAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def CoarseSelectionFunction(self, coarse):
return [ Symbol.Create(x, SecurityType.Equity, Market.USA) for x in tickers ]


# sort the data by P/E ratio and take the top 'NumberOfSymbolsFine'
# sort the data by market capitalization and take the top 'NumberOfSymbolsFine'
def FineSelectionFunction(self, fine):
# sort descending by P/E ratio
sortedByPeRatio = sorted(fine, key=lambda x: x.ValuationRatios.PERatio, reverse=True)
# sort descending by market capitalization
sortedByMarketCap = sorted(fine, key=lambda x: x.MarketCap, reverse=True)

# take the top entries from our sorted collection
return [ x.Symbol for x in sortedByPeRatio[:self.numberOfSymbolsFine] ]
return [ x.Symbol for x in sortedByMarketCap[:self.numberOfSymbolsFine] ]

def OnData(self, data):
# if we have no changes, do nothing
Expand Down
9 changes: 9 additions & 0 deletions Common/Data/Fundamental/FineFundamental.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public override DateTime EndTime
set { Time = value - QuantConnect.Time.OneDay; }
}

/// <summary>
/// Market capitalization is the aggregate market value of a company represented in dollar amount.
/// </summary>
/// <remarks>
/// Returns zero if <see cref="BasicAverageShares"/> is null
/// </remarks>
[JsonIgnore]
public decimal MarketCap => EarningReports?.BasicAverageShares?.ThreeMonths * Value ?? 0;

/// <summary>
/// Creates the universe symbol used for fine fundamental data
/// </summary>
Expand Down
90 changes: 90 additions & 0 deletions Tests/Common/Data/Fundamental/FineFundamentalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 NUnit.Framework;
using QuantConnect.Data.Fundamental;

namespace QuantConnect.Tests.Common.Data.Fundamental
{
[TestFixture]
public class FineFundamentalTests
{
[Test]
public void ComputesMarketCapCorrectly()
{
var fine = new FineFundamental
{
Symbol = Symbols.AAPL,
EndTime = DateTime.Now,
Value = 267.18m,
CompanyReference = new CompanyReference
{
CountryId = "USA",
PrimaryExchangeID = "NYS",
IndustryTemplateCode = "N"
},
SecurityReference = new SecurityReference
{
IPODate = new DateTime(1980,12,12)
},
ValuationRatios = new ValuationRatios
{
PERatio = 22.476871m
},
EarningReports = new EarningReports
{
BasicAverageShares = new BasicAverageShares
{
ThreeMonths = 449081100m
},
BasicEPS = new BasicEPS
{
TwelveMonths = 11.97m
}
}
};

Assert.AreEqual(119985488298m, fine.MarketCap);
}

[Test]
public void ZeroMarketCapForDefaultBasicAverageShares()
{
var fine = new FineFundamental
{
Symbol = Symbols.AAPL,
EndTime = DateTime.Now,
Value = 267.18m,
};
fine.EarningReports.BasicAverageShares = null;

Assert.AreEqual(267.18m, fine.Price);
Assert.IsNull(fine.EarningReports.BasicAverageShares);
Assert.AreEqual(0, fine.MarketCap);
}

[Test]
public void ZeroMarketCapForDefaultObject()
{
var fine = new FineFundamental();
fine.EarningReports.BasicAverageShares = null;

Assert.AreEqual(0, fine.Price);
Assert.IsNull(fine.EarningReports.BasicAverageShares);
Assert.AreEqual(0, fine.MarketCap);
}
}
}
1 change: 1 addition & 0 deletions Tests/QuantConnect.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
<Compile Include="Brokerages\Alpaca\AlpacaBrokerageTests.cs" />
<Compile Include="Common\Data\Auxiliary\FactorFileTests.cs" />
<Compile Include="Common\Data\Auxiliary\MapFileTests.cs" />
<Compile Include="Common\Data\Fundamental\FineFundamentalTests.cs" />
<Compile Include="Common\Storage\LocalObjectStoreTests.cs" />
<Compile Include="Common\Data\BaseDataTests.cs" />
<Compile Include="Common\Data\CalendarConsolidatorsTests.cs" />
Expand Down

0 comments on commit 14d0b47

Please sign in to comment.