Beispiel #1
0
        private static void TransactionParse(string str, TWTransaction tr)
        {
            switch (str.ToLower())
            {
            case "buy to close":
                tr.BuySell   = "Buy";
                tr.OpenClose = "Close";
                break;

            case "buy to open":
                tr.BuySell   = "Buy";
                tr.OpenClose = "Open";
                break;

            case "sell to close":
                tr.BuySell   = "Sell";
                tr.OpenClose = "Close";
                break;

            case "sell to open":
                tr.BuySell   = "Sell";
                tr.OpenClose = "Open";
                break;
            }
        }
Beispiel #2
0
        private static void CompleteInstance(TWTransaction tr)
        {
            if ((tr.TransactionSubcode == "Sell to Open") || (tr.TransactionSubcode == "Sell to Close"))
            {
                tr.Quantity *= -1;
            }

            if (tr.TransactionCode == "Receive Deliver")
            {
                if (tr.TransactionSubcode == "Assignment")
                {
                    tr.OpenClose = "Close";
                }
                else if (tr.TransactionSubcode == "Exercise")
                {
                    tr.OpenClose = "Close";
                    tr.Quantity *= -1;
                }
                else if (tr.TransactionSubcode == "Expiration")
                {
                    tr.BuySell   = "Expired";
                    tr.OpenClose = "Close";
                }
                else if (tr.TransactionSubcode == "Forward Split")
                {
                    if ((tr.Action == "Sell to Open") || (tr.Action == "Sell to Close"))
                    {
                        tr.Quantity *= -1;
                    }
                    TransactionParse(tr.Action, tr);
                }
                else
                {
                    // all that's left is Sell to Open and Buy to Open
                    tr.InsType = "Stock";
                    string[] s = tr.TransactionSubcode.Split(' ');
                    if (s.Length == 3)
                    {
                        tr.BuySell   = s[0];
                        tr.OpenClose = s[2];
                    }
                }
            }
            else if (tr.TransactionCode == "Trade")
            {
                TransactionParse(tr.TransactionSubcode, tr);
            }
        }
Beispiel #3
0
        public static TWTransactions Transactions(string accountNumber, DateTime?start, DateTime?end)
        {
            SetHeaders(Token);

            string url = "https://api.tastyworks.com/accounts/" + accountNumber + "/transactions?";

            if (start != null)
            {
                url += "start-date=" + String.Format("{0:yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'}", start) + "&";
            }
            if (end != null)
            {
                url += "end-date=" + String.Format("{0:yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'}", end);
            }

            string reply = Web.DownloadString(url);
            //Debug.WriteLine(reply);
            JObject package = JObject.Parse(reply);

            TWTransactions returnList = new TWTransactions();

            Int32         pages      = Convert.ToInt32(package["pagination"]["total-pages"]);
            Int32         pageOffset = 0;
            List <JToken> list       = package["data"]["items"].Children().ToList();

            do
            {
                foreach (JToken item in list)
                {
                    //Debug.WriteLine(item.ToString());

                    TWTransaction inst = new TWTransaction();
                    inst.TransID = Convert.ToInt32(item["id"]);
                    inst.Time    = Convert.ToDateTime(item["executed-at"]).ToUniversalTime();

                    inst.TransactionCode    = item["transaction-type"].ToString();
                    inst.TransactionSubcode = item["transaction-sub-type"].ToString();
                    if (item["action"] != null)
                    {
                        inst.Action = item["action"].ToString();
                    }
                    inst.Description = item["description"].ToString();
                    inst.AccountRef  = item["account-number"].ToString();

                    inst.Price  = Convert.ToDecimal(item["price"]);
                    inst.Fees   = Convert.ToDecimal(item["commission"]) + Convert.ToDecimal(item["clearing-fees"]) + Convert.ToDecimal(item["regulatory-fees"]);
                    inst.Amount = Convert.ToDecimal(item["value"]) * ((item["value-effect"].ToString() == "Debit") ? -1 : 1);

                    if ((inst.TransactionCode == "Trade") || (inst.TransactionCode == "Receive Deliver"))
                    {
                        inst.Symbol   = item["underlying-symbol"].ToString();
                        inst.Quantity = Convert.ToDecimal(item["quantity"]);

                        SymbolDecoder symbol = new SymbolDecoder(item["symbol"].ToString(), item["instrument-type"].ToString());
                        inst.InsType    = symbol.Type;
                        inst.ExpireDate = symbol.Expiration;
                        inst.Strike     = symbol.Strike;
                    }
                    CompleteInstance(inst);

                    returnList.Add(inst);
                }

                if (pages > 1)
                {
                    SetHeaders(Token);
                    reply   = Web.DownloadString(url + "&page-offset=" + ++pageOffset);
                    package = JObject.Parse(reply);
                    list    = package["data"]["items"].Children().ToList();
                }

                pages--;
            } while (pages > 0);

            return((returnList.Count > 0) ? returnList : null);
        }