forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Custom Data from US Energy Information Administration (eia.gov) (Q…
…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
1 parent
6164e4c
commit 3f00762
Showing
9 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Algorithm.CSharp/USEnergyInformationAdministrationAlgorithm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Algorithm.Python/USEnergyInformationAdministrationAlgorithm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.