Skip to content

Commit

Permalink
Trend calculation optimized.
Browse files Browse the repository at this point in the history
  • Loading branch information
pranjal-joshi committed May 6, 2021
1 parent 29b5ef7 commit 80b80c1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/classes/Screener.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,36 @@ def findBreakout(data, dict, saveDict, daysToLookback):
if ((hs - hc) <= (hs*2/100)):
saveDict['Breaking-Out'] = str(hc)
if rc >= hc:
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "Yes (BO: " + str(hc) + " R: " + str(hs) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "BO: " + str(hc) + " R: " + str(hs) + colorText.END
return True
else:
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "NO (BO: " + str(hc) + " R: " + str(hs) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "BO: " + str(hc) + " R: " + str(hs) + colorText.END
return False
else:
noOfHigherShadows = len(data[data.High > hc])
if(daysToLookback/noOfHigherShadows <= 3):
saveDict['Breaking-Out'] = str(hs)
if rc >= hs:
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "Yes (BO: " + str(hs) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "BO: " + str(hs) + colorText.END
return True
else:
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "NO (BO: " + str(hs) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "BO: " + str(hs) + colorText.END
return False
else:
saveDict['Breaking-Out'] = str(hc) + ", " + str(hs)
if rc >= hc:
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "Yes (BO: " + str(hc) + " R: " + str(hs) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "BO: " + str(hc) + " R: " + str(hs) + colorText.END
return True
else:
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "NO (BO: " + str(hc) + " R: " + str(hs) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "BO: " + str(hc) + " R: " + str(hs) + colorText.END
return False
else:
saveDict['Breaking-Out'] = str(hc)
if rc >= hc:
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "Yes (BO: " + str(hc) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.GREEN + "BO: " + str(hc) + colorText.END
return True
else:
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "NO (BO: " + str(hc) + ")" + colorText.END
dict['Breaking-Out'] = colorText.BOLD + colorText.FAIL + "BO: " + str(hc) + colorText.END
return False

# Validate 'Inside Bar' structure for recent days
Expand Down Expand Up @@ -169,20 +169,34 @@ def findTrend(data, dict, saveDict, daysToLookback=ConfigManager.daysToLookback)
data = data.set_index(np.arange(len(data)))
data['tops'] = data['Close'].iloc[list(argrelextrema(np.array(data['Close']), np.greater_equal, order=1)[0])]
data = data.fillna(0)
slope,c = np.polyfit(data.index[data.tops > 0], data['tops'][data.tops > 0], 1)
angle = np.rad2deg(np.arctan(slope))
if(slope <= 2.5 and slope >= -2.5):
dict['Trend'] = colorText.BOLD + colorText.WARN + "Sideways" + colorText.END
saveDict['Trend'] = 'Sideways'
elif slope >= 2.5:
dict['Trend'] = colorText.BOLD + colorText.GREEN + "Uptrend" + colorText.END
saveDict['Trend'] = 'Uptrend'
elif slope <= -2.5:
dict['Trend'] = colorText.BOLD + colorText.FAIL + "Downtrend" + colorText.END
saveDict['Trend'] = 'Downtrend'
try:
slope,c = np.polyfit(data.index[data.tops > 0], data['tops'][data.tops > 0], 1)
angle = np.rad2deg(np.arctan(slope))
if (angle <= 30 and angle >= -30):
dict['Trend'] = colorText.BOLD + colorText.WARN + "Sideways" + colorText.END
saveDict['Trend'] = 'Sideways'
elif (angle >= 30 and angle < 61):
dict['Trend'] = colorText.BOLD + colorText.GREEN + "Weak Up" + colorText.END
saveDict['Trend'] = 'Weak Up'
elif angle >= 60:
dict['Trend'] = colorText.BOLD + colorText.GREEN + "Strong Up" + colorText.END
saveDict['Trend'] = 'Strong Up'
elif (angle >= -30 and angle < -61):
dict['Trend'] = colorText.BOLD + colorText.FAIL + "Weak Down" + colorText.END
saveDict['Trend'] = 'Weak Down'
elif angle <= -60:
dict['Trend'] = colorText.BOLD + colorText.FAIL + "Strong Down" + colorText.END
saveDict['Trend'] = 'Strong Down'
except np.linalg.LinAlgError:
dict['Trend'] = colorText.BOLD + "Unknown" + colorText.END
saveDict['Trend'] = 'Unknown'
return saveDict['Trend']

# Debugging - Experiment with data
# import matplotlib.pyplot as plt
# print(saveDict['Trend'])
# print(slope)
# print(math.degrees(math.atan(slope)))
# plt.scatter(data.index[data.tops > 0], data['tops'][data.tops > 0], c='r')
# plt.plot(data.index, data['Close'])
# plt.plot(data.index, slope*data.index+c,)
Expand Down
1 change: 1 addition & 0 deletions src/classes/Utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def getLastScreenedResults():
df = pd.read_pickle(lastScreened)
print(colorText.BOLD + colorText.GREEN + '\n[+] Showing recently screened results..\n' + colorText.END)
print(tabulate(df, headers='keys', tablefmt='psql'))
print(colorText.BOLD + colorText.WARN + "[+] Note: Trend calculation is based on number of recent days to screen as per your configuration." + colorText.END)
input(colorText.BOLD + colorText.GREEN + '[+] Press any key to continue..' + colorText.END)
except:
print(colorText.BOLD + colorText.FAIL + '[+] Failed to load recently screened result table from disk! Skipping..' + colorText.END)
Expand Down
1 change: 1 addition & 0 deletions src/screenipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def main(testing=False):
print(tabulate(screenResults, headers='keys', tablefmt='psql'))
Utility.tools.setLastScreenedResults(screenResults)
Utility.tools.promptSaveResults(saveResults)
print(colorText.BOLD + colorText.WARN + "[+] Note: Trend calculation is based on number of days recent to screen as per your configuration." + colorText.END)
print(colorText.BOLD + colorText.GREEN + "[+] Screening Completed! Happy Trading! :)" + colorText.END)
input('')
main()
Expand Down

0 comments on commit 80b80c1

Please sign in to comment.