I took some time off and tried the whole thing in the research notebook. Im was trying to archieve what i wrote in the first post by using two approaches:
1. Get amount of days from T=0 for the lowest number of standarddeviation (T-30)
2. Get amount of days from T=0 in decending value
Thats what i got:
import pandas as pd
import numpy as np
import scipy
import matplotlib as mpl
import matplotlib.pyplot as plt
import math
from datetime import datetime, timedelta
# Set this between 150 to 300 for higher resolution graphs
mpl.rcParams['figure.dpi']= 300
# Load the QuantBook. The QuantBook is the main
# access point you will use to request data from
# For more information see [https://www.quantconnect.com/docs/research/overview]
qb = QuantBook()
# Aliasing qb as `self` for portability purposes :)
self = qb
#setting time value
#msft_start = datetime(2015, 1, 1)
#msft_end = datetime(2018, 12 ,31)
bb = BollingerBands(10, 2, MovingAverageType.Exponential)
start_date = datetime(2017,1,1)
end_date = datetime(2018,1,1)
msft = self.AddEquity("MSFT", Resolution.Minute).Symbol
msft_10Q = qb.AddData(SECReport10Q, msft)
df1_bb = self.Indicator(bb, msft, 360, Resolution.Daily)
report_history = self.History(SECReport10Q, msft_10Q.Symbol, timedelta(days=360))
report_history.drop(['datasourceid','report' ], axis=1, inplace=True)
df1_bb.drop(['bandwidth', 'percentb', 'bollingerbands','lowerband','middleband','price','upperband' ], axis=1, inplace=True)
df1_bb = df1_bb.rename_axis('time')
report_history.index = report_history.index.droplevel('symbol')
'''
Going back 30 days and finding lowest value
'''
newone=[]
actual = []
for date in report_history.index:
r = 0
n = 1
number= 5
safe = 0
date1 = date
print(date1)
while(r <= 30):
while((date - timedelta(days=n)) not in df1_bb.index):
n = n+1
r=r+1
if (df1_bb.loc[date - timedelta(days=n)]['standarddeviation'] < number):
number = df1_bb.loc[date - timedelta(days=n)]['standarddeviation']
date = date - timedelta(days=n)
n=1
r =r+1
safe = r
while((date - timedelta(days=n)) not in df1_bb.index):
n = n+1
r =r+1
elif (df1_bb.loc[date - timedelta(days=n)]['standarddeviation'] > number):
date = date - timedelta(days=n)
n=1
r =r+1
while((date - timedelta(days=n)) not in df1_bb.index):
n = n+1
r =r+1
n=1
newone.append(safe)
display(newone)
average2 = [i for i in newone if i != 0]
print(average2)
average2 = sum(average2)/len(average2)
math.ceil(average2)
'''
Finding lowest value in descending order
'''
tryme = []
for date in report_history.index:
i = 0
n=1
while((date - timedelta(days=n)) not in df1_bb.index):
n = n+1
while(df1_bb.loc[date]['standarddeviation'] > df1_bb.loc[date - timedelta(days=n)]['standarddeviation']):
date = date - timedelta(days=n)
n=1
i = 1+i
while((date - timedelta(days=n)) not in df1_bb.index):
n = n+1
tryme.append(i)
print('list' +str(tryme))
average = [i for i in tryme if i != 0]
print(average)
average = sum(average)/len(average)
math.ceil(average)
As you can see i tried it using just one stock and not a whole universe. I tried implementing it into QC but i dont know how to change this line of code.
df1_bb = self.Indicator(bb, msft, 360, Resolution.Daily)
Also if I import pandas and the other libraries into QC i simply can paste the code that i wrote into QC, right?