Esempio n. 1
0
        private DividendHistory parsePublicDividendHistory(string s)
        {
            DividendHistory dh = new DividendHistory();
            List<Dividend> dl = new List<Dividend>();

            JContainer r;

            if (s.ToUpper().Contains("INVALID TICKER"))
            {
                throw (new BTCTException("Invalid ticker."));
            }

            try
            {
                r = JObject.Parse(s);
            }
            catch (Newtonsoft.Json.JsonReaderException ex)
            {
                throw (new BTCTException("Invalid response format."));
            }

            foreach (JProperty ch in r.Children())
            {
                Dividend d = new Dividend();
                Security sec = new Security();

                JToken c = ch.First;
                if (c.HasValues)
                {
                    d.shares = Convert.ToInt32((string)c["shares_paid"]);
                    d.shares = d.shares == 0 ? 1 : d.shares;
                    d.id = Convert.ToInt32((string)c["id"]);
                    d.dateTime = BTCTUtils.UnixTimeStampToDateTime(Convert.ToInt32((string)c["process_time"]));
                    d.dividend = BTCTUtils.StringToSatoshi((string)c["amount"]) / d.shares;
                    d.status = BTCTUtils.StringToDivStatus((string)c["status"]);
                    sec.name = (string)c["ticker"];
                    d.security = sec;
                    dl.Add(d);
                }
            }
            dh.lastUpdate = BTCTUtils.UnixTimeStampToDateTime(Convert.ToInt32((string)r.Last.First));
            dh.dividends = dl;

            return dh;
        }
Esempio n. 2
0
        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;
        }
Esempio n. 3
0
        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;
        }
Esempio n. 4
0
        private DividendHistory parseDividendHistory(string s)
        {
            DividendHistory dh = new DividendHistory();
            List<Dividend> l = new List<Dividend>();

            string[] lines = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 1; i < lines.Length; i++)
            {
                Dividend d = new Dividend();
                Security se = new Security();

                string[] fields = lines[i].Split(new Char[] { ',' });

                se.name = fields[0];
                d.shares = Convert.ToInt32(fields[1]);
                d.dividend = BTCTUtils.StringToSatoshi(fields[2]);
                d.dateTime = DateTime.Parse(fields[4].Substring(1, fields[4].Length - 2));
                d.security = se;

                l.Add(d);
            }

            dh.dividends = l;
            dh.lastUpdate = DateTime.Now;

            return dh;
        }
Esempio n. 5
0
        private ContractDetails parseContractDetails(string s)
        {
            ContractDetails c = new ContractDetails();
            JObject r;

            try
            {
                r = JObject.Parse(s);
            }
            catch (Newtonsoft.Json.JsonReaderException ex)
            {
                throw (new BTCTException("Invalid response format."));
            }

            Security sec = new Security();
            sec.name = (string)r["Ticker"];
            sec.type = BTCTUtils.StringToSecurityType((string)r["Type"]);
            c.security = sec;
            c.approved = ((string)r["Approved"]) == "1";
            c.adminLock = ((string)r["Admin Lock"]) == "1";
            c.publicTradeLock = ((string)r["Public Trade Lock"]) == "1";
            c.issuerLock = ((string)r["Issuer Lock"]) == "1";
            c.peerApproval = (string)r["Peer Approval"];
            c.sharesIssued = Convert.ToInt32((string)r["Shares Issued"]);
            c.sharesOutstanding = Convert.ToInt32((string)r["Shares Outstanding"]);
            c.issuer = (string)r["Issuer"];
            c.issuerDetail = (string)r["Issuer Detail"];

            return c;
        }
Esempio n. 6
0
 public SecurityOwned(Security s, int a)
 {
     security = s;
     amount = a;
 }
Esempio n. 7
0
        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;
        }