PageRenderTime 148ms CodeModel.GetById 38ms RepoModel.GetById 7ms app.codeStats 1ms

/src/main/scala/com.coinmachine.ares/trade/stock/WaveCutter.scala

https://gitlab.com/coinmachine/ares
Scala | 122 lines | 106 code | 12 blank | 4 comment | 19 complexity | 04dfb5078b7bbc421370feb3b8641a28 MD5 | raw file
  1. package com.coinmachine.ares.trade.stock
  2. import com.alibaba.fastjson.{JSONObject, JSON}
  3. import com.coinmachine.ares.Logging
  4. import com.coinmachine.ares.trade.{Exchange, UserInfo, Order, Strategy}
  5. import com.coinmachine.ares.util.{StrategyUtil, LoopQuery}
  6. import scala.collection.mutable
  7. /**
  8. *
  9. */
  10. class WaveCutter(val user: UserInfo) extends Strategy with Logging{
  11. val upPlace = new mutable.Stack[Order]()
  12. val downPlace = new mutable.Stack[Order]()
  13. val coinType = "ltc"
  14. val currency = "cny"
  15. val orderDistance = 0.01f
  16. val tradeUnit = 1F
  17. override def init(): Unit = {
  18. val ticker = JSON.parse(user.stockGetV1.ticker(coinType + "_" + currency)).asInstanceOf[JSONObject].getJSONObject("ticker")
  19. logInfo(ticker.toJSONString)
  20. Option(ticker) match {
  21. case Some(t) =>
  22. val buyOne = t.getFloatValue("buy")
  23. placeUpDownOrders(upPlace, downPlace, buyOne)
  24. case None =>
  25. logError("Can't get ticker.")
  26. }
  27. }
  28. def placeUpDownOrders(upPlace: mutable.Stack[Order], downPlace: mutable.Stack[Order], price: Float): Unit = {
  29. //check whether balance is enough to place an order at best.
  30. val downOrderPrice = price - orderDistance
  31. if (!downPlace.exists(o => o.price == downOrderPrice)) {
  32. val orderId = LoopQuery.placeStockOrder(user, coinType + "_" + currency, "buy", downOrderPrice, tradeUnit)
  33. downPlace.push(new Order(orderId, downOrderPrice))
  34. Thread.sleep(20L)
  35. logInfo(s"Place an buy order:$orderId with price:$downOrderPrice and amount:$tradeUnit")
  36. } else {
  37. logInfo(s"Already placed an buy order with price:" + downOrderPrice)
  38. }
  39. val upOrderPrice = price + orderDistance
  40. if (!upPlace.exists(o => o.price == upOrderPrice)) {
  41. val orderId = LoopQuery.placeStockOrder(user, coinType + "_" + currency, "sell", upOrderPrice, tradeUnit)
  42. upPlace.push(new Order(orderId, upOrderPrice))
  43. Thread.sleep(20L)
  44. logInfo(s"Place an buy order:$orderId with price:$upOrderPrice and amount:$tradeUnit")
  45. } else {
  46. logInfo(s"Already placed an buy order with price:" + upOrderPrice)
  47. }
  48. }
  49. def processDeals(): Unit = {
  50. try {
  51. if (upPlace.nonEmpty) {
  52. val order = upPlace.top
  53. logDebug("Top Order of upPlace:" + order)
  54. val orderInfo = LoopQuery.stockOrderInfo(user, coinType + "_" + currency, order.id)
  55. Option(orderInfo.getIntValue("status")) match {
  56. case Some(oi) =>
  57. Option(orderInfo.getIntValue("status")) match {
  58. case Some(x) if x == -1 => //already canceled,被人工取消了理应再放一次
  59. upPlace.pop()
  60. case Some(x) if x == 2 => //totally concluded
  61. upPlace.pop()
  62. val price = orderInfo.getFloatValue("price")
  63. placeUpDownOrders(upPlace, downPlace, price)
  64. case _ =>
  65. }
  66. case None =>
  67. logError(s"Can't get order info for $order")
  68. }
  69. } else {
  70. logWarn(s"upPlace is empty.$upPlace")
  71. }
  72. if (downPlace.nonEmpty) {
  73. val order = downPlace.top
  74. val orderInfo = LoopQuery.stockOrderInfo(user, coinType + "_" + currency, order.id)
  75. Option(orderInfo) match {
  76. case Some(oi) =>
  77. Option(orderInfo.getIntValue("status")) match {
  78. case Some(x) if x == -1 =>
  79. downPlace.pop()
  80. case Some(x) if x == 2 =>
  81. downPlace.pop()
  82. val price = orderInfo.getFloatValue("price")
  83. placeUpDownOrders(upPlace, downPlace, price)
  84. case _ =>
  85. }
  86. case None =>
  87. logError(s"Can't get order info for $order")
  88. }
  89. } else {
  90. logWarn(s"downPlace is empty:$downPlace")
  91. }
  92. } catch {
  93. case e: Exception =>
  94. logError("Exception when placing order", e)
  95. }
  96. }
  97. override def run(): Unit = {
  98. init()
  99. while (!shutDownFlag) {
  100. processDeals()
  101. }
  102. }
  103. }
  104. object WaveCutter {
  105. def main(args: Array[String]) {
  106. val (userInfo, coinType, distance, unit, srr) = StrategyUtil.config()
  107. val waveCutter = new WaveCutter(userInfo)
  108. new Thread(waveCutter).run()
  109. }
  110. }