ExchangeIntraday.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Reflection;
  7. using System.Xml.Serialization;
  8. public class ExchangeIntraday : ICloneable
  9. {
  10. private ArrayList alRemoveDay;
  11. private ArrayList alTimePeriods = new ArrayList();
  12. private bool nativeCycle;
  13. private bool showFirstXLabel;
  14. private double timeZone;
  15. private int yahooDelay = 30;
  16. public static double YahooTimeZone = -4.0;
  17. public virtual void Add(TimePeriod tp)
  18. {
  19. this.alTimePeriods.Add(tp);
  20. }
  21. public void AddRemoveDays(int Start, int End)
  22. {
  23. if (this.alRemoveDay == null)
  24. {
  25. this.alRemoveDay = new ArrayList();
  26. }
  27. else
  28. {
  29. this.alRemoveDay.Clear();
  30. }
  31. for (int i = Start + 1; i < End; i++)
  32. {
  33. this.alRemoveDay.Add(i);
  34. }
  35. }
  36. public object Clone()
  37. {
  38. ExchangeIntraday intraday = new ExchangeIntraday();
  39. intraday.nativeCycle = this.nativeCycle;
  40. intraday.showFirstXLabel = this.showFirstXLabel;
  41. intraday.timeZone = this.timeZone;
  42. intraday.yahooDelay = this.yahooDelay;
  43. intraday.TimePeriods = (TimePeriod[]) this.TimePeriods.Clone();
  44. return intraday;
  45. }
  46. public DateTime GetCurrentTradingDay()
  47. {
  48. DateTime d = DateTime.UtcNow.AddHours(this.TimeZone);
  49. if (this.IsBeforeMarketStart(d))
  50. {
  51. d = d.AddDays(-1.0);
  52. }
  53. while ((d.DayOfWeek == DayOfWeek.Saturday) || (d.DayOfWeek == DayOfWeek.Sunday))
  54. {
  55. d = d.AddDays(-1.0);
  56. }
  57. return d.Date;
  58. }
  59. public static ExchangeIntraday GetExchangeIntraday(string Exchange)
  60. {
  61. if (Exchange == "")
  62. {
  63. return US;
  64. }
  65. try
  66. {
  67. Type type = typeof(ExchangeIntraday);
  68. return (ExchangeIntraday) type.InvokeMember(Exchange, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Static, null, null, null);
  69. }
  70. catch
  71. {
  72. return US;
  73. }
  74. }
  75. public double[] GetMinuteDate(DateTime t1, DateTime t2)
  76. {
  77. ArrayList list = new ArrayList();
  78. double num = t1.ToOADate();
  79. double num2 = t2.ToOADate();
  80. while (num <= num2)
  81. {
  82. foreach (TimePeriod period in this.alTimePeriods)
  83. {
  84. int num3 = 0;
  85. double num4 = period.Time1;
  86. while (num4 < period.Time2)
  87. {
  88. num4 = period.Time1 + ((((double) num3) / 24.0) / 60.0);
  89. list.Add(num + num4);
  90. num3++;
  91. }
  92. }
  93. num++;
  94. }
  95. return (double[]) list.ToArray(typeof(double));
  96. }
  97. public double GetOpenTimePerDay()
  98. {
  99. double num = 0.0;
  100. foreach (TimePeriod period in this.alTimePeriods)
  101. {
  102. num += period.Time2 - period.Time1;
  103. }
  104. return num;
  105. }
  106. public bool InRemoveDays(double D)
  107. {
  108. return ((this.alRemoveDay != null) && (this.alRemoveDay.IndexOf((int) D) >= 0));
  109. }
  110. public bool InTimePeriod(double D)
  111. {
  112. double num = D - ((int) D);
  113. foreach (TimePeriod period in this.alTimePeriods)
  114. {
  115. if ((num >= period.Time1) && (num <= (period.Time2 + 0.00069444444444444436)))
  116. {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. public bool IsBeforeMarketStart(DateTime D)
  123. {
  124. TimePeriod period = this[0];
  125. DateTime time = DateTime.FromOADate(period.Time1);
  126. return ((D.Hour < time.Hour) || ((D.Hour == time.Hour) && (D.Minute < time.Minute)));
  127. }
  128. public bool IsEstimateOpen(DateTime D)
  129. {
  130. TimePeriod period = this[0];
  131. return ((D.Hour >= DateTime.FromOADate(period.Time1).Hour) && (D.Hour <= DateTime.FromOADate(this[this.Count - 1].Time2).AddMinutes((double) this.YahooDelay).Hour));
  132. }
  133. public double OneDayTime(double D)
  134. {
  135. double num = D - ((int) D);
  136. double num2 = 0.0;
  137. foreach (TimePeriod period in this.alTimePeriods)
  138. {
  139. if (num > period.Time2)
  140. {
  141. num2 += period.Time2 - period.Time1;
  142. }
  143. else
  144. {
  145. if (num > period.Time1)
  146. {
  147. num2 += num - period.Time1;
  148. }
  149. return num2;
  150. }
  151. }
  152. return num2;
  153. }
  154. public int RawDaysBetween(double d1, double d2)
  155. {
  156. int num = 0;
  157. int num2 = ((int) d1) + 1;
  158. int num3 = (int) d2;
  159. if (this.alRemoveDay != null)
  160. {
  161. for (int i = num2; i < num3; i++)
  162. {
  163. if (this.alRemoveDay.IndexOf(i) < 0)
  164. {
  165. num++;
  166. }
  167. }
  168. return num;
  169. }
  170. return (num3 - num2);
  171. }
  172. public static ExchangeIntraday ASX
  173. {
  174. get
  175. {
  176. ExchangeIntraday intraday = new ExchangeIntraday();
  177. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 0x10, 0, 0)));
  178. intraday.timeZone = 9.0;
  179. return intraday;
  180. }
  181. }
  182. public static ExchangeIntraday ASXE
  183. {
  184. get
  185. {
  186. ExchangeIntraday intraday = new ExchangeIntraday();
  187. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 0x11, 0, 0)));
  188. intraday.timeZone = 10.0;
  189. return intraday;
  190. }
  191. }
  192. public static string[] BuildInExchange
  193. {
  194. get
  195. {
  196. PropertyInfo[] properties = typeof(ExchangeIntraday).GetProperties(BindingFlags.Public | BindingFlags.Static);
  197. string[] strArray = new string[properties.Length - 1];
  198. for (int i = 1; i < properties.Length; i++)
  199. {
  200. strArray[i - 1] = properties[i].Name;
  201. }
  202. return strArray;
  203. }
  204. }
  205. public static ExchangeIntraday Canada
  206. {
  207. get
  208. {
  209. ExchangeIntraday intraday = new ExchangeIntraday();
  210. DateTime time = new DateTime(1, 1, 1, 9, 30, 0);
  211. DateTime time2 = new DateTime(1, 1, 1, 0x10, 0, 0);
  212. intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate()));
  213. intraday.timeZone = YahooTimeZone;
  214. return intraday;
  215. }
  216. }
  217. public static ExchangeIntraday China
  218. {
  219. get
  220. {
  221. ExchangeIntraday intraday = new ExchangeIntraday();
  222. intraday.NativeCycle = true;
  223. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 30, 0), new DateTime(1, 1, 1, 11, 30, 0)));
  224. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 13, 0, 0), new DateTime(1, 1, 1, 15, 0, 0)));
  225. intraday.timeZone = 8.0;
  226. intraday.showFirstXLabel = true;
  227. return intraday;
  228. }
  229. }
  230. [Browsable(false), XmlIgnore]
  231. public int Count
  232. {
  233. get
  234. {
  235. return this.alTimePeriods.Count;
  236. }
  237. }
  238. [Browsable(false), XmlIgnore]
  239. public DateTime ExchangeDate
  240. {
  241. get
  242. {
  243. return this.ExchangeTime.Date;
  244. }
  245. }
  246. [Browsable(false), XmlIgnore]
  247. public DateTime ExchangeTime
  248. {
  249. get
  250. {
  251. return DateTime.UtcNow.AddHours(this.TimeZone);
  252. }
  253. }
  254. public static ExchangeIntraday Forex
  255. {
  256. get
  257. {
  258. ExchangeIntraday intraday = new ExchangeIntraday();
  259. DateTime time = new DateTime(1, 1, 1, 0, 0, 0);
  260. DateTime time2 = new DateTime(1, 1, 1, 0x17, 0x3b, 0x3b);
  261. intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate()));
  262. intraday.TimeZone = 0.0;
  263. intraday.yahooDelay = 0;
  264. intraday.ShowFirstXLabel = true;
  265. return intraday;
  266. }
  267. }
  268. public static ExchangeIntraday France
  269. {
  270. get
  271. {
  272. ExchangeIntraday intraday = new ExchangeIntraday();
  273. DateTime time = new DateTime(1, 1, 1, 9, 0, 0);
  274. DateTime time2 = new DateTime(1, 1, 1, 0x11, 30, 0);
  275. intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate()));
  276. intraday.timeZone = 6.0 + YahooTimeZone;
  277. return intraday;
  278. }
  279. }
  280. public static ExchangeIntraday Germany
  281. {
  282. get
  283. {
  284. ExchangeIntraday intraday = new ExchangeIntraday();
  285. DateTime time = new DateTime(1, 1, 1, 9, 0, 0);
  286. DateTime time2 = new DateTime(1, 1, 1, 0x11, 0, 0);
  287. intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate()));
  288. intraday.timeZone = 6.0 + YahooTimeZone;
  289. return intraday;
  290. }
  291. }
  292. public static ExchangeIntraday HK
  293. {
  294. get
  295. {
  296. ExchangeIntraday intraday = new ExchangeIntraday();
  297. intraday.NativeCycle = true;
  298. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 12, 30, 0)));
  299. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 14, 30, 0), new DateTime(1, 1, 1, 0x10, 0, 0)));
  300. intraday.timeZone = 8.0;
  301. intraday.showFirstXLabel = true;
  302. return intraday;
  303. }
  304. }
  305. public virtual TimePeriod this[int Index]
  306. {
  307. get
  308. {
  309. return (TimePeriod) this.alTimePeriods[Index];
  310. }
  311. }
  312. public static ExchangeIntraday Japan
  313. {
  314. get
  315. {
  316. ExchangeIntraday intraday = new ExchangeIntraday();
  317. intraday.NativeCycle = true;
  318. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 11, 0, 0)));
  319. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 12, 30, 0), new DateTime(1, 1, 1, 15, 0, 0)));
  320. intraday.timeZone = 9.0;
  321. intraday.showFirstXLabel = true;
  322. return intraday;
  323. }
  324. }
  325. [Description("Use exchange data cycle in X-axis, used in two periods exchanges."), DefaultValue(false), XmlAttribute]
  326. public bool NativeCycle
  327. {
  328. get
  329. {
  330. return this.nativeCycle;
  331. }
  332. set
  333. {
  334. this.nativeCycle = true;
  335. }
  336. }
  337. public static ExchangeIntraday Sg
  338. {
  339. get
  340. {
  341. ExchangeIntraday intraday = new ExchangeIntraday();
  342. intraday.nativeCycle = true;
  343. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 12, 30, 0)));
  344. intraday.Add(new TimePeriod(new DateTime(1, 1, 1, 14, 0, 0), new DateTime(1, 1, 1, 0x11, 0, 0)));
  345. intraday.timeZone = 8.0;
  346. intraday.showFirstXLabel = true;
  347. return intraday;
  348. }
  349. }
  350. [Description("Show first label in the X-axis"), DefaultValue(false), XmlAttribute]
  351. public bool ShowFirstXLabel
  352. {
  353. get
  354. {
  355. return this.showFirstXLabel;
  356. }
  357. set
  358. {
  359. this.showFirstXLabel = value;
  360. }
  361. }
  362. [TypeConverter(typeof(TimePeriodsConverter)), Description("Time periods, separate by comma.")]
  363. public TimePeriod[] TimePeriods
  364. {
  365. get
  366. {
  367. return (TimePeriod[]) this.alTimePeriods.ToArray(typeof(TimePeriod));
  368. }
  369. set
  370. {
  371. this.alTimePeriods.Clear();
  372. this.alTimePeriods.AddRange(value);
  373. }
  374. }
  375. [DefaultValue(0), XmlAttribute, Description("Time zone of the exchange")]
  376. public double TimeZone
  377. {
  378. get
  379. {
  380. return this.timeZone;
  381. }
  382. set
  383. {
  384. this.timeZone = value;
  385. }
  386. }
  387. public static ExchangeIntraday UK
  388. {
  389. get
  390. {
  391. ExchangeIntraday intraday = new ExchangeIntraday();
  392. DateTime time = new DateTime(1, 1, 1, 8, 0, 0);
  393. DateTime time2 = new DateTime(1, 1, 1, 0x10, 0x2d, 0);
  394. intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate()));
  395. intraday.timeZone = 5.0 + YahooTimeZone;
  396. return intraday;
  397. }
  398. }
  399. public static ExchangeIntraday US
  400. {
  401. get
  402. {
  403. ExchangeIntraday intraday = new ExchangeIntraday();
  404. DateTime time = new DateTime(1, 1, 1, 9, 30, 0);
  405. DateTime time2 = new DateTime(1, 1, 1, 0x10, 0, 0);
  406. intraday.Add(new TimePeriod(time.ToOADate(), time2.ToOADate()));
  407. intraday.timeZone = YahooTimeZone;
  408. return intraday;
  409. }
  410. }
  411. [DefaultValue(30), Browsable(false), XmlIgnore]
  412. public int YahooDelay
  413. {
  414. get
  415. {
  416. return this.yahooDelay;
  417. }
  418. set
  419. {
  420. this.yahooDelay = value;
  421. }
  422. }
  423. }
  424. }