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()
In [3]:
btc = qb.AddEquity("SPY")
In [4]:
# Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution
h1 = qb.History(qb.Securities.Keys, datetime(2018,2,1), datetime(2018,4,1), Resolution.Daily)
In [5]:
h1.head()
Out[5]:
open high low close volume
symbol time
SPY 2018-02-02 278.660519 280.663416 278.303567 279.215777 53468898.0
2018-02-03 277.688816 277.857377 273.078186 273.187255 101915098.0
2018-02-06 271.134781 273.514461 261.080633 261.943267 176589413.0
2018-02-07 257.788743 267.416531 256.509665 266.900934 202733428.0
2018-02-08 266.226691 270.054010 265.364057 265.393803 93255735.0
In [6]:
mom = Momentum(10)
bb = BollingerBands(10, 2, MovingAverageType.Exponential) 
In [7]:
momdf = qb.Indicator(mom, "SPY", datetime(2018,2,1), datetime(2018,4,1), Resolution.Daily)
bbdf = qb.Indicator(bb, "SPY",  datetime(2018,2,1), datetime(2018,4,1), Resolution.Daily)
In [8]:
results = pd.concat([bbdf, momdf], axis=1)
In [9]:
results.head()
Out[9]:
bollingerbands lowerband middleband standarddeviation upperband momentum
2018-02-15 267.416531 253.504565 266.317858 6.406646 279.131151 -11.799246
2018-02-16 270.688591 257.245813 267.112537 4.933362 276.979261 -8.527186
2018-02-17 270.777829 258.660870 267.778954 4.559042 276.897037 -2.409426
2018-02-21 269.131884 258.691917 268.024941 4.666512 277.357964 7.188616
2018-02-22 267.713991 258.563891 267.968404 4.702257 277.372918 0.813057
In [10]:
h1.reset_index(level = 0, drop = True, inplace = True)
In [11]:
h1.head()
Out[11]:
open high low close volume
time
2018-02-02 278.660519 280.663416 278.303567 279.215777 53468898.0
2018-02-03 277.688816 277.857377 273.078186 273.187255 101915098.0
2018-02-06 271.134781 273.514461 261.080633 261.943267 176589413.0
2018-02-07 257.788743 267.416531 256.509665 266.900934 202733428.0
2018-02-08 266.226691 270.054010 265.364057 265.393803 93255735.0
In [12]:
total_df = pd.concat([h1,results], axis=1)
In [13]:
total_df
Out[13]:
open high low close volume bollingerbands lowerband middleband standarddeviation upperband momentum
2018-02-02 278.660519 280.663416 278.303567 279.215777 53468898.0 NaN NaN NaN NaN NaN NaN
2018-02-03 277.688816 277.857377 273.078186 273.187255 101915098.0 NaN NaN NaN NaN NaN NaN
2018-02-06 271.134781 273.514461 261.080633 261.943267 176589413.0 NaN NaN NaN NaN NaN NaN
2018-02-07 257.788743 267.416531 256.509665 266.900934 202733428.0 NaN NaN NaN NaN NaN NaN
2018-02-08 266.226691 270.054010 265.364057 265.393803 93255735.0 NaN NaN NaN NaN NaN NaN
2018-02-09 265.701178 265.899485 255.418978 255.537962 123274023.0 NaN NaN NaN NaN NaN NaN
2018-02-10 258.591885 261.378093 250.778602 259.454519 161401326.0 NaN NaN NaN NaN NaN NaN
2018-02-13 261.635892 264.749306 259.662741 263.014123 77783193.0 NaN NaN NaN NaN NaN NaN
2018-02-14 261.764791 264.357651 261.080633 263.757773 44646480.0 NaN NaN NaN NaN NaN NaN
2018-02-15 262.230812 267.713991 261.735045 267.416531 67480698.0 267.416531 253.504565 266.317858 6.406646 279.131151 -11.799246
2018-02-16 269.290529 270.698506 266.494405 270.688591 58730886.0 270.688591 257.245813 267.112537 4.933362 276.979261 -8.527186
2018-02-17 270.004433 272.988948 269.964772 270.777829 62819387.0 270.777829 258.660870 267.778954 4.559042 276.897037 -2.409426
2018-02-21 269.726804 271.352918 268.209758 269.131884 45375017.0 269.131884 258.691917 268.024941 4.666512 277.357964 7.188616
2018-02-22 269.597904 272.394028 267.694160 267.713991 55130019.0 267.713991 258.563891 267.968404 4.702257 277.372918 0.813057
2018-02-23 268.794762 270.738168 267.357039 268.031282 52837117.0 268.031282 258.431491 267.979837 4.774173 277.528182 2.637479
2018-02-24 269.508666 272.374198 268.953408 272.364282 45042677.0 272.364282 261.142197 268.777009 3.817406 276.411820 16.826320
2018-02-27 273.593783 275.656173 272.929456 275.537189 42241564.0 275.537189 262.843772 270.006132 3.581180 277.168493 16.082670
2018-02-28 275.755326 276.558468 272.037076 272.056907 56874950.0 272.056907 264.168737 270.379000 3.105132 276.589264 9.042784
2018-03-01 273.345900 273.851582 269.012900 269.032730 64496277.0 269.032730 265.306444 270.134224 2.413890 274.962004 5.274957
2018-03-02 269.112053 270.857152 263.747858 265.463210 104704373.0 265.463210 263.886058 269.284949 2.699445 274.683840 -1.953321
2018-03-03 263.549551 267.446277 262.577849 266.851357 73014840.0 266.851357 263.134597 268.842478 2.853940 274.550358 -3.837234
2018-03-06 265.463210 270.579522 265.344226 269.905280 50619522.0 269.905280 263.370128 269.035714 2.832793 274.701301 -0.872549
2018-03-07 270.956305 271.075289 268.884000 270.579522 40045719.0 270.579522 263.633012 269.316407 2.841697 274.999801 1.447639
2018-03-08 268.150266 270.867067 267.912298 270.490284 48742357.0 270.490284 264.003014 269.529839 2.763413 275.056664 2.776293
2018-03-09 271.224019 271.918092 270.113502 271.759447 36498819.0 271.759447 264.496042 269.935222 2.719590 275.374402 3.728165
2018-03-10 273.365731 276.489061 272.998863 276.459315 47248139.0 276.459315 264.637326 271.121421 3.242048 277.605516 4.095033
2018-03-13 276.836097 277.540086 275.735495 276.142024 35535002.0 276.142024 265.366363 272.034258 3.333947 278.702153 0.604835
2018-03-14 277.500425 278.035853 273.692937 274.347349 46972844.0 274.347349 265.489411 272.454820 3.482704 279.420228 2.290442
2018-03-15 275.457866 275.666088 272.344452 272.939371 53800014.0 272.939371 265.647938 272.542920 3.447491 279.437902 3.906641
2018-03-16 273.544207 274.268026 272.106484 272.681573 37918924.0 272.681573 266.957943 272.568130 2.805093 278.178316 7.218362
2018-03-17 273.249012 274.144976 272.930447 272.960313 37095576.0 272.960313 268.315844 272.639436 2.161796 276.963028 6.108956
2018-03-20 272.104170 272.153945 267.415293 269.276906 62818531.0 269.276906 267.522064 272.028067 2.253001 276.534069 -0.628373
2018-03-21 269.714933 270.451614 268.968297 269.724888 30329689.0 269.724888 266.912470 271.609307 2.348419 276.306144 -0.854634
2018-03-22 269.685068 272.044439 268.978252 269.217176 46648467.0 269.217176 266.187182 271.174374 2.493596 276.161566 -1.273109
2018-03-23 266.718432 267.664172 262.178883 262.487493 83666643.0 262.487493 261.740570 269.594941 3.927186 277.449312 -9.271954
2018-03-24 262.965340 263.831438 256.673684 256.852877 91155429.0 256.852877 256.132291 267.278202 5.572956 278.424114 -19.606438
2018-03-27 260.954399 264.239600 258.246598 263.980766 68678525.0 263.980766 255.981029 266.678668 5.348819 277.376307 -12.161258
2018-03-28 264.976281 265.573590 257.679154 259.361575 83421257.0 259.361575 254.188210 265.348288 5.580039 276.508365 -14.985774
2018-03-29 259.580588 261.472067 257.420320 258.674670 84788009.0 258.674670 252.758084 264.134903 5.688409 275.511721 -14.264702
2018-03-30 259.929019 264.070362 259.122651 262.308300 67110933.0 262.308300 253.374289 263.802793 5.214252 274.231297 -10.373273
In [ ]: