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
+
1
16
using System ;
2
17
using System . Collections . Generic ;
3
- using System . Globalization ;
4
18
using System . Linq ;
5
- using System . Threading . Tasks ;
6
19
using QuantConnect . Data ;
7
- using QuantConnect . Data . UniverseSelection ;
8
20
using QuantConnect . Interfaces ;
9
21
10
22
namespace QuantConnect . Algorithm . CSharp
@@ -16,11 +28,12 @@ public class PersistentCustomDataUniverseRegressionAlgorithm : QCAlgorithm, IReg
16
28
17
29
public override void Initialize ( )
18
30
{
19
- SetStartDate ( 2018 , 1 , 3 ) ;
20
- SetEndDate ( 2018 , 1 , 20 ) ;
31
+ SetStartDate ( 2018 , 6 , 1 ) ;
32
+ SetEndDate ( 2018 , 6 , 19 ) ;
21
33
22
34
var universe = AddUniverse < StockDataSource > ( "my-stock-data-source" , Resolution . Daily , UniverseSelector ) ;
23
35
_universeSymbol = universe . Symbol ;
36
+ var history = History ( _universeSymbol , new DateTime ( 2018 , 1 , 1 ) , new DateTime ( 2018 , 6 , 1 ) , Resolution . Daily ) . ToList ( ) ;
24
37
}
25
38
26
39
private IEnumerable < Symbol > UniverseSelector ( IEnumerable < BaseData > data )
@@ -48,53 +61,57 @@ public override void OnEndOfAlgorithm()
48
61
}
49
62
}
50
63
64
+
51
65
/// <summary>
52
66
/// Our custom data type that defines where to get and how to read our backtest and live data.
53
67
/// </summary>
54
- class StockDataSource : BaseData
68
+ private class StockDataSource : BaseData
55
69
{
56
- private const string Url = @"https://www.dropbox.com/s/ae1couew5ir3z9y/daily-stock-picker-backtest.csv?dl=1" ;
57
-
58
70
public List < string > Symbols { get ; set ; }
59
71
60
72
public StockDataSource ( )
61
73
{
62
74
Symbols = new List < string > ( ) ;
63
75
}
64
76
77
+ public override DateTime EndTime
78
+ {
79
+ get { return Time + Period ; }
80
+ set { Time = value - Period ; }
81
+ }
82
+
83
+ public TimeSpan Period
84
+ {
85
+ get { return QuantConnect . Time . OneDay ; }
86
+ }
87
+
65
88
public override SubscriptionDataSource GetSource ( SubscriptionDataConfig config , DateTime date , bool isLiveMode )
66
89
{
67
- return new SubscriptionDataSource ( Url , SubscriptionTransportMedium . RemoteFile ) ;
90
+ string source = @"..\..\..\Tests\TestData\daily-stock-picker-backtest.csv" ;
91
+ return new SubscriptionDataSource ( source , SubscriptionTransportMedium . LocalFile , FileFormat . Csv ) ;
68
92
}
69
93
70
94
public override BaseData Reader ( SubscriptionDataConfig config , string line , DateTime date , bool isLiveMode )
71
95
{
96
+ if ( string . IsNullOrWhiteSpace ( line ) || ! char . IsDigit ( line [ 0 ] ) )
97
+ {
98
+ return null ;
99
+ }
100
+
101
+ var stocks = new StockDataSource { Symbol = config . Symbol } ;
102
+
72
103
try
73
104
{
74
- // create a new StockDataSource and set the symbol using config.Symbol
75
- var stocks = new StockDataSource { Symbol = config . Symbol } ;
76
- // break our line into csv pieces
77
105
var csv = line . ToCsv ( ) ;
78
- if ( isLiveMode )
79
- {
80
- // our live mode format does not have a date in the first column, so use date parameter
81
- stocks . Time = date ;
82
- stocks . Symbols . AddRange ( csv ) ;
83
- }
84
- else
85
- {
86
- // our backtest mode format has the first column as date, parse it
87
- stocks . Time = DateTime . ParseExact ( csv [ 0 ] , "yyyyMMdd" , null ) ;
88
- // any following comma separated values are symbols, save them off
89
- stocks . Symbols . AddRange ( csv . Skip ( 1 ) ) ;
90
- }
91
- return stocks ;
106
+ stocks . Time = DateTime . ParseExact ( csv [ 0 ] , "yyyyMMdd" , null ) ;
107
+ stocks . Symbols . AddRange ( csv [ 1 ..] ) ;
92
108
}
93
- // return null if we encounter any errors
94
- catch
109
+ catch ( FormatException )
95
110
{
96
111
return null ;
97
112
}
113
+
114
+ return stocks ;
98
115
}
99
116
}
100
117
@@ -111,12 +128,12 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
111
128
/// <summary>
112
129
/// Data Points count of all timeslices of algorithm
113
130
/// </summary>
114
- public long DataPoints => 26024 ;
131
+ public long DataPoints => 8767 ;
115
132
116
133
/// <summary>
117
134
/// Data Points count of the algorithm history
118
135
/// </summary>
119
- public int AlgorithmHistoryDataPoints => 0 ;
136
+ public int AlgorithmHistoryDataPoints => 149 ;
120
137
121
138
/// <summary>
122
139
/// Final status of the algorithm
@@ -147,8 +164,8 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
147
164
{ "Beta" , "0" } ,
148
165
{ "Annual Standard Deviation" , "0" } ,
149
166
{ "Annual Variance" , "0" } ,
150
- { "Information Ratio" , "-12.133 " } ,
151
- { "Tracking Error" , "0.059 " } ,
167
+ { "Information Ratio" , "-3.9 " } ,
168
+ { "Tracking Error" , "0.045 " } ,
152
169
{ "Treynor Ratio" , "0" } ,
153
170
{ "Total Fees" , "$0.00" } ,
154
171
{ "Estimated Strategy Capacity" , "$0" } ,
0 commit comments