{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![QuantConnect Logo](https://cdn.quantconnect.com/web/i/icon.png)\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Custom data history" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "vscode": { "languageId": "csharp" } }, "outputs": [], "source": [ "// We need to load assemblies at the start in their own cell\n", "#load \"./Initialize.csx\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "vscode": { "languageId": "csharp" } }, "outputs": [], "source": [ "// Initialize Lean Engine.\n", "#load \"./QuantConnect.csx\"\n", "\n", "using System.Globalization;\n", "using System.Linq;\n", "using QuantConnect;\n", "using QuantConnect.Data;\n", "using QuantConnect.Algorithm;\n", "using QuantConnect.Research;" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "vscode": { "languageId": "csharp" } }, "outputs": [], "source": [ "class CustomDataType : DynamicData\n", "{\n", " public decimal Open;\n", " public decimal High;\n", " public decimal Low;\n", " public decimal Close;\n", "\n", " public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)\n", " {\n", " var source = \"https://www.dl.dropboxusercontent.com/s/d83xvd7mm9fzpk0/path_to_my_csv_data.csv?dl=0\";\n", " return new SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile);\n", " }\n", "\n", " public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)\n", " {\n", " if (string.IsNullOrWhiteSpace(line.Trim()))\n", " {\n", " return null;\n", " }\n", "\n", " try\n", " {\n", " var csv = line.Split(\",\");\n", " var data = new CustomDataType()\n", " {\n", " Symbol = config.Symbol,\n", " Time = DateTime.ParseExact(csv[0], DateFormat.DB, CultureInfo.InvariantCulture).AddHours(20),\n", " Value = csv[4].ToDecimal(),\n", " Open = csv[1].ToDecimal(),\n", " High = csv[2].ToDecimal(),\n", " Low = csv[3].ToDecimal(),\n", " Close = csv[4].ToDecimal()\n", " };\n", "\n", " return data;\n", " }\n", " catch\n", " {\n", " return null;\n", " }\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "vscode": { "languageId": "csharp" } }, "outputs": [], "source": [ "var qb = new QuantBook();\n", "var symbol = qb.AddData(\"CustomDataType\", Resolution.Hour).Symbol;\n", "\n", "var start = new DateTime(2017, 8, 20);\n", "var end = start.AddHours(48);\n", "var history = qb.History(symbol, start, end, Resolution.Hour).ToList();\n", "\n", "if (history.Count == 0)\n", "{\n", " throw new Exception(\"No history data returned\");\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "9.0" } }, "nbformat": 4, "nbformat_minor": 2 }