/// <summary> /// </summary> public override void Update(DateTime now) { base.Update(now); CancelOpenOrders(); HuobiAccountInfo info = m_huobi.GetAccountInfo(); HuobiMarketSummary summary = m_huobi.GetMarketSummary(m_market); decimal midPrice = (summary.GetAskPrice(0) + summary.GetBidPrice(0)) * 0.5M; CalculateProfit(now, midPrice, info); decimal buyPrice = MarketBase.NormalisePrice(summary.GetBidPrice(0)); decimal sellPrice = MarketBase.NormalisePrice(summary.GetAskPrice(0)); decimal amountCanBuy = MarketBase.NormaliseAmount(info.available_cny_display / buyPrice); decimal amountCanSell = MarketBase.NormaliseAmount(info.available_btc_display); bool canBuy = amountCanBuy >= Huobi.kMinAmount; bool canSell = amountCanSell >= Huobi.kMinAmount; bool dontTrade = sellPrice <= buyPrice + kMinTradeThresh; if (!dontTrade) { if (canBuy) { // we can action a buy! HuobiOrderResult result = m_huobi.Buy(m_market, buyPrice, Huobi.kMinAmount); Console.WriteLine("Buy " + Huobi.kMinAmount + "BTC at " + buyPrice); m_renderer.AddMarker(true, false, buyPrice, now); } if (canSell) { // we can action a buy! HuobiOrderResult result = m_huobi.Sell(m_market, sellPrice, Huobi.kMinAmount); Console.WriteLine("Sell " + Huobi.kMinAmount + "BTC at " + sellPrice); m_renderer.AddMarker(false, false, sellPrice, now); } } m_lastOpenOrders.AddRange(m_huobi.GetOpenOrders(m_market)); m_renderer.ReformatGraph(); }
static void Main() { // attach graph renderer Rendering renderer = new Rendering(800, 400); long lastTradeId = -1; var marketAccesses = JsonConvert.DeserializeObject <List <MarketAccess> >(File.ReadAllText("accessKeys.txt")); var huobiAccess = marketAccesses.First(e => e.Name == "Huobi"); var btceAccess = marketAccesses.First(e => e.Name == "BTCE"); IMarket huobi = new Huobi(huobiAccess.AccessKey, huobiAccess.SecretKey); //huobi = new BTCeMarket(btceAccess.AccessKey, btceAccess.SecretKey); AlgoBase alogo = new NaiveMarketMaker(huobi, HuobiMarket.btc, renderer); BcwTrade lastTrade = null; TimeSpan timeOffset = new TimeSpan(); DateTime lastTime = new DateTime(); while (true) { try { List <BcwTrade> newTrades = huobi.GetPublicTrades(BcwMarket.huobibtccny, lastTradeId); newTrades.Reverse(); HuobiMarketSummary depth = huobi.GetMarketSummary(HuobiMarket.btc); BcwTicker ticker = huobi.GetTicker(BcwMarket.huobibtccny); DateTime now = UnixTime.ConvertToDateTime(ticker.date) + timeOffset; if (newTrades.Count > 0) { if (timeOffset.TotalSeconds == 0) { DateTime firstTradeDate = UnixTime.ConvertToDateTime(newTrades[0].date); if (firstTradeDate < lastTime) { timeOffset = firstTradeDate - lastTime; } } foreach (BcwTrade t in newTrades) { if (t.trade_type == BcwOrderType.ask) { // this condition means that a BUY ORDER was filled } else { // this condition means that a SELL ORDER was filled } renderer.AddDataPoint(depth.GetBidPrice(0), depth.GetAskPrice(0), t.price, UnixTime.ConvertToDateTime(t.date)); } lastTrade = newTrades.Last(); lastTradeId = newTrades.Last().tid; now = UnixTime.ConvertToDateTime(lastTrade.date); } else { renderer.AddDataPoint(depth.GetBidPrice(0), depth.GetAskPrice(0), lastTrade.price, now); } // // update the algorithm // alogo.Update(now); } catch (HuobiApi.RetryCountExceededException) { } catch (Newtonsoft.Json.JsonReaderException e) { Console.WriteLine(e.ToString()); } Thread.Sleep(5000); } }