-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQuiverWikipedia.cs
159 lines (143 loc) · 5.94 KB
/
QuiverWikipedia.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* 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.IO;
using Newtonsoft.Json;
using NodaTime;
using QuantConnect.Data;
using QuantConnect.Util;
using static QuantConnect.StringExtensions;
namespace QuantConnect.DataSource
{
/// <summary>
/// Wikipedia Page Views for the specified company
/// </summary>
public class QuiverWikipedia : BaseData
{
private static readonly TimeSpan _period = TimeSpan.FromDays(1);
/// <summary>
/// Data source ID
/// </summary>
public static int DataSourceId { get; } = 2017;
/// <summary>
/// The date of the Page View count
/// </summary>
[JsonProperty(PropertyName = "Date")]
[JsonConverter(typeof(DateTimeJsonConverter), "yyyy-MM-dd")]
public DateTime Date { get; set; }
/// <summary>
/// The company's Wikipedia Page Views on the given date
/// </summary>
[JsonProperty(PropertyName = "Views")]
public decimal? PageViews { get; set; }
/// <summary>
/// The view count % change over the week prior to the date.
/// Represented as a whole number (e.g. 100% = 100.0)
/// </summary>
[JsonProperty(PropertyName = "pct_change_week")]
public decimal? WeekPercentChange { get; set; }
/// <summary>
/// The view count % change over the month prior to the date
/// Represented as a whole number (e.g. 100% = 100.0)
/// </summary>
[JsonProperty(PropertyName = "pct_change_month")]
public decimal? MonthPercentChange { get; set; }
/// <summary>
/// The time the data point ends at and becomes available to the algorithm
/// </summary>
public override DateTime EndTime => Time + _period;
/// <summary>
/// Required for successful Json.NET deserialization
/// </summary>
public QuiverWikipedia()
{
}
/// <summary>
/// Creates a new instance of QuiverWikipedia from a CSV line
/// </summary>
/// <param name="csvLine">CSV line</param>
public QuiverWikipedia(string csvLine)
{
// Date[0], Ticker[1], PageViews[2], WeekPercentChange[3], MonthPercentChange[4]
var csv = csvLine.Split(',');
Date = Parse.DateTimeExact(csv[0], "yyyyMMdd");
PageViews = csv[1].IfNotNullOrEmpty<decimal?>(s => Parse.Decimal(s));
WeekPercentChange = csv[2].IfNotNullOrEmpty<decimal?>(s => Parse.Decimal(s));
MonthPercentChange = csv[3].IfNotNullOrEmpty<decimal?>(s => Parse.Decimal(s));
Time = Date;
}
/// <summary>
/// Return the Subscription Data Source gained from the URL
/// </summary>
/// <param name="config">Configuration object</param>
/// <param name="date">Date of this source file</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>Subscription Data Source.</returns>
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
{
var source = Path.Combine(
Globals.DataFolder,
"alternative",
"quiver",
"wikipedia",
$"{config.Symbol.Value.ToLowerInvariant()}.csv"
);
return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
}
/// <summary>
/// Reader converts each line of the data source into BaseData objects.
/// </summary>
/// <param name="config">Subscription data config setup object</param>
/// <param name="line">Content of the source document</param>
/// <param name="date">Date of the requested data</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>
/// Quiver Wikipedia object
/// </returns>
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
{
return new QuiverWikipedia(line)
{
Symbol = config.Symbol
};
}
/// <summary>
/// Formats a string with the Quiver Wikipedia information.
/// </summary>
public override string ToString()
{
return Invariant($"{Symbol}({Date}) :: ") +
Invariant($"Page Views: {PageViews} ") +
Invariant($"Percentage Change Week: {WeekPercentChange} ") +
Invariant($"Percentage Change Month: {MonthPercentChange}");
}
/// <summary>
/// Indicates if there is support for mapping
/// </summary>
/// <returns>True indicates mapping should be used</returns>
public override bool RequiresMapping()
{
return true;
}
/// <summary>
/// Specifies the data time zone for this data type. This is useful for custom data types
/// </summary>
/// <returns>The <see cref="DateTimeZone"/> of this data type</returns>
public override DateTimeZone DataTimeZone()
{
return TimeZones.Utc;
}
}
}