-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ADV FML pg 44 name error #502
Comments
You need to feed it a series of prices: getDailyVol(df.price,100) |
getDailyVol(df0['Adj Close'],100)UnboundLocalError Traceback (most recent call last) in () in getDailyVol(Close, span0) UnboundLocalError: local variable 'df0' referenced before assignment |
The function was fine before, you just need to call it with a Series of prices. |
ser = pd.Series(df0['Adj Close'])
|
I just used the function you posted in the first post. I don't know where it comes from (in my copy of the book it's slightly different although functionally identical): import numpy as np
import pandas as pd
import time
def getDailyVol(close,span0=100):
# daily vol reindexed to close
df0=close.index.searchsorted(close.index-pd.Timedelta(days=1))
df0=df0[df0>0]
df0=(pd.Series(close.index[df0-1],
index=close.index[close.shape[0]-df0.shape[0]:]))
try:
df0=close.loc[df0.index]/close.loc[df0.values].values-1 # daily rets
except Exception as e:
print(f'error: {e}\nplease confirm no duplicate indices')
df0=df0.ewm(span=span0).std().rename('dailyVol')
return df0
def get_prices():
indices = np.arange(1622483891, 1625075912, 60)
random_prices = 100*(np.exp(np.random.normal(0,0.005,len(indices)))).cumprod()
prices = pd.Series(random_prices, indices)
prices.index= pd.to_datetime(prices.index, unit='s')
return prices
prices = get_prices()
res = getDailyVol(prices) |
This code is a snippet from Lopez De Prado Advances in Financial Machine Learning page 44
def getDailyVol(close,span0=100):
# daily vol reindexed to close
getDailyVol(close,100)
NameError Traceback (most recent call last)
in ()
1
----> 2 getDailyVol(close,100)
NameError: name 'close' is not defined
Thks
The text was updated successfully, but these errors were encountered: