Hi - I am hoping that someone can help me overcome some of the issues I'm running into on my algo build. I am developing a system that leverages volatility. One key component to my system is an EWMA of volatility, leveraging weights built off lambda. The challenges that I'm running into are two-fold:
1) The lambda weights will be a list that is multiplied against another variable (x). This list of weights should be the length of the entire backtest period and the values of the weights are:
n0, (1-lambda_constant)*(lambda_constant)^0
n1, (1-lambda_constant)*(lambda_constant)^1
n2, (1-lambda_constant)*(lambda_constant)^2
etc...
How do I create this variable? Is there a way to create a variable based on the start bar and end bar and subtract the two to determine the length of the list? I was planning to create the weights using the function below where x is the length of a series indexed by the dates of the security's price:
def lambda_val(x):
mylambda=somevalue
length=pd.Series((range(len(x))[::-1]))
list_length=length
constant_weight=(1-mylambda)+(list_length*0)
lambda_weight=mylambda+(list_length*0)
lambda_calc=constant_weight*(lambda_weight**list_length)
return lambda_calc
The issue that I'm running into here is that on line 3 of the function above I am getting an error: Runtime exception: TypeError: object of type 'float' has no len(). However, x is a list of close prices within handle_data...so I'm confused as to why this isn't working. I think it has something to do with arguments passing through handle_data...
close_price = data[stock].close_price
In addition, I tried creating
2) Once I have a solve to create the lambda weight list, I need to turn this into a series or dataframe with a date index using the dates of the entire backtest. How do I pull the timestamp of the stock prices as a variable to pass through the dataframe such as index=data.index?
Again, the end goal in mind is to multiple the lambda weight list by another variable (x) to create new variable (y), which is what I will be using as a rolling average - or my EWMA.
Separately and on a side note - why would I use history(bar_count) on a backtest? Doesn't the backtest leverage the inputted dates that I assign for all calculations?
Sorry if this is an obvious answer, but thanks for your help!