QuantConnect Logo

Welcome to The QuantConnect Research Page¶

Refer to this page for documentation https://www.quantconnect.com/docs#Introduction-to-Jupyter¶

Contribute to this template file https://github.com/QuantConnect/Lean/blob/master/Jupyter/BasicQuantBookTemplate.ipynb¶

QuantBook Basics¶

Start QuantBook¶

  • Add the references and imports
  • Create a QuantBook instance
In [2]:
%matplotlib inline
# Imports
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Jupyter")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Market import TradeBar, QuoteBar
from QuantConnect.Jupyter import *
from QuantConnect.Indicators import *
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import pandas as pd

# Create an instance
qb = QuantBook()

Selecting Asset Data¶

Checkout the QuantConnect docs to learn how to select asset data.

In [3]:
eur = qb.AddForex("EURCZK", Resolution.Daily, Market.Oanda)

Historical Data Requests¶

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.

In [4]:
# Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution
h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily)
In [5]:
h1
Out[5]:
open high low close askopen askhigh asklow askclose bidopen bidhigh bidlow bidclose
symbol time
EURCZK 2017-07-12 20:00:00 26.131505 26.145175 26.062215 26.103765 26.14721 26.16325 26.07176 26.12170 26.11580 26.12710 26.05267 26.08583
2017-07-13 20:00:00 26.103735 26.138935 26.078375 26.105380 26.12170 26.14966 26.09890 26.13251 26.08577 26.12821 26.05785 26.07825
2017-07-14 20:00:00 26.105395 26.117140 26.061965 26.075130 26.13254 26.13254 26.06926 26.09510 26.07825 26.10174 26.05467 26.05516
2017-07-16 20:00:00 26.078445 26.198845 25.958725 26.078745 26.09244 26.33050 26.08695 26.09495 26.06445 26.06719 25.83050 26.06254
2017-07-17 20:00:00 26.078715 26.104675 26.055900 26.074930 26.09495 26.11004 26.06755 26.09405 26.06248 26.09931 26.04425 26.05581
2017-07-18 20:00:00 26.074905 26.143725 26.059995 26.123095 26.09405 26.16102 26.07474 26.14324 26.05576 26.12643 26.04525 26.10295
2017-07-19 20:00:00 26.123200 26.136555 26.018320 26.089430 26.14333 26.15318 26.02259 26.10816 26.10307 26.11993 26.01405 26.07070
2017-07-20 20:00:00 26.089440 26.120195 25.978865 26.044280 26.10816 26.14314 25.99798 26.05681 26.07072 26.09725 25.95975 26.03175
2017-07-21 20:00:00 26.044280 26.115165 25.989940 26.050035 26.05681 26.13905 26.01606 26.10182 26.03175 26.09128 25.96382 25.99825
2017-07-23 20:00:00 26.042570 26.110810 25.986835 26.061505 26.16807 26.16807 26.05660 26.07296 25.91707 26.05355 25.91707 26.05005
2017-07-24 20:00:00 26.061470 26.066240 25.997520 26.048485 26.07289 26.07832 26.01022 26.06322 26.05005 26.05416 25.98482 26.03375
2017-07-25 20:00:00 26.048365 26.168550 25.893380 26.044715 26.06298 26.28539 26.00137 26.05425 26.03375 26.05171 25.78539 26.03518
2017-07-26 20:00:00 26.044705 26.057825 25.999090 26.040930 26.05425 26.06763 26.01072 26.05683 26.03516 26.04802 25.98746 26.02503
2017-07-27 20:00:00 26.040895 26.094650 26.011870 26.055030 26.05675 26.10873 26.02438 26.06551 26.02504 26.08057 25.99936 26.04455
2017-07-28 20:00:00 26.055060 26.081110 26.032050 26.069685 26.06557 26.09347 26.04018 26.09290 26.04455 26.06875 26.02392 26.04647
2017-07-30 20:00:00 26.055085 26.173495 25.934475 26.055615 26.10884 26.30200 26.06695 26.06695 26.00133 26.04499 25.80200 26.04428
2017-07-31 20:00:00 26.055640 26.130135 26.042100 26.087980 26.06695 26.13627 26.04868 26.09765 26.04433 26.12400 26.03552 26.07831
2017-08-01 20:00:00 26.087995 26.185810 26.084230 26.165935 26.09765 26.20714 26.09440 26.17817 26.07834 26.16448 26.07406 26.15370
2017-08-02 20:00:00 26.166140 26.167755 26.086775 26.098305 26.17858 26.18146 26.09613 26.10725 26.15370 26.15405 26.07742 26.08936
2017-08-03 20:00:00 26.098275 26.124575 25.889620 26.035945 26.10715 26.13221 25.90802 26.05025 26.08940 26.11694 25.87122 26.02164
2017-08-04 20:00:00 26.035990 26.266335 25.921885 26.112790 26.05025 26.39535 26.02090 26.13713 26.02173 26.13732 25.82287 26.08845
2017-08-06 20:00:00 26.111890 26.236655 25.987510 26.090400 26.19233 26.36820 26.10682 26.11396 26.03145 26.10511 25.86820 26.06684
2017-08-07 20:00:00 26.090480 26.175415 26.073990 26.136535 26.11384 26.19228 26.08733 26.15245 26.06712 26.15855 26.06065 26.12062
2017-08-08 20:00:00 26.136545 26.174245 26.107390 26.149730 26.15255 26.18712 26.12504 26.16845 26.12054 26.16137 26.08974 26.13101
2017-08-09 20:00:00 26.149850 26.210035 26.102855 26.169865 26.16845 26.23072 26.11586 26.18483 26.13125 26.18935 26.08985 26.15490
2017-08-10 20:00:00 26.169940 26.190015 26.135600 26.183265 26.18498 26.19958 26.14127 26.19185 26.15490 26.18045 26.12993 26.17468
2017-08-11 20:00:00 26.183215 26.189265 26.138050 26.164925 26.19185 26.19852 26.14670 26.19745 26.17458 26.18001 26.12940 26.13240
2017-08-13 20:00:00 26.172355 26.193550 26.156440 26.165710 26.21229 26.21945 26.18112 26.18333 26.13242 26.16765 26.13176 26.14809
2017-08-14 20:00:00 26.165365 26.173920 26.109645 26.133580 26.18293 26.18773 26.11855 26.14466 26.14780 26.16011 26.10074 26.12250
2017-08-15 20:00:00 26.133595 26.186400 26.063080 26.146340 26.14469 26.19358 26.11308 26.15922 26.12250 26.17922 26.01308 26.13346
... ... ... ... ... ... ... ... ... ... ... ... ...
2018-08-01 20:00:00 25.579225 25.610020 25.559425 25.586480 25.58845 25.62109 25.56715 25.59940 25.57000 25.59895 25.55170 25.57356
2018-08-02 20:00:00 25.586480 25.694260 25.539795 25.654030 25.59931 25.71408 25.55275 25.67295 25.57365 25.67444 25.52684 25.63511
2018-08-03 20:00:00 25.654000 25.709530 25.595675 25.616870 25.67291 25.71604 25.62032 25.65873 25.63509 25.70302 25.57103 25.57501
2018-08-05 20:00:00 25.649550 25.704415 25.638420 25.658475 25.68305 25.73821 25.68034 25.69649 25.61605 25.67062 25.59650 25.62046
2018-08-06 20:00:00 25.658475 25.696575 25.605460 25.631100 25.69648 25.72309 25.61892 25.64630 25.62047 25.67006 25.59200 25.61590
2018-08-07 20:00:00 25.631180 25.666040 25.577415 25.592995 25.64630 25.69208 25.59190 25.60510 25.61606 25.64000 25.56293 25.58089
2018-08-08 20:00:00 25.592780 25.608860 25.558195 25.594425 25.60486 25.61781 25.56375 25.60592 25.58070 25.59991 25.55264 25.58293
2018-08-09 20:00:00 25.594445 25.636430 25.545465 25.585605 25.60592 25.64496 25.55545 25.59346 25.58297 25.62790 25.53548 25.57775
2018-08-10 20:00:00 25.585580 25.686255 25.557130 25.647620 25.59341 25.70684 25.57201 25.68813 25.57775 25.66567 25.54225 25.60711
2018-08-12 20:00:00 25.652780 25.680240 25.633440 25.674650 25.70278 25.70278 25.67881 25.69645 25.60278 25.65770 25.58807 25.65285
2018-08-13 20:00:00 25.674545 25.793145 25.628530 25.760400 25.69632 25.80975 25.63602 25.77639 25.65277 25.77654 25.62104 25.74441
2018-08-14 20:00:00 25.760480 25.763385 25.660360 25.714295 25.77607 25.77893 25.67508 25.74417 25.74489 25.74784 25.64564 25.68442
2018-08-15 20:00:00 25.713955 25.827175 25.633805 25.763930 25.74383 25.83670 25.64417 25.77365 25.68408 25.81765 25.62344 25.75421
2018-08-16 20:00:00 25.763895 25.792380 25.664825 25.738495 25.77365 25.80624 25.69475 25.74795 25.75414 25.77852 25.63490 25.72904
2018-08-17 20:00:00 25.738500 25.772570 25.663040 25.695505 25.74786 25.79785 25.69785 25.72635 25.72914 25.74729 25.62823 25.66466
2018-08-19 20:00:00 25.685805 25.746835 25.660020 25.691620 25.71079 25.76879 25.69354 25.71751 25.66082 25.72488 25.62650 25.66573
2018-08-20 20:00:00 25.691625 25.777385 25.669930 25.748580 25.71734 25.79402 25.69821 25.76824 25.66591 25.76075 25.64165 25.72892
2018-08-21 20:00:00 25.748480 25.795805 25.694765 25.733665 25.76813 25.80468 25.70725 25.75043 25.72883 25.78693 25.68228 25.71690
2018-08-22 20:00:00 25.733685 25.751250 25.696090 25.727985 25.75031 25.75775 25.70296 25.73739 25.71706 25.74475 25.68922 25.71858
2018-08-23 20:00:00 25.727965 25.784485 25.702515 25.730820 25.73735 25.80371 25.70767 25.75156 25.71858 25.76526 25.69736 25.71008
2018-08-24 20:00:00 25.730810 25.782810 25.704375 25.750855 25.75153 25.80094 25.71845 25.79539 25.71009 25.76468 25.69030 25.70632
2018-08-26 20:00:00 25.753945 25.786120 25.741825 25.759455 25.77972 25.81350 25.77015 25.77204 25.72817 25.75874 25.71350 25.74687
2018-08-27 20:00:00 25.759460 25.771650 25.675345 25.702600 25.77200 25.78346 25.70304 25.71100 25.74692 25.75984 25.64765 25.69420
2018-08-28 20:00:00 25.702335 25.742405 25.684850 25.699350 25.71075 25.75196 25.69462 25.70822 25.69392 25.73285 25.67508 25.69048
2018-08-29 20:00:00 25.699340 25.779330 25.695770 25.730025 25.70820 25.78751 25.70693 25.74663 25.69048 25.77115 25.68461 25.71342
2018-08-30 20:00:00 25.730125 25.784555 25.719180 25.773995 25.74669 25.79255 25.73200 25.78385 25.71356 25.77656 25.70636 25.76414
2018-08-31 20:00:00 25.773995 25.804200 25.715330 25.759500 25.78405 25.81825 25.72232 25.80950 25.76394 25.79015 25.70834 25.70950
2018-09-02 20:00:00 25.759005 25.779015 25.725250 25.748800 25.78224 25.81354 25.76059 25.78818 25.73577 25.74449 25.68991 25.70942
2018-09-03 20:00:00 25.748800 25.783505 25.721280 25.736055 25.78805 25.80715 25.73980 25.74436 25.70955 25.75986 25.70276 25.72775
2018-09-04 20:00:00 25.736060 25.764340 25.699550 25.742475 25.74440 25.77168 25.70333 25.75123 25.72772 25.75700 25.69577 25.73372

362 rows × 12 columns

In [ ]: