Skip to content

Commit

Permalink
zerodhaDownloader support for new market convention - Market.India (Q…
Browse files Browse the repository at this point in the history
…uantConnect#5775)

* zerodhaDownloader support for India Market

(cherry picked from commit c36272e)

* indenation styling fix

* zerodha donwloader

* Market.India support from zerodhaSymbolMapper

* code styling changes
  • Loading branch information
rjra2611 authored Jul 19, 2021
1 parent f75e81b commit e2e2b5f
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions ToolBox/ZerodhaDownloader/ZerodhaDataDownloaderProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ public class ZerodhaDataDownloaderProgram
/// </summary>
public static void ZerodhaDataDownloader(IList<string> tickers, string market, string resolution, string securityType, DateTime startDate, DateTime endDate)
{

if (resolution.IsNullOrEmpty() || tickers.IsNullOrEmpty())
{
Console.WriteLine("ZerodhaDataDownloader ERROR: '--tickers=', --securityType, '--market' or '--resolution=' parameter is missing");
Console.WriteLine("--tickers=eg JSWSTEEL,TCS,INFY");
Console.WriteLine("--market=MCX/NSE/NFO/CDS/BSE");
Console.WriteLine("--security-type=Equity/Future/Option/Commodity");
Console.WriteLine("--resolution=Minute/Hour/Daily/Tick");
Log.Error("ZerodhaDataDownloader ERROR: '--tickers=', --securityType, '--market' or '--resolution=' parameter is missing");
Log.Error("--tickers=eg JSWSTEEL,TCS,INFY");
Log.Error("--market=MCX/NSE/NFO/CDS/BSE");
Log.Error("--security-type=Equity/Future/Option/Commodity");
Log.Error("--resolution=Minute/Hour/Daily/Tick");
Environment.Exit(1);
}
try
{
var _kite = new Kite(_apiKey, _accessToken);
var kite = new Kite(_apiKey, _accessToken);
var symbolMapper = new ZerodhaSymbolMapper(kite);
var castResolution = (Resolution)Enum.Parse(typeof(Resolution), resolution);
var castSecurityType = (SecurityType)Enum.Parse(typeof(SecurityType), securityType);

// Load settings from config.json and create downloader
var dataDirectory = Config.Get("data-directory", "../../../Data");
var dataDirectory = Config.Get("data-folder", "../../../Data");

foreach (var pair in tickers)
{
var quoteTicker = market + ":" + pair;
var instrumentQuotes = _kite.GetQuote(new string[] { quoteTicker });
var quote = instrumentQuotes[quoteTicker];
var zerodhaTokenList = symbolMapper.GetZerodhaInstrumentTokenList(pair);

// Download data
var pairObject = Symbol.Create(pair, castSecurityType, market);
Expand All @@ -84,7 +84,7 @@ public static void ZerodhaDataDownloader(IList<string> tickers, string market, s
// Write data
var writer = new LeanDataWriter(castResolution, pairObject, dataDirectory);
IList<TradeBar> fileEnum = new List<TradeBar>();
var history = new List<Historical>();
IEnumerable<Historical> history = new List<Historical>();
var timeSpan = new TimeSpan();
switch (castResolution)
{
Expand All @@ -95,21 +95,21 @@ public static void ZerodhaDataDownloader(IList<string> tickers, string market, s

if ((end - start).Days > 60)
throw new ArgumentOutOfRangeException("For minutes data Zerodha support 60 days data download");
history = _kite.GetHistoricalData(quote.InstrumentToken.ToStringInvariant(), start, end, "minute").ToList();
history = GetHistoryFromZerodha(kite, zerodhaTokenList, startDate, endDate, "minute");
timeSpan = Time.OneMinute;
break;

case Resolution.Hour:
if ((end - start).Days > 400)
throw new ArgumentOutOfRangeException("For daily data Zerodha support 400 days data download");
history = _kite.GetHistoricalData(quote.InstrumentToken.ToStringInvariant(), start, end, "60minute").ToList();
history = GetHistoryFromZerodha(kite, zerodhaTokenList, startDate, endDate, "60minute");
timeSpan = Time.OneHour;
break;

case Resolution.Daily:
if ((end - start).Days > 400)
throw new ArgumentOutOfRangeException("For daily data Zerodha support 400 days data download");
history = _kite.GetHistoricalData(quote.InstrumentToken.ToStringInvariant(), start, end, "day").ToList();
history = GetHistoryFromZerodha(kite, zerodhaTokenList, startDate, endDate, "day");
timeSpan = Time.OneDay;
break;
}
Expand All @@ -128,7 +128,18 @@ public static void ZerodhaDataDownloader(IList<string> tickers, string market, s
{
Log.Error($"ZerodhaDataDownloadManager.OnError(): Message: {err.Message} Exception: {err.InnerException}");
}
}

private static IEnumerable<Historical> GetHistoryFromZerodha(Kite kite, List<uint> zerodhaTokenList, DateTime startDate, DateTime endDate, string interval)
{
var history = Enumerable.Empty<Historical>();
foreach (var token in zerodhaTokenList)
{
var tempHistory = kite.GetHistoricalData(token.ToStringInvariant(), startDate, endDate, interval);
history = history.Concat(tempHistory);
}
history = history.OrderBy(x=>x.TimeStamp);
return history;
}
}
}

0 comments on commit e2e2b5f

Please sign in to comment.