PageRenderTime 822ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/okcoin/rest/StockClient.java

https://gitlab.com/coinmachine/ares
Java | 74 lines | 28 code | 21 blank | 25 comment | 0 complexity | bbd45475579152b6524d8bbc31d420ad MD5 | raw file
  1. package com.okcoin.rest;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.okcoin.rest.stock.IStockRestApi;
  4. import com.okcoin.rest.stock.impl.StockRestApi;
  5. import org.apache.http.HttpException;
  6. import java.io.IOException;
  7. /**
  8. * 现货 REST API 客户端请求
  9. * @author zhangchi
  10. *
  11. */
  12. public class StockClient {
  13. public static void main(String[] args) throws HttpException, IOException{
  14. String api_key = ""; //OKCoin申请的apiKey
  15. String secret_key = ""; //OKCoin 申请的secret_key
  16. String url_prex = ""; //注意:请求URL 国际站https://www.okcoin.com ; 国内站https://www.okcoin.cn
  17. /**
  18. * get请求无需发送身份认证,通常用于获取行情,市场深度等公共信息
  19. *
  20. */
  21. IStockRestApi stockGet = new StockRestApi(url_prex);
  22. /**
  23. * post请求需发送身份认证,获取用户个人相关信息时,需要指定api_key,与secret_key并与参数进行签名,
  24. * 此处对构造方法传入api_key与secret_key,在请求用户相关方法时则无需再传入,
  25. * 发送post请求之前,程序会做自动加密,生成签名。
  26. *
  27. */
  28. IStockRestApi stockPost = new StockRestApi(url_prex, api_key, secret_key);
  29. //现货行情
  30. stockGet.ticker("btc_usd");
  31. //现货市场深度
  32. stockGet.depth("btc_usd");
  33. //现货OKCoin历史交易信息
  34. stockGet.trades("btc_usd", "20");
  35. //现货用户信息
  36. stockPost.userinfo();
  37. //现货下单交易
  38. String tradeResult = stockPost.trade("btc_usd", "buy", "50", "0.02");
  39. System.out.println(tradeResult);
  40. JSONObject tradeJSV1 = JSONObject.parseObject(tradeResult);
  41. String tradeOrderV1 = tradeJSV1.getString("order_id");
  42. //现货获取用户订单信息
  43. stockPost.order_info("btc_usd", tradeOrderV1);
  44. //现货撤销订单
  45. stockPost.cancel_order("btc_usd", tradeOrderV1);
  46. //现货批量下单
  47. stockPost.batch_trade("btc_usd", "buy", "[{price:50, amount:0.02}, {price:50, amount:0.03}]");
  48. //批量获取用户订单
  49. stockPost.orders_info("0", "btc_usd", "125420341, 125420342");
  50. //获取用户历史订单信息,只返回最近七天的信息
  51. stockPost.order_history("btc_usd", "0", "1", "20");
  52. }
  53. }