namespace IndexFormula.Finance { using System; using System.Collections; using System.ComponentModel; using System.Reflection; using System.Xml.Serialization; public class ExchangeIntraday : ICloneable { private ArrayList alRemoveDay; private ArrayList alTimePeriods = new ArrayList(); private bool nativeCycle; private bool showFirstXLabel; private double timeZone; private int yahooDelay = 30; public static double YahooTimeZone = -4.0; public virtual void Add(TimePeriod tp) { this.alTimePeriods.Add(tp); } public void AddRemoveDays(int Start, int End) { if (this.alRemoveDay == null) { this.alRemoveDay = new ArrayList(); } else { this.alRemoveDay.Clear(); } for (int i = Start + 1; i < End; i++) { this.alRemoveDay.Add(i); } } public object Clone() { ExchangeIntraday intraday = new ExchangeIntraday(); intraday.nativeCycle = this.nativeCycle; intraday.showFirstXLabel = this.showFirstXLabel; intraday.timeZone = this.timeZone; intraday.yahooDelay = this.yahooDelay; intraday.TimePeriods = (TimePeriod[]) this.TimePeriods.Clone(); return intraday; } public DateTime GetCurrentTradingDay() { DateTime d = DateTime.UtcNow.AddHours(this.TimeZone); if (this.IsBeforeMarketStart(d)) { d = d.AddDays(-1.0); } while ((d.DayOfWeek == DayOfWeek.Saturday) || (d.DayOfWeek == DayOfWeek.Sunday)) { d = d.AddDays(-1.0); } return d.Date; } public static ExchangeIntraday GetExchangeIntraday(string Exchange) { if (Exchange == "") { return US; } try { Type type = typeof(ExchangeIntraday); return (ExchangeIntraday) type.InvokeMember(Exchange, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Static, null, null, null); } catch { return US; } } public double[] GetMinuteDate(DateTime t1, DateTime t2) { ArrayList list = new ArrayList(); double num = t1.ToOADate(); double num2 = t2.ToOADate(); while (num <= num2) { foreach (TimePeriod period in this.alTimePeriods) { int num3 = 0; double num4 = period.Time1; while (num4 < period.Time2) { num4 = period.Time1 + ((((double) num3) / 24.0) / 60.0); list.Add(num + num4); num3++; } } num++; } return (double[]) list.ToArray(typeof(double)); } public double GetOpenTimePerDay() { double num = 0.0; foreach (TimePeriod period in this.alTimePeriods) { num += period.Time2 - period.Time1; } return num; } public bool InRemoveDays(double D) { return ((this.alRemoveDay != null) && (this.alRemoveDay.IndexOf((int) D) >= 0)); } public bool InTimePeriod(double D) { double num = D - ((int) D); foreach (TimePeriod period in this.alTimePeriods) { if ((num >= period.Time1) && (num <= (period.Time2 + 0.00069444444444444436))) { return true; } } return false; } public bool IsBeforeMarketStart(DateTime D) { TimePeriod period = this[0]; DateTime time = DateTime.FromOADate(period.Time1); return ((D.Hour < time.Hour) || ((D.Hour == time.Hour) && (D.Minute < time.Minute))); } public bool IsEstimateOpen(DateTime D) { TimePeriod period = this[0]; return ((D.Hour >= DateTime.FromOADate(period.Time1).Hour) && (D.Hour <= DateTime.FromOADate(this[this.Count - 1].Time2).AddMinutes((double) this.YahooDelay).Hour)); } public double OneDayTime(double D) { double num = D - ((int) D); double num2 = 0.0; foreach (TimePeriod period in this.alTimePeriods) { if (num > period.Time2) { num2 += period.Time2 - period.Time1; } else { if (num > period.Time1) { num2 += num - period.Time1; } return num2; } } return num2; } public int RawDaysBetween(double d1, double d2) { int num = 0; int num2 = ((int) d1) + 1; int num3 = (int) d2; if (this.alRemoveDay != null) { for (int i = num2; i < num3; i++) { if (this.alRemoveDay.IndexOf(i) < 0) { num++; } } return num; } return (num3 - num2); } public static ExchangeIntraday ASX { get { ExchangeIntraday intraday = new ExchangeIntraday(); intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 0x10, 0, 0))); intraday.timeZone = 9.0; return intraday; } } public static ExchangeIntraday ASXE { get { ExchangeIntraday intraday = new ExchangeIntraday(); intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 0x11, 0, 0))); intraday.timeZone = 10.0; return intraday; } } public static string[] BuildInExchange { get { PropertyInfo[] properties = typeof(ExchangeIntraday).GetProperties(BindingFlags.Public | BindingFlags.Static); string[] strArray = new string[properties.Length - 1]; for (int i = 1; i < properties.Length; i++) { strArray[i - 1] = properties[i].Name; } return strArray; } } public static ExchangeIntraday Canada { get { ExchangeIntraday intraday = new ExchangeIntraday(); DateTime time = new DateTime(1, 1, 1, 9, 30, 0); DateTime time2 = new DateTime(1, 1, 1, 0x10, 0, 0); intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate())); intraday.timeZone = YahooTimeZone; return intraday; } } public static ExchangeIntraday China { get { ExchangeIntraday intraday = new ExchangeIntraday(); intraday.NativeCycle = true; intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 30, 0), new DateTime(1, 1, 1, 11, 30, 0))); intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 13, 0, 0), new DateTime(1, 1, 1, 15, 0, 0))); intraday.timeZone = 8.0; intraday.showFirstXLabel = true; return intraday; } } [Browsable(false), XmlIgnore] public int Count { get { return this.alTimePeriods.Count; } } [Browsable(false), XmlIgnore] public DateTime ExchangeDate { get { return this.ExchangeTime.Date; } } [Browsable(false), XmlIgnore] public DateTime ExchangeTime { get { return DateTime.UtcNow.AddHours(this.TimeZone); } } public static ExchangeIntraday Forex { get { ExchangeIntraday intraday = new ExchangeIntraday(); DateTime time = new DateTime(1, 1, 1, 0, 0, 0); DateTime time2 = new DateTime(1, 1, 1, 0x17, 0x3b, 0x3b); intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate())); intraday.TimeZone = 0.0; intraday.yahooDelay = 0; intraday.ShowFirstXLabel = true; return intraday; } } public static ExchangeIntraday France { get { ExchangeIntraday intraday = new ExchangeIntraday(); DateTime time = new DateTime(1, 1, 1, 9, 0, 0); DateTime time2 = new DateTime(1, 1, 1, 0x11, 30, 0); intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate())); intraday.timeZone = 6.0 + YahooTimeZone; return intraday; } } public static ExchangeIntraday Germany { get { ExchangeIntraday intraday = new ExchangeIntraday(); DateTime time = new DateTime(1, 1, 1, 9, 0, 0); DateTime time2 = new DateTime(1, 1, 1, 0x11, 0, 0); intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate())); intraday.timeZone = 6.0 + YahooTimeZone; return intraday; } } public static ExchangeIntraday HK { get { ExchangeIntraday intraday = new ExchangeIntraday(); intraday.NativeCycle = true; intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 12, 30, 0))); intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 14, 30, 0), new DateTime(1, 1, 1, 0x10, 0, 0))); intraday.timeZone = 8.0; intraday.showFirstXLabel = true; return intraday; } } public virtual TimePeriod this[int Index] { get { return (TimePeriod) this.alTimePeriods[Index]; } } public static ExchangeIntraday Japan { get { ExchangeIntraday intraday = new ExchangeIntraday(); intraday.NativeCycle = true; intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 11, 0, 0))); intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 12, 30, 0), new DateTime(1, 1, 1, 15, 0, 0))); intraday.timeZone = 9.0; intraday.showFirstXLabel = true; return intraday; } } [Description("Use exchange data cycle in X-axis, used in two periods exchanges."), DefaultValue(false), XmlAttribute] public bool NativeCycle { get { return this.nativeCycle; } set { this.nativeCycle = true; } } public static ExchangeIntraday Sg { get { ExchangeIntraday intraday = new ExchangeIntraday(); intraday.nativeCycle = true; intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 12, 30, 0))); intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 14, 0, 0), new DateTime(1, 1, 1, 0x11, 0, 0))); intraday.timeZone = 8.0; intraday.showFirstXLabel = true; return intraday; } } [Description("Show first label in the X-axis"), DefaultValue(false), XmlAttribute] public bool ShowFirstXLabel { get { return this.showFirstXLabel; } set { this.showFirstXLabel = value; } } [TypeConverter(typeof(TimePeriodsConverter)), Description("Time periods, separate by comma.")] public TimePeriod[] TimePeriods { get { return (TimePeriod[]) this.alTimePeriods.ToArray(typeof(TimePeriod)); } set { this.alTimePeriods.Clear(); this.alTimePeriods.AddRange(value); } } [DefaultValue(0), XmlAttribute, Description("Time zone of the exchange")] public double TimeZone { get { return this.timeZone; } set { this.timeZone = value; } } public static ExchangeIntraday UK { get { ExchangeIntraday intraday = new ExchangeIntraday(); DateTime time = new DateTime(1, 1, 1, 8, 0, 0); DateTime time2 = new DateTime(1, 1, 1, 0x10, 0x2d, 0); intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate())); intraday.timeZone = 5.0 + YahooTimeZone; return intraday; } } public static ExchangeIntraday US { get { ExchangeIntraday intraday = new ExchangeIntraday(); DateTime time = new DateTime(1, 1, 1, 9, 30, 0); DateTime time2 = new DateTime(1, 1, 1, 0x10, 0, 0); intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate())); intraday.timeZone = YahooTimeZone; return intraday; } } [DefaultValue(30), Browsable(false), XmlIgnore] public int YahooDelay { get { return this.yahooDelay; } set { this.yahooDelay = value; } } } }