Files
quantconnect--lean/ToolBox/Program.cs
T
Balamurali Pandranki c22a538bad Indian stock markets support & Zerodha brokerage implementation (#4873)
* Indian stock markets & Samco and Zerodha brokerage implementations

* Build fixes & Implement multi leg orders (BracketOrder & CoverOrder)

* Build fixes

* Clean & refactor

* Clean up & remove samco brokerage

* Clean up & remove samco brokerage

* Fix Nifty, BankNifty & Sensex Index futures expiry time functions

* Fix Nifty, BankNifty & Sensex Index futures expiry time functions

* Fix Futures Expiry Testcases

* Fix Futures Expiry Testcases

* Refactor Zerodha Symbol Mapper

* Refactor Zerodha Symbol Mapper

* Add Future symbols to symbol prop db csv

* Fix Symbol Mapper context

* Fix Market Hours Database

* Fix OrderJsonConverter

* Add Zerodha AccountBaseCurrency

* Add QuantConnect License headers to new source files

* cleanup config.json formatting & tick aggregator implementation

* cleanup config.json formatting & tick aggregator implementation

* Refactor ZerodhaBrokerageModel

* Fix

* Build Fixes

* Refactor

* Refactor Brokerage class & remove TextFieldParser

* Refactor Brokerage FeeModel

* Add ZerodhaOrderProperties

* Add Refactor HistoryProvider

* Refactor CanExecuteOrder

* Refactor SymbolMapper

* Refactor market names

* Refactor & remove Zerodha subscription manager

* Refactor & remove ZerodhaWebSocketChannels

* Refactor & remove ZerodhaWebSocketChannels

* Refactor ZerodhaBrokerage

* Refactor symbol prop db

* zerodha update downloader ticker data using symbol name

* 1 Zerodha mapper class update to get instruments based on Market. 2 Zerodha Getholdings data fetch fix.

* Update market-hours-database.json

* Implement basic testcases for ZerodhaBrokerage

* Update market-hours-database.json

* Update Market.cs

* 1 Fix for Zerodha subscriber and unsubscribe 2 User of CSVHelper to read instrument list

* Rename Zerodha DataQueueHandler class implementation

* Changes related to TradeBar and emit tick

* Fix DataQueueUniverseProvider & Handle Timezone conversion in WS EmitQuotes

* Emit Order Fixes

* DataQueueHandler refactoring and build fix

* Update config.json

* Removal of IHistoryProvider impplementation

* Reverting timezone logic as already taken care by BrokerImplementation using Unix time

* Place, modify and cancel order implementation

* fix zerodha test cases

* Updating get quotes and restricting getHoldings to MIS

* Testcase Fixes

* Testcase Fixes

* Testcase Fixes

* Updating emitFillOrder

* Add ProductType to ZerodhaOrderProperties

* Addition of ZerodhaProduct Type property and test update

* Fix for unit tests and minor changes for place and update order

* Unit test fix for Zerodha

* Addition of the product type and trading segment configs

* PR review fixes

* Addition of trading segment and product type configs

* Nuget Fixes

* Fix ZerodhaBrokerage DataQueueHandler

* Cleanup OrderTypes & rm global.json

* Update UpdateOrderRequest.cs

* Removal of custom csvreader

* Implementing additional method CanPerformSelection

* Add LiveOptionChainProvider

* Clean up and add QC license headers

* Quick cleanup

* Update ZerodhaBrokerage.cs

* Fix OrderEvent timezone conversions

* use item.Unrealised for Intraday position holdings

* Refactor Option chain get instruments calls

* Refactor Option chain get instruments calls

* Review fixes

* Remove whitespaces

* Updating options strike price

* Undo time stamp change

* Optional gethistory

* Addition of comments and minor changes

* Build Fixes

* Add comments for LogType enums

* Options fix

* Fix json name in CsvInstrument

* Update tick generation in DataQueueHandler

- Use Timestamp field for both trades and quotes
- Fix incorrect bid/ask sizes
- Avoid reading depth on each tick to find top quote
- Use new Tick constructors
- Remove unnecessary locking

* Add missing null check in Utils.StringToDate

* Order fixes

- Include TriggerPending orders in GetOpenOrders
- Remove unnecessary invalid check in PlaceOrder
- Fix GetOrderPrice and GetOrderTriggerPrice

* Zerodha account balance fetch

* Removal of Futures and Options

* Remove Futures & Options Support

* Update ZerodhaBrokerage.cs

* Update ZerodhaBrokerage.cs

* Add License headers

* Add License Headers

* Refactor

* historical TradeBar & security fee calculation fixes

* fix brokerage test case

* Update ZerodhaBrokerage.cs

* Update ZerodhaBrokerageHistoryProviderTests.cs

Co-authored-by: Aman Ray <ray.aman9876@gmail.com>
Co-authored-by: Stefano Raggi <stefano.raggi67@gmail.com>
2021-03-08 18:51:54 -03:00

363 lines
19 KiB
C#

/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using QuantConnect.Configuration;
using QuantConnect.Logging;
using QuantConnect.ToolBox.AlgoSeekFuturesConverter;
using QuantConnect.ToolBox.AlgoSeekOptionsConverter;
using QuantConnect.ToolBox.Benzinga;
using QuantConnect.ToolBox.BinanceDownloader;
using QuantConnect.ToolBox.BitfinexDownloader;
using QuantConnect.ToolBox.CoarseUniverseGenerator;
using QuantConnect.ToolBox.CoinApiDataConverter;
using QuantConnect.ToolBox.CryptoiqDownloader;
using QuantConnect.ToolBox.DukascopyDownloader;
using QuantConnect.ToolBox.EstimizeDataDownloader;
using QuantConnect.ToolBox.FxcmDownloader;
using QuantConnect.ToolBox.FxcmVolumeDownload;
using QuantConnect.ToolBox.GDAXDownloader;
using QuantConnect.ToolBox.IBDownloader;
using QuantConnect.ToolBox.IEX;
using QuantConnect.ToolBox.IQFeedDownloader;
using QuantConnect.ToolBox.IVolatilityEquityConverter;
using QuantConnect.ToolBox.KaikoDataConverter;
using QuantConnect.ToolBox.KrakenDownloader;
using QuantConnect.ToolBox.NseMarketDataConverter;
using QuantConnect.ToolBox.OandaDownloader;
using QuantConnect.ToolBox.Polygon;
using QuantConnect.ToolBox.QuandlBitfinexDownloader;
using QuantConnect.ToolBox.QuantQuoteConverter;
using QuantConnect.ToolBox.RandomDataGenerator;
using QuantConnect.ToolBox.SECDataDownloader;
using QuantConnect.ToolBox.USTreasuryYieldCurve;
using QuantConnect.ToolBox.YahooDownloader;
using QuantConnect.Util;
using QuantConnect.ToolBox.SmartInsider;
using QuantConnect.ToolBox.TiingoNewsConverter;
using QuantConnect.ToolBox.ZerodhaDownloader;
namespace QuantConnect.ToolBox
{
public class Program
{
public static void Main(string[] args)
{
Log.DebuggingEnabled = Config.GetBool("debug-mode");
Log.FilePath = Path.Combine(Config.Get("results-destination-folder"), "log.txt");
Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));
var optionsObject = ToolboxArgumentParser.ParseArguments(args);
if (optionsObject.Count == 0)
{
PrintMessageAndExit();
}
var targetApp = GetParameterOrExit(optionsObject, "app").ToLowerInvariant();
if (targetApp.Contains("download") || targetApp.EndsWith("dl"))
{
var fromDate = Parse.DateTimeExact(GetParameterOrExit(optionsObject, "from-date"), "yyyyMMdd-HH:mm:ss");
var resolution = optionsObject.ContainsKey("resolution") ? optionsObject["resolution"].ToString() : "";
var market = optionsObject.ContainsKey("market") ? optionsObject["market"].ToString() : "";
var securityType = optionsObject.ContainsKey("security-type") ? optionsObject["security-type"].ToString() : "";
var tickers = ToolboxArgumentParser.GetTickers(optionsObject);
var toDate = optionsObject.ContainsKey("to-date")
? Parse.DateTimeExact(optionsObject["to-date"].ToString(), "yyyyMMdd-HH:mm:ss")
: DateTime.UtcNow;
switch (targetApp)
{
case "zdl":
case "zerodhadownloader":
ZerodhaDataDownloaderProgram.ZerodhaDataDownloader(tickers,market, resolution, securityType, fromDate, toDate);
break;
case "gdaxdl":
case "gdaxdownloader":
GDAXDownloaderProgram.GDAXDownloader(tickers, resolution, fromDate, toDate);
break;
case "cdl":
case "cryptoiqdownloader":
CryptoiqDownloaderProgram.CryptoiqDownloader(tickers, GetParameterOrExit(optionsObject, "exchange"), fromDate, toDate);
break;
case "ddl":
case "dukascopydownloader":
DukascopyDownloaderProgram.DukascopyDownloader(tickers, resolution, fromDate, toDate);
break;
case "fdl":
case "fxcmdownloader":
FxcmDownloaderProgram.FxcmDownloader(tickers, resolution, fromDate, toDate);
break;
case "fvdl":
case "fxcmvolumedownload":
FxcmVolumeDownloadProgram.FxcmVolumeDownload(tickers, resolution, fromDate, toDate);
break;
case "ibdl":
case "ibdownloader":
IBDownloaderProgram.IBDownloader(tickers, resolution, fromDate, toDate);
break;
case "iexdl":
case "iexdownloader":
IEXDownloaderProgram.IEXDownloader(tickers, resolution, fromDate, toDate);
break;
case "iqfdl":
case "iqfeeddownloader":
IQFeedDownloaderProgram.IQFeedDownloader(tickers, resolution, fromDate, toDate);
break;
case "kdl":
case "krakendownloader":
KrakenDownloaderProgram.KrakenDownloader(tickers, resolution, fromDate, toDate);
break;
case "odl":
case "oandadownloader":
OandaDownloaderProgram.OandaDownloader(tickers, resolution, fromDate, toDate);
break;
case "qbdl":
case "quandlbitfinexdownloader":
QuandlBitfinexDownloaderProgram.QuandlBitfinexDownloader(fromDate, GetParameterOrExit(optionsObject, "api-key"));
break;
case "ydl":
case "yahoodownloader":
YahooDownloaderProgram.YahooDownloader(tickers, resolution, fromDate, toDate);
break;
case "bfxdl":
case "bitfinexdownloader":
BitfinexDownloaderProgram.BitfinexDownloader(tickers, resolution, fromDate, toDate);
break;
case "mbxdl":
case "binancedownloader":
BinanceDownloaderProgram.DataDownloader(tickers, resolution, fromDate, toDate);
break;
case "secdl":
case "secdownloader":
SECDataDownloaderProgram.SECDataDownloader(
GetParameterOrExit(optionsObject, "destination-dir"),
fromDate,
toDate
);
break;
case "ecdl":
case "estimizeconsensusdownloader":
EstimizeConsensusDataDownloaderProgram.EstimizeConsensusDataDownloader();
break;
case "eedl":
case "estimizeestimatedownloader":
EstimizeEstimateDataDownloaderProgram.EstimizeEstimateDataDownloader();
break;
case "erdl":
case "estimizereleasedownloader":
EstimizeReleaseDataDownloaderProgram.EstimizeReleaseDataDownloader();
break;
case "ustycdl":
case "ustreasuryyieldcurvedownloader":
USTreasuryYieldCurveProgram.USTreasuryYieldCurveRateDownloader(
fromDate,
toDate,
GetParameterOrExit(optionsObject, "destination-dir")
);
break;
case "bzndl":
case "benzinganewsdownloader":
BenzingaProgram.BenzingaNewsDataDownloader(
fromDate,
toDate,
GetParameterOrExit(optionsObject, "destination-dir"),
GetParameterOrExit(optionsObject, "api-key")
);
break;
case "tecdl":
case "tradingeconomicscalendardownloader":
TradingEconomicsDataDownloader.TradingEconomicsCalendarDownloaderProgram.TradingEconomicsCalendarDownloader();
break;
case "pdl":
case "polygondownloader":
PolygonDownloaderProgram.PolygonDownloader(
tickers,
GetParameterOrExit(optionsObject, "security-type"),
GetParameterOrExit(optionsObject, "market"),
resolution,
fromDate,
toDate);
break;
default:
PrintMessageAndExit(1, "ERROR: Unrecognized --app value");
break;
}
}
else if (targetApp.Contains("updater") || targetApp.EndsWith("spu"))
{
switch (targetApp)
{
case "mbxspu":
case "binancesymbolpropertiesupdater":
BinanceDownloaderProgram.ExchangeInfoDownloader();
break;
default:
PrintMessageAndExit(1, "ERROR: Unrecognized --app value");
break;
}
}
else
{
switch (targetApp)
{
case "asfc":
case "algoseekfuturesconverter":
AlgoSeekFuturesProgram.AlgoSeekFuturesConverter(GetParameterOrExit(optionsObject, "date"));
break;
case "asoc":
case "algoseekoptionsconverter":
AlgoSeekOptionsConverterProgram.AlgoSeekOptionsConverter(GetParameterOrExit(optionsObject, "date"));
break;
case "ivec":
case "ivolatilityequityconverter":
IVolatilityEquityConverterProgram.IVolatilityEquityConverter(GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "source-meta-dir"),
GetParameterOrExit(optionsObject, "destination-dir"),
GetParameterOrExit(optionsObject, "resolution"));
break;
case "kdc":
case "kaikodataconverter":
KaikoDataConverterProgram.KaikoDataConverter(GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "date"),
GetParameterOrDefault(optionsObject, "exchange", string.Empty));
break;
case "cadc":
case "coinapidataconverter":
CoinApiDataConverterProgram.CoinApiDataProgram(GetParameterOrExit(optionsObject, "date"), GetParameterOrExit(optionsObject, "market"),
GetParameterOrExit(optionsObject, "source-dir"), GetParameterOrExit(optionsObject, "destination-dir"));
break;
case "nmdc":
case "nsemarketdataconverter":
NseMarketDataConverterProgram.NseMarketDataConverter(GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "destination-dir"));
break;
case "qqc":
case "quantquoteconverter":
QuantQuoteConverterProgram.QuantQuoteConverter(GetParameterOrExit(optionsObject, "destination-dir"),
GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "resolution"));
break;
case "cug":
case "coarseuniversegenerator":
CoarseUniverseGeneratorProgram.CoarseUniverseGenerator();
break;
case "rdg":
case "randomdatagenerator":
RandomDataGeneratorProgram.RandomDataGenerator(
GetParameterOrExit(optionsObject, "start"),
GetParameterOrExit(optionsObject, "end"),
GetParameterOrExit(optionsObject, "symbol-count"),
GetParameterOrDefault(optionsObject, "market", null),
GetParameterOrDefault(optionsObject, "security-type", "Equity"),
GetParameterOrDefault(optionsObject, "resolution", "Minute"),
GetParameterOrDefault(optionsObject, "data-density", "Dense"),
GetParameterOrDefault(optionsObject, "include-coarse", "true"),
GetParameterOrDefault(optionsObject, "quote-trade-ratio", "1"),
GetParameterOrDefault(optionsObject, "random-seed", null),
GetParameterOrDefault(optionsObject, "ipo-percentage", "5.0"),
GetParameterOrDefault(optionsObject, "rename-percentage", "30.0"),
GetParameterOrDefault(optionsObject, "splits-percentage", "15.0"),
GetParameterOrDefault(optionsObject, "dividends-percentage", "60.0"),
GetParameterOrDefault(optionsObject, "dividend-every-quarter-percentage", "30.0")
);
break;
case "seccv":
case "secconverter":
var start = Parse.DateTimeExact(GetParameterOrExit(optionsObject, "date"), "yyyyMMdd");
SECDataDownloaderProgram.SECDataConverter(
GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrDefault(optionsObject, "destination-dir", Globals.DataFolder),
start);
break;
case "ustyccv":
case "ustreasuryyieldcurveconverter":
USTreasuryYieldCurveProgram.USTreasuryYieldCurveConverter(
GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "destination-dir"));
break;
case "sidc":
case "smartinsiderconverter":
SmartInsiderProgram.SmartInsiderConverter(
DateTime.ParseExact(GetParameterOrExit(optionsObject, "date"), "yyyyMMdd", CultureInfo.InvariantCulture),
GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "destination-dir"),
GetParameterOrDefault(optionsObject, "source-meta-dir", null));
break;
case "tiinc":
case "tiingonewsconverter":
var date = GetParameterOrDefault(optionsObject, "date", null);
TiingoNewsConverterProgram.TiingoNewsConverter(
GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "destination-dir"),
date != null ? DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture) : (DateTime?) null);
break;
case "bzncv":
case "benzinganewsconverter":
BenzingaProgram.BenzingaNewsDataConverter(
GetParameterOrExit(optionsObject, "source-dir"),
GetParameterOrExit(optionsObject, "destination-dir"),
GetParameterOrDefault(optionsObject, "source-meta-dir", Path.Combine(Globals.DataFolder, "alternative", "benzinga")),
GetParameterOrExit(optionsObject, "date"));
break;
default:
PrintMessageAndExit(1, "ERROR: Unrecognized --app value");
break;
}
}
}
private static void PrintMessageAndExit(int exitCode = 0, string message = "")
{
if (!message.IsNullOrEmpty())
{
Console.WriteLine("\n" + message);
}
Console.WriteLine("\nUse the '--help' parameter for more information");
Console.WriteLine("Press any key to quit");
Console.ReadLine();
Environment.Exit(exitCode);
}
private static string GetParameterOrExit(IReadOnlyDictionary<string, object> optionsObject, string parameter)
{
if (!optionsObject.ContainsKey(parameter))
{
PrintMessageAndExit(1, "ERROR: REQUIRED parameter --" + parameter + "= is missing");
}
return optionsObject[parameter].ToString();
}
private static string GetParameterOrDefault(IReadOnlyDictionary<string, object> optionsObject, string parameter, string defaultValue)
{
object value;
if (!optionsObject.TryGetValue(parameter, out value))
{
Console.WriteLine($"'{parameter}' was not specified. Using default value: '{defaultValue}'");
return defaultValue;
}
return value.ToString();
}
}
}