static CollectionInfo Initialize() { CollectionInfo info = new CollectionInfo(); foreach (Exchange e in Exchange.Registered) { ExchangeCollectionInfo ei = new ExchangeCollectionInfo() { ExchangeName = e.Name, Exchange = e }; if (!ei.Exchange.Connect()) { throw new Exception("exchange not connected"); } info.Exchanges.Add(ei); foreach (Ticker ticker in ei.Exchange.Tickers) { TickerCollectionInfo ti = new TickerCollectionInfo() { CollectOrderBook = true, CollectTradingHistory = true, TickerName = ticker.Name, Ticker = ticker, Enabled = false }; ei.Tickers.Add(ti); } } return(info); }
private static void PrintExchanges(CollectionInfo info) { Console.WriteLine("Got tickers: "); foreach (var item in info.Exchanges) { foreach (var ticker in item.Tickers) { Console.WriteLine(string.Format("{0}:{1}\t\t\torder book:{1}, trading history:{2}, kline:{3}", item.Exchange.Name, ticker.CollectOrderBook, ticker.CollectTradingHistory, ticker.CollectKline)); } } }
static void Main(string[] args) { CollectionInfo info = null; if (args.Length > 0 && args[0] == "--init") { info = Initialize(); info.SaveToXml(); Console.WriteLine("collection_settings.xml initialized. press any key"); Console.ReadKey(); return; } info = new CollectionInfo(); info.RestoreFromXml(); PrintExchanges(info); Connect(info); Listen(info); while (true) { int connected = 0, total = 0; foreach (ExchangeCollectionInfo ei in info.Exchanges) { foreach (TickerCollectionInfo ti in ei.Tickers) { if (!ti.Enabled) { continue; } if (ti.CollectOrderBook) { total++; connected = connected + (ti.Ticker.IsListeningOrderBook ? 1 : 0); } if (ti.CollectTradingHistory) { total++; connected = connected + (ti.Ticker.IsListeningTradingHistory ? 1 : 0); } if (ti.CollectKline) { total++; connected = connected + (ti.Ticker.IsListeningKline ? 1 : 0); } } } Console.WriteLine(string.Format("connected (listening) {0} of {1}.", connected, total)); } StopListening(info); Console.ReadKey(); }
private static void Listen(CollectionInfo info) { foreach (ExchangeCollectionInfo e in info.Exchanges) { foreach (TickerCollectionInfo t in e.Tickers) { if (!t.Enabled) { continue; } EnsureTableCreated(t.Ticker); if (t.CollectOrderBook) { t.Ticker.OrderBookChanged += OnOrderBookChanged; t.Ticker.StartListenOrderBook(); Console.WriteLine(string.Format("{0} \t\tlistening order book", t.Ticker.Name)); } } } foreach (ExchangeCollectionInfo e in info.Exchanges) { foreach (TickerCollectionInfo t in e.Tickers) { if (!t.Enabled) { continue; } if (t.CollectTradingHistory) { t.Ticker.TradeHistoryChanged += OnTradeHistoryChanged; t.Ticker.StartListenTradingHistory(); Console.WriteLine(string.Format("{0} \t\tlistening trade hisotry", t.Ticker.Name)); } } } foreach (ExchangeCollectionInfo e in info.Exchanges) { foreach (TickerCollectionInfo t in e.Tickers) { if (!t.Enabled) { continue; } if (t.CollectKline) { t.Ticker.StartListenKlineStream(); Console.WriteLine(string.Format("{0} \t\tlistening kline", t.Ticker.Name)); } } } }
private static void StopListening(CollectionInfo info) { foreach (ExchangeCollectionInfo e in info.Exchanges) { foreach (TickerCollectionInfo t in e.Tickers) { if (!t.Enabled) { continue; } if (t.CollectOrderBook) { t.Ticker.StopListenOrderBook(); Console.WriteLine(string.Format("{0} \t\tstop listening order book", t.Ticker.Name)); } } } foreach (ExchangeCollectionInfo e in info.Exchanges) { foreach (TickerCollectionInfo t in e.Tickers) { if (!t.Enabled) { continue; } if (t.CollectTradingHistory) { t.Ticker.StopListenTradingHistory(); Console.WriteLine(string.Format("{0} \t\tstop listening trade hisotry", t.Ticker.Name)); } } } foreach (ExchangeCollectionInfo e in info.Exchanges) { foreach (TickerCollectionInfo t in e.Tickers) { if (!t.Enabled) { continue; } if (t.CollectKline) { t.Ticker.StopListenKlineStream(); Console.WriteLine(string.Format("{0} \t\tstop listening kline", t.Ticker.Name)); } } } }
private static void Connect(CollectionInfo list) { foreach (var item in list.Exchanges) { if (item.Exchange.IsConnected) { continue; } bool res = item.Exchange.Connect(); if (res) { foreach (var ticker in item.Tickers) { ticker.Ticker = item.Exchange.Tickers.FirstOrDefault(t => t.CurrencyPair.ToLower() == ticker.TickerName.ToLower()); } } Console.WriteLine(string.Format("{0} connecting: \t\t\t {1}", item.Exchange.Name, res.ToString().ToLower())); } }