{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![QuantConnect Logo](https://cdn.quantconnect.com/web/i/qc_notebook_logo_rev0.png)\n", "## Welcome to The QuantConnect Research Page\n", "#### Refer to this page for documentation https://www.quantconnect.com/docs/research/overview#\n", "#### Contribute to this template file https://github.com/QuantConnect/Lean/blob/master/Research/BasicQuantBookTemplate.ipynb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## QuantBook Basics\n", "\n", "### Start QuantBook\n", "- Add the references and imports\n", "- Create a QuantBook instance" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load in our startup script, required to set runtime for PythonNet\n", "%run ./start.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class CustomDataType(PythonData):\n", "\n", " def GetSource(self, config: SubscriptionDataConfig, date: datetime, isLive: bool) -> SubscriptionDataSource:\n", " source = \"https://www.dl.dropboxusercontent.com/s/d83xvd7mm9fzpk0/path_to_my_csv_data.csv?dl=0\"\n", " return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile)\n", "\n", " def Reader(self, config: SubscriptionDataConfig, line: str, date: datetime, isLive: bool) -> BaseData:\n", " if not (line.strip()):\n", " return None\n", "\n", " data = line.split(',')\n", " obj_data = CustomDataType()\n", " obj_data.Symbol = config.Symbol\n", "\n", " try:\n", " obj_data.Time = datetime.strptime(data[0], '%Y-%m-%d %H:%M:%S') + timedelta(hours=20)\n", " obj_data[\"open\"] = float(data[1])\n", " obj_data[\"high\"] = float(data[2])\n", " obj_data[\"low\"] = float(data[3])\n", " obj_data[\"close\"] = float(data[4])\n", " obj_data.Value = obj_data[\"close\"]\n", "\n", " # property for asserting the correct data is fetched\n", " obj_data[\"some_property\"] = \"some property value\"\n", " except ValueError:\n", " return None\n", "\n", " return obj_data\n", "\n", " def __str__ (self):\n", " return f\"Time: {self.Time}, Value: {self.Value}, SomeProperty: {self['some_property']}, Open: {self['open']}, High: {self['high']}, Low: {self['low']}, Close: {self['close']}\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create an instance\n", "qb = QuantBook()\n", "symbol = qb.AddData(CustomDataType, \"CustomDataType\", Resolution.Hour).Symbol\n", "\n", "startDate = datetime(2017, 8, 20)\n", "endDate = startDate + timedelta(hours=48)\n", "history = list(qb.History[CustomDataType](symbol, startDate, endDate, Resolution.Hour))\n", "\n", "if len(history) == 0:\n", " raise Exception(\"No history data returned\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "vscode": { "interpreter": { "hash": "9650cb4e16cdd4a8e8e2d128bf38d875813998db22a3c986335f89e0cb4d7bb2" } } }, "nbformat": 4, "nbformat_minor": 4 }