交易客户端是用C#开发语言实现,前端界面使用WPF前端框架,通过HTTP 客户端连接Okex交易所,获得各个数字货币的行情数据。 Okex提供了两种风格的Api,一种是REST风格,是Representational State Transfer的缩写;另一种是WebSocket,WebSocket是HTML5一种新的协议,实现了客户端和服务器进行全双工通信。Api提供的主要功能:
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();
}
}
}