Exemplo n.º 1
0
        //public static BitstampExchange Create(string key,string secret,string uid,string username)
        //{
        //    if (_instance == null)
        //    {
        //        _instance = new BitstampExchange(key, secret, uid, username);
        //    }
        //    else
        //    {
        //        if (_key == key && _secret == secret && _uid == uid && _username == username)
        //        {
        //            return _instance;
        //        }
        //        else
        //        {

        //        }
        //    }
        //    return _instance;

        //}

        public bool Subscribe(CommonLab.TradePair tp, SubscribeTypes st)
        {
            //throw new NotImplementedException();
            //订阅
            string tradingpairs = GetLocalTradingPairString(tp, st);

            if (st == SubscribeTypes.WSS)
            {
                if (_subscribedtradingpairs == null)
                {
                    _subscribedtradingpairs = new Dictionary <string, KFCC.ExchangeInterface.SubscribeInterface>();
                }
                if (_subscribedtradingpairs.ContainsKey(tradingpairs))
                {
                    //有这个订阅
                }
                else
                {
                    string raw;
                    Ticker t = GetTicker(GetLocalTradingPairString(tp), out raw);
                    Depth  d = GetDepth(GetLocalTradingPairString(tp), out raw);
                    _subscribedtradingpairs.Add(tradingpairs, new PusherHelper(tradingpairs, t, d, tp));
                    _subscribedtradingpairs[tradingpairs].TradeInfoEvent += BitstampExchange_TradeInfoEvent;
                }
            }
            if (SubscribedEvent != null)
            {
                SubscribedEvent(this, st, tp);
            }
            return(true);
        }
Exemplo n.º 2
0
 public bool Compare(TradePair x)
 {
     if (this.FromSymbol == x.FromSymbol && this.ToSymbol == x.ToSymbol)
     {
         return(true);
     }
     return(false);
 }
Exemplo n.º 3
0
 public TradingInfo(CommonLab.SubscribeTypes _type, string _tradingpair, TradePair _tp)
 {
     t           = new Ticker();
     d           = new Depth();
     trade       = new Trade();
     type        = _type;
     tradingpair = _tradingpair;
     tp          = _tp;
 }
Exemplo n.º 4
0
        public PusherHelper(string tradingpair, CommonLab.TradePair tp, string appkey = "de504dc5763aeef9ff52")
        {
            _tradingpair = tradingpair;

            _tradinginfo = new TradingInfo(SubscribeTypes.WSS, tradingpair, tp);
            Tp           = tp;
            _appkey      = appkey;
            _pusher      = new Pusher(_appkey);
            _pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
            _pusher.Error += _pusher_Error;
            _pusher.Connect();
        }
Exemplo n.º 5
0
        public bool Subscribe(CommonLab.TradePair tp, SubscribeTypes st)
        {
            //throw new NotImplementedException();
            //订阅
            string tradingpairs = GetLocalTradingPairString(tp, st);

            if (st == SubscribeTypes.WSS)
            {
                //throw new Exception("This exchange does not support wss subscribing");
                if (_subscribedtradingpairs == null)
                {
                    _subscribedtradingpairs = new Dictionary <string, KFCC.ExchangeInterface.SubscribeInterface>();
                }
                if (_subscribedtradingpairs.ContainsKey(tradingpairs))
                {
                    //有这个订阅
                }
                else
                {
                    string raw;
                    Ticker t = GetTicker(GetLocalTradingPairString(tp), out raw);
                    Depth  d = GetDepth(GetLocalTradingPairString(tp), out raw);
                    _subscribedtradingpairs.Add(tradingpairs, new RESTHelper(tp, t, d));
                    //这里根据循环给出假事件
                    //_subscribedtradingpairs[tradingpairs].TradeInfoEvent += OkCoinExchange_TradeInfoEvent;
                }
            }
            else if (st == SubscribeTypes.RESTAPI)
            {
                if (_subscribedtradingpairs == null)
                {
                    _subscribedtradingpairs = new Dictionary <string, KFCC.ExchangeInterface.SubscribeInterface>();
                }
                if (_subscribedtradingpairs.ContainsKey(tradingpairs))
                {
                    //有这个订阅
                }
                else
                {
                    string raw;
                    Ticker t = GetTicker(GetLocalTradingPairString(tp), out raw);
                    Depth  d = GetDepth(GetLocalTradingPairString(tp), out raw);
                    _subscribedtradingpairs.Add(tradingpairs, new RESTHelper(tp, t, d));
                    //_subscribedtradingpairs[tradingpairs].TradeInfoEvent += OkCoinExchange_TradeInfoEvent;
                }
            }
            if (SubscribedEvent != null)
            {
                SubscribedEvent(this, st, tp);
            }
            return(true);
        }
