C#集成Okex Api(区块链相关数字货币行情获取、交易及资讯开发)

交易客户端是用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。

C#集成Okex Api(区块链相关数字货币行情获取、交易及资讯开发)_第1张图片

整个界面分为菜单项、交易所tab页、行情分类、行情列表等,界面如下:

C#集成Okex Api(区块链相关数字货币行情获取、交易及资讯开发)_第2张图片

为了避免界面阻塞,行情数据和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();
        }
    }  
}



你可能感兴趣的:(C#,区块链和数字货币)