private Portfolio parsePortfolio(string json, bool isOAuth, bool isHistory) { JObject r = JObject.Parse(json); Portfolio pf = new Portfolio(); // Parse simple fields like username & generation time. string st = (string)r["generated"]; string[] formats = { "MM/dd/yyyy HH:mm:ss" }; pf.lastUpdate = DateTime.ParseExact(st, formats, new CultureInfo("en-US"), DateTimeStyles.None); if (isOAuth || !isHistory) pf.balance = BTCTUtils.StringToSatoshi((string)r["balance"][_coin]); if (isOAuth) pf.apiKey = (string)r["api_key"]; if (isOAuth) pf.username = (string)r["username"]; // Parse list of currently held securities. List<SecurityOwned> SOList = new List<SecurityOwned>(); foreach (JProperty c in r["securities"].Children()) { Security s = new Security(); s.name = c.Name; int a = Convert.ToInt32((string)c.First["quantity"]); SecurityOwned so = new SecurityOwned(s, a); SOList.Add(so); } pf.securities = SOList; // Parse list of active orders if (isOAuth) { List<Order> OList = new List<Order>(); foreach (JProperty c in r["orders"].Children()) { Order o = new Order(); Security s = new Security(); o.id = Convert.ToInt32(c.Name); JToken c2 = c.First; s.name = (string)c2["ticker"]; o.security = s; o.amount = Convert.ToInt32((string)c2["quantity"]); o.price = BTCTUtils.StringToSatoshi((string)c2["amount"]); o.orderType = BTCTUtils.StringToOrderType((string)c2["type"]); OList.Add(o); } pf.orders = OList; } return pf; }
private TradeHistory parsePublicTradeHistory(string s, bool isSingle) { List<Order> OList = new List<Order>(); TradeHistory t = new TradeHistory(); JContainer r; if (s.ToUpper().Contains("INVALID TICKER")) { throw (new BTCTException("Invalid ticker.")); } if (isSingle) { try { r = JArray.Parse(s); } catch (Newtonsoft.Json.JsonReaderException ex) { throw (new BTCTException("Invalid response format.")); } } else { try { r = JObject.Parse(s); } catch (Newtonsoft.Json.JsonReaderException ex) { throw (new BTCTException("Invalid response format.")); } } if (!isSingle) { foreach (JProperty ch in r.Children()) { Order o = new Order(); Security sec = new Security(); JToken c = ch.First; if (c.HasValues) { o.active = false; o.id = Convert.ToInt32((string)c["trade_id"]); o.amount = Convert.ToInt32((string)c["quantity"]); o.dateTime = BTCTUtils.UnixTimeStampToDateTime(Convert.ToInt32((string)c["timestamp"])); o.price = BTCTUtils.StringToSatoshi((string)c["amount"]); o.orderType = BTCTUtils.StringToOrderType((string)c["type"]); sec.name = (string)c["ticker"]; o.security = sec; OList.Add(o); } } t.lastUpdate = BTCTUtils.UnixTimeStampToDateTime(Convert.ToInt32((string)r.Last.First)); } else { for (int i = 0; i < r.Count; i++) { Order o = new Order(); Security sec = new Security(); o.active = false; o.id = Convert.ToInt32((string)r[i]["trade_id"]); o.amount = Convert.ToInt32((string)r[i]["quantity"]); o.dateTime = BTCTUtils.UnixTimeStampToDateTime(Convert.ToInt32((string)r[i]["timestamp"])); o.price = BTCTUtils.StringToSatoshi((string)r[i]["amount"]); o.orderType = BTCTUtils.StringToOrderType((string)r[i]["type"]); sec.name = (string)r[i]["ticker"]; o.security = sec; OList.Add(o); } } t.orders = OList; return t; }
private TradeHistory parseTradeHistory(string s) { List<Order> OList = new List<Order>(); TradeHistory t = new TradeHistory(); string[] lines = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); // First line contains column headers, which we can ignore for (int i = 1; i < lines.Length; i++) { Order o = new Order(); Security se = new Security(); string[] fields = lines[i].Split(new Char[] { ',' }); o.id = Convert.ToInt32(fields[0]); se.name = fields[1]; o.orderType = BTCTUtils.StringToOrderType(fields[2]); if (o.orderType == OrderType.OT_TIN || o.orderType == OrderType.OT_TOUT) { int start = fields[2].IndexOf('('); int stop = fields[2].IndexOf(')'); o.transferUser = fields[2].Substring(start + 1, stop - start - 1); } o.amount = Convert.ToInt32(fields[3]); o.price = BTCTUtils.StringToSatoshi(fields[4]); // date/time string comes in quotes from BTCT for some reason. o.dateTime = DateTime.Parse(fields[5].Substring(1, fields[5].Length - 2)); o.security = se; OList.Add(o); } t.orders = OList; t.lastUpdate = DateTime.Now; return t; }