新建Okcoin目录,把future、rest、stock对应的目录集成到项目。rest对应http rest相关操作的类的封装;future对应合约相关的api;stock为现货行情和交易的相关的api。
整个界面分为菜单项、交易所tab页、行情分类、行情列表等,界面如下:
为了避免界面阻塞,行情数据和K线数据通过创建线程获得,线程和主界面的交互通过delegate代理实现。行情线程类:
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() { } } } } BaseThread基础线程类:
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(); } } }