Python Forum
Have I implemented this function correctly?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Have I implemented this function correctly?
#1
Brick 
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
Reply


Messages In This Thread
Have I implemented this function correctly? - by naggafin - Oct-18-2021, 11:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  DF.groupby(col).min works, mean gets a "not implemented" error samgardner5 3 4,736 Feb-29-2024, 06:13 PM
Last Post: deanhystad
  NotImplementedError: pseudo-class is not implemented - how to Update Python to solve apollo 1 4,085 May-16-2021, 08:03 AM
Last Post: buran
  A function for correctly using "a/an" before a word Exsul1 2 6,282 Oct-07-2019, 07:51 PM
Last Post: newbieAuggie2019
  Could I override a fully implemented method zatlas1 2 3,184 Jun-06-2019, 02:20 AM
Last Post: zatlas1
  How to you find the file where a class or a function was implemented? MisterX 4 5,312 Mar-16-2017, 09:51 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020