Hi python experts. I know that for many of you this will be a trivially (and probably laughably) easy question, but i am forced to admit that my own python skills are just not up to it, and so i need to reach out for your help, with all due humility on my part. Here's my problem...
In my own trading outside of Q, i make a lot of use of digital signal processing (DSP) methods in which recursive function calls are applied to price data series. I'm sure you know the sort of stuff; Ehler's books are full of these types of things. In general mathematical (not pythonic) notation, at each price data point P(i) for i = 0, 1, 2,... the result, Result(i) = some algebraic function of the previous result, Result(i-1). Including the initialization condition, this is easy for me to write and execute in most languages as something like the following pseudo-code:
function (PriceArray, parameter(s) e.g. Nperiod)
{
result[0] = Price[0] ... or some other function of initial price
define my_function coefficients here ... for example k = 2/(Nperiod +1) in the case of N-period EMA of Price.
loop for i = 1 to imax
result[i] = my_function (result[i-1]) ... for example result[i] = k*Price[i] + (1-k)*result[i-1] .... in the case of EMA.
}
Its easy for me to do in OTHER languages, but I am embarrassed to say that I have not been successful in implementing this type of recursive function here in the Q / python context. I'm not sure, but i think it requires 2 steps, namely a "class" function to handle the data array part, preceded by a "helper" function to define the actual user function itself. The following is my attempt (illustrated by the standard "hello world" example of an EMA), but i have not been able to get it to work. My aim here is NOT actually that i want to get an EMA (yes, i know its available in TAlib) but rather to understand the correct mechanics of "pythonizing" these sorts of functions. Please, without re-writing it all completely, can someone show me what corrections are required to make THE FOLLOWING = my attempted Q / python code snippet workable? (Apologies about the non-visibility of the indentation which is not showing up here).
# the "helper" function
recursionStarted = False
previousResult = 0.0
def my_recursive_function (priceArray, periodN):
global recursionStarted
global previousResult
if not recursionStarted:
initialResult = priceArray[0]
previousResult = initialResult
recursionStarted = True
# else if recursionStarted
k = 2.0 / (periodN + 1) # Using EMA here as an example only.
result = (k* priceArray[-1]) + (1-k)* previousResult
previousResult = result
return result
# the main "class" function as required in Q algos
class deltaClosePct_vsRecursivePrice (CustomFactor):
inputs= [USEquityPricing.close]
window_length = 252
global Nbars
Nbars = 21
def compute(self, today, assets, out, close):
functionVal = my_recursive_function(close, Nbars)
out[:] = 100*(close[-1]/functionVal[-1] -1) # calculating % difference between closing price and the "filter function" value (here EMA).
Thanks in anticipation, best regards, TonyM.