PageRenderTime 79ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/com.coinmachine.ares/util/LoopQuery.scala

https://gitlab.com/coinmachine/ares
Scala | 198 lines | 183 code | 12 blank | 3 comment | 45 complexity | cee09e1c9f84e5ca73b867f198712f6c MD5 | raw file
  1. package com.coinmachine.ares.util
  2. import com.alibaba.fastjson.{JSON, JSONObject}
  3. import com.coinmachine.ares.Logging
  4. import com.coinmachine.ares.trade.UserInfo
  5. /**
  6. * Created by bobo on 16/1/3.
  7. */
  8. object LoopQuery extends Logging {
  9. def futurePosition(user: UserInfo, symbol: String, contractType: String, retryTimes: Int = 100000): JSONObject = {
  10. for (retry <- 0 to retryTimes) {
  11. try {
  12. val pos = JSON.parse(user.futurePostV1.future_position(symbol, contractType)).asInstanceOf[JSONObject]
  13. if (Option(pos).nonEmpty) {
  14. logDebug(s"Future position(symbol:$symbol, contractType:$contractType): $pos")
  15. return pos
  16. } else {
  17. logInfo(s"Illegal future position(symbol:$symbol, contractType:$contractType):$pos")
  18. }
  19. } catch {
  20. case e: Exception =>
  21. if (retry % 1000 == 1) logWarn(s"Can't get future position(symbol:$symbol, contractType:$contractType)", e)
  22. else logDebug(s"Can't get future position(symbol:$symbol, contractType:$contractType)")
  23. }
  24. }
  25. null
  26. }
  27. def futureUserInfo(user: UserInfo, retryTimes: Int = 100000): JSONObject = {
  28. for (retry <- 0 to retryTimes) {
  29. try {
  30. val userInfo = JSON.parse(user.futurePostV1.future_userinfo()).asInstanceOf[JSONObject]
  31. if (userInfo.containsKey("info")) {
  32. logDebug(s"userInfo:$userInfo")
  33. return userInfo.getJSONObject("info")
  34. } else {
  35. logInfo("Illegal user info:" + userInfo)
  36. }
  37. Thread.sleep(1000)
  38. } catch {
  39. case e: Exception =>
  40. if (retry % 1000 == 1) logWarn(s"Can't get User Info. Retry($retry)", e)
  41. else logDebug(s"Can't get User info. Retry($retry)")
  42. }
  43. }
  44. null
  45. }
  46. def stockOrderInfo(user: UserInfo, symbol: String, orderId: String, retryTimes: Int = 100000): JSONObject = {
  47. var retry = 0
  48. var order: JSONObject = null
  49. while (retry < retryTimes) {
  50. retry += 1
  51. try {
  52. val orders = JSON.parse(user.stockPostV1.order_info(symbol, orderId)).asInstanceOf[JSONObject].getJSONArray("orders")
  53. Option(orders) match {
  54. case Some(m) =>
  55. if (m.getJSONObject(0).getString("order_id") == orderId) {
  56. retry = retryTimes
  57. order = m.getJSONObject(0)
  58. } else {
  59. logInfo("Order id" + m.getJSONObject(0).getString("order_id") + " is not Expected:" + orderId)
  60. }
  61. case None =>
  62. }
  63. Thread.sleep(1000)
  64. } catch {
  65. case e: Exception =>
  66. if (retry % 1000 == 1) logWarn("Can't get Order Info for:" + orderId, e)
  67. else logInfo("Can't get Order info for:" + orderId)
  68. }
  69. }
  70. order
  71. }
  72. def futureOrderInfo(user: UserInfo, symbol: String, contractType: String, orderId: String, retryTimes: Int = 100000): JSONObject = {
  73. for (retry <- 0 to retryTimes) {
  74. try {
  75. val orders = JSON.parse(user.futurePostV1.future_orders_info(symbol, contractType, orderId)).asInstanceOf[JSONObject].getJSONArray("orders")
  76. Option(orders) match {
  77. case Some(m) =>
  78. if (m.getJSONObject(0).getString("order_id") == orderId) {
  79. return m.getJSONObject(0)
  80. } else {
  81. logInfo("Order id" + m.getJSONObject(0).getString("order_id") + " is not Expected:" + orderId)
  82. }
  83. case None =>
  84. }
  85. Thread.sleep(1000)
  86. logInfo(s"orders:$orders")
  87. } catch {
  88. case e: Exception =>
  89. if (retry % 1000 == 1) logWarn(s"Can't get Order info for: $orderId", e)
  90. else logInfo(s"Can't get Order info for: $orderId")
  91. }
  92. }
  93. return null
  94. }
  95. def placeStockOrder(user: UserInfo, symbol: String, placeType: String, price: Float, amount: Float, retryTimes: Int = 100000): String = {
  96. var retry = 0
  97. var orderId: String = null
  98. while (retry < retryTimes) {
  99. retry += 1
  100. try {
  101. val tmpOrderId = JSON.parse(user.stockPostV1.trade(symbol, placeType, price.toString, amount.toString))
  102. .asInstanceOf[JSONObject].getString("order_id")
  103. Option(tmpOrderId) match {
  104. case Some(m) =>
  105. retry = retryTimes
  106. orderId = tmpOrderId
  107. case None =>
  108. logInfo("Can't get orderId, re-place an order")
  109. }
  110. Thread.sleep(1000)
  111. } catch {
  112. case e: Exception =>
  113. if (retry % 1000 == 1) logWarn("Can't place an order for:" + orderId, e)
  114. else logInfo("Can't get Order info for:" + orderId)
  115. }
  116. }
  117. Option(orderId) match {
  118. case Some(id) =>
  119. id
  120. case _ =>
  121. throw new IllegalStateException("Can't place an order")
  122. }
  123. }
  124. def placeFutureOrder(user: UserInfo, symbol: String, contractType: String, price: Float, amount: Float, ot: String, retryTimes: Int = 100000): String = {
  125. for (retry <- 0 to retryTimes) {
  126. try {
  127. Thread.sleep(100)
  128. val tmpOrderId = JSON.parse(user.futurePostV1.future_trade(symbol, contractType, price.toString, amount.toString, ot, "0"))
  129. .asInstanceOf[JSONObject].getString("order_id")
  130. logInfo(s"place future orderId:$tmpOrderId")
  131. Option(tmpOrderId) match {
  132. case Some(m) =>
  133. return m
  134. case _ =>
  135. throw new IllegalStateException("Can't place an order")
  136. case None =>
  137. logInfo("Can't get orderId, re-place an order")
  138. }
  139. } catch {
  140. case e: Exception =>
  141. if (retry % 1000 == 1) logWarn("Can't place an order", e)
  142. else logInfo("Can't place an order")
  143. }
  144. }
  145. return null
  146. }
  147. def futureTicker(user: UserInfo, symbol: String, contractType: String, retryTimes: Int = 10000): JSONObject = {
  148. for (retry <- 0 to retryTimes) {
  149. try {
  150. val ticker = JSON.parse(user.futureGetV1.future_ticker(symbol, contractType)).asInstanceOf[JSONObject]
  151. if (ticker.containsKey("ticker")) {
  152. logDebug(s"Ticker: $ticker")
  153. return ticker.getJSONObject("ticker")
  154. }
  155. Thread.sleep(100)
  156. } catch {
  157. case e: Exception =>
  158. if (retry % 1000 == 1) logWarn("Can't get future ticker.", e)
  159. else logInfo("Can't get future ticker.")
  160. }
  161. }
  162. return null
  163. }
  164. def cancelStockOrder(user: UserInfo, symbol: String, order: String, retryTimes: Int = 10000): Unit = {
  165. var retry = 0
  166. while (retry < retryTimes) {
  167. retry += 1
  168. try {
  169. val tmpOrderId = JSON.parse(user.stockPostV1.cancel_order(symbol, order))
  170. .asInstanceOf[JSONObject].getString("order_id")
  171. Thread.sleep(1000)
  172. Option(tmpOrderId) match {
  173. case Some(oid) if order == oid =>
  174. retry = retryTimes
  175. logInfo(s"Cancel order: $order")
  176. case None =>
  177. logInfo(s"Can't get orderId, re-cancel order: $order")
  178. }
  179. } catch {
  180. case e: Exception =>
  181. if (retry % 1000 == 1) logWarn(s"Can't calcel order: $order", e)
  182. else logInfo(s"Can't calcel order: $order")
  183. }
  184. }
  185. }
  186. }