PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/AmiBrokerPlugin/AmiBrokerPlugin/Plugin.External.cs

#
C# | 272 lines | 113 code | 38 blank | 121 comment | 6 complexity | 86adc9779bfe94a1ec6bcb0bb4c1f928 MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // AmiBroker Plug-in SDK | Copyright © 2010 by Koistya `Navin | http://code.google.com/p/amibroker/
  3. // ------------------------------------------------------------------------------------------------
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. // except in compliance with the License. You may obtain a copy of the License at:
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. // Unless required by applicable law or agreed to in writing, software distributed under the
  8. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. // either express or implied. See the License for the specific language governing permissions and
  10. // limitations under the License.
  11. ///////////////////////////////////////////////////////////////////////////////////////////////////
  12. using System;
  13. using System.Runtime.InteropServices;
  14. using System.Drawing;
  15. using System.Windows.Forms;
  16. namespace AmiBrokerPlugin
  17. {
  18. /// <summary>
  19. /// All public static methods of this class will be exported for use by AmiBroker
  20. /// </summary>
  21. public static partial class Plugin
  22. {
  23. #region Common Exported Functions
  24. /////////////////////////////////////////////////////////////////////////
  25. // COMMON EXPORTED FUNCTONS
  26. //
  27. // Each AmiBroker plug-in DLL must export the following
  28. // functions:
  29. // 1. GetPluginInfo - called when DLL is loaded
  30. // 2. Init - called when AFL engine is being initialized
  31. // 3. Release - called when AFL engine is being closed
  32. /////////////////////////////////////////////////////////////////////////
  33. /// <summary>
  34. /// Called when DLL is loaded
  35. /// </summary>
  36. public static int GetPluginInfo(ref PluginInfo pluginInfo)
  37. {
  38. // TODO: Insert information about the plugin
  39. pluginInfo.Name = "AmiBroker® data Plug-in"; // Name of the plug-in
  40. pluginInfo.Vendor = "AmiBroker.CodePlex.com"; // Vendor of the plug-in
  41. pluginInfo.Type = PluginType.Data; // Plug-in type. AFL, Data or Optimizer
  42. pluginInfo.Version = 10001; // Plug-in version. v1.0.0
  43. pluginInfo.IDCode = GetIDCode("AMIP"); // Unique ID of the plug-in
  44. pluginInfo.Certificate = 0; // Is this plug-in certified by AmiBroker
  45. pluginInfo.MinAmiVersion = 387000; // Minimum supported version of AmiBroker
  46. pluginInfo.StructSize = Marshal.SizeOf(pluginInfo); // Just keep it
  47. return 1;
  48. }
  49. /// <summary>
  50. /// Called when AFL engine is being initialized
  51. /// </summary>
  52. public static int Init()
  53. {
  54. // TODO: Add initialization logic here
  55. return 1;
  56. }
  57. /// <summary>
  58. /// Called when AFL engine is being closed
  59. /// </summary>
  60. public static int Release()
  61. {
  62. // TODO: Add cleanup logic here
  63. return 1;
  64. }
  65. #endregion
  66. #region AFL Exported Functions
  67. /////////////////////////////////////////////////////////////////////////
  68. // INDICATOR PLUGIN EXPORTED FUNCTIONS
  69. //
  70. // 1. GetFunctionTable - called when AFL engine is being initialized
  71. // 1. SetSiteInteface - called when AFL engine is being initialized
  72. //
  73. // Each function may be called multiple times.
  74. //
  75. // The order of calling functions during intialization is as follows:
  76. //
  77. // SetSiteInterface -> GetFunctionTable -> Init ->
  78. // ... normal work ....
  79. // Release
  80. //
  81. // This cycle may repeat multiple times
  82. /////////////////////////////////////////////////////////////////////////
  83. public static int GetFunctionTable(IntPtr functionTablePtr)
  84. {
  85. return 1;
  86. }
  87. public static int SetSiteInterface(IntPtr siteInterfacePtr)
  88. {
  89. return 1;
  90. }
  91. #endregion
  92. #region Data Exported Functions
  93. /// <summary>
  94. /// GetQuotesEx function is functional equivalent fo GetQuotes but
  95. /// handles new Quotation format with 64 bit date/time stamp and floating point volume/open int
  96. /// and new Aux fields
  97. /// it also takes pointer to context that is reserved for future use (can be null)
  98. /// Called by AmiBroker 5.27 and above
  99. /// </summary>
  100. unsafe public static int GetQuotesEx(string ticker, Periodicity periodicity, int lastValid, int size, Quotation* quotes, GQEContext* context)
  101. {
  102. // TODO: Add logic here. Take a look at the demo below:
  103. //for (var i = 0; i < 5; i++)
  104. //{
  105. // quotes[i].DateTime = PackDate(DateTime.Today.AddDays(i - 5));
  106. // quotes[i].Price = 10;
  107. // quotes[i].Open = 15;
  108. // quotes[i].High = 16;
  109. // quotes[i].Low = 9;
  110. // quotes[i].Volume = 1000;
  111. // quotes[i].OpenInterest = 0;
  112. // quotes[i].AuxData1 = 0;
  113. // quotes[i].AuxData2 = 0;
  114. //}
  115. //return 5;
  116. // return 'lastValid + 1' if no updates are found and you want to keep all existing records
  117. return lastValid + 1;
  118. }
  119. unsafe public delegate void* Alloc(uint size);
  120. ///// <summary>
  121. ///// GetExtra data is optional function for retrieving non-quotation data
  122. ///// </summary>
  123. public static AmiVar GetExtraData(string ticker, string name, int arraySize, Periodicity periodicity, Alloc alloc)
  124. {
  125. return new AmiVar();
  126. }
  127. /// <summary>
  128. /// Configure function is called when user presses "Configure" button in File->Database Settings
  129. /// </summary>
  130. /// <param name="path">Path to AmiBroker database</param>
  131. /// <param name="site">A pointer to <see cref="AmiBrokerPlugin.InfoSite"/></param>
  132. public static int Configure(string path, IntPtr infoSitePtr)
  133. {
  134. GetStockQty = (GetStockQtyDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(Marshal.ReadInt32(new IntPtr(infoSitePtr.ToInt32() + 4))), typeof(GetStockQtyDelegate));
  135. SetCategoryName = (SetCategoryNameDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(Marshal.ReadInt32(new IntPtr(infoSitePtr.ToInt32() + 12))), typeof(SetCategoryNameDelegate));
  136. GetCategoryName = (GetCategoryNameDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(Marshal.ReadInt32(new IntPtr(infoSitePtr.ToInt32() + 16))), typeof(GetCategoryNameDelegate));
  137. SetIndustrySector = (SetIndustrySectorDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(Marshal.ReadInt32(new IntPtr(infoSitePtr.ToInt32() + 20))), typeof(SetIndustrySectorDelegate));
  138. GetIndustrySector = (GetIndustrySectorDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(Marshal.ReadInt32(new IntPtr(infoSitePtr.ToInt32() + 24))), typeof(GetIndustrySectorDelegate));
  139. AddStock = (AddStockDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(Marshal.ReadInt32(new IntPtr(infoSitePtr.ToInt32() + 20))), typeof(AddStockDelegate));
  140. MessageBox.Show(GetStockQty().ToString());
  141. return 1;
  142. }
  143. /// <summary>
  144. /// GetRecentInfo function is optional, used only by real-time plugins
  145. /// </summary>
  146. public static RecentInfo GetRecentInfo(string ticker)
  147. {
  148. return new RecentInfo();
  149. }
  150. /// <summary>
  151. /// New API function, optional, only for RT plugins
  152. /// </summary>
  153. public static int IsBackfillComplete(string ticker)
  154. {
  155. return 1;
  156. }
  157. /// <summary>
  158. /// GetSymbolLimit function is optional, used only by real-time plugins
  159. /// </summary>
  160. public static int GetSymbolLimit()
  161. {
  162. return 1;
  163. }
  164. /// <summary>
  165. /// GetStatus function is optional, used mostly by few real-time plugins
  166. /// </summary>
  167. /// <param name="statusPtr">A pointer to <see cref="AmiBrokerPlugin.PluginStatus"/></param>
  168. public static int GetStatus(IntPtr statusPtr)
  169. {
  170. switch (Status)
  171. {
  172. case StatusCode.OK:
  173. SetStatus(statusPtr, StatusCode.OK, Color.LightGreen, "OK", "Plugin works");
  174. break;
  175. case StatusCode.Wait:
  176. SetStatus(statusPtr, StatusCode.Wait, Color.LightBlue, "WAIT", "Wait a sec...");
  177. break;
  178. case StatusCode.Error:
  179. SetStatus(statusPtr, StatusCode.Error, Color.Red, "ERR", "An error occured");
  180. break;
  181. default:
  182. SetStatus(statusPtr, StatusCode.Unknown, Color.LightGray, "Ukno", "Unknown status");
  183. break;
  184. }
  185. return 1;
  186. }
  187. /// <summary>
  188. /// SetTimeBase function is called when user is changing base time interval in File->Database Settings
  189. /// </summary>
  190. public static int SetTimeBase(int periodicity)
  191. {
  192. return periodicity >= (int)Periodicity.OneHour && periodicity <= (int)Periodicity.EndOfDay ? 1 : 0;
  193. }
  194. /// <summary>
  195. /// Notify function (optional) is called when database is loaded, unloaded
  196. /// settings are changed, or right mouse button in the plugin status area is clicked
  197. /// </summary>
  198. public static int Notify(ref PluginNotification notifyData)
  199. {
  200. if (MainWnd == IntPtr.Zero)
  201. {
  202. MainWnd = notifyData.MainWnd;
  203. }
  204. if (notifyData.Reason == PluginNotificationReason.StatusRightClick)
  205. {
  206. MessageBox.Show(GetStockQty().ToString());
  207. }
  208. return 1;
  209. }
  210. #endregion
  211. #region Optimizer Plugin API
  212. /////////////////////////////////////////////////////////////////////////
  213. // OPTIMIZER PLUGIN API
  214. /////////////////////////////////////////////////////////////////////////
  215. //public static int OptimizerInit(ref OptimizeParams optimizeParams)
  216. //{
  217. // return 1;
  218. //}
  219. //public delegate double EvaluateFuncDelegate(IntPtr param);
  220. //public static int OptimizerRun(ref OptimizeParams optimizeParams, EvaluateFuncDelegate evaluateFunc, IntPtr context)
  221. //{
  222. // return 1;
  223. //}
  224. //public static int OptimizerFinalize(ref OptimizeParams optimizeParams)
  225. //{
  226. // return 1;
  227. //}
  228. //public static int OptimizerSetOption(string param, AmiVar newValue)
  229. //{
  230. // return 1;
  231. //}
  232. #endregion
  233. }
  234. }