Python Forum
Have I implemented this function correctly? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Have I implemented this function correctly? (/thread-35312.html)



Have I implemented this function correctly? - naggafin - Oct-18-2021

Hi guys! I'm writing a function for an algorithmic trading bot. The function is derived from a white paper from Pauna Cristian which I've linked here. In essence it is supposed to determine price cyclicality. The basic algorithm is given on page 148 (although the PDF excerpt is only a few pages; nonetheless that is the page number displayed). I believe I have implemented this correctly using the pandas library, however my confusion is in the fact that the paper mentions the oscillation for this function should be between 0 and 100. For my implementation it is 0.0 through 1.0. This leaves me doubting whether or not I'm fully understanding how to implement what is shown. If anyone would like to give input I would certainly appreciate it! Code sample below:

def price_cyclicality_function(df: DataFrame, p1=20, p2=50, n=20, a=0.33) -> DataFrame:
	if p1 > p2:
		return df
	
	# Make a DataFrame with the same number of rows
	tmp = DataFrame(index=df.index)
	
	# Get calculate our moving averages using a function from talib
	tmp['Ma'] = ta.SMA(df, timeperiod=p1)
	tmp['ma'] = ta.SMA(df, timeperiod=p2)
	
	# Find min/max values for Ma - ma in a window size of n
	tmp['min'] = (tmp['Ma'] - tmp['ma']).rolling(n).min()
	tmp['max'] = (tmp['Ma'] - tmp['ma']).rolling(n).max()
	
	# Get the derivative
	tmp['delta'] = (
		( tmp['max'] - (tmp['Ma'] - tmp['ma']) )/
		( tmp['max'] - tmp['min'] )
	)
	
	# And finally calculate PCY
	tmp['pcy'] = 0.0
	for i in range(1, len(df)):
		tmp.at[i, 'pcy'] = a*(tmp.at[i, 'delta'] - tmp.at[i-1, 'pcy']) + tmp.at[i-1, 'pcy']
	
	return tmp



RE: Have I implemented this function correctly? - 22_alias - Oct-19-2021

It follows from the formula that the value you get is between 0 and 1. Then they probably just multiply it by 100. I don't know if your code is correct but values between 0 and 1 are what you should expect, so that's one test passed.


RE: Have I implemented this function correctly? - naggafin - Oct-19-2021

(Oct-19-2021, 01:19 PM)22_alias Wrote: It follows from the formula that the value you get is between 0 and 1. Then they probably just multiply it by 100. I don't know if your code is correct but values between 0 and 1 are what you should expect, so that's one test passed.

I see. It does sound as if it is behaving as expected then. Thanks for taking the time to review my problem. I really appreciate it!


RE: Have I implemented this function correctly? - amusinR - May-19-2022

(Oct-19-2021, 09:09 PM)naggafin Wrote:
(Oct-19-2021, 01:19 PM)22_alias Wrote: It follows from the formula that the value you get is between 0 and 1. Then they probably just multiply it by 100. I don't know if your code is correct but values between 0 and 1 are what you should expect, so that's one test passed.

I see. It does sound as if it is behaving as expected then. Thanks for taking the time to review my problem. I really appreciate it!

I made a copy of my dataframe and build the pcy according to this code example. But when using the above code and call the function, it gives me an "KeyError: 1". What can be the reason? Maybe it is trivial.. but I dont get it Doh


RE: Have I implemented this function correctly? - stevendaprano - May-22-2022

(May-19-2022, 06:52 PM)amusinR Wrote: it gives me an "KeyError: 1". What can be the reason?

Dunno. What's the full error traceback you get? How confident are you that the "pcy" (what's a pcy?) is correct?