{ "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#Introduction-to-Jupyter\n", "#### Contribute to this template file https://github.com/QuantConnect/Lean/blob/master/Research/BasicQuantBookTemplate.ipynb" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "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": [ "%matplotlib inline\n", "# Imports\n", "from clr import AddReference\n", "AddReference(\"System\")\n", "AddReference(\"QuantConnect.Common\")\n", "AddReference(\"QuantConnect.Research\")\n", "AddReference(\"QuantConnect.Indicators\")\n", "from System import *\n", "from QuantConnect import *\n", "from QuantConnect.Data.Custom import *\n", "from QuantConnect.Data.Market import TradeBar, QuoteBar\n", "from QuantConnect.Research import *\n", "from QuantConnect.Indicators import *\n", "from datetime import datetime, timedelta\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "\n", "# Create an instance\n", "qb = QuantBook()\n", "\n", "# Select asset data\n", "spy = qb.AddEquity(\"SPY\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Historical Data Requests\n", "\n", "We can use the QuantConnect API to make Historical Data Requests. The data will be presented as multi-index pandas.DataFrame where the first index is the Symbol.\n", "\n", "For more information, please follow the [link](https://www.quantconnect.com/docs#Historical-Data-Historical-Data-Requests)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution\n", "h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily)\n", "\n", "# Plot closing prices from \"SPY\" \n", "h1.loc[\"SPY\"][\"close\"].plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indicators\n", "\n", "We can easily get the indicator of a given symbol with QuantBook. \n", "\n", "For all indicators, please checkout QuantConnect Indicators [Reference Table](https://www.quantconnect.com/docs#Indicators-Reference-Table)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Example with BB, it is a datapoint indicator\n", "# Define the indicator\n", "bb = BollingerBands(30, 2)\n", "\n", "# Gets historical data of indicator\n", "bbdf = qb.Indicator(bb, \"SPY\", 360, Resolution.Daily)\n", "\n", "# drop undesired fields\n", "bbdf = bbdf.drop('standarddeviation', 1)\n", "\n", "# Plot\n", "bbdf.plot()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }