Skip to content

Commit

Permalink
Adds Custom Data from US Energy Information Administration (eia.gov) (Q…
Browse files Browse the repository at this point in the history
…uantConnect#3136)

New custom data class USEnergyInformation with new demonstration algorithms, the updated config file for users to set their EIA token.

Adds `CloseTime` to represent the time that the data period end. `EndTime` represents, in turn, the time the data is emitted. There is an offset between `CloseTime` and `EndTime` that is defined by the difference between the last bar as emitted and its time. 

In live mode, if the `USEnergyInformation.Reader` returns null, the `CollectionSubscriptionDataSourceReader.Read` method will pull for new data constantly. Therefore it should return an empty `BaseDataCollection` object.
  • Loading branch information
AlexCatarino authored and jaredbroad committed May 1, 2019
1 parent 6164e4c commit 3f00762
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 0 deletions.
1 change: 1 addition & 0 deletions Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
<Compile Include="UniverseSelectionDefinitionsAlgorithm.cs" />
<Compile Include="UniverseSharingSecurityDifferentSubscriptionRequestRegressionAlgorithm.cs" />
<Compile Include="UniverseSharingSubscriptionRequestRegressionAlgorithm.cs" />
<Compile Include="USEnergyInformationAdministrationAlgorithm.cs" />
<Compile Include="UserDefinedUniverseAlgorithm.cs" />
<Compile Include="VolumeWeightedAveragePriceExecutionModelRegressionAlgorithm.cs" />
<Compile Include="WarmupAlgorithm.cs" />
Expand Down
92 changes: 92 additions & 0 deletions Algorithm.CSharp/USEnergyInformationAdministrationAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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;
using QuantConnect.Data.Custom;
using QuantConnect.Data.Custom.Tiingo;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// This example algorithm shows how to import and use Tiingo daily prices data.
/// </summary>
/// <meta name="tag" content="strategy example" />
/// <meta name="tag" content="using data" />
/// <meta name="tag" content="custom data" />
/// <meta name="tag" content="tiingo" />
public class USEnergyInformationAdministrationAlgorithm : QCAlgorithm
{
private const string tiingoTicker = "AAPL";
private const string energyTicker = "NUC_STATUS.OUT.US.D"; // US nuclear capacity outage (Daily)
private Symbol _tiingoSymbol;
private Symbol _energySymbol;

private ExponentialMovingAverage _emaFast;
private ExponentialMovingAverage _emaSlow;

/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2017, 1, 1);
SetEndDate(2017, 12, 31);
SetCash(100000);

// Set your Tiingo API Token here
Tiingo.SetAuthCode("my-tiingo-api-token");
// Set your US Energy Information Administration (EIA) Token here
USEnergyInformation.SetAuthCode("my-us-energy-information-api-token");

_tiingoSymbol = AddData<TiingoDailyData>(tiingoTicker, Resolution.Daily).Symbol;
_energySymbol = AddData<USEnergyInformation>(energyTicker).Symbol;

_emaFast = EMA(_tiingoSymbol, 5);
_emaSlow = EMA(_tiingoSymbol, 10);
}

/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="slice">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice slice)
{
// Extract Tiingo data from the slice
var tiingoData = slice.Get<TiingoDailyData>();
foreach (var row in tiingoData.Values)
{
Log($"{Time} - {row.Symbol.Value} - {row.Close} {row.Value} {row.Price} - EmaFast:{_emaFast} - EmaSlow:{_emaSlow}");
}

// Extract US EIA data from the slice
var energyData = slice.Get<USEnergyInformation>();
foreach (var row in energyData.Values)
{
Log($"{Time} - {row.Symbol.Value} - {row.Value} ");
}

// Simple EMA cross
if (!Portfolio.Invested && _emaFast > _emaSlow)
{
SetHoldings(_tiingoSymbol, 1);
}
else if (Portfolio.Invested && _emaFast < _emaSlow)
{
Liquidate(_tiingoSymbol);
}
}
}
}
1 change: 1 addition & 0 deletions Algorithm.Python/QuantConnect.Algorithm.Python.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Content Include="Alphas\TripleLeverageETFPairVolatilityDecayAlpha.py" />
<Content Include="Alphas\VIXDualThrustAlpha.py" />
<Content Include="BasicSetAccountCurrencyAlgorithm.py" />
<Content Include="USEnergyInformationAdministrationAlgorithm.py" />
<None Include="RawPricesUniverseRegressionAlgorithm.py" />
<None Include="CapmAlphaRankingFrameworkAlgorithm.py" />
<None Include="SmaCrossUniverseSelectionAlgorithm.py" />
Expand Down
73 changes: 73 additions & 0 deletions Algorithm.Python/USEnergyInformationAdministrationAlgorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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.

from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data.Custom import USEnergyInformation
from QuantConnect.Data.Custom.Tiingo import *

### <summary>
### This example algorithm shows how to import and use Tiingo daily prices data.
### </summary>
### <meta name="tag" content="strategy example" />
### <meta name="tag" content="using data" />
### <meta name="tag" content="custom data" />
### <meta name="tag" content="tiingo" />
class USEnergyInformationAdministrationAlgorithm(QCAlgorithm):

def Initialize(self):
# Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
self.SetStartDate(2017, 1, 1)
self.SetEndDate(2017, 12, 31)
self.SetCash(100000)

# Set your Tiingo API Token here
Tiingo.SetAuthCode("my-tiingo-api-token")
# Set your US Energy Information Administration (EIA) API Token here
USEnergyInformation.SetAuthCode("my-us-energy-information-api-token")


self.tiingoTicker = "AAPL"
self.energyTicker = "NUC_STATUS.OUT.US.D"
self.tiingoSymbol = self.AddData(TiingoDailyData, self.tiingoTicker, Resolution.Daily).Symbol
self.energySymbol = self.AddData(USEnergyInformation, self.energyTicker).Symbol


self.emaFast = self.EMA(self.tiingoSymbol, 5)
self.emaSlow = self.EMA(self.tiingoSymbol, 10)


def OnData(self, slice):
# OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

if (not slice.ContainsKey(self.tiingoTicker)) or (not slice.ContainsKey(self.energyTicker)): return

# Extract Tiingo data from the slice
tiingoRow = slice[self.tiingoTicker]
energyRow = slice[self.energyTicker]

self.Log(f"{self.Time} - {tiingoRow.Symbol.Value} - {tiingoRow.Close} {tiingoRow.Value} {tiingoRow.Price} - EmaFast:{self.emaFast} - EmaSlow:{self.emaSlow}")
self.Log(f"{self.Time} - {energyRow.Symbol.Value} - {energyRow.Value}")

# Simple EMA cross
if not self.Portfolio.Invested and self.emaFast > self.emaSlow:
self.SetHoldings(self.tiingoSymbol, 1)

elif self.Portfolio.Invested and self.emaFast < self.emaSlow:
self.Liquidate(self.tiingoSymbol)
Loading

0 comments on commit 3f00762

Please sign in to comment.