/* * 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.IO; using System.Linq; namespace QuantConnect.Data.Auxiliary { /// /// Represents a single row in a map_file. This is a csv file ordered as {date, mapped symbol} /// public class MapFileRow : IEquatable { /// /// Gets the date associated with this data /// public DateTime Date { get; private set; } /// /// Gets the mapped symbol /// public string MappedSymbol { get; private set; } /// /// Initializes a new instance of the class. /// public MapFileRow(DateTime date, string mappedSymbol) { Date = date; MappedSymbol = mappedSymbol.LazyToUpper(); } /// /// Reads in the map_file for the specified equity symbol /// public static IEnumerable Read(string permtick, string market) { var path = MapFile.GetMapFilePath(permtick, market); return File.Exists(path) ? Read(path) : Enumerable.Empty(); } /// /// Reads in the map_file at the specified path /// public static IEnumerable Read(string path) { return File.ReadAllLines(path).Where(l => !string.IsNullOrWhiteSpace(l)).Select(Parse); } /// /// Parses the specified line into a MapFileRow /// public static MapFileRow Parse(string line) { var csv = line.Split(','); return new MapFileRow(DateTime.ParseExact(csv[0], DateFormat.EightCharacter, null), csv[1]); } #region Equality members /// /// Indicates whether the current object is equal to another object of the same type. /// /// /// true if the current object is equal to the parameter; otherwise, false. /// /// An object to compare with this object. public bool Equals(MapFileRow other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Date.Equals(other.Date) && string.Equals(MappedSymbol, other.MappedSymbol); } /// /// Determines whether the specified is equal to the current . /// /// /// true if the specified object is equal to the current object; otherwise, false. /// /// The object to compare with the current object. 2 public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != this.GetType()) return false; return Equals((MapFileRow)obj); } /// /// Serves as a hash function for a particular type. /// /// /// A hash code for the current . /// /// 2 public override int GetHashCode() { unchecked { return (Date.GetHashCode() * 397) ^ (MappedSymbol != null ? MappedSymbol.GetHashCode() : 0); } } /// /// Determines whether or not the two instances are equal /// public static bool operator ==(MapFileRow left, MapFileRow right) { return Equals(left, right); } /// /// Determines whether or not the two instances are not equal /// public static bool operator !=(MapFileRow left, MapFileRow right) { return !Equals(left, right); } #endregion /// /// Writes this row to csv format /// public string ToCsv() { return $"{Date.ToStringInvariant(DateFormat.EightCharacter)},{MappedSymbol.ToLowerInvariant()}"; } public override string ToString() { return Date.ToShortDateString() + ": " + MappedSymbol; } } }