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 { 00010 [Serializable] 00011 public class InterestRateTimeSeries : ITimeSeries 00012 { 00013 public int _dateTimeColIndex; 00014 public int _dataColIndex; 00015 public string[] _colDelimiters; 00016 public int _dataStartRow; 00017 public int _dataEndRow; 00018 public DateTimeFormatInfo _dateTimeFormats; 00019 public string[] _dateTimeFormatsArr; 00020 public CultureInfo _cultureInfo; 00021 public DateTime _from; 00022 public DateTime _to; 00023 public float _devider; 00024 public string _timePeriod; 00025 public int _delay; 00026 00027 public InterestRateTimeSeries() { } 00028 00029 public float[] ReadFile(string filePath, int maxLookBehind) 00030 { 00031 CultureInfo replacedCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; 00032 FileStream readStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 00033 StreamReader streamReader = new StreamReader(readStream); 00034 SortedDictionary<DateTime, float> list = new SortedDictionary<DateTime, float>(); 00035 int rowIndex = 0; 00036 float parsed; 00037 string line; 00038 string[] row; 00039 line = streamReader.ReadLine(); 00040 row = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 00041 _dataStartRow = int.Parse(row[0]); 00042 _dataEndRow = int.Parse(row[1]); 00043 _dateTimeColIndex = int.Parse(row[2]); 00044 _dataColIndex = int.Parse(row[3]); 00045 _devider = float.Parse(row[4]); 00046 _delay = int.Parse(row[5]); 00047 _timePeriod = streamReader.ReadLine(); 00048 _cultureInfo = new CultureInfo(streamReader.ReadLine()); 00049 System.Threading.Thread.CurrentThread.CurrentCulture = _cultureInfo; 00050 string emptyStr = ""; 00051 List<string> dateTimePatternsL = new List<string>(); 00052 while ((line = streamReader.ReadLine()) != emptyStr) 00053 { 00054 dateTimePatternsL.Add(line); 00055 } 00056 _dateTimeFormatsArr = dateTimePatternsL.ToArray(); 00057 _dateTimeFormats = new DateTimeFormatInfo(); 00058 _dateTimeFormats.SetAllDateTimePatterns(_dateTimeFormatsArr, 'd'); 00059 List<string> colDelimitersL = new List<string>(); 00060 while ((line = streamReader.ReadLine()) != emptyStr) 00061 { 00062 colDelimitersL.Add(line); 00063 } 00064 _colDelimiters = colDelimitersL.ToArray(); 00065 _delay = -_delay; 00066 maxLookBehind = -maxLookBehind; 00067 DateTime from = _from; 00068 DateTime to = _to; 00069 switch (_timePeriod) 00070 { 00071 case "M1": 00072 from = from.AddMinutes(_delay + maxLookBehind); 00073 to = to.AddMinutes(_delay); 00074 break; 00075 case "M5": 00076 from = from.AddMinutes((_delay + maxLookBehind) * 5); 00077 to = to.AddMinutes(_delay * 5); 00078 break; 00079 case "M15": 00080 from = from.AddMinutes((_delay + maxLookBehind) * 15); 00081 to = to.AddMinutes(_delay * 15); 00082 break; 00083 case "M30": 00084 from = from.AddMinutes((_delay + maxLookBehind) * 30); 00085 to = to.AddMinutes(_delay * 30); 00086 break; 00087 case "H1": 00088 from = from.AddHours(_delay + maxLookBehind); 00089 to = to.AddHours(_delay); 00090 break; 00091 case "H4": 00092 from = from.AddHours((_delay + maxLookBehind) * 4); 00093 to = to.AddHours(_delay * 4); 00094 break; 00095 case "D1": 00096 from = from.AddDays(_delay + maxLookBehind); 00097 to = to.AddDays(_delay); 00098 break; 00099 case "W1": 00100 from = from.AddDays((_delay + maxLookBehind) * 7); 00101 to = to.AddDays(_delay); 00102 break; 00103 case "MN": 00104 from = from.AddMonths(_delay + maxLookBehind); 00105 to = to.AddMonths(_delay); 00106 break; 00107 case "Q1": 00108 from = from.AddMonths((_delay + maxLookBehind) * 3); 00109 to = to.AddMonths(_delay * 3); 00110 break; 00111 case "Y1": 00112 from = from.AddYears(_delay + maxLookBehind); 00113 to = to.AddYears(_delay); 00114 break; 00115 default: 00116 from = from.AddMonths(_delay + maxLookBehind); 00117 to = to.AddMonths(_delay); 00118 break; 00119 } 00120 DateTime closestToFrom = new DateTime(0); 00121 float closestToFromValue = 0; 00122 DateTime closestToTo = new DateTime(0); 00123 float closestToToValue = 0; 00124 while (!streamReader.EndOfStream && (rowIndex <= _dataEndRow || _dataEndRow < 0)) 00125 { 00126 line = streamReader.ReadLine(); 00127 if (rowIndex >= _dataStartRow) 00128 { 00129 row = line.Split(_colDelimiters, StringSplitOptions.RemoveEmptyEntries); 00130 DateTime rowDateTime; 00131 rowDateTime = DateTime.ParseExact(row[_dateTimeColIndex], _dateTimeFormatsArr, 00132 _dateTimeFormats, DateTimeStyles.None); 00133 if (row[_dateTimeColIndex].Contains('q') || row[_dateTimeColIndex].Contains('Q')) 00134 { 00135 rowDateTime = rowDateTime.AddMonths((rowDateTime.Month - 1) * 2); 00136 } 00137 if (from <= rowDateTime && rowDateTime <= to) 00138 { 00139 parsed = float.Parse(row[_dataColIndex]); 00140 list.Add(rowDateTime, parsed); 00141 } 00142 else 00143 { 00144 parsed = float.Parse(row[_dataColIndex]); 00145 if (Math.Abs(from.Ticks - rowDateTime.Ticks) < Math.Abs(from.Ticks - closestToFrom.Ticks)) 00146 { 00147 closestToFrom = rowDateTime; 00148 closestToFromValue = parsed; 00149 } 00150 if (Math.Abs(rowDateTime.Ticks - to.Ticks) < Math.Abs(closestToTo.Ticks - to.Ticks)) 00151 { 00152 closestToTo = rowDateTime; 00153 closestToToValue = parsed; 00154 } 00155 } 00156 } 00157 rowIndex++; 00158 } 00159 list.Add(closestToTo, closestToToValue); 00160 list.Add(closestToFrom, closestToFromValue); 00161 streamReader.Close(); 00162 List<float> result = new List<float>(); 00163 KeyValuePair<DateTime, float>? previousPair = null; 00164 if (_timePeriod == "MN") 00165 { 00166 SortedDictionary<DateTime, float> rates = new SortedDictionary<DateTime, float>(); 00167 DateTime monthStart = (from == from.Date ? from : from.Date.AddMonths(1)); 00168 DateTime monthEnd = monthStart.AddMonths(1).AddTicks(-1); 00169 long ticksInMonth; 00170 float monthRate; 00171 do 00172 { 00173 closestToFrom = new DateTime(0); 00174 closestToFromValue = 0; 00175 closestToTo = new DateTime(0); 00176 closestToToValue = 0; 00177 foreach (KeyValuePair<DateTime, float> pair in list) 00178 { 00179 if (monthStart <= pair.Key && pair.Key <= monthEnd) 00180 { 00181 rates.Add(pair.Key, pair.Value); 00182 } 00183 else 00184 { 00185 if (pair.Key < monthStart && 00186 Math.Abs(monthStart.Ticks - pair.Key.Ticks) < 00187 Math.Abs(monthStart.Ticks - closestToFrom.Ticks)) 00188 { 00189 closestToFrom = pair.Key; 00190 closestToFromValue = pair.Value; 00191 } 00192 if (monthEnd < pair.Key && 00193 Math.Abs(pair.Key.Ticks - monthEnd.Ticks) < 00194 Math.Abs(closestToTo.Ticks - monthEnd.Ticks)) 00195 { 00196 closestToTo = pair.Key; 00197 closestToToValue = pair.Value; 00198 } 00199 } 00200 } 00201 rates.Add(closestToTo, closestToToValue); 00202 rates.Add(closestToFrom, closestToFromValue); 00203 monthRate = 0; 00204 ticksInMonth = monthEnd.Subtract(monthStart).Ticks; 00205 foreach (KeyValuePair<DateTime, float> pair in rates) 00206 { 00207 if (previousPair != null) 00208 monthRate += previousPair.Value.Value * 00209 Intersect(monthStart, monthEnd, previousPair.Value.Key, pair.Key).Ticks / ticksInMonth; 00210 previousPair = pair; 00211 } 00212 previousPair = null; 00213 result.Add(monthRate); 00214 rates.Clear(); 00215 monthStart = monthStart.AddMonths(1); 00216 monthEnd = monthStart.AddMonths(1).AddTicks(-1); 00217 } while (monthStart <= to); 00218 } 00219 System.Threading.Thread.CurrentThread.CurrentCulture = replacedCultureInfo; 00220 return result.ToArray(); 00221 } 00222 00223 public float[][] ReadFile(string filePath, int[] lookBehinds, int frequency) 00224 { 00225 int maxLookBehind = 0; 00226 for (int i = 0; i < lookBehinds.Length; i++) 00227 { 00228 if (maxLookBehind < lookBehinds[i]) 00229 maxLookBehind = lookBehinds[i]; 00230 } 00231 float[] all = ReadFile(filePath, maxLookBehind); 00232 float[][] result = new float[lookBehinds.Length][]; 00233 int timeSeriesLength = (all.Length - maxLookBehind) * frequency; 00234 for (int i = 0; i < result.Length; i++) 00235 { 00236 result[i] = new float[timeSeriesLength]; 00237 for (int k = maxLookBehind - lookBehinds[i], m = 0; m < timeSeriesLength; k++, m++) 00238 { 00239 for (int l = 0; l < frequency; l++, m++) 00240 { 00241 result[i][m] = all[k]; 00242 } 00243 m--; 00244 } 00245 } 00246 return result; 00247 } 00248 00249 public void ReadFileAndAddToAlphabet(Alphabet variablesSet, ref int variablesSetStartIndex, 00250 string filePath, int[] variableDisplayIndexs, string variableName, int[] lookBehinds, int frequency) 00251 { 00252 float[][] varVals = ReadFile(filePath, lookBehinds, frequency); 00253 for (int i = 0; i < varVals.Length; i++) 00254 { 00255 variablesSet[variablesSetStartIndex++] = 00256 new Variable(varVals[i], variableName + " " + variableDisplayIndexs[i]); 00257 } 00258 } 00259 00260 public void ReadFileAndAddToAlphabet(Alphabet variablesSet, ref int variablesSetIndex, 00261 string filePath, int variableDisplayIndex, string variableName) 00262 { 00263 float[] varVals = ReadFile(filePath, 0); 00264 variablesSet[variablesSetIndex++] = new Variable(varVals, variableName + " " + variableDisplayIndex); 00265 } 00266 00267 protected TimeSpan Intersect(DateTime i1Start, DateTime i1End, DateTime i2Start, DateTime i2End) 00268 { 00269 DateTime min = DateTime.MaxValue; 00270 DateTime max = DateTime.MinValue; 00271 if (i2Start <= i1Start && i1Start <= i2End) 00272 { 00273 max = (max < i1Start ? i1Start : max); 00274 min = (min < i1Start ? min : i1Start); 00275 } 00276 if (i2Start <= i1End && i1End <= i2End) 00277 { 00278 max = (max < i1End ? i1End : max); 00279 min = (min < i1End ? min : i1End); 00280 } 00281 if (i1Start <= i2Start && i2Start <= i1End) 00282 { 00283 max = (max < i2Start ? i2Start : max); 00284 min = (min < i2Start ? min : i2Start); 00285 } 00286 if (i1Start <= i2End && i2End <= i1End) 00287 { 00288 max = (max < i2End ? i2End : max); 00289 min = (min < i2End ? min : i2End); 00290 } 00291 return max.Subtract(min); 00292 } 00293 } 00294 }