Exemplo n.º 6
0
        public WssHelper(CommonLab.TradePair tp, Ticker t, Depth d)
        {
            if (tp.FromSymbol.ToLower() == "bch")
            {
                _tradingpair = "bcc" + tp.ToSymbol.ToLower();
            }
            else if (tp.ToSymbol.ToLower() == "bch")
            {
                _tradingpair = tp.FromSymbol.ToLower() + "bcc";
            }
            else
            {
                _tradingpair = tp.FromSymbol.ToLower() + tp.ToSymbol.ToLower();
            }

            _tradinginfo            = new TradingInfo(SubscribeTypes.WSS, _tradingpair, tp);
            _tradinginfo.t          = t;
            _tradinginfo.d          = d;
            Tp                      = tp;
            CheckTread              = new Thread(CheckState);
            CheckTread.IsBackground = true;
            wsticker                = new WebSocket(spotwssurl + _tradingpair + "@ticker");
            wstrade                 = new WebSocket(spotwssurl + _tradingpair + "@trade");
            wsdepth                 = new WebSocket(spotwssurl + _tradingpair + "@depth20");
            wsticker.SetProxy("http://127.0.0.1:1080", "", "");
            wstrade.SetProxy("http://127.0.0.1:1080", "", "");
            wsdepth.SetProxy("http://127.0.0.1:1080", "", "");
            //wsticker.OnOpen += (sender, e) =>
            //{
            //    ws.Send("{'event':'addChannel','channel':'ok_sub_spot_"+_tradingpair+"_ticker'}");
            //    ws.Send("{'event':'addChannel','channel':'ok_sub_spot_" + _tradingpair + "_depth_20'}");
            //    ws.Send("{'event':'addChannel','channel':'ok_sub_spot_" + _tradingpair + "_deals'}");

            //    if (!CheckTread.IsAlive) CheckTread.Start();
            //};

            wsticker.OnMessage += (sender, e) =>
            {
                if (e.IsText)
                {
                    LastCommTimeStamp = DateTime.Now;

                    JObject ticker = JObject.Parse(e.Data);
                    try
                    {
                        t.High                 = Convert.ToDouble(ticker["h"].ToString());
                        t.Low                  = Convert.ToDouble(ticker["l"].ToString());
                        t.Last                 = Convert.ToDouble(ticker["C"].ToString());
                        t.Sell                 = Convert.ToDouble(ticker["a"].ToString());
                        t.Buy                  = Convert.ToDouble(ticker["b"].ToString());
                        t.Volume               = Convert.ToDouble(ticker["q"].ToString());
                        t.Open                 = 0;// Convert.ToDouble(ticker["open"].ToString());
                        t.ExchangeTimeStamp    = Convert.ToDouble(ticker["E"].ToString());
                        t.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                        //UpdateTicker(tradingpair, t);

                        _tradinginfo.t = t;
                        if (TradeInfoEvent != null)
                        {
                            TradeInfoEvent(_tradinginfo, TradeEventType.TICKER);
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine("ticker err:" + err.Message + e.Data);
                    }
                }
            };

            wstrade.OnMessage += (sender, e) =>
            {
                if (e.IsText)
                {
                    LastCommTimeStamp = DateTime.Now;

                    JObject trade = JObject.Parse(e.Data);

                    try
                    {
                        _tradinginfo.trade.TradeID           = trade["t"].ToString();
                        _tradinginfo.trade.Price             = Convert.ToDouble(trade["p"].ToString());
                        _tradinginfo.trade.Amount            = Convert.ToDouble(trade["q"].ToString());
                        _tradinginfo.trade.ExchangeTimeStamp = Convert.ToDouble(trade["E"].ToString());
                        if (trade["m"].ToString() == "m")
                        {
                            _tradinginfo.trade.Type = TradeType.Buy;
                        }
                        else
                        {
                            _tradinginfo.trade.Type = TradeType.Sell;
                        }
                        _tradinginfo.trade.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                        if (TradeInfoEvent != null)
                        {
                            TradeInfoEvent(_tradinginfo, TradeEventType.TRADE);
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine("trade err:" + err.Message + e.Data);
                    }
                }
            };
            wsdepth.OnMessage += (sender, e) =>
            {
                if (e.IsText)
                {
                    LastCommTimeStamp = DateTime.Now;

                    JObject depth = JObject.Parse(e.Data);

                    try
                    {
                        JArray jasks = JArray.Parse(depth["asks"].ToString());
                        _tradinginfo.d.Asks = new List <MarketOrder>();
                        for (int i = 0; i < jasks.Count; i++)
                        {
                            MarketOrder m = new MarketOrder();
                            m.Price  = Convert.ToDouble(JArray.Parse(jasks[i].ToString())[0]);
                            m.Amount = Convert.ToDouble(JArray.Parse(jasks[i].ToString())[1]);
                            _tradinginfo.d.AddNewAsk(m);
                        }

                        JArray jbids = JArray.Parse(depth["bids"].ToString());
                        _tradinginfo.d.Bids = new List <MarketOrder>();
                        for (int i = 0; i < jbids.Count; i++)
                        {
                            MarketOrder m = new MarketOrder();
                            m.Price  = Convert.ToDouble(JArray.Parse(jbids[i].ToString())[0]);
                            m.Amount = Convert.ToDouble(JArray.Parse(jbids[i].ToString())[1]);
                            _tradinginfo.d.AddNewBid(m);
                        }
                        _tradinginfo.d.ExchangeTimeStamp    = 0;// Convert.ToDouble(obj["timestamp"].ToString());
                        _tradinginfo.d.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                        if (TradeInfoEvent != null)
                        {
                            TradeInfoEvent(_tradinginfo, TradeEventType.ORDERS);
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine("depth err:" + err.Message + e.Data);
                    }
                }
            };
            wsticker.OnError += (sender, e) =>
            {
                Thread.Sleep(10000);
                CommonLab.Log log = new Log("/log/binance_wss_err.log");
                log.WriteLine(e.Message);
                log.WriteLine(e.Exception.StackTrace);
                wsticker.Connect();
            };
            wstrade.OnError += (sender, e) =>
            {
                Thread.Sleep(10000);
                CommonLab.Log log = new Log("/log/binance_wss_err.log");
                log.WriteLine(e.Message);
                log.WriteLine(e.Exception.StackTrace);
                wstrade.Connect();
            };
            wsdepth.OnError += (sender, e) =>
            {
                Thread.Sleep(10000);
                CommonLab.Log log = new Log("/log/binance_wss_err.log");
                log.WriteLine(e.Message);
                log.WriteLine(e.Exception.StackTrace);
                wsdepth.Connect();
            };
            wsticker.Connect();
            wsdepth.Connect();
            wstrade.Connect();
            if (!CheckTread.IsAlive)
            {
                CheckTread.Start();
            }
        }
Exemplo n.º 7
0
        public WssHelper(CommonLab.TradePair tp, Ticker t, Depth d)
        {
            _tradingpair            = tp.FromSymbol.ToLower() + tp.ToSymbol.ToLower();
            _tradinginfo            = new TradingInfo(SubscribeTypes.WSS, _tradingpair, tp);
            _tradinginfo.t          = t;
            _tradinginfo.d          = d;
            Tp                      = tp;
            CheckTread              = new Thread(CheckState);
            CheckTread.IsBackground = true;
            ws                      = new WebSocket(spotwssurl);
            ws.OnOpen              += (sender, e) =>
            {
                LastCommTimeStamp = DateTime.Now;
                ws.Send("{'event':'addChannel','channel':'" + _tradingpair + "_ticker'}");
                ws.Send("{'event':'addChannel','channel':'" + _tradingpair + "_depth'}");
                ws.Send("{'event':'addChannel','channel':'" + _tradingpair + "_trades'}");
                if (!CheckTread.IsAlive)
                {
                    CheckTread.Start();
                }
            };

            ws.OnMessage += (sender, e) =>
            {
                LastCommTimeStamp = DateTime.Now;
                if (e.IsText)
                {
                    JObject obj = null;

                    try
                    {
                        obj = JObject.Parse(e.Data);
                    }
                    catch
                    {
                        Console.WriteLine(e.Data);


                        return;
                    }



                    if (obj.Property("ping") != null)
                    {
                        long pong = Convert.ToInt64(obj["ping"].ToString());
                        ws.Send("{\"pong\": " + pong + "}");
                    }
                    if (obj.Property("status") != null)
                    {
                        if (obj["status"].ToString() == "error")
                        {
                            Exception err = new Exception(obj["err-msg"].ToString());
                            throw err;
                        }
                    }
                    if (obj.Property("channel") != null)
                    {
                        string ch = obj["channel"].ToString();
                        if (ch.IndexOf("ticker") > 0) //kline数据
                        {
                            try
                            {
                                JObject ticker = JObject.Parse(obj["ticker"].ToString());
                                t.High                 = Convert.ToDouble(ticker["high"].ToString());
                                t.Low                  = Convert.ToDouble(ticker["low"].ToString());
                                t.Last                 = Convert.ToDouble(ticker["last"].ToString());
                                t.Sell                 = Convert.ToDouble(ticker["sell"].ToString());
                                t.Buy                  = Convert.ToDouble(ticker["buy"].ToString());
                                t.Volume               = Convert.ToDouble(ticker["vol"].ToString());
                                t.Open                 = 0;// Convert.ToDouble(ticker["open"].ToString()); ;// Convert.ToDouble(ticker["open"].ToString());
                                t.ExchangeTimeStamp    = Convert.ToDouble(obj["date"].ToString()) / 1000;
                                t.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                                t.Delay                = t.LocalServerTimeStamp - t.ExchangeTimeStamp;
                                //UpdateTicker(tradingpair, t);
                                _tradinginfo.t = t;

                                TradeInfoEvent(_tradinginfo, TradeEventType.TICKER);
                            }
                            catch (Exception err)
                            {
                                throw err;
                            }
                        }
                        if (ch.IndexOf("depth") > 0) //深度数据
                        {
                            JArray jasks = JArray.Parse(obj["asks"].ToString());
                            _tradinginfo.d.Asks = new List <MarketOrder>();
                            for (int i = 0; i < jasks.Count; i++)
                            {
                                MarketOrder m = new MarketOrder();
                                m.Price  = Convert.ToDouble(JArray.Parse(jasks[i].ToString())[0]);
                                m.Amount = Convert.ToDouble(JArray.Parse(jasks[i].ToString())[1]);
                                _tradinginfo.d.AddNewAsk(m);
                            }

                            JArray jbids = JArray.Parse(obj["bids"].ToString());
                            _tradinginfo.d.Bids = new List <MarketOrder>();
                            for (int i = 0; i < jbids.Count; i++)
                            {
                                MarketOrder m = new MarketOrder();
                                m.Price  = Convert.ToDouble(JArray.Parse(jbids[i].ToString())[0]);
                                m.Amount = Convert.ToDouble(JArray.Parse(jbids[i].ToString())[1]);
                                _tradinginfo.d.AddNewBid(m);
                            }
                            _tradinginfo.d.ExchangeTimeStamp    = 0; // Convert.ToDouble(obj["tick"]["ts"]) ;// Convert.ToDouble(obj["timestamp"].ToString());
                            _tradinginfo.d.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                            _tradinginfo.d.Delay = 0;                // _tradinginfo.d.LocalServerTimeStamp - _tradinginfo.d.ExchangeTimeStamp;
                            if ((_tradinginfo.t.Buy != _tradinginfo.d.Bids[0].Price) || (_tradinginfo.t.Sell != _tradinginfo.d.Asks[0].Price))
                            {
                                TradeInfoEvent(_tradinginfo, TradeEventType.TICKER);
                            }
                            _tradinginfo.t.UpdateTickerBuyDepth(_tradinginfo.d);
                            TradeInfoEvent(_tradinginfo, TradeEventType.ORDERS);
                        }
                        if (ch.IndexOf("trades") > 0) //交易数据
                        {
                            JObject trade = JObject.Parse(obj["data"][0].ToString());

                            _tradinginfo.trade.TradeID           = trade["tid"].ToString();
                            _tradinginfo.trade.Price             = Convert.ToDouble(trade["price"].ToString());
                            _tradinginfo.trade.Amount            = Convert.ToDouble(trade["amount"].ToString());
                            _tradinginfo.trade.ExchangeTimeStamp = Convert.ToDouble(trade["date"].ToString());
                            _tradinginfo.trade.Type = Trade.GetType(trade["type"].ToString());
                            _tradinginfo.trade.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                            //UpdateTicker(tradingpair, t);



                            TradeInfoEvent(_tradinginfo, TradeEventType.TRADE);
                        }
                        if (ch.IndexOf("detail") > 0) //市场数据
                        {
                        }
                    }
                }
            };
            ws.OnError += (sender, e) =>
            {
                Thread.Sleep(10000);
                CommonLab.Log log = new Log("/log/huobi_wss_err.log");
                log.WriteLine(e.Message);
                log.WriteLine(e.Exception.StackTrace);
                ws.Connect();
            };
            ws.Connect();
        }
Exemplo n.º 8
0
        public WssHelper(CommonLab.TradePair tp, Ticker t, Depth d)
        {
            _tradingpair            = tp.FromSymbol.ToLower() + "_" + tp.ToSymbol.ToLower();
            _tradinginfo            = new TradingInfo(SubscribeTypes.WSS, _tradingpair, tp);
            _tradinginfo.t          = t;
            _tradinginfo.d          = d;
            Tp                      = tp;
            CheckTread              = new Thread(CheckState);
            CheckTread.IsBackground = true;
            ws                      = new WebSocket(spotwssurl);
            ws.OnOpen              += (sender, e) =>
            {
                ws.Send("{'event':'addChannel','channel':'ok_sub_spot_" + _tradingpair + "_ticker'}");
                ws.Send("{'event':'addChannel','channel':'ok_sub_spot_" + _tradingpair + "_depth_20'}");
                ws.Send("{'event':'addChannel','channel':'ok_sub_spot_" + _tradingpair + "_deals'}");

                //if (!CheckTread.IsAlive) CheckTread.Start();
            };

            ws.OnMessage += (sender, e) => {
                if (e.IsText)
                {
                    LastCommTimeStamp = DateTime.Now;
                    JArray jaraw = JArray.Parse(e.Data);
                    if (jaraw.Count > 0)
                    {
                        JObject  robj = JObject.Parse(jaraw[0].ToString());
                        string[] strs = robj["channel"].ToString().Split('_');
                        if (strs.Length < 2)
                        {
                            return;
                        }
                        if (strs[strs.Length - 1] == "ticker")
                        {
                            //JObject obj = JObject.Parse(robj["data"].ToString());
                            try
                            {
                                JObject ticker = JObject.Parse(robj["data"].ToString());
                                t.High              = Convert.ToDouble(ticker["high"].ToString());
                                t.Low               = Convert.ToDouble(ticker["low"].ToString());
                                t.Last              = Convert.ToDouble(ticker["last"].ToString());
                                t.Sell              = Convert.ToDouble(ticker["sell"].ToString());
                                t.Buy               = Convert.ToDouble(ticker["buy"].ToString());
                                t.Volume            = Convert.ToDouble(ticker["vol"].ToString());
                                t.Open              = 0;// Convert.ToDouble(ticker["open"].ToString());
                                t.ExchangeTimeStamp = Convert.ToDouble(ticker["timestamp"].ToString());

                                t.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                                t.Delay = t.LocalServerTimeStamp - t.ExchangeTimeStamp;
                                //UpdateTicker(tradingpair, t);

                                _tradinginfo.t = t;

                                TradeInfoEvent(_tradinginfo, TradeEventType.TICKER);
                            }
                            catch (Exception err)
                            {
                                Console.WriteLine(err.Message + e.Data);
                            }
                        }
                        else if (strs[strs.Length - 2] == "depth")
                        {
                            JObject obj = JObject.Parse(robj["data"].ToString());
                            try
                            {
                                if (obj.Property("asks") != null)
                                {
                                    _tradinginfo.d.Asks = new List <MarketOrder>();
                                    JArray jasks = JArray.Parse(obj["asks"].ToString());

                                    for (int i = 0; i < jasks.Count; i++)
                                    {
                                        MarketOrder m = new MarketOrder();
                                        m.Price  = Convert.ToDouble(JArray.Parse(jasks[i].ToString())[0]);
                                        m.Amount = Convert.ToDouble(JArray.Parse(jasks[i].ToString())[1]);
                                        _tradinginfo.d.AddNewAsk(m);
                                    }
                                }
                            }
                            catch
                            { }
                            try
                            {
                                if (obj.Property("bids") != null)
                                {
                                    JArray jbids = JArray.Parse(obj["bids"].ToString());
                                    _tradinginfo.d.Bids = new List <MarketOrder>();
                                    for (int i = 0; i < jbids.Count; i++)
                                    {
                                        MarketOrder m = new MarketOrder();
                                        m.Price  = Convert.ToDouble(JArray.Parse(jbids[i].ToString())[0]);
                                        m.Amount = Convert.ToDouble(JArray.Parse(jbids[i].ToString())[1]);
                                        _tradinginfo.d.AddNewBid(m);
                                    }
                                }
                            }
                            catch
                            { }
                            _tradinginfo.d.ExchangeTimeStamp    = Convert.ToDouble(obj["timestamp"].ToString());
                            _tradinginfo.d.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                            _tradinginfo.d.Delay = _tradinginfo.d.LocalServerTimeStamp - _tradinginfo.d.ExchangeTimeStamp;

                            _tradinginfo.t.UpdateTickerBuyDepth(_tradinginfo.d);
                            TradeInfoEvent(_tradinginfo, TradeEventType.ORDERS);
                        }
                        else if (strs[strs.Length - 1] == "deals")
                        {
                            try
                            {
                                JArray trade = JArray.Parse((JArray.Parse(robj["data"].ToString()))[0].ToString());
                                _tradinginfo.trade.TradeID           = trade[0].ToString();
                                _tradinginfo.trade.Price             = Convert.ToDouble(trade[1].ToString());
                                _tradinginfo.trade.Amount            = Convert.ToDouble(trade[2].ToString());
                                _tradinginfo.trade.ExchangeTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(Convert.ToDateTime(trade[3].ToString()));
                                _tradinginfo.trade.Type = Trade.GetType(trade[4].ToString());
                                _tradinginfo.trade.LocalServerTimeStamp = CommonLab.TimerHelper.GetTimeStampMilliSeconds(DateTime.Now);
                                //UpdateTicker(tradingpair, t);



                                TradeInfoEvent(_tradinginfo, TradeEventType.TRADE);
                            }
                            catch (Exception err)
                            {
                                Console.WriteLine(err.Message + e.Data);
                            }
                        }
                    }
                }
            };
            ws.OnError += (sender, e) =>
            {
                Thread.Sleep(1000);
                CommonLab.Log log = new Log("/log/okcoin_wss_err.log");
                log.WriteLine(e.Message);
                log.WriteLine(e.Exception.StackTrace);
                ws.Close();
                ws.Connect();
            };
            ws.Connect();
        }