|
| 1 | +/* |
| 2 | + * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 3 | + * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using NodaTime; |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Globalization; |
| 20 | +using System.IO; |
| 21 | + |
| 22 | +namespace QuantConnect.Data.Custom.Robintrack |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// Aggregate unique user stock holdings |
| 26 | + /// |
| 27 | + /// Data sourced from Robintrack - https://robintrack.net |
| 28 | + /// </summary> |
| 29 | + public class RobintrackHoldings : BaseData |
| 30 | + { |
| 31 | + private static List<Resolution> _supportedResolutions = new List<Resolution> |
| 32 | + { |
| 33 | + Resolution.Second, |
| 34 | + Resolution.Minute, |
| 35 | + Resolution.Hour, |
| 36 | + Resolution.Daily |
| 37 | + }; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Number of unique users holding a given stock |
| 41 | + /// </summary> |
| 42 | + public int UsersHolding { get; set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Total number of unique holdings across all stocks by users |
| 46 | + /// </summary> |
| 47 | + public decimal TotalUniqueHoldings { get; set; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Percentage of the U.S. equities universe that unique users hold stock for |
| 51 | + /// </summary> |
| 52 | + public decimal UniverseHoldingPercent => UsersHolding / TotalUniqueHoldings; |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Alias for <see cref="UsersHolding"/> |
| 56 | + /// </summary> |
| 57 | + public override decimal Value |
| 58 | + { |
| 59 | + get { return UsersHolding; } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets the source of the file |
| 64 | + /// </summary> |
| 65 | + /// <param name="config">Subscription config</param> |
| 66 | + /// <param name="date">Data for the given date to read</param> |
| 67 | + /// <param name="isLiveMode">Is live mode</param> |
| 68 | + /// <returns>Subscription data source</returns> |
| 69 | + public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode) |
| 70 | + { |
| 71 | + if (isLiveMode) |
| 72 | + { |
| 73 | + throw new NotImplementedException("Live trading currently is not implemented for this data set"); |
| 74 | + } |
| 75 | + |
| 76 | + return new SubscriptionDataSource( |
| 77 | + Path.Combine( |
| 78 | + Globals.DataFolder, |
| 79 | + "alternative", |
| 80 | + "robintrack", |
| 81 | + $"{config.Symbol.Underlying.Value.ToLowerInvariant()}.csv" |
| 82 | + ), |
| 83 | + SubscriptionTransportMedium.LocalFile, |
| 84 | + FileFormat.Csv |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Reads the data from the source provided in <see cref="GetSource(SubscriptionDataConfig, DateTime, bool)"/> |
| 90 | + /// and converts it into an instance of this class (<see cref="RobintrackHoldings"/>). |
| 91 | + /// </summary> |
| 92 | + /// <param name="config">Subscription config</param> |
| 93 | + /// <param name="line">Line to parse</param> |
| 94 | + /// <param name="date">Date that the data is being read</param> |
| 95 | + /// <param name="isLiveMode">Is live mode</param> |
| 96 | + /// <returns>Instance of <see cref="RobintrackHoldings"/> casted as <see cref="BaseData"/></returns> |
| 97 | + public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) |
| 98 | + { |
| 99 | + if (isLiveMode) |
| 100 | + { |
| 101 | + throw new NotImplementedException("Live trading currently is not implemented for this data set"); |
| 102 | + } |
| 103 | + |
| 104 | + var instance = Read(line); |
| 105 | + instance.Symbol = config.Symbol; |
| 106 | + |
| 107 | + return instance; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Creates a clone of the current object |
| 112 | + /// </summary> |
| 113 | + /// <returns>Copy of the current object</returns> |
| 114 | + public override BaseData Clone() |
| 115 | + { |
| 116 | + return new RobintrackHoldings |
| 117 | + { |
| 118 | + EndTime = EndTime, |
| 119 | + Symbol = Symbol, |
| 120 | + UsersHolding = UsersHolding, |
| 121 | + TotalUniqueHoldings = TotalUniqueHoldings, |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Parses a line of Robintrack data with an optional dateFormat |
| 127 | + /// </summary> |
| 128 | + /// <param name="line">Line to parse</param> |
| 129 | + /// <param name="dateFormat">Date format to parse timestamp in</param> |
| 130 | + /// <returns>Instance of this class</returns> |
| 131 | + public static RobintrackHoldings Read(string line, string dateFormat = "yyyyMMdd HH:mm:ss") |
| 132 | + { |
| 133 | + var i = 0; |
| 134 | + var csv = line.ToCsvData(size: 3); |
| 135 | + var timestamp = Parse.DateTimeExact(csv[i++], dateFormat, DateTimeStyles.AdjustToUniversal); |
| 136 | + var usersHolding = Parse.Int(csv[i++]); |
| 137 | + var totalUniqueHoldings = Parse.Decimal(csv[i]); |
| 138 | + |
| 139 | + return new RobintrackHoldings |
| 140 | + { |
| 141 | + EndTime = timestamp, |
| 142 | + UsersHolding = usersHolding, |
| 143 | + TotalUniqueHoldings = totalUniqueHoldings |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Sets the data timezone |
| 150 | + /// </summary> |
| 151 | + /// <returns>Timezone</returns> |
| 152 | + public override DateTimeZone DataTimeZone() |
| 153 | + { |
| 154 | + return TimeZones.Utc; |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Enables mapping of the underlying symbol |
| 159 | + /// </summary> |
| 160 | + /// <returns>true</returns> |
| 161 | + public override bool RequiresMapping() |
| 162 | + { |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Provides list of supported resolutions |
| 168 | + /// </summary> |
| 169 | + /// <returns>List of supported resolutions</returns> |
| 170 | + public override List<Resolution> SupportedResolutions() |
| 171 | + { |
| 172 | + return _supportedResolutions; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments