C#整合Okex Api(區塊鏈相關數字貨幣行情獲取、交易及資訊開發)
阿新 • • 發佈:2019-01-27
交易客戶端是用C#開發語言實現,前端介面使用WPF前端框架,通過HTTP 客戶端連線Okex交易所,獲得各個數字貨幣的行情資料。 Okex提供了兩種風格的Api,一種是REST風格,是Representational State Transfer的縮寫;另一種是WebSocket,WebSocket是HTML5一種新的協議,實現了客戶端和伺服器進行全雙工通訊。Api提供的主要功能:
1、獲取市場最新行情
2、獲取買賣深度資訊
3、查詢可用和凍結金額
4、查詢自己當前尚未成交的掛單
5、快速買進賣出
6、批量撤單
7、快速提現到您的認證地址
Okex提供了C#、C++、JAVA、PHP、Python 版本的開發示例,為了避免重複造輪子,我們集成了C#示例部分的程式碼。具體如下:
新建Okcoin目錄,把future、rest、stock對應的目錄整合到專案。rest對應http rest相關操作的類的封裝;future對應合約相關的api;stock為現貨行情和交易的相關的api。
整個介面分為選單項、交易所tab頁、行情分類、行情列表等,介面如下:
為了避免介面阻塞,行情資料和K線資料通過建立執行緒獲得,執行緒和主介面的互動通過delegate代理實現。行情執行緒類:
BaseThread基礎執行緒類:using BlockchainDeal.common; using BlockchainDeal.entity; using com.okcoin.rest.future; using com.okcoin.rest.stock; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; using System.Threading; using System.Threading.Tasks; namespace BlockchainDeal.MyThread { class TickerThread:BaseThread { private MainWindow.TickerDelegate tickerDelegate = null; private DataTable operDataTable; private StockRestApi okcoincnGetRequest; OperationEntity operationEntity; private string symbolEnd; public TickerThread(OperationEntity operEntity,DataTable operDt, MainWindow.TickerDelegate tickerDelegate) { okcoincnGetRequest = new StockRestApi(CommonType.OKCOINCN_URL); operDataTable = operDt; this.tickerDelegate = tickerDelegate; this.operationEntity = operEntity; this.symbolEnd = operEntity.Symbol; } public override void run() { while (true) { for (int i=0; i < this.operDataTable.Rows.Count; i++) { this.operationEntity.Symbol = this.operDataTable.Rows[i]["cointype"] + "_" + this.symbolEnd; TickerInfo tickerInfo = requestTicker();// (TickerInfo)JsonToObject(ticker, new TickerInfo()); if (tickerInfo != null) { if (tickerInfo.Ticker != null) { tickerInfo.Ticker.Date = tickerInfo.Date; tickerDelegate.BeginInvoke(this.operationEntity, tickerInfo.Ticker, null, null); } } Thread.Sleep(5000); } } //throw new NotImplementedException(); } private TickerInfo requestTicker() { TickerInfo tickerInfo = null; String ticker; switch (this.operationEntity.Exchange) { case "Okex": FutureRestApiV1 futureRest = new FutureRestApiV1(CommonType.OKEX_URL, CommonType.OKEX_APIKEY, CommonType.OKEX_SECRETKEY); //法幣行情(usdt)相對美元 if (this.operationEntity.TickerType.Equals("0")) { StockRestApi stockRest = new StockRestApi(CommonType.OKEX_URL); ticker=stockRest.ticker(this.operationEntity.Symbol); //ticker = futureRest.future_ticker(this.operationEntity.Symbol, this.operationEntity.ContractType); if(!ticker.Equals("")) tickerInfo = (TickerInfo)JsonToObject(ticker, new TickerInfo()); } //幣幣行情 if (this.operationEntity.TickerType.Equals("1")) { ticker = futureRest.future_ticker(this.operationEntity.Symbol, this.operationEntity.ContractType); tickerInfo = (TickerInfo)JsonToObject(ticker, new TickerInfo()); } //合約行情 if (this.operationEntity.TickerType.Equals("2")) { ticker = futureRest.future_ticker(this.operationEntity.Symbol, this.operationEntity.ContractType); tickerInfo = (TickerInfo)JsonToObject(ticker, new TickerInfo()); } break; case "Okcoin": break; case "Huobi": break; } return tickerInfo; } public static object JsonToObject(string jsonString, Object obj) { //jsonString="{\"date\":\"1515640192\",\"ticker\":{\"high\":\"340.0\",\"vol\":\"110.557\",\"last\":\"340.0\",\"low\":\"340.0\",\"buy\":\"340.0\",\"sell\":\"345.0\"}}"; DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); return serializer.ReadObject(mStream); } [DataContract] public class TickerInfo { [DataMember] private long date; public long Date { get { return date; } set { date = value; } } [DataMember] private TickerEntity ticker; public TickerEntity Ticker { get { return ticker; } set { ticker = value; } } public TickerInfo() { } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace BlockchainDeal.MyThread { abstract class BaseThread { Thread thread = null; abstract public void run(); public void start() { if (thread == null) thread = new Thread(run); thread.IsBackground = true; thread.Start(); } } }