00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 using System.Globalization; 00006 using System.IO; 00007 00008 namespace GEP.Core.Float.TimeSeries 00009 { 00056 [Serializable] 00057 public class NormalTimeSeries : ITimeSeries 00058 { 00062 public int _dateTimeColIndex; 00066 public int _dataColIndex; 00070 public string[] _colDelimiters; 00074 public int _dataStartRow; 00078 public int _dataEndRow; 00082 public DateTimeFormatInfo _dateTimeFormats; 00086 public string[] _dateTimeFormatsArr; 00091 public CultureInfo _cultureInfo; 00095 public DateTime _from; 00099 public DateTime _to; 00104 public float _devider; 00115 public string _timePeriod; 00122 public int _delay; 00129 public int _n_TimeFramesToSkip = -1; 00130 00131 public NormalTimeSeries() { } 00132 00133 public float[] ReadFile(string filePath, int maxLookBehind) 00134 { 00135 CultureInfo replacedCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; 00136 FileStream readStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 00137 StreamReader streamReader = new StreamReader(readStream); 00138 SortedDictionary<DateTime, float> list = new SortedDictionary<DateTime, float>(); 00139 int rowIndex = 0; 00140 float parsed; 00141 string line; 00142 string[] row; 00143 line = streamReader.ReadLine(); 00144 row = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 00145 _dataStartRow = int.Parse(row[0]); 00146 _dataEndRow = int.Parse(row[1]); 00147 _dateTimeColIndex = int.Parse(row[2]); 00148 _dataColIndex = int.Parse(row[3]); 00149 _devider = float.Parse(row[4]); 00150 _delay = int.Parse(row[5]); 00151 _timePeriod = streamReader.ReadLine(); 00152 _cultureInfo = new CultureInfo(streamReader.ReadLine()); 00153 System.Threading.Thread.CurrentThread.CurrentCulture = _cultureInfo; 00154 string emptyStr = ""; 00155 List<string> dateTimePatternsL = new List<string>(); 00156 while ((line = streamReader.ReadLine()) != emptyStr) 00157 { 00158 dateTimePatternsL.Add(line); 00159 } 00160 _dateTimeFormatsArr = dateTimePatternsL.ToArray(); 00161 _dateTimeFormats = new DateTimeFormatInfo(); 00162 _dateTimeFormats.SetAllDateTimePatterns(_dateTimeFormatsArr, 'd'); 00163 List<string> colDelimitersL = new List<string>(); 00164 while ((line = streamReader.ReadLine()) != emptyStr) 00165 { 00166 colDelimitersL.Add(line); 00167 } 00168 _colDelimiters = colDelimitersL.ToArray(); 00169 _delay = -_delay; 00170 maxLookBehind = -maxLookBehind; 00171 DateTime from = _from; 00172 DateTime to = _to; 00173 switch (_timePeriod) 00174 { 00175 case "M1": 00176 from = from.AddMinutes(_delay + maxLookBehind); 00177 to = to.AddMinutes(_delay); 00178 break; 00179 case "M5": 00180 from = from.AddMinutes((_delay + maxLookBehind) * 5); 00181 to = to.AddMinutes(_delay * 5); 00182 break; 00183 case "M15": 00184 from = from.AddMinutes((_delay + maxLookBehind) * 15); 00185 to = to.AddMinutes(_delay * 15); 00186 break; 00187 case "M30": 00188 from = from.AddMinutes((_delay + maxLookBehind) * 30); 00189 to = to.AddMinutes(_delay * 30); 00190 break; 00191 case "H1": 00192 from = from.AddHours(_delay + maxLookBehind); 00193 to = to.AddHours(_delay); 00194 break; 00195 case "H4": 00196 from = from.AddHours((_delay + maxLookBehind) * 4); 00197 to = to.AddHours(_delay * 4); 00198 break; 00199 case "D1": 00200 from = from.AddDays(_delay + maxLookBehind); 00201 to = to.AddDays(_delay); 00202 break; 00203 case "W1": 00204 from = from.AddDays((_delay + maxLookBehind) * 7); 00205 to = to.AddDays(_delay); 00206 break; 00207 case "MN": 00208 from = from.AddMonths(_delay + maxLookBehind); 00209 to = to.AddMonths(_delay); 00210 break; 00211 case "Q1": 00212 from = from.AddMonths((_delay + maxLookBehind) * 3); 00213 to = to.AddMonths(_delay * 3); 00214 break; 00215 case "Y1": 00216 from = from.AddYears(_delay + maxLookBehind); 00217 to = to.AddYears(_delay); 00218 break; 00219 default: 00220 from = from.AddMonths(_delay + maxLookBehind); 00221 to = to.AddMonths(_delay); 00222 break; 00223 } 00224 DateTime previousDate = new DateTime(1, from.Month + 1, 1); 00225 while (!streamReader.EndOfStream && (rowIndex <= _dataEndRow || _dataEndRow < 0)) 00226 { 00227 line = streamReader.ReadLine(); 00228 if (rowIndex >= _dataStartRow) 00229 { 00230 row = line.Split(_colDelimiters, StringSplitOptions.RemoveEmptyEntries); 00231 DateTime rowDateTime; 00232 rowDateTime = DateTime.ParseExact(row[_dateTimeColIndex], _dateTimeFormatsArr, 00233 _dateTimeFormats, DateTimeStyles.None); 00234 if (row[_dateTimeColIndex].Contains('q') || row[_dateTimeColIndex].Contains('Q')) 00235 { 00236 rowDateTime = rowDateTime.AddMonths((rowDateTime.Month - 1) * 2); 00237 } 00238 if (from <= rowDateTime && rowDateTime <= to) 00239 { 00240 if (_n_TimeFramesToSkip > -1) 00241 { 00242 switch (_timePeriod) 00243 { 00244 default: 00245 DateTime thresholdDate = 00246 new DateTime(rowDateTime.Year, rowDateTime.Month, _n_TimeFramesToSkip); 00247 if (previousDate.Month == rowDateTime.Month) 00248 continue; 00249 else if (rowDateTime < thresholdDate) 00250 continue; 00251 else 00252 previousDate = rowDateTime; 00253 break; 00254 } 00255 } 00256 parsed = float.Parse(row[_dataColIndex]); 00257 list.Add(rowDateTime, parsed); 00258 } 00259 } 00260 rowIndex++; 00261 } 00262 streamReader.Close(); 00263 float[] result = new float[list.Count]; 00264 int j = 0; 00265 foreach (KeyValuePair<DateTime, float> pair in list) 00266 { 00267 result[j++] = pair.Value / _devider; 00268 } 00269 System.Threading.Thread.CurrentThread.CurrentCulture = replacedCultureInfo; 00270 return result; 00271 } 00272 00273 public float[][] ReadFile(string filePath, int[] lookBehinds, int frequency) 00274 { 00275 int maxLookBehind = 0; 00276 for (int i = 0; i < lookBehinds.Length; i++) 00277 { 00278 if (maxLookBehind < lookBehinds[i]) 00279 maxLookBehind = lookBehinds[i]; 00280 } 00281 00282 float[] all = ReadFile(filePath, maxLookBehind); 00283 float[][] result = new float[lookBehinds.Length][]; 00284 int timeSeriesLength = (all.Length - maxLookBehind) * frequency; 00285 for (int i = 0; i < result.Length; i++) 00286 { 00287 result[i] = new float[timeSeriesLength]; 00288 for (int k = maxLookBehind - lookBehinds[i], m = 0; m < timeSeriesLength; k++, m++) 00289 { 00290 for (int l = 0; l < frequency; l++, m++) 00291 { 00292 result[i][m] = all[k]; 00293 } 00294 m--; 00295 } 00296 } 00297 return result; 00298 } 00299 00300 public void ReadFileAndAddToAlphabet(Alphabet variablesSet, ref int variablesSetStartIndex, 00301 string filePath, int[] variableDisplayIndexs, string variableName, int[] lookBehinds, int frequency) 00302 { 00303 float[][] varVals = ReadFile(filePath, lookBehinds, frequency); 00304 for (int i = 0; i < varVals.Length; i++) 00305 { 00306 variablesSet[variablesSetStartIndex++] = 00307 new Variable(varVals[i], variableName + " " + variableDisplayIndexs[i]); 00308 } 00309 } 00310 00311 public void ReadFileAndAddToAlphabet(Alphabet variablesSet, ref int variablesSetIndex, 00312 string filePath, int variableDisplayIndex, string variableName) 00313 { 00314 float[] varVals = ReadFile(filePath, 0); 00315 variablesSet[variablesSetIndex++] = new Variable(varVals, variableName + " " + variableDisplayIndex); 00316 } 00317 } 00318 }