Skip to content
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

Open
cl7805 opened this issue Jun 30, 2021 · 6 comments
Open

ADV FML pg 44 name error #502

cl7805 opened this issue Jun 30, 2021 · 6 comments
Labels
question Further information is requested

Comments

@cl7805
Copy link

cl7805 commented Jun 30, 2021

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

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

getDailyVol(close,100)


NameError Traceback (most recent call last)

in ()
1
----> 2 getDailyVol(close,100)

NameError: name 'close' is not defined

Thks

@ghgr
Copy link

ghgr commented Jun 30, 2021

You need to feed it a series of prices:

getDailyVol(df.price,100)

@cl7805
Copy link
Author

cl7805 commented Jun 30, 2021

getDailyVol(df0['Adj Close'],100)

UnboundLocalError Traceback (most recent call last)

in ()
1
----> 2 getDailyVol(df0['Adj Close'],100)

in getDailyVol(Close, span0)
1 def getDailyVol(Close,span0=100):
2 # daily vol reindexed to close
----> 3 df0=df0['Adj Close'].index.searchsorted(Close.index-pd.Timedelta(days=1))
4 df0=df0[df0>0]
5 df0=(pd.Series(df0['Adj Close'].index[df0-1],

UnboundLocalError: local variable 'df0' referenced before assignment

@ghgr
Copy link

ghgr commented Jun 30, 2021

The function was fine before, you just need to call it with a Series of prices.

@cl7805
Copy link
Author

cl7805 commented Jun 30, 2021

ser = pd.Series(df0['Adj Close'])
print(ser)

ValueError Traceback (most recent call last)

in ()
----> 1 ser = pd.Series(df0['Adj Close'])
2 print(ser)

2 frames

/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in nonzero(self)
1328 def nonzero(self):
1329 raise ValueError(
-> 1330 f"The truth value of a {type(self).name} is ambiguous. "
1331 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1332 )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

@ghgr
Copy link

ghgr commented Jun 30, 2021

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)

@PanPip PanPip added the question Further information is requested label Oct 11, 2021
@PanPip
Copy link
Contributor

PanPip commented Oct 11, 2021

@ghgr Thank you for helping to solve this issue!

@cl7805 is this now resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants