PageRenderTime 167ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/coinmachine/ares
Scala | 88 lines | 74 code | 11 blank | 3 comment | 4 complexity | 37ad896a5ef75dca472dd22aa43b3663 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.{Exchange, UserInfo}
  5. /**
  6. * Created by bobo on 16/4/25.
  7. */
  8. object StrategyUtil extends Logging {
  9. def config(): (UserInfo, String, Float, Float, Float) = {
  10. val conf = new JSONObject()
  11. val exchange: Exchange = {
  12. print(Console.CYAN_B + "Choose an exchange,ok for okcoin.cn; okf for okcoin.com future; hb for huobi(okf): ")
  13. val x = readLine()
  14. x match {
  15. case "ok" => Exchange.OKCOIN
  16. case "okf" => Exchange.OKCOINCOM_FUTURE
  17. case _ =>
  18. Exchange.OKCOINCOM_FUTURE
  19. }
  20. }
  21. println(Console.MAGENTA + "Exchange you choose:" + exchange)
  22. val userInfo = {
  23. print(Console.GREEN_B + Console.RED + "Input your API key:")
  24. val apiKey = readLine()
  25. print(Console.YELLOW_B + Console.RED + "Input your Secret key:")
  26. val secretKey = readLine()
  27. new UserInfo(apiKey, secretKey, exchange)
  28. }
  29. val userInfoData = JSON.parse(userInfo.futurePostV1.future_userinfo()).asInstanceOf[JSONObject]
  30. Option(userInfoData) match {
  31. case Some(info) if !info.containsKey("error_code") =>
  32. logInfo(s"user info:$info")
  33. case _ =>
  34. logError(s"Error: $userInfoData, check whether your keys is correct and the market is match.")
  35. System.exit(-1)
  36. }
  37. val coinType: String = {
  38. print(Console.BLUE_B + Console.WHITE + "btc or ltc?(btc)")
  39. if (readLine().trim == "ltc") "ltc" else "btc"
  40. }
  41. println(Console.MAGENTA + "Coin type you choose:" + coinType)
  42. val distance: Float = {
  43. print(Console.CYAN_B + "Trade Distance(1.0):")
  44. try {
  45. readLine().trim.toFloat
  46. } catch {
  47. case e: Exception =>
  48. logWarn("Invalid trade distance giving. Use 1.0 by default.")
  49. 1.0f
  50. }
  51. }
  52. println(Console.MAGENTA + "Trade Distance you choose:" + distance)
  53. val unit: Float = {
  54. print(Console.GREEN_B + "Trade Unit(1.0):")
  55. try {
  56. readLine.trim.toFloat
  57. } catch {
  58. case e: Exception =>
  59. logWarn("Invalid trade unit giving. Use 1.0 by default.")
  60. 1.0f
  61. }
  62. }
  63. println(Console.MAGENTA + "Trade Unit you choose:" + unit)
  64. val srr: Float = {
  65. println(Console.YELLOW_B + "Stop Risk Rate(20.0):")
  66. try {
  67. readLine.trim.toFloat
  68. } catch {
  69. case e: Exception =>
  70. logWarn("Invalid Stop Risk Rate giving. Use 20.0 by default.")
  71. 20.0f
  72. }
  73. }
  74. println(Console.MAGENTA + "Stop Risk Rate you choose:" + srr)
  75. (userInfo, coinType, distance, unit, srr)
  76. }
  77. }