/*
* 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 Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Data.Custom.SEC;
using QuantConnect.Interfaces;
namespace QuantConnect.Algorithm.CSharp
{
///
/// Regression algorithm demonstrating use of map files with custom data
///
///
///
///
///
///
///
///
///
public class CustomDataUsingMapFileRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private Symbol _symbol;
private bool _changedSymbol;
private Dictionary _tickers = new Dictionary();
///
/// Ticker we use for testing
///
public const string Ticker = "TWX";
///
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
///
public override void Initialize()
{
SetStartDate(2001, 1, 1);
SetEndDate(2003, 12, 31);
SetCash(100000);
// AOL renames to TWX in 2003
_symbol = AddData(Ticker, Resolution.Daily).Symbol;
AddEquity(Ticker, Resolution.Daily);
}
///
/// Checks to see if the stock has been renamed, and places an order once the symbol has changed
///
///
public override void OnData(Slice slice)
{
if (slice.SymbolChangedEvents.ContainsKey(_symbol))
{
// Check to see if it was renamed on the 16th
_changedSymbol = Time.Date == new DateTime(2003, 10, 16);
Log($"{Time} - Ticker changed from: {slice.SymbolChangedEvents[_symbol].OldSymbol} to {slice.SymbolChangedEvents[_symbol].NewSymbol}");
}
foreach (var report in slice.Get())
{
var ticker = report.Key.Value;
var date = Time.Date;
if (date == new DateTime(2001, 1, 26) || date == new DateTime(2003, 10, 22))
{
_tickers[date] = ticker;
}
Log($"{Time} - Received 8-K report for {ticker}");
}
}
///
/// Final step of the algorithm
///
public override void OnEndOfAlgorithm()
{
if (!_changedSymbol)
{
throw new Exception("The ticker did not rename throughout the course of its life even though it should have");
}
var expectedTickers = new Dictionary
{
{ new DateTime(2001, 1, 26), "AOL" },
{ new DateTime(2003, 10, 22), "TWX" },
};
// Check for dictionary equality: https://stackoverflow.com/a/3804852
if (_tickers.Count != expectedTickers.Count && _tickers.Except(expectedTickers).Any())
{
Log($"Found: {JsonConvert.SerializeObject(_tickers, Formatting.None)}");
Log($"Expected: {JsonConvert.SerializeObject(expectedTickers, Formatting.None)}");
throw new Exception("SEC data event tickers do not match test case");
}
}
///
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
///
public bool CanRunLocally { get; } = false;
///
/// This is used by the regression test system to indicate which languages this algorithm is written in.
///
public Language[] Languages { get; } = { Language.CSharp, Language.Python };
///
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
///
public Dictionary ExpectedStatistics => new Dictionary
{
{"Total Trades", "0"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "0"},
{"Tracking Error", "0"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
};
}
}