463 lines
13 KiB
Plaintext
463 lines
13 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"\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/KitchenSinkQuantBookTemplate.ipynb"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## QuantBook Basics\n",
|
|
"The following example is ready to be used in our Docker container, reference the readme for more details on setting this up.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"In order to use this notebook locally you will need to make a few small changes:\n",
|
|
"\n",
|
|
"1. Either create the notebook in your build folder (`bin/debug`) **or** set working directory of the notebook to it like so in the first cell:\n",
|
|
"\n",
|
|
" ```%cd \"PathToLean/Lean/Launcher/bin/Debug/```\n",
|
|
"\n",
|
|
"2. Run the following command in another cell to load in QuantConnect libraries:\n",
|
|
"\n",
|
|
" ```%run start.py```\n",
|
|
"\n",
|
|
"### Start QuantBook\n",
|
|
"- Add the references and imports\n",
|
|
"- Create a QuantBook instance"
|
|
]
|
|
},
|
|
{
|
|
"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": [
|
|
"# Create an instance of our QuantBook\n",
|
|
"qb = QuantBook()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Using the Web API\n",
|
|
"Our script `start.py` automatically loads an instance of the web API for you to use.**\n",
|
|
"\n",
|
|
"Look at Lean's [Api](https://github.com/QuantConnect/Lean/tree/master/Api) class for more functions to interact with the cloud\n",
|
|
"\n",
|
|
"\n",
|
|
"##### **Note: This will only connect if you have your User ID and Api token in `config.json` \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Show that our api object is connected to the Web Api\n",
|
|
"print(api.Connected)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Get our list of projects from the cloud and print their names\n",
|
|
"projectResponse = api.ListProjects()\n",
|
|
"for project in projectResponse.Projects:\n",
|
|
" print(project.Name)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Selecting Asset Data\n",
|
|
"Checkout the QuantConnect [docs](https://www.quantconnect.com/docs#Initializing-Algorithms-Selecting-Asset-Data) to learn how to select asset data."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"spy = qb.AddEquity(\"SPY\")\n",
|
|
"eur = qb.AddForex(\"EURUSD\")\n",
|
|
"btc = qb.AddCrypto(\"BTCUSD\")\n",
|
|
"fxv = qb.AddData[FxcmVolume](\"EURUSD_Vol\", Resolution.Hour)"
|
|
]
|
|
},
|
|
{
|
|
"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)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Plot closing prices from \"SPY\" \n",
|
|
"h1.loc[\"SPY\"][\"close\"].plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Gets historical data from the subscribed assets, from the last 30 days with daily resolution\n",
|
|
"h2 = qb.History(qb.Securities.Keys, datetime(2014,1,1), datetime.now(), Resolution.Daily)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Plot high prices from \"EURUSD\" \n",
|
|
"h2.loc[\"EURUSD\"][\"high\"].plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Gets historical data from the subscribed assets, between two dates with daily resolution\n",
|
|
"h3 = qb.History([btc.Symbol], datetime(2014,1,1), datetime.now(), Resolution.Daily)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Plot closing prices from \"BTCUSD\" \n",
|
|
"h3.loc[\"BTCUSD\"][\"close\"].plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Only fetchs historical data from a desired symbol\n",
|
|
"# NOTE: This will return empty when ran locally because this data is not included\n",
|
|
"h4 = qb.History([spy.Symbol], timedelta(360), Resolution.Daily)\n",
|
|
"# or qb.History([\"SPY\"], 360, Resolution.Daily)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Only fetchs historical data from a desired symbol\n",
|
|
"# NOTE: This will return empty when ran locally because this data is not included\n",
|
|
"h5 = qb.History([eur.Symbol], timedelta(30), Resolution.Daily)\n",
|
|
"# or qb.History([\"EURUSD\"], 30, Resolution.Daily)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Historical Options Data Requests\n",
|
|
"- Select the option data\n",
|
|
"- Sets the filter, otherwise the default will be used SetFilter(-1, 1, timedelta(0), timedelta(35))\n",
|
|
"- Get the OptionHistory, an object that has information about the historical options data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"goog = qb.AddOption(\"GOOG\")\n",
|
|
"goog.SetFilter(-2, 2, timedelta(0), timedelta(180))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"option_history = qb.GetOptionHistory(goog.Symbol, datetime(2015, 12, 24))\n",
|
|
"print (option_history.GetStrikes())\n",
|
|
"print (option_history.GetExpiryDates())\n",
|
|
"h7 = option_history.GetAllData()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Historical Future Data Requests\n",
|
|
"- Select the future data\n",
|
|
"- Sets the filter, otherwise the default will be used SetFilter(timedelta(0), timedelta(35))\n",
|
|
"- Get the FutureHistory, an object that has information about the historical future data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"es = qb.AddFuture(\"ES\")\n",
|
|
"es.SetFilter(timedelta(0), timedelta(180))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"future_history = qb.GetFutureHistory(es.Symbol, datetime(2013, 10, 7))\n",
|
|
"print (future_history.GetExpiryDates())\n",
|
|
"h7 = future_history.GetAllData()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Get Fundamental Data\n",
|
|
"\n",
|
|
"- *GetFundamental([symbol], selector, start_date = datetime(1998,1,1), end_date = datetime.now())*\n",
|
|
"\n",
|
|
"We will get a pandas.DataFrame with fundamental data."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"data = qb.GetFundamental([\"AAPL\",\"AIG\",\"BAC\",\"GOOG\",\"IBM\"], \"ValuationRatios.PERatio\")\n",
|
|
"data"
|
|
]
|
|
},
|
|
{
|
|
"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()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# For EURUSD\n",
|
|
"bbdf = qb.Indicator(bb, \"EURUSD\", 360, Resolution.Daily)\n",
|
|
"bbdf = bbdf.drop('standarddeviation', 1)\n",
|
|
"bbdf.plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Example with ADX, it is a bar indicator\n",
|
|
"adx = AverageDirectionalIndex(\"adx\", 14)\n",
|
|
"adxdf = qb.Indicator(adx, \"SPY\", 360, Resolution.Daily)\n",
|
|
"adxdf.plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# For EURUSD\n",
|
|
"adxdf = qb.Indicator(adx, \"EURUSD\", 360, Resolution.Daily)\n",
|
|
"adxdf.plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Example with ADO, it is a tradebar indicator (requires volume in its calculation)\n",
|
|
"ado = AccumulationDistributionOscillator(\"ado\", 5, 30)\n",
|
|
"adodf = qb.Indicator(ado, \"SPY\", 360, Resolution.Daily)\n",
|
|
"adodf.plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# For EURUSD. \n",
|
|
"# Uncomment to check that this SHOULD fail, since Forex is data type is not TradeBar.\n",
|
|
"# adodf = qb.Indicator(ado, \"EURUSD\", 360, Resolution.Daily)\n",
|
|
"# adodf.plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# SMA cross:\n",
|
|
"symbol = \"EURUSD\"\n",
|
|
"# Get History \n",
|
|
"hist = qb.History([symbol], 500, Resolution.Daily)\n",
|
|
"# Get the fast moving average\n",
|
|
"fast = qb.Indicator(SimpleMovingAverage(50), symbol, 500, Resolution.Daily)\n",
|
|
"# Get the fast moving average\n",
|
|
"slow = qb.Indicator(SimpleMovingAverage(200), symbol, 500, Resolution.Daily)\n",
|
|
"\n",
|
|
"# Remove undesired columns and rename others \n",
|
|
"fast = fast.drop('rollingsum', 1).rename(columns={'simplemovingaverage': 'fast'})\n",
|
|
"slow = slow.drop('rollingsum', 1).rename(columns={'simplemovingaverage': 'slow'})\n",
|
|
"\n",
|
|
"# Concatenate the information and plot \n",
|
|
"df = pd.concat([hist.loc[symbol][\"close\"], fast, slow], axis=1).dropna(axis=0)\n",
|
|
"df.plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Get indicator defining a lookback period in terms of timedelta\n",
|
|
"ema1 = qb.Indicator(ExponentialMovingAverage(50), \"SPY\", timedelta(100), Resolution.Daily)\n",
|
|
"# Get indicator defining a start and end date\n",
|
|
"ema2 = qb.Indicator(ExponentialMovingAverage(50), \"SPY\", datetime(2016,1,1), datetime(2016,10,1), Resolution.Daily)\n",
|
|
"\n",
|
|
"ema = pd.concat([ema1, ema2], axis=1)\n",
|
|
"ema.plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rsi = RelativeStrengthIndex(14)\n",
|
|
"\n",
|
|
"# Selects which field we want to use in our indicator (default is Field.Close)\n",
|
|
"rsihi = qb.Indicator(rsi, \"SPY\", 360, Resolution.Daily, Field.High)\n",
|
|
"rsilo = qb.Indicator(rsi, \"SPY\", 360, Resolution.Daily, Field.Low)\n",
|
|
"rsihi = rsihi.rename(columns={'relativestrengthindex': 'high'})\n",
|
|
"rsilo = rsilo.rename(columns={'relativestrengthindex': 'low'})\n",
|
|
"rsi = pd.concat([rsihi['high'], rsilo['low']], axis=1)\n",
|
|
"rsi.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.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
} |