chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
{
|
||||
"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/BasicCSharpQuantBookTemplate.ipynb"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## QuantBook Basics\n",
|
||||
"\n",
|
||||
"### Start QuantBook\n",
|
||||
"- Load \"../QuantConnect.csx\" with all the basic imports\n",
|
||||
"- Create a QuantBook instance"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// We need to load assemblies at the start in their own cell\n",
|
||||
"#load \"../Initialize.csx\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#load \"../QuantConnect.csx\"\n",
|
||||
"\n",
|
||||
"using QuantConnect;\n",
|
||||
"using QuantConnect.Data;\n",
|
||||
"using QuantConnect.Research;\n",
|
||||
"using QuantConnect.Algorithm;\n",
|
||||
"\n",
|
||||
"var qb = new QuantBook();\n",
|
||||
"\n",
|
||||
"// Selecting asset data\n",
|
||||
"var spy = qb.AddEquity(\"SPY\");\n",
|
||||
"var eur = qb.AddForex(\"EURUSD\");\n",
|
||||
"var btc = qb.AddCrypto(\"BTCUSD\");\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution\n",
|
||||
"var h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily);\n",
|
||||
"Console.WriteLine(string.Join(\",\", h1.SelectMany(slice => slice.Keys).Distinct()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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": 4
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
{
|
||||
"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/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": [
|
||||
"# Load in our startup script, required to set runtime for PythonNet\n",
|
||||
"%run ../start.py # %run start.py # in Dev Container"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 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).data_frame\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": []
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
|
||||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Python.Runtime;
|
||||
using QuantConnect.Data;
|
||||
using QuantConnect.Python;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace QuantConnect.Research
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to manage information from History Request of Futures
|
||||
/// </summary>
|
||||
public class FutureHistory : DataHistory<Slice>
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="FutureHistory"/>.
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public FutureHistory(IEnumerable<Slice> data)
|
||||
: base(data, new Lazy<PyObject>(() => new PandasConverter().GetDataFrame(data), isThreadSafe: false))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all data from the History Request that are written in a pandas.DataFrame
|
||||
/// </summary>
|
||||
[Obsolete("Please use the 'DataFrame' property")]
|
||||
public PyObject GetAllData() => DataFrame;
|
||||
|
||||
/// <summary>
|
||||
/// Gets all expity dates in the future history
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public PyObject GetExpiryDates()
|
||||
{
|
||||
var expiry = Data.SelectMany(x => x.FuturesChains.SelectMany(y => y.Value.Contracts.Keys.Select(z => z.ID.Date).Distinct()));
|
||||
using (Py.GIL())
|
||||
{
|
||||
return expiry.Distinct().ToList().ToPython();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
var currentDirectory = Directory.GetCurrentDirectory();
|
||||
var parentDirectory = Directory.GetParent(currentDirectory).FullName;
|
||||
|
||||
// If our parent directory contains QC Dlls use it, otherwise default to current working directory
|
||||
// In cloud and CLI research cases we expect the parent directory to contain the Dlls; but locally it may be cwd
|
||||
var directoryToLoad = Directory.GetFiles(parentDirectory, "QuantConnect.*.dll").Any() ? parentDirectory : currentDirectory;
|
||||
|
||||
// Load in all QC dll's from this directory
|
||||
Console.WriteLine($"Initialize.csx: Loading assemblies from {directoryToLoad}");
|
||||
foreach (var file in Directory.GetFiles(directoryToLoad, "QuantConnect.*.dll"))
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly.LoadFrom(file.ToString());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"File: {file}. Exception: {e}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
{
|
||||
"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/KitchenSinkCSharpQuantBookTemplate.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",
|
||||
"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",
|
||||
" ```\n",
|
||||
" using System.IO\n",
|
||||
" Directory.SetCurrentDirectory(\"PathToLean/Lean/Launcher/bin/Debug/\");\n",
|
||||
" ```\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"2. Load \"QuantConnect.csx\" instead of \"../QuantConnect.csx\", this is again because of the Notebook position relative to the build files. \n",
|
||||
"\n",
|
||||
"### Start QuantBook\n",
|
||||
"- Load \"Initialize.csx\" to load our assemblies into our C# Kernel\n",
|
||||
"- Load \"QuantConnect.csx\" with all the basic imports\n",
|
||||
"- Create a QuantBook instance"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// We need to load assemblies at the start in their own cell\n",
|
||||
"#load \"../Initialize.csx\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Load in our startup script, this creates our Api Object\n",
|
||||
"#load \"../QuantConnect.csx\"\n",
|
||||
"\n",
|
||||
"// Load in the namespaces we are going to use\n",
|
||||
"using QuantConnect;\n",
|
||||
"using QuantConnect.Data;\n",
|
||||
"using QuantConnect.Data.Custom;\n",
|
||||
"using QuantConnect.Data.Market;\n",
|
||||
"using QuantConnect.Research;\n",
|
||||
"using QuantConnect.Algorithm;\n",
|
||||
"\n",
|
||||
"var qb = new QuantBook();"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Using the Web API\n",
|
||||
"Our script `QuantConnect.csx` 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` "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Show that our api object is connected to the Web Api\n",
|
||||
"Console.WriteLine(api.Connected);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Get our list of projects from the cloud and print their names\n",
|
||||
"var projectResponse = api.ListProjects();\n",
|
||||
"foreach (var project in projectResponse.Projects) {\n",
|
||||
" Console.WriteLine(project.Name);\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": [
|
||||
"var spy = qb.AddEquity(\"SPY\");\n",
|
||||
"var eur = qb.AddForex(\"EURUSD\");\n",
|
||||
"var btc = qb.AddCrypto(\"BTCUSD\");\n",
|
||||
"var 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": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution\n",
|
||||
"var h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"var h2 = qb.History(qb.Securities.Keys, TimeSpan.FromDays(360), Resolution.Daily);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Gets historical data from the subscribed assets, between two dates with daily resolution\n",
|
||||
"var h3 = qb.History(btc.Symbol, new DateTime(2014,1,1), DateTime.Now, Resolution.Daily);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Only fetchs historical data from a desired symbol\n",
|
||||
"var h4 = qb.History(spy.Symbol, 360, Resolution.Daily);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Only fetchs historical data from a desired symbol\n",
|
||||
"var h5 = qb.History<QuoteBar>(eur.Symbol, TimeSpan.FromDays(360), Resolution.Daily);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Fetchs custom data\n",
|
||||
"var h6 = qb.History<FxcmVolume>(fxv.Symbol, TimeSpan.FromDays(360));"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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": 4
|
||||
}
|
||||
@@ -0,0 +1,463 @@
|
||||
{
|
||||
"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
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
|
||||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Python.Runtime;
|
||||
using QuantConnect.Data;
|
||||
using QuantConnect.Python;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace QuantConnect.Research
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to manage information from History Request of Options
|
||||
/// </summary>
|
||||
public class OptionHistory : DataHistory<Slice>
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="OptionHistory"/>.
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public OptionHistory(IEnumerable<Slice> data)
|
||||
: base(data, new Lazy<PyObject>(() => new PandasConverter().GetDataFrame(data), isThreadSafe: false))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all data from the History Request that are written in a pandas.DataFrame
|
||||
/// </summary>
|
||||
[Obsolete("Please use the 'DataFrame' property")]
|
||||
public PyObject GetAllData() => DataFrame;
|
||||
|
||||
/// <summary>
|
||||
/// Gets all strikes in the option history
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public PyObject GetStrikes()
|
||||
{
|
||||
var strikes = Data.SelectMany(x => x.OptionChains.SelectMany(y => y.Value.Contracts.Keys.Select(z => (double)z.ID.StrikePrice).Distinct()));
|
||||
using (Py.GIL())
|
||||
{
|
||||
return strikes.Distinct().ToList().ToPython();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all expiry dates in the option history
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public PyObject GetExpiryDates()
|
||||
{
|
||||
var expiry = Data.SelectMany(x => x.OptionChains.SelectMany(y => y.Value.Contracts.Keys.Select(z => z.ID.Date).Distinct()));
|
||||
using (Py.GIL())
|
||||
{
|
||||
return expiry.Distinct().ToList().ToPython();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("QuantConnect.Research")]
|
||||
[assembly: AssemblyProduct("QuantConnect.Research")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("9561d14a-467e-40ad-928e-ee9f758d7d98")]
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,74 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<RootNamespace>QuantConnect.Research</RootNamespace>
|
||||
<AssemblyName>QuantConnect.Research</AssemblyName>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
|
||||
<OutputPath>bin\$(Configuration)\</OutputPath>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<Description>QuantConnect LEAN Engine: Research Project - Core implementation for jupyter research environment</Description>
|
||||
<NoWarn>CA1062</NoWarn>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\Common\Properties\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Plotly.NET" Version="5.1.0" />
|
||||
<PackageReference Include="Plotly.NET.CSharp" Version="0.13.0" />
|
||||
<PackageReference Include="Plotly.NET.Interactive" Version="5.0.0" />
|
||||
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.60" />
|
||||
<PackageReference Include="NodaTime" Version="3.0.5" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Algorithm.Framework\QuantConnect.Algorithm.Framework.csproj" />
|
||||
<ProjectReference Include="..\Algorithm\QuantConnect.Algorithm.csproj" />
|
||||
<ProjectReference Include="..\Api\QuantConnect.Api.csproj" />
|
||||
<ProjectReference Include="..\Common\QuantConnect.csproj" />
|
||||
<ProjectReference Include="..\Configuration\QuantConnect.Configuration.csproj" />
|
||||
<ProjectReference Include="..\Engine\QuantConnect.Lean.Engine.csproj" />
|
||||
<ProjectReference Include="..\Indicators\QuantConnect.Indicators.csproj" />
|
||||
<ProjectReference Include="..\Logging\QuantConnect.Logging.csproj" />
|
||||
<ProjectReference Include="..\Queues\QuantConnect.Queues.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="BasicCSharpQuantBookTemplate.ipynb">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="BasicQuantBookTemplate.ipynb">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Initialize.csx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="QuantConnect.csx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Include="..\LICENSE">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath></PackagePath>
|
||||
</None>
|
||||
<None Include="readme.md" />
|
||||
<Content Include="start.py">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<PackageCopyToOutput>true</PackageCopyToOutput>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,88 @@
|
||||
#r "Python.Runtime.dll"
|
||||
#r "QuantConnect.Algorithm.dll"
|
||||
#r "QuantConnect.Algorithm.Framework.dll"
|
||||
#r "QuantConnect.Common.dll"
|
||||
#r "QuantConnect.Indicators.dll"
|
||||
#r "QuantConnect.Research.dll"
|
||||
#r "NodaTime.dll"
|
||||
#r "Accord.dll"
|
||||
#r "Accord.Fuzzy.dll"
|
||||
#r "Accord.Math.Core.dll"
|
||||
#r "Accord.Math.dll"
|
||||
#r "MathNet.Numerics.dll"
|
||||
#r "Newtonsoft.Json.dll"
|
||||
#r "QuantConnect.AlgorithmFactory.dll"
|
||||
#r "QuantConnect.Logging.dll"
|
||||
#r "QuantConnect.Messaging.dll"
|
||||
#r "QuantConnect.Configuration.dll"
|
||||
#r "QuantConnect.Lean.Engine.dll"
|
||||
#r "QuantConnect.Algorithm.CSharp.dll"
|
||||
#r "QuantConnect.Api.dll"
|
||||
// Note: #r directives must be in the beggining of the file
|
||||
|
||||
/*
|
||||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
|
||||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* This C# Script File (.csx) can be loaded in a notebook (ipynb file)
|
||||
* in order to reference QuantConnect assemblies
|
||||
* https://github.com/scriptcs/scriptcs/wiki/Writing-a-script#referencing-assemblies
|
||||
*
|
||||
* Usage:
|
||||
* #load "QuantConnect.csx"
|
||||
*/
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Globalization;
|
||||
using QuantConnect;
|
||||
using QuantConnect.Algorithm;
|
||||
using QuantConnect.Algorithm.Framework;
|
||||
using QuantConnect.Algorithm.Framework.Selection;
|
||||
using QuantConnect.Algorithm.Framework.Alphas;
|
||||
using QuantConnect.Algorithm.Framework.Portfolio;
|
||||
using QuantConnect.Algorithm.Framework.Execution;
|
||||
using QuantConnect.Algorithm.Framework.Risk;
|
||||
using QuantConnect.Api;
|
||||
using QuantConnect.Parameters;
|
||||
using QuantConnect.Benchmarks;
|
||||
using QuantConnect.Brokerages;
|
||||
using QuantConnect.Util;
|
||||
using QuantConnect.Interfaces;
|
||||
using QuantConnect.Indicators;
|
||||
using QuantConnect.Research;
|
||||
using QuantConnect.Data;
|
||||
using QuantConnect.Data.Consolidators;
|
||||
using QuantConnect.Data.Custom;
|
||||
using QuantConnect.Data.Fundamental;
|
||||
using QuantConnect.Data.Market;
|
||||
using QuantConnect.Data.UniverseSelection;
|
||||
using QuantConnect.Notifications;
|
||||
using QuantConnect.Orders;
|
||||
using QuantConnect.Orders.Fees;
|
||||
using QuantConnect.Orders.Fills;
|
||||
using QuantConnect.Orders.Slippage;
|
||||
using QuantConnect.Scheduling;
|
||||
using QuantConnect.Securities;
|
||||
using QuantConnect.Securities.Equity;
|
||||
using QuantConnect.Securities.Forex;
|
||||
using QuantConnect.Securities.Interfaces;
|
||||
using QuantConnect.Configuration;
|
||||
using QuantConnect.Lean.Engine;
|
||||
|
||||
Config.Reset();
|
||||
Initializer.Start();
|
||||
Api api = (Api)Initializer.GetSystemHandlers().Api;
|
||||
var algorithmHandlers = Initializer.GetAlgorithmHandlers(researchMode: true);
|
||||
@@ -0,0 +1,4 @@
|
||||
IMAGE=quantconnect/research:latest
|
||||
DATA_DIR=
|
||||
NOTEBOOK_DIR=
|
||||
UPDATE=Y
|
||||
@@ -0,0 +1,108 @@
|
||||
QuantConnect Research Project
|
||||
=============
|
||||
Currently we have a few ways to use QuantConnect research notebooks:
|
||||
- Lean CLI (Recommended)
|
||||
- Install locally and run directly on your OS.
|
||||
|
||||
This document will cover the setup, getting started, and known issues.
|
||||
|
||||
<br>
|
||||
|
||||
# Setup
|
||||
Below we cover how to get setup with our two options listed above.
|
||||
|
||||
<br>
|
||||
|
||||
## Research with Lean CLI (Recommended)
|
||||
|
||||
Our research docker image has been integrated with Lean CLI to streamline the process and allow user to use their cloud and local projects in the research environment. Please refer to Lean CLI documentation [here](https://www.quantconnect.com/docs/v2/lean-cli/getting-started/lean-cli) on how to get started.
|
||||
|
||||
Lean CLI research specific documentation is found [here](https://www.quantconnect.com/docs/v2/lean-cli/tutorials/research).
|
||||
|
||||
We highly recommend using Lean CLI with docker for research but below in [Running Jupyter Locally](#running-jupyter-locally) we cover how to install and prepare the environment on your personal desktop.
|
||||
|
||||
<br>
|
||||
|
||||
## Running Jupyter Locally
|
||||
Note: we recommend using the above approach with our Docker container, where the setup and environment is tested and stable.
|
||||
|
||||
Before we enable Jupyter support, follow [Lean installation](https://github.com/QuantConnect/Lean#installation-instructions)
|
||||
and [Python installation](https://github.com/QuantConnect/Lean/tree/master/Algorithm.Python#quantconnect-python-algorithm-project) to get LEAN running Python algorithms on your machine. Then be sure to build Lean at least once before the following.
|
||||
|
||||
**1. Installation:**
|
||||
1. Install [JupyterLab](https://pypi.org/project/jupyterlab/):
|
||||
```
|
||||
pip install jupyterlab
|
||||
```
|
||||
2. Install [QuantConnect Python API](https://pypi.python.org/pypi/quantconnect/0.1)
|
||||
```
|
||||
pip install quantconnect
|
||||
```
|
||||
3. Install [pythonnet/clr-loader](https://github.com/pythonnet/clr-loader)
|
||||
```
|
||||
pip install clr-loader
|
||||
```
|
||||
**2. Run Jupyter:**
|
||||
1. Run Jupyter from the command line
|
||||
```
|
||||
cd Lean/Launcher/bin/Debug
|
||||
jupyter lab
|
||||
```
|
||||
<br>
|
||||
|
||||
# Getting Started with Research
|
||||
|
||||
## C# Notebook
|
||||
When using C# for research notebooks it requires that you load our setup script CSX file `Initialize.csx` into your notebook. This will load our QuantConnect libraries into your C# Kernel. In both docker setups, the file is one directory above the notebooks dir. Be sure to use the following line in your first cell to load in this csx file:
|
||||
|
||||
`#load "../Initialize.csx"`
|
||||
|
||||
After this the environment is ready to use; take a look at our reference notebook `KitchenSinkCSharpQuantBookTemplate.ipynb` for an example of how to use our `QuantBook` interface!
|
||||
|
||||
Note: All Lean namespaces you want to use in your notebook need to be directly added via `using` statements.
|
||||
|
||||
<br>
|
||||
|
||||
## Python Notebook
|
||||
With Python we have a setup script that will automatically load QuantBooks libraries into the Python kernel so there is no need to import them. In our docker image the script should run automatically, but locally you will need to call `%run "start.py"` in the first cell.
|
||||
|
||||
You notebook is ready to use; take a look at our reference notebook `KitchenSinkQuantBookTemplate.ipynb` for an example of how to use our `QuantBook` interface!
|
||||
|
||||
<br>
|
||||
|
||||
## Using the Web Api from Notebook
|
||||
Both of our setup scripts for Python & C# include a instantiated `Api` object under the variable name `api`. Before you can use this api object to interact with the cloud you must edit your config in the **root** of your Notebook directory. Once this has been done once, it does not need to be done again.
|
||||
|
||||
In `config.json` add the following entries with your respective values
|
||||
```
|
||||
job-user-id: 12345, // Your id here
|
||||
api-access-token: "token13432", // Your api token here
|
||||
```
|
||||
|
||||
Once this has been done, you may restart your kernel and begin to use the `api` variable.
|
||||
Reference our examples mentioned above for practical uses of this object.
|
||||
|
||||
<br>
|
||||
|
||||
## Shutting Down the Notebook Lab
|
||||
When you are done with the research environment be sure to stop the container with **Docker Dashboard** or via **Docker CLI**.
|
||||
|
||||
<br>
|
||||
|
||||
|
||||
## Build a new image
|
||||
For most users this will not be necessary, simply use `docker pull quantconnect/research` to get the latest image.
|
||||
|
||||
`docker build -t quantconnect/research - < DockerfileJupyter` will build a new docker image using the latest version of lean. To build from particular tag of lean a build arg can be provided, for example `--build-arg LEAN_TAG=8631`.
|
||||
|
||||
<br>
|
||||
|
||||
|
||||
# Known Issues
|
||||
- Python research is extremely dependent on the `start.py` script as it is responsible for assigning core clr as the runtime for PythonNet and clr-loader to use for C# library. For local use where the script is not launched automatically by Jupyter, one must call `%run "start.py"` in their first notebook cell for research to work properly. Note that the location of `start.py` is in the launcher bin directory so you may have to use `../start.py` or specify the full path.
|
||||
|
||||
- C# research latest kernel no longer supports using statements outside of the notebook context, meaning that `#load ./QuantConnect.csx` no longer applies QC namespaces to the notebook out of the box. Therefore one must specify the namespace directly in a cell. Our default notebooks include these statements as examples.
|
||||
|
||||
- Python can sometimes have issues when paired with our quantconnect stubs package on Windows. This issue can cause modules not to be found because `site-packages` directory is not present in the python path. If you have the required modules installed and are seeing errors about them not being found, please try the following steps:
|
||||
- remove stubs -> pip uninstall quantconnect-stubs
|
||||
- reinstall stubs -> pip install quantconnect-stubs
|
||||
@@ -0,0 +1,48 @@
|
||||
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
|
||||
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# This Python script can be loaded in a notebook (ipynb file)
|
||||
# in order to reference QuantConnect assemblies
|
||||
#
|
||||
# Usage:
|
||||
# %run "start.py"
|
||||
|
||||
import clr_loader
|
||||
import os
|
||||
from pythonnet import set_runtime
|
||||
|
||||
# The runtimeconfig.json is stored alongside start.py, but start.py may be a
|
||||
# symlink and the directory start.py is stored in is not necessarily the
|
||||
# current working directory. We therefore construct the absolute path to the
|
||||
# start.py file, and find the runtimeconfig.json relative to that.
|
||||
set_runtime(clr_loader.get_coreclr(runtime_config=os.path.join(os.path.dirname(os.path.realpath(__file__)), "QuantConnect.Lean.Launcher.runtimeconfig.json")))
|
||||
|
||||
from AlgorithmImports import *
|
||||
|
||||
# Used by pythonNet
|
||||
AddReference("Fasterflect")
|
||||
|
||||
Config.Reset()
|
||||
Initializer.Start()
|
||||
api = Initializer.GetSystemHandlers().Api
|
||||
algorithmHandlers = Initializer.GetAlgorithmHandlers(researchMode=True)
|
||||
|
||||
# Required to configure pythonpath with additional paths the user may have
|
||||
# set in the config, like a project library.
|
||||
PythonInitializer.Initialize(False)
|
||||
|
||||
try:
|
||||
get_ipython().run_line_magic('matplotlib', 'inline')
|
||||
except NameError:
|
||||
# can happen if start is triggered from python and not Ipython
|
||||
pass
|
||||
Reference in New Issue
Block a user