-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Akshat111111 patch 2 #374
Akshat111111 patch 2 #374
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,127 +5,86 @@ | |
""" | ||
|
||
|
||
def gentrends(dataframe, field="close", window=1 / 3.0, charts=False): | ||
""" | ||
Returns a Pandas dataframe with support and resistance lines. | ||
|
||
:param dataframe: incomming data matrix | ||
:param field: for which column would you like to generate the trendline | ||
:param window: How long the trendlines should be. If window < 1, then it | ||
will be taken as a percentage of the size of the data | ||
:param charts: Boolean value saying whether to print chart to screen | ||
""" | ||
|
||
x = dataframe[field] | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technical doesn't depend on matplotlib - and we don't intend to. The way it was previously made this optional - where the code would fail if matplotlib isn't installed. Please keep the matplotlib import where it was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I will make the desired changes and then redirect to you. |
||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
x = np.array(x) | ||
def gentrends(dataframe, field="close", window=1 / 3.0, charts=False): | ||
x = np.array(dataframe[field]) | ||
|
||
if window < 1: | ||
window = int(window * len(x)) | ||
|
||
max1 = np.where(x == max(x))[0][0] # find the index of the abs max | ||
min1 = np.where(x == min(x))[0][0] # find the index of the abs min | ||
max1_idx = np.argmax(x) | ||
min1_idx = np.argmin(x) | ||
|
||
# First the max | ||
if max1 + window >= len(x): | ||
max2 = max(x[0 : (max1 - window)]) | ||
max1 = x[max1_idx] | ||
min1 = x[min1_idx] | ||
|
||
if max1_idx + window >= len(x): | ||
max2 = np.max(x[0 : (max1_idx - window)]) | ||
else: | ||
max2 = max(x[(max1 + window) :]) | ||
max2 = np.max(x[(max1_idx + window) :]) | ||
|
||
# Now the min | ||
if min1 - window <= 0: | ||
min2 = min(x[(min1 + window) :]) | ||
if min1_idx - window <= 0: | ||
min2 = np.min(x[(min1_idx + window) :]) | ||
else: | ||
min2 = min(x[0 : (min1 - window)]) | ||
|
||
# Now find the indices of the secondary extrema | ||
max2 = np.where(x == max2)[0][0] # find the index of the 2nd max | ||
min2 = np.where(x == min2)[0][0] # find the index of the 2nd min | ||
|
||
# Create & extend the lines | ||
maxslope = (x[max1] - x[max2]) / (max1 - max2) # slope between max points | ||
minslope = (x[min1] - x[min2]) / (min1 - min2) # slope between min points | ||
a_max = x[max1] - (maxslope * max1) # y-intercept for max trendline | ||
a_min = x[min1] - (minslope * min1) # y-intercept for min trendline | ||
b_max = x[max1] + (maxslope * (len(x) - max1)) # extend to last data pt | ||
b_min = x[min1] + (minslope * (len(x) - min1)) # extend to last data point | ||
maxline = np.linspace(a_max, b_max, len(x)) # Y values between max's | ||
minline = np.linspace(a_min, b_min, len(x)) # Y values between min's | ||
|
||
# OUTPUT | ||
trends = np.transpose(np.array((x, maxline, minline))) | ||
min2 = np.min(x[0 : (min1_idx - window)]) | ||
|
||
max2_idx = np.where(x == max2)[0][0] | ||
min2_idx = np.where(x == min2)[0][0] | ||
|
||
maxslope = (max1 - max2) / (max1_idx - max2_idx) | ||
minslope = (min1 - min2) / (min1_idx - min2_idx) | ||
a_max = max1 - (maxslope * max1_idx) | ||
a_min = min1 - (minslope * min1_idx) | ||
b_max = max1 + (maxslope * (len(x) - max1_idx)) | ||
Check failure on line 41 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.9)Ruff (F841)
Check failure on line 41 in technical/trendline.py GitHub Actions / build_windows (windows-latest, 3.10)Ruff (F841)
Check failure on line 41 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.11)Ruff (F841)
Check failure on line 41 in technical/trendline.py GitHub Actions / build (ubuntu-22.04, 3.9)Ruff (F841)
|
||
Comment on lines
+34
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see why we would remove the comments (not just on the highlighted lines). Please revert / keep the comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will readd the comments in order to make it beginner friendly |
||
b_min = min1 + (minslope * (len(x) - min1_idx)) | ||
Check failure on line 42 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.9)Ruff (F841)
Check failure on line 42 in technical/trendline.py GitHub Actions / build_windows (windows-latest, 3.10)Ruff (F841)
Check failure on line 42 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.11)Ruff (F841)
Check failure on line 42 in technical/trendline.py GitHub Actions / build (ubuntu-22.04, 3.9)Ruff (F841)
|
||
maxline = a_max + np.arange(len(x)) * maxslope | ||
minline = a_min + np.arange(len(x)) * minslope | ||
|
||
trends = pd.DataFrame( | ||
trends, index=np.arange(0, len(x)), columns=["Data", "Max Line", "Min Line"] | ||
{"Data": x, "Max Line": maxline, "Min Line": minline} | ||
) | ||
|
||
if charts: | ||
from matplotlib.pyplot import close, grid, plot, savefig | ||
|
||
plot(trends) | ||
grid() | ||
plt.plot(trends) | ||
plt.grid() | ||
|
||
if isinstance(charts, str): | ||
savefig(f"{charts}.png") | ||
plt.savefig(f"{charts}.png") | ||
else: | ||
savefig(f"{x[0]}_{x[len(x) - 1]}.png") | ||
close() | ||
plt.savefig(f"{x[0]}_{x[-1]}.png") | ||
|
||
return trends | ||
plt.close() | ||
|
||
return trends | ||
|
||
def segtrends(dataframe, field="close", segments=2, charts=False): | ||
""" | ||
Turn minitrends to iterative process more easily adaptable to | ||
implementation in simple trading systems; allows backtesting functionality. | ||
|
||
:param dataframe: incomming data matrix | ||
:param field: for which column would you like to generate the trendline | ||
:param segments: Number of Trend line segments to generate | ||
:param charts: Boolean value saying whether to print chart to screen | ||
""" | ||
|
||
x = dataframe[field] | ||
import numpy as np | ||
|
||
y = np.array(x) | ||
x = np.array(dataframe[field]) | ||
|
||
# Implement trendlines | ||
segments = int(segments) | ||
maxima = np.ones(segments) | ||
minima = np.ones(segments) | ||
segsize = int(len(y) / segments) | ||
for i in range(1, segments + 1): | ||
ind2 = i * segsize | ||
ind1 = ind2 - segsize | ||
maxima[i - 1] = max(y[ind1:ind2]) | ||
minima[i - 1] = min(y[ind1:ind2]) | ||
|
||
# Find the indexes of these maxima in the data | ||
x_maxima = np.ones(segments) | ||
x_minima = np.ones(segments) | ||
for i in range(0, segments): | ||
x_maxima[i] = np.where(y == maxima[i])[0][0] | ||
x_minima[i] = np.where(y == minima[i])[0][0] | ||
segsize = len(x) // segments | ||
maxima = np.array([np.max(x[i:i+segsize]) for i in range(0, len(x), segsize)]) | ||
minima = np.array([np.min(x[i:i+segsize]) for i in range(0, len(x), segsize)]) | ||
|
||
if charts: | ||
import matplotlib.pyplot as plt | ||
x_maxima = np.array([np.argmax(x[i:i+segsize]) + i for i in range(0, len(x), segsize)]) | ||
x_minima = np.array([np.argmin(x[i:i+segsize]) + i for i in range(0, len(x), segsize)]) | ||
|
||
plt.plot(y) | ||
plt.grid(True) | ||
if charts: | ||
plt.plot(x) | ||
plt.grid() | ||
|
||
for i in range(0, segments - 1): | ||
for i in range(segments - 1): | ||
maxslope = (maxima[i + 1] - maxima[i]) / (x_maxima[i + 1] - x_maxima[i]) | ||
a_max = maxima[i] - (maxslope * x_maxima[i]) | ||
b_max = maxima[i] + (maxslope * (len(y) - x_maxima[i])) | ||
maxline = np.linspace(a_max, b_max, len(y)) | ||
b_max = maxima[i] + (maxslope * (len(x) - x_maxima[i])) | ||
Check failure on line 81 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.9)Ruff (F841)
Check failure on line 81 in technical/trendline.py GitHub Actions / build_windows (windows-latest, 3.10)Ruff (F841)
Check failure on line 81 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.11)Ruff (F841)
Check failure on line 81 in technical/trendline.py GitHub Actions / build (ubuntu-22.04, 3.9)Ruff (F841)
|
||
maxline = a_max + np.arange(len(x)) * maxslope | ||
|
||
minslope = (minima[i + 1] - minima[i]) / (x_minima[i + 1] - x_minima[i]) | ||
a_min = minima[i] - (minslope * x_minima[i]) | ||
b_min = minima[i] + (minslope * (len(y) - x_minima[i])) | ||
minline = np.linspace(a_min, b_min, len(y)) | ||
b_min = minima[i] + (minslope * (len(x) - x_minima[i])) | ||
Check failure on line 86 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.9)Ruff (F841)
Check failure on line 86 in technical/trendline.py GitHub Actions / build_windows (windows-latest, 3.10)Ruff (F841)
Check failure on line 86 in technical/trendline.py GitHub Actions / build (ubuntu-20.04, 3.11)Ruff (F841)
Check failure on line 86 in technical/trendline.py GitHub Actions / build (ubuntu-22.04, 3.9)Ruff (F841)
|
||
minline = a_min + np.arange(len(x)) * minslope | ||
|
||
if charts: | ||
plt.plot(maxline, "g") | ||
|
@@ -134,12 +93,8 @@ | |
if charts: | ||
plt.show() | ||
|
||
import pandas as pd | ||
|
||
# OUTPUT | ||
# return x_maxima, maxima, x_minima, minima | ||
trends = np.transpose(np.array((x, maxline, minline))) | ||
trends = pd.DataFrame( | ||
trends, index=np.arange(0, len(x)), columns=["Data", "Max Line", "Min Line"] | ||
{"Data": x, "Max Line": maxline, "Min Line": minline} | ||
) | ||
|
||
return trends |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is producing the same results, actually (i'm pretty sure it will not).
We used to look at a rolling price (for all 3 necessary units) - which seems to have been removed completely - which will automaticall also completely change results.
I'm not per se against it - but some explanation about your thoughts (why do it this way instead of the other) will for sure be necessary to accept this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I have implemented using Numpy function,So it should work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err ... no ?
what i mean is - the prior calculation of
high
andlow
was using a rolling mean - in pandas / numpy terms something aroundhigh = dataframe['high'].rolling(timeperiod).mean()
not using qtpylib here is certainly preferred - and as said above, i'm also not per se against not using rolling averages (and instead use the price directly) - but i'd like to understand the reason for that change (if it was intentional ...).