/// <summary>
 /// Constructs default instance of the Zerodha Sybol Mapper
 /// </summary>
 public ZerodhaSymbolMapper(Kite kite, string exchange = "")
 {
     KnownSymbolsList = GetTradableInstrumentsList(kite, exchange);
 }
        /// <summary>
        /// Get list of tradable symbol
        /// </summary>
        /// <param name="kite">Kite</param>
        /// <param name="exchange">Exchange</param>
        /// <returns></returns>
        public List <Symbol> GetTradableInstrumentsList(Kite kite, string exchange = "")
        {
            var tradableInstruments       = kite.GetInstruments(exchange);
            var symbols                   = new List <Symbol>();
            var zerodhaInstrumentsMapping = new Dictionary <string, uint>();


            foreach (var tp in tradableInstruments)
            {
                var         securityType = SecurityType.Equity;
                var         market       = Market.NSE;
                OptionRight optionRight  = 0;

                switch (tp.InstrumentType)
                {
                //Equities
                case "EQ":
                    securityType = SecurityType.Equity;
                    break;

                //Call Options
                case "CE":
                    securityType = SecurityType.Option;
                    optionRight  = OptionRight.Call;
                    break;

                //Put Options
                case "PE":
                    securityType = SecurityType.Option;
                    optionRight  = OptionRight.Put;
                    break;

                //Stock Futures
                case "FUT":
                    securityType = SecurityType.Future;
                    break;

                default:
                    securityType = SecurityType.Base;
                    break;
                }


                switch (tp.Exchange)
                {
                case "NSE":
                    market = Market.NSE;
                    break;

                case "NFO":
                    market = Market.NFO;
                    break;

                case "CDS":
                    market = Market.CDS;
                    break;

                case "BSE":
                    market = Market.BSE;
                    break;

                case "BCD":
                    market = Market.BSE;
                    break;

                case "MCX":
                    market = Market.MCX;
                    break;

                default:
                    market = Market.NSE;
                    break;
                }


                if (securityType == SecurityType.Option)
                {
                    var strikePrice = tp.Strike;
                    var expiryDate  = tp.Expiry;
                    //TODO: Handle parsing of BCDOPT strike price
                    if (tp.Segment != "BCD-OPT")
                    {
                        var symbol = GetLeanSymbol(tp.Name.Trim().Replace(" ", ""), securityType, market, (DateTime)expiryDate, GetStrikePrice(tp), optionRight);
                        symbols.Add(symbol);
                        zerodhaInstrumentsMapping.Add(tp.TradingSymbol.Trim().Replace(" ", "") + "^" + market, tp.InstrumentToken);
                    }
                }
                if (securityType == SecurityType.Future)
                {
                    var expiryDate = tp.Expiry;
                    var symbol     = GetLeanSymbol(tp.TradingSymbol.Trim().Replace(" ", ""), securityType, market, (DateTime)expiryDate);
                    symbols.Add(symbol);
                    zerodhaInstrumentsMapping.Add(tp.TradingSymbol.Trim().Replace(" ", "") + "^" + market, tp.InstrumentToken);
                }
                if (securityType == SecurityType.Equity)
                {
                    var symbol = GetLeanSymbol(tp.TradingSymbol.Trim().Replace(" ", ""), securityType, market);
                    symbols.Add(symbol);
                    zerodhaInstrumentsMapping.Add(tp.TradingSymbol.Trim().Replace(" ", "") + "^" + market, tp.InstrumentToken);
                }
            }
            ZerodhaInstrumentsList = zerodhaInstrumentsMapping;
            return(symbols);
        }
Beispiel #3
0
        /// <summary>
        /// Get list of tradable symbol
        /// </summary>
        /// <param name="kite">Kite</param>
        /// <param name="exchange">Exchange</param>
        /// <returns></returns>
        private List <Symbol> GetTradableInstrumentsList(Kite kite, string exchange = "")
        {
            var tradableInstruments       = kite.GetInstruments(exchange);
            var symbols                   = new List <Symbol>();
            var zerodhaInstrumentsMapping = new Dictionary <string, List <SymbolData> >();
            var zerodhaTokenExchangeDict  = new Dictionary <uint, string>();

            foreach (var tp in tradableInstruments)
            {
                var securityType = SecurityType.Equity;
                var market       = Market.India;
                zerodhaTokenExchangeDict[tp.InstrumentToken] = tp.Exchange.ToLowerInvariant();
                OptionRight optionRight = 0;

                switch (tp.InstrumentType)
                {
                //Equities
                case "EQ":
                    securityType = SecurityType.Equity;
                    break;

                //Call Options
                case "CE":
                    securityType = SecurityType.Option;
                    optionRight  = OptionRight.Call;
                    break;

                //Put Options
                case "PE":
                    securityType = SecurityType.Option;
                    optionRight  = OptionRight.Put;
                    break;

                //Stock Futures
                case "FUT":
                    securityType = SecurityType.Future;
                    break;

                default:
                    securityType = SecurityType.Base;
                    break;
                }

                if (securityType == SecurityType.Option)
                {
                    var strikePrice = tp.Strike;
                    var expiryDate  = tp.Expiry;
                    //TODO: Handle parsing of BCDOPT strike price
                    if (tp.Segment != "BCD-OPT")
                    {
                        var symbol = GetLeanSymbol(tp.Name.Trim().Replace(" ", ""), securityType, market, (DateTime)expiryDate, GetStrikePrice(tp), optionRight);
                        symbols.Add(symbol);
                        var cleanSymbol = tp.TradingSymbol.Trim().Replace(" ", "");
                        if (!zerodhaInstrumentsMapping.ContainsKey(cleanSymbol))
                        {
                            zerodhaInstrumentsMapping[cleanSymbol] = new List <SymbolData>();
                        }
                        zerodhaInstrumentsMapping[cleanSymbol].Add(new SymbolData(tp.InstrumentToken, market));
                    }
                }
                if (securityType == SecurityType.Future)
                {
                    var expiryDate  = tp.Expiry;
                    var cleanSymbol = tp.TradingSymbol.Trim().Replace(" ", "");
                    var symbol      = GetLeanSymbol(cleanSymbol, securityType, market, (DateTime)expiryDate);
                    symbols.Add(symbol);
                    if (!zerodhaInstrumentsMapping.ContainsKey(cleanSymbol))
                    {
                        zerodhaInstrumentsMapping[cleanSymbol] = new List <SymbolData>();
                    }
                    zerodhaInstrumentsMapping[cleanSymbol].Add(new SymbolData(tp.InstrumentToken, market));
                }
                if (securityType == SecurityType.Equity)
                {
                    var cleanSymbol = tp.TradingSymbol.Trim().Replace(" ", "");
                    var symbol      = GetLeanSymbol(cleanSymbol, securityType, market);
                    symbols.Add(symbol);
                    if (!zerodhaInstrumentsMapping.ContainsKey(cleanSymbol))
                    {
                        zerodhaInstrumentsMapping[cleanSymbol] = new List <SymbolData>();
                    }
                    zerodhaInstrumentsMapping[cleanSymbol].Add(new SymbolData(tp.InstrumentToken, market));
                }
            }
            ZerodhaInstrumentsList            = zerodhaInstrumentsMapping;
            ZerodhaInstrumentsExchangeMapping = zerodhaTokenExchangeDict;
            return(symbols);
        }