diff --git a/project-templates/python/custom-data-ticker-mapping/research.ipynb b/project-templates/python/custom-data-ticker-mapping/research.ipynb new file mode 100644 index 0000000000..97fc5be9d9 --- /dev/null +++ b/project-templates/python/custom-data-ticker-mapping/research.ipynb @@ -0,0 +1,163 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "cdt-logo", + "metadata": {}, + "source": [ + "![QuantConnect Logo](https://cdn.quantconnect.com/web/i/icon.png)\n", + "
\n", + "\n", + "## Custom Data with Ticker Mapping Research\n", + "\n", + "This notebook loads the mapped trade data from the Object Store and shows how the FB ticker resolves to the current META symbol." + ] + }, + { + "cell_type": "markdown", + "id": "cdt-setup-md", + "metadata": {}, + "source": [ + "### Set Up QuantBook\n", + "\n", + "Save the selected-trades file to the Object Store for the custom data reader." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdt-content", + "metadata": {}, + "outputs": [], + "source": [ + "SELECTED_TRADES_FILE = \"selected_trades.csv\"\n", + "\n", + "CONTENT = \"\"\"\n", + "2020-01-20,FB,100\n", + "2020-01-20,MSFT,200\n", + "2020-01-20,NVDA,300\n", + "2024-09-03,META,-100\n", + "2024-09-03,MSFT,-200\n", + "2024-09-03,NVDA,-300\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdt-setup", + "metadata": {}, + "outputs": [], + "source": [ + "qb = QuantBook()\n", + "qb.set_start_date(2024, 12, 31)\n", + "qb.object_store.save(SELECTED_TRADES_FILE, CONTENT)" + ] + }, + { + "cell_type": "markdown", + "id": "cdt-class-md", + "metadata": {}, + "source": [ + "### Define the Custom Data Type\n", + "\n", + "Define a [custom securities](https://www.quantconnect.com/docs/v2/writing-algorithms/importing-data/streaming-data/custom-securities) reader that maps each point-in-time ticker to a Symbol." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdt-class", + "metadata": {}, + "outputs": [], + "source": [ + "class SelectedTrades(PythonData):\n", + "\n", + " def get_source(self, config: SubscriptionDataConfig, date: datetime, is_live_mode: bool) -> SubscriptionDataSource:\n", + " return SubscriptionDataSource(SELECTED_TRADES_FILE, SubscriptionTransportMedium.OBJECT_STORE)\n", + "\n", + " def reader(self, config: SubscriptionDataConfig, line: str, date: datetime, is_live_mode: bool) -> BaseData:\n", + " if not line.strip():\n", + " return None\n", + " data = [x.strip() for x in line.split(',')]\n", + " ticker = data[1]\n", + " time = datetime.strptime(data[0], \"%Y-%m-%d\")\n", + " # Resolve the point-in-time SecurityIdentifier so FB maps to today's META symbol.\n", + " security_id = SecurityIdentifier.generate_equity(ticker, Market.USA, mapping_resolve_date=time)\n", + " if security_id.date.year < 1998:\n", + " return None\n", + " trade = SelectedTrades()\n", + " trade.symbol = Symbol(security_id, ticker)\n", + " trade.end_time = time\n", + " trade.time = time - timedelta(1)\n", + " trade.quantity = float(data[2])\n", + " return trade" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdt-add", + "metadata": {}, + "outputs": [], + "source": [ + "trades = qb.add_data(SelectedTrades, \"X\")" + ] + }, + { + "cell_type": "markdown", + "id": "cdt-build-md", + "metadata": {}, + "source": [ + "### Build Time Series\n", + "\n", + "Request the full history of the mapped trade data and inspect the resolved symbols and quantities." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f02b270", + "metadata": {}, + "outputs": [], + "source": [ + "history = qb.history(SelectedTrades, trades.symbol, datetime(2020, 1, 1), datetime(2024, 12, 31))\n", + "history" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdt-asset-history", + "metadata": {}, + "outputs": [], + "source": [ + "# Extract the unique asset Symbols from the custom data history.\n", + "asset_symbols = list(history.index.levels[0])\n", + "# Request daily history for the assets referenced by the custom data.\n", + "asset_history = qb.history(asset_symbols, datetime(2020, 1, 1), datetime(2024, 12, 31), Resolution.DAILY)\n", + "asset_history" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Foundation-Py-Default", + "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.11.14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}