Exemplo n.º 1
0
        public static TWMarketInfos MarketInfo(List <string> symbols)
        {
            string symbolString = "";

            foreach (string sym in symbols)
            {
                symbolString += sym + ",";
            }
            symbolString = symbolString.TrimEnd(',');

            SetHeaders(Token);
            string reply = Web.DownloadString("https://api.tastyworks.com/market-metrics?symbols=" + symbolString);

            JObject package = JObject.Parse(reply);

            TWMarketInfos returnList = new TWMarketInfos();

            List <JToken> list = package["data"]["items"].Children().ToList();

            foreach (JToken item in list)
            {
                TWMarketInfo info = new TWMarketInfo();
                info.Symbol                = item["symbol"].ToString();
                info.ImpliedVolatility     = Convert.ToDouble(item["implied-volatility-index"]);
                info.ImpliedVolatilityRank = Convert.ToDouble(item["implied-volatility-index-rank"]);
                info.DividendYield         = Convert.ToDouble(item["dividend-yield"]);
                returnList.Add(info.Symbol, info);
            }

            return((returnList.Count > 0) ? returnList : null);
        }
Exemplo n.º 2
0
        private void RetrieveCurrentData(TransactionGroup grp)
        {
            decimal currentValue       = 0;
            decimal previousCloseValue = 0;

            try
            {
                // retrieve and cache current data from tastyworks for this a subsequent passes
                if (twpositions == null)
                {
                    if (TastyWorks.ActiveSession())
                    {
                        List <string> symbols = new List <string>();

                        twpositions = new Dictionary <string, TWPositions>();
                        foreach (Account a in accounts)
                        {
                            if (a.Active)
                            {
                                // retrieve Tastyworks positions for given account
                                TWPositions pos = TastyWorks.Positions(a.ID);
                                twpositions.Add(a.ID, pos);

                                if (pos != null)
                                {
                                    foreach (KeyValuePair <string, TWPosition> p in pos)
                                    {
                                        if (!symbols.Contains(p.Value.Symbol))
                                        {
                                            symbols.Add(p.Value.Symbol);
                                        }
                                    }
                                }
                            }
                        }

                        twmarketinfo = TastyWorks.MarketInfo(symbols);  // get IV's
                    }
                }

                // ensure that positions got instanciated AND that the particular account isn't empty
                if ((twpositions != null) && (twpositions.Count > 0) && (twpositions[grp.Account] != null))
                {
                    foreach (KeyValuePair <string, Position> item in grp.Holdings)
                    {
                        Position pos = item.Value;

                        // this loop could be eliminated if the long symbol name gets persisted in database
                        foreach (KeyValuePair <string, TWPosition> p in twpositions[grp.Account])
                        {
                            TWPosition twpos = p.Value;
                            if ((pos.Symbol == twpos.Symbol) && (pos.Type == twpos.Type) && (pos.Strike == twpos.Strike) && (pos.ExpDate == twpos.ExpDate))
                            {
                                //Debug.WriteLine(twpos.Market);
                                currentValue       += pos.Quantity * twpos.Market;
                                previousCloseValue += pos.Quantity * twpos.PreviousClose * twpos.Multiplier;

                                // capture current details while we have it
                                pos.Market          = twpos.Market;
                                pos.Multiplier      = twpos.Multiplier;
                                pos.UnderlyingPrice = twpos.UnderlyingPrice;

                                // capture the underlying price from the first position for the overall group
                                if (grp.UnderlyingPrice == 0)
                                {
                                    grp.UnderlyingPrice = twpos.UnderlyingPrice;
                                }

                                // update groups order status based on any of items constituent holdings
                                grp.OrderActive = twpos.OrderActive;
                            }
                        }
                    }
                }

                grp.CurrentValue            = currentValue;
                grp.PreviousCloseValue      = previousCloseValue;
                grp.ChangeFromPreviousClose = currentValue - previousCloseValue;
                if (twmarketinfo.ContainsKey(grp.ShortSymbol))
                {
                    grp.ImpliedVolatility     = twmarketinfo[grp.ShortSymbol].ImpliedVolatility;
                    grp.ImpliedVolatilityRank = twmarketinfo[grp.ShortSymbol].ImpliedVolatilityRank;
                    grp.DividendYield         = twmarketinfo[grp.ShortSymbol].DividendYield;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("RetrieveCurrentData: " + ex.Message);
            }
        }