|
| 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 QuantConnect.Logging; |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.IO; |
| 20 | +using System.Linq; |
| 21 | +using QuantConnect.DataLibrary; |
| 22 | + |
| 23 | +namespace QuantConnect.DataProcessing |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Data downloader/converter class example |
| 27 | + /// </summary> |
| 28 | + public class VendorDataDownloaderConverter : IDisposable |
| 29 | + { |
| 30 | + /// <summary> |
| 31 | + /// Name of the vendor. Only alphanumeric characters, all lowercase and no underscores/spaces. |
| 32 | + /// </summary> |
| 33 | + public const string VendorName = "gaussgodel"; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Type of data that we are downloading/converting. Only alphanumeric characters, all lowercase and no underscores/spaces. |
| 37 | + /// </summary> |
| 38 | + public const string VendorDataName = "flights"; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// API key to download data with |
| 42 | + /// </summary> |
| 43 | + private readonly string _apiKey; |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Directory that data will be outputted to |
| 47 | + /// </summary> |
| 48 | + private readonly string _destinationDirectory; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Has the class been cleaned up yet |
| 52 | + /// </summary> |
| 53 | + private bool _disposed; |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Creates a new instance of the data downloader/converter |
| 57 | + /// </summary> |
| 58 | + /// <param name="destinationDataFolder">The path to the data folder we want to save data to</param> |
| 59 | + /// <param name="apiKey">The API key to download data with</param> |
| 60 | + public VendorDataDownloaderConverter(string destinationDataFolder, string apiKey = null) |
| 61 | + { |
| 62 | + _apiKey = apiKey; |
| 63 | + _destinationDirectory = Path.Combine( |
| 64 | + destinationDataFolder, |
| 65 | + "alternative", |
| 66 | + VendorName, |
| 67 | + VendorDataName); |
| 68 | + |
| 69 | + // Create the directory ahead of time so that we don't get |
| 70 | + // errors when trying to write to this output directory. |
| 71 | + Directory.CreateDirectory(_destinationDirectory); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Begins running the downloader/converter |
| 76 | + /// </summary> |
| 77 | + /// <returns>True if downloading/converting was successful, false otherwise</returns> |
| 78 | + public bool Run() |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + // Your data downloading/processing code goes here. The lines below |
| 83 | + // can be deleted since they are only meant to be an example. |
| 84 | + // ================================================================ |
| 85 | + var underlying = Symbol.Create("SPY", SecurityType.Equity, Market.USA); |
| 86 | + var symbol = Symbol.CreateBase( |
| 87 | + typeof(MyCustomDataType), |
| 88 | + underlying, |
| 89 | + Market.USA); |
| 90 | + |
| 91 | + var lines = new[] |
| 92 | + { |
| 93 | + "20131001,buy", |
| 94 | + "20131003,buy", |
| 95 | + "20131006,buy", |
| 96 | + "20131007,sell", |
| 97 | + "20131009,buy", |
| 98 | + "20131011,sell" |
| 99 | + }; |
| 100 | + |
| 101 | + var instances = lines.Select(x => ParseLine(symbol, x)); |
| 102 | + var csvLines = instances.Select(x => ToCsvLine(x)); |
| 103 | + |
| 104 | + SaveContentsToFile(symbol, csvLines); |
| 105 | + } |
| 106 | + catch (Exception e) |
| 107 | + { |
| 108 | + Log.Error(e, $"Failed to download/convert {VendorName} {VendorDataName} data"); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Example method to parse and create an instance from a line of CSV |
| 117 | + /// </summary> |
| 118 | + /// <param name="symbol">Symbol</param> |
| 119 | + /// <param name="line">Line of raw data</param> |
| 120 | + /// <returns>Instance of <see cref="MyCustomDataType"/></returns> |
| 121 | + private MyCustomDataType ParseLine(Symbol symbol, string line) |
| 122 | + { |
| 123 | + var csv = line.Split(','); |
| 124 | + return new MyCustomDataType |
| 125 | + { |
| 126 | + Time = Parse.DateTimeExact(csv[0], "yyyyMMdd"), |
| 127 | + Symbol = symbol, |
| 128 | + |
| 129 | + SomeCustomProperty = csv[1] |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Example method to convert an instance to a CSV line |
| 135 | + /// </summary> |
| 136 | + /// <param name="instance">Custom Data instance</param> |
| 137 | + /// <returns>CSV line</returns> |
| 138 | + private string ToCsvLine(MyCustomDataType instance) |
| 139 | + { |
| 140 | + return string.Join(",", |
| 141 | + $"{instance.Time:yyyyMMdd}", |
| 142 | + instance.SomeCustomProperty); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Example method to save CSV lines to disk |
| 147 | + /// </summary> |
| 148 | + /// <param name="symbol">Symbol of the data</param> |
| 149 | + /// <param name="csvLines">CSV lines to write</param> |
| 150 | + private void SaveContentsToFile(Symbol symbol, IEnumerable<string> csvLines) |
| 151 | + { |
| 152 | + var ticker = symbol.Value.ToLowerInvariant(); |
| 153 | + |
| 154 | + var tempPath = new FileInfo(Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}-{ticker}.csv")); |
| 155 | + var finalPath = Path.Combine(_destinationDirectory, $"{ticker}.csv"); |
| 156 | + |
| 157 | + File.WriteAllLines(tempPath.FullName, csvLines); |
| 158 | + tempPath.MoveTo(finalPath, true); |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// If you need to shut down things like database connections, threads, or other |
| 163 | + /// resources that require manual shutdown, do it here. This will be called after |
| 164 | + /// we're done with the <see cref="Run"/> method. |
| 165 | + /// |
| 166 | + /// You don't have to implement this if you don't need to cleanup resources after we're done downloading/processing. |
| 167 | + /// </summary> |
| 168 | + public void Dispose() |
| 169 | + { |
| 170 | + if (_disposed) |
| 171 | + { |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + // Your cleanup goes here. You don't have to implement it |
| 176 | + // if you have nothing that needs to be cleaned up. |
| 177 | + _disposed = true; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments