Research/KitchenSinkCSharpQuantBookTemplate.ipynb
The following example is ready to be used in our Docker container, reference the readme for more details on setting this up.
In order to use this notebook locally you will need to make a few small changes:
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:
using System.IO
Directory.SetCurrentDirectory("PathToLean/Lean/Launcher/bin/Debug/");
Load "QuantConnect.csx" instead of "../QuantConnect.csx", this is again because of the Notebook position relative to the build files.
// We need to load assemblies at the start in their own cell
#load "../Initialize.csx"
// Load in our startup script, this creates our Api Object
#load "../QuantConnect.csx"
// Load in the namespaces we are going to use
using QuantConnect;
using QuantConnect.Data;
using QuantConnect.Data.Custom;
using QuantConnect.Data.Market;
using QuantConnect.Research;
using QuantConnect.Algorithm;
var qb = new QuantBook();
Our script QuantConnect.csx automatically loads an instance of the web API for you to use.**
Look at Lean's Api class for more functions to interact with the cloud
config.json// Show that our api object is connected to the Web Api
Console.WriteLine(api.Connected);
// Get our list of projects from the cloud and print their names
var projectResponse = api.ListProjects();
foreach (var project in projectResponse.Projects) {
Console.WriteLine(project.Name);
}
Checkout the QuantConnect docs to learn how to select asset data.
var spy = qb.AddEquity("SPY");
var eur = qb.AddForex("EURUSD");
var btc = qb.AddCrypto("BTCUSD");
var fxv = qb.AddData<FxcmVolume>("EURUSD_Vol", Resolution.Hour);
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.
For more information, please follow the link.
// Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution
var h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily);
// Gets historical data from the subscribed assets, from the last 30 days with daily resolution
var h2 = qb.History(qb.Securities.Keys, TimeSpan.FromDays(360), Resolution.Daily);
// Gets historical data from the subscribed assets, between two dates with daily resolution
var h3 = qb.History(btc.Symbol, new DateTime(2014,1,1), DateTime.Now, Resolution.Daily);
// Only fetchs historical data from a desired symbol
var h4 = qb.History(spy.Symbol, 360, Resolution.Daily);
// Only fetchs historical data from a desired symbol
var h5 = qb.History<QuoteBar>(eur.Symbol, TimeSpan.FromDays(360), Resolution.Daily);
// Fetchs custom data
var h6 = qb.History<FxcmVolume>(fxv.Symbol, TimeSpan.FromDays(360));