/StingrayOQ/Helpers.cs

https://github.com/DrKoch/StingrayOQ · C# · 317 lines · 298 code · 14 blank · 5 comment · 10 complexity · 4f089bd92bf63fbde8825f9a67e10bc3 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using Krs.Ats.IBNet96;
  7. using OpenQuant.API;
  8. using IB = Krs.Ats.IBNet96;
  9. using OQ = OpenQuant.API;
  10. namespace finantic.OQPlugins
  11. {
  12. public class Helpers
  13. {
  14. #region Converters
  15. // These converters convert OQ enums to IB enums and back
  16. // all follow the same pattern
  17. // if a conversion is not possible they return false.
  18. // the caller is expected to create an error message and ignore
  19. // the class containing the enum which is not convertible
  20. public static bool ActionSideToOrderSide(ActionSide action, out OrderSide side)
  21. {
  22. side = OrderSide.Buy;
  23. switch (action)
  24. {
  25. case ActionSide.Buy:
  26. side = OrderSide.Buy;
  27. break;
  28. case ActionSide.Sell:
  29. side = OrderSide.Sell;
  30. break;
  31. case ActionSide.SShort:
  32. side = OrderSide.Sell;
  33. break;
  34. case ActionSide.Undefined:
  35. default:
  36. return false;
  37. }
  38. return true;
  39. }
  40. public static bool OrderStateToOrderStatus(IB.OrderStatus ibstatus, out OQ.OrderStatus status)
  41. {
  42. status = OQ.OrderStatus.New;
  43. switch (ibstatus)
  44. {
  45. case IB.OrderStatus.ApiCancelled:
  46. case IB.OrderStatus.Canceled:
  47. status = OQ.OrderStatus.Cancelled;
  48. break;
  49. case IB.OrderStatus.Inactive:
  50. case IB.OrderStatus.ApiPending:
  51. case IB.OrderStatus.PendingSubmit:
  52. case IB.OrderStatus.PreSubmitted:
  53. case IB.OrderStatus.Submitted:
  54. status = OQ.OrderStatus.PendingNew;
  55. break;
  56. case IB.OrderStatus.Filled:
  57. status = OQ.OrderStatus.Filled;
  58. break;
  59. case IB.OrderStatus.PartiallyFilled:
  60. status = OQ.OrderStatus.PartiallyFilled;
  61. break;
  62. case IB.OrderStatus.PendingCancel:
  63. status = OQ.OrderStatus.PendingCancel;
  64. break;
  65. default:
  66. case IB.OrderStatus.Unknown:
  67. return false;
  68. }
  69. return true;
  70. }
  71. public static bool OrderTypeToOrderType(IB.OrderType ibordertype, out OQ.OrderType oqordertype)
  72. {
  73. oqordertype = OQ.OrderType.Market;
  74. switch (ibordertype)
  75. {
  76. case IB.OrderType.Limit:
  77. case IB.OrderType.LimitOnClose:
  78. oqordertype = OQ.OrderType.Limit;
  79. break;
  80. case IB.OrderType.Market:
  81. oqordertype = OQ.OrderType.Market;
  82. break;
  83. case IB.OrderType.MarketOnClose:
  84. oqordertype = OQ.OrderType.MarketOnClose;
  85. break;
  86. case IB.OrderType.Stop:
  87. oqordertype = OQ.OrderType.Stop;
  88. break;
  89. case IB.OrderType.StopLimit:
  90. oqordertype = OQ.OrderType.StopLimit;
  91. break;
  92. case IB.OrderType.TrailingStop:
  93. oqordertype = OQ.OrderType.Trail;
  94. break;
  95. case IB.OrderType.TrailingStopLimit:
  96. oqordertype = OQ.OrderType.TrailLimit;
  97. break;
  98. default:
  99. case IB.OrderType.Volatility:
  100. case IB.OrderType.VolumeWeightedAveragePrice:
  101. case IB.OrderType.Scale:
  102. case IB.OrderType.Relative:
  103. case IB.OrderType.PeggedToMarket:
  104. case IB.OrderType.Default:
  105. case IB.OrderType.Empty:
  106. return false;
  107. }
  108. return true;
  109. }
  110. public static bool SecurityTypeToInstrumentType(SecurityType secType, out InstrumentType result)
  111. {
  112. result = InstrumentType.Stock; // default
  113. switch (secType)
  114. {
  115. case SecurityType.Bond:
  116. result = InstrumentType.Bond;
  117. break;
  118. case SecurityType.Cash:
  119. result = InstrumentType.FX;
  120. break;
  121. case SecurityType.Future:
  122. result = InstrumentType.Futures;
  123. break;
  124. case SecurityType.FutureOption:
  125. case SecurityType.Index:
  126. result = InstrumentType.Index;
  127. break;
  128. case SecurityType.Option:
  129. result = InstrumentType.FutOpt;
  130. break;
  131. case SecurityType.Stock:
  132. result = InstrumentType.Stock;
  133. break;
  134. case SecurityType.Bag:
  135. case SecurityType.Undefined:
  136. return false;
  137. }
  138. return true;
  139. }
  140. public static bool OrderSideToActionSide(OrderSide orderside, out ActionSide action)
  141. {
  142. action = ActionSide.Undefined;
  143. switch (orderside)
  144. {
  145. case OrderSide.Buy:
  146. action = ActionSide.Buy;
  147. break;
  148. case OrderSide.Sell:
  149. action = ActionSide.Sell;
  150. break;
  151. default:
  152. return false;
  153. }
  154. return true;
  155. }
  156. public static bool OQOrderTypeToIBOrderType(OQ.OrderType oqtype, out IB.OrderType ibtype)
  157. {
  158. ibtype = IB.OrderType.Empty;
  159. switch (oqtype)
  160. {
  161. case OQ.OrderType.Limit:
  162. ibtype = IB.OrderType.Limit;
  163. break;
  164. case OQ.OrderType.Market:
  165. ibtype = IB.OrderType.Market;
  166. break;
  167. case OQ.OrderType.MarketOnClose:
  168. ibtype = IB.OrderType.MarketOnClose;
  169. break;
  170. case OQ.OrderType.Stop:
  171. ibtype = IB.OrderType.Stop;
  172. break;
  173. case OQ.OrderType.StopLimit:
  174. ibtype = IB.OrderType.StopLimit;
  175. break;
  176. case OQ.OrderType.Trail:
  177. ibtype = IB.OrderType.TrailingStop;
  178. break;
  179. case OQ.OrderType.TrailLimit:
  180. ibtype = IB.OrderType.TrailingStopLimit;
  181. break;
  182. default:
  183. return false;
  184. }
  185. return true;
  186. }
  187. public static bool OQTimeInForceToIBTimeInForce(OQ.TimeInForce oqtif, out IB.TimeInForce ibtif)
  188. {
  189. ibtif = IB.TimeInForce.Undefined;
  190. switch (oqtif)
  191. {
  192. case OQ.TimeInForce.Day:
  193. ibtif = IB.TimeInForce.Day;
  194. break;
  195. case OQ.TimeInForce.FOK:
  196. ibtif = IB.TimeInForce.FillOrKill;
  197. break;
  198. case OQ.TimeInForce.GTC:
  199. ibtif = IB.TimeInForce.GoodTillCancel;
  200. break;
  201. case OQ.TimeInForce.GTD:
  202. ibtif = IB.TimeInForce.GoodTillDate;
  203. break;
  204. case OQ.TimeInForce.OPG:
  205. ibtif = IB.TimeInForce.MarketOnOpen;
  206. break;
  207. case OQ.TimeInForce.ATC:
  208. case OQ.TimeInForce.GFS:
  209. case OQ.TimeInForce.GTX:
  210. case OQ.TimeInForce.IOC:
  211. default:
  212. return false;
  213. }
  214. return true;
  215. }
  216. public static bool InstrumentTypeToSecurityType(OQ.InstrumentType instrType, out IB.SecurityType secType)
  217. {
  218. secType = SecurityType.Undefined;
  219. switch (instrType)
  220. {
  221. case InstrumentType.Bond:
  222. secType = SecurityType.Bond;
  223. break;
  224. case InstrumentType.ETF:
  225. secType = SecurityType.Stock;
  226. break;
  227. case InstrumentType.FX:
  228. secType = SecurityType.Cash;
  229. break;
  230. case InstrumentType.FutOpt:
  231. secType = SecurityType.FutureOption;
  232. break;
  233. case InstrumentType.Futures:
  234. secType = SecurityType.Future;
  235. break;
  236. case InstrumentType.Index:
  237. secType = SecurityType.Index;
  238. break;
  239. case InstrumentType.Option:
  240. secType = SecurityType.Option;
  241. break;
  242. case InstrumentType.Stock:
  243. secType = SecurityType.Stock;
  244. break;
  245. case InstrumentType.MultiLeg:
  246. default:
  247. return false;
  248. }
  249. return true;
  250. }
  251. public static bool OQFAMethodTOIBFAMethod(OQ.IBFaMethod oqFaMethod,
  252. out IB.FinancialAdvisorAllocationMethod ibFaMethod)
  253. {
  254. ibFaMethod = FinancialAdvisorAllocationMethod.None;
  255. switch (oqFaMethod)
  256. {
  257. case IBFaMethod.AvailableEquity:
  258. ibFaMethod = FinancialAdvisorAllocationMethod.AvailableEquity;
  259. break;
  260. case IBFaMethod.EqualQuantity:
  261. ibFaMethod = FinancialAdvisorAllocationMethod.EqualQuantity;
  262. break;
  263. case IBFaMethod.NetLiq:
  264. ibFaMethod = FinancialAdvisorAllocationMethod.NetLiquidity;
  265. break;
  266. case IBFaMethod.PctChange:
  267. ibFaMethod = FinancialAdvisorAllocationMethod.PercentChange;
  268. break;
  269. case IBFaMethod.Undefined:
  270. ibFaMethod = FinancialAdvisorAllocationMethod.None;
  271. break;
  272. default:
  273. return false;
  274. }
  275. return true;
  276. }
  277. public static bool FAAllocationMethodTOIBFAMethod(IB.FinancialAdvisorAllocationMethod ibFaMethod,
  278. out OQ.IBFaMethod oqFaMethod)
  279. {
  280. oqFaMethod = IBFaMethod.Undefined;
  281. switch (ibFaMethod)
  282. {
  283. case FinancialAdvisorAllocationMethod.AvailableEquity:
  284. oqFaMethod = IBFaMethod.AvailableEquity;
  285. break;
  286. case FinancialAdvisorAllocationMethod.EqualQuantity:
  287. oqFaMethod = IBFaMethod.EqualQuantity;
  288. break;
  289. case FinancialAdvisorAllocationMethod.NetLiquidity:
  290. oqFaMethod = IBFaMethod.NetLiq;
  291. break;
  292. case FinancialAdvisorAllocationMethod.PercentChange:
  293. oqFaMethod = IBFaMethod.PctChange;
  294. break;
  295. case FinancialAdvisorAllocationMethod.None:
  296. default:
  297. return false;
  298. }
  299. return true;
  300. }
  301. #endregion
  302. }
  303. }