如何设计一个能够识别波峰和波谷以确定突破点的Python交易算法?

huangapple go评论194阅读模式
英文:

How do I devise a python trading algorithm that identifies pivot highs and lows to inform breakout points?

问题

Here's the translated code part without any additional information:

  1. 我想创建一个算法它将帮助我指出交易中的突破点并相应地制定我的策略
  2. 我的代码
  3. ```python
  4. import pandas as pd
  5. import pandas_ta as ta
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. from statistics import variance
  9. # 将价格数据加载到pandas DataFrame中
  10. data = pd.read_csv('data.csv')
  11. close = data['close']
  12. high = data['high']
  13. low = data['low']
  14. def find_pivot_highs(data, length):
  15. pivot_highs = []
  16. for i in range(length, len(data) - length):
  17. if (
  18. data[i] > max(data[i - length : i])
  19. and data[i] > max(data[i + 1 : i + length + 1])
  20. ):
  21. pivot_highs.append(i)
  22. return pivot_highs
  23. def find_pivot_lows(data, length):
  24. pivot_lows = []
  25. for i in range(length, len(data) - length):
  26. if (
  27. data[i] < min(data[i - length : i])
  28. and data[i] < min(data[i + 1 : i + length + 1])
  29. ):
  30. pivot_lows.append(i)
  31. return pivot_lows
  32. length = 14
  33. k = 1.0
  34. show = False
  35. n = range(len(close))
  36. # 使用pandas-ta计算指标
  37. data['atr'] = ta.atr(high, low, close, length)
  38. data['stddev'] = ta.stdev(close, length)
  39. data['sma1'] = ta.sma(close, length)
  40. data['sma2'] = ta.sma(close * n, length)
  41. data['var'] = variance(close)
  42. data['n'] = data.index
  43. data['slope'] = 0.0
  44. data['slope_atr'] = data['atr'] / length * k
  45. data['slope_stdev'] = data['stddev'] / length * k
  46. data['slope_linereg'] = (data['sma1'] - data['sma2'] * data['n']) / data['var'] / 2 * k
  47. # 寻找顶点高点和低点
  48. data['ph'] = find_pivot_highs(high, length)
  49. data['pl'] = find_pivot_lows(low, length)
  50. data['slope_ph'] = data['slope_atr'].where(data.index.isin(data['ph'])).ffill()
  51. data['slope_pl'] = data['slope_atr'].where(data.index.isin(data['pl'])).ffill()
  52. data['upper'] = data['ph'].where(data.index.isin(data['ph'])).ffill() - data['slope_ph']
  53. data['lower'] = data['pl'].where(data.index.isin(data['pl'])).ffill() + data['slope_pl']

你遇到的错误信息是:

  1. File "/Users/dev.barbhaya9gmail.com/Library/CloudStorage/OneDrive-IITKanpur/Beyond/BRAINWorks/Internship/Project #304/strategy (pandas-ta).py", line 60, in <module>
  2. data['ph'] = find_pivot_highs(high, length)
  3. File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py", line 3977, in __setitem__
  4. self._set_item(key, value)
  5. File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py", line 4171, in _set_item
  6. value = self._sanitize_column(value)
  7. File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py", line 4904, in _sanitize_column
  8. com.require_length_match(value, self.index)
  9. File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/common.py", line 561, in require_length_match
  10. raise ValueError(
  11. ValueError: Length of values (6) does not match length of index (374)

这个错误表示数据长度不匹配,values的长度是6,但index的长度是374。这可能是由于数据不匹配或输入数据的问题导致的。你需要检查输入数据以确保其格式正确,并且与代码中的预期格式相匹配。

英文:

I want to create an algorithm, which will help me point the breakout points in the trading and devise my strategy accordingly.

My code:

  1. import pandas as pd
  2. import pandas_ta as ta
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from statistics import variance
  6. # Load your price data into a pandas DataFrame
  7. data = pd.read_csv(&#39;data.csv&#39;)
  8. close = data[&#39;close&#39;]
  9. high = data[&#39;high&#39;]
  10. low = data[&#39;low&#39;]
  11. def find_pivot_highs(data, length):
  12. pivot_highs = []
  13. for i in range(length, len(data) - length):
  14. if (
  15. data[i] &gt; max(data[i - length : i])
  16. and data[i] &gt; max(data[i + 1 : i + length + 1])
  17. ):
  18. pivot_highs.append(i)
  19. return pivot_highs
  20. def find_pivot_lows(data, length):
  21. pivot_lows = []
  22. for i in range(length, len(data) - length):
  23. if (
  24. data[i] &lt; min(data[i - length : i])
  25. and data[i] &lt; min(data[i + 1 : i + length + 1])
  26. ):
  27. pivot_lows.append(i)
  28. return pivot_lows
  29. length = 14
  30. k = 1.0
  31. #method = &#39;Atr&#39;
  32. show = False
  33. n = range(len(close))
  34. # Calculate the indicators using pandas-ta
  35. data[&#39;atr&#39;] = ta.atr(high, low, close, length)
  36. data[&#39;stddev&#39;] = ta.stdev(close, length)
  37. data[&#39;sma1&#39;] = ta.sma(close, length)
  38. data[&#39;sma2&#39;] = ta.sma(close * n, length)
  39. data[&#39;var&#39;] = variance(close)
  40. data[&#39;n&#39;] = data.index
  41. data[&#39;slope&#39;] = 0.0
  42. #data[&#39;slope&#39;] = np.where(method == &#39;Atr&#39;, data[&#39;atr&#39;] / length * k,
  43. # np.where(method == &#39;Stdev&#39;, data[&#39;stddev&#39;] / length * k,
  44. # method == &#39;Linreg&#39;, data[&#39;sma1&#39;] - data[&#39;sma2&#39;] * data[&#39;n&#39;] / data[&#39;var&#39;] / 2 * k))
  45. data[&#39;slope_atr&#39;] = data[&#39;atr&#39;] / length * k
  46. data[&#39;slope_stdev&#39;] = data[&#39;stddev&#39;] / length * k
  47. data[&#39;slope_linereg&#39;] = (data[&#39;sma1&#39;] - data[&#39;sma2&#39;] * data[&#39;n&#39;]) / data[&#39;var&#39;] / 2 * k
  48. # Find pivot highs and lows
  49. data[&#39;ph&#39;] = find_pivot_highs(high, length)
  50. data[&#39;pl&#39;] = find_pivot_lows(low, length)
  51. data[&#39;slope_ph&#39;] = data[&#39;slope_atr&#39;].where(data.index.isin(data[&#39;ph&#39;])).ffill()
  52. data[&#39;slope_pl&#39;] = data[&#39;slope_atr&#39;].where(data.index.isin(data[&#39;pl&#39;])).ffill()
  53. data[&#39;upper&#39;] = data[&#39;ph&#39;].where(data.index.isin(data[&#39;ph&#39;])).ffill() - data[&#39;slope_ph&#39;]
  54. data[&#39;lower&#39;] = data[&#39;pl&#39;].where(data.index.isin(data[&#39;pl&#39;])).ffill() + data[&#39;slope_pl&#39;]

Here, I'm encountering a strange error:

  1. File &quot;/Users/dev.barbhaya9gmail.com/Library/CloudStorage/OneDrive-IITKanpur/Beyond/BRAINWorks/Internship/Project #304/strategy (pandas-ta).py&quot;, line 60, in &lt;module&gt;
  2. data[&#39;ph&#39;] = find_pivot_highs(high, length)
  3. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 3977, in __setitem__
  4. self._set_item(key, value)
  5. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 4171, in _set_item
  6. value = self._sanitize_column(value)
  7. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 4904, in _sanitize_column
  8. com.require_length_match(value, self.index)
  9. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/common.py&quot;, line 561, in require_length_match
  10. raise ValueError(
  11. ValueError: Length of values (6) does not match length of index (374)

I've been using the same find_pivot_highs function in a different code and it is being called in a same manner, but there is this error which arises in this case, I'm trying to ascertain the reason. Any leads will be very helpful.

答案1

得分: 1

查找枢轴高点和低点

data['ph'] = np.nan
data['pl'] = np.nan

pivot_highs = find_pivot_highs(high, length)
pivot_lows = find_pivot_lows(low, length)

为相应的索引分配值 True

for i in pivot_highs:
data.loc[i, 'ph'] = True

for i in pivot_lows:
data.loc[i, 'pl'] = True

for i, row in data.iterrows():
if row['ph'] == True:
data.loc[i, 'ph_linreg_slope'] = (row['sma1'] - row['sma2'] * row['n']) / row['var'] / 2 * k
data.loc[i, 'ph_stdev_slope'] = row['stddev'] / length * k
data.loc[i, 'ph_atr_slope'] = row['atr'] / length * k

for i, row in data.iterrows():
if row['pl'] == True:
data.loc[i, 'pl_linreg_slope'] = (row['sma1'] - row['sma2'] * row['n']) / row['var'] / 2 * k
data.loc[i, 'pl_stdev_slope'] = row['stddev'] / length * k
data.loc[i, 'pl_atr_slope'] = row['atr'] / length * k

英文:
  1. # Find pivot highs and lows
  2. data[&#39;ph&#39;] = np.nan
  3. data[&#39;pl&#39;] = np.nan
  4. pivot_highs = find_pivot_highs(high, length)
  5. pivot_lows = find_pivot_lows(low, length)
  6. # assign the value True to the corresponding indices
  7. for i in pivot_highs:
  8. data.loc[i, &#39;ph&#39;] = True
  9. for i in pivot_lows:
  10. data.loc[i, &#39;pl&#39;] = True
  11. for i, row in data.iterrows():
  12. if row[&#39;ph&#39;] == True:
  13. data.loc[i, &#39;ph_linreg_slope&#39;] = (row[&#39;sma1&#39;] - row[&#39;sma2&#39;] * row[&#39;n&#39;]) / row[&#39;var&#39;] / 2 * k
  14. data.loc[i, &#39;ph_stdev_slope&#39;] = row[&#39;stddev&#39;] / length * k
  15. data.loc[i, &#39;ph_atr_slope&#39;] = row[&#39;atr&#39;] / length * k
  16. for i, row in data.iterrows():
  17. if row[&#39;pl&#39;] == True:
  18. data.loc[i, &#39;pl_linreg_slope&#39;] = (row[&#39;sma1&#39;] - row[&#39;sma2&#39;] * row[&#39;n&#39;]) / row[&#39;var&#39;] / 2 * k
  19. data.loc[i, &#39;pl_stdev_slope&#39;] = row[&#39;stddev&#39;] / length * k
  20. data.loc[i, &#39;pl_atr_slope&#39;] = row[&#39;atr&#39;] / length * k

答案2

得分: 0

以下是您要翻译的内容:

您原始代码中出现错误的原因是这些索引列表(表示枢轴点)的长度与您的DataFrame的长度不匹配。

要正确地将这些枢轴点分配给DataFrame,您需要在相应的索引处将这些枢轴点插入到DataFrame中。下面是如何做到这一点的方法:

  1. # 使用np.nan或其他填充值初始化新列
  2. data['ph'] = np.nan
  3. data['pl'] = np.nan
  4. pivot_highs = find_pivot_highs(high, length)
  5. pivot_lows = find_pivot_lows(low, length)
  6. # 将值True分配给相应的索引
  7. for i in pivot_highs:
  8. data.loc[i, 'ph'] = True
  9. for i in pivot_lows:
  10. data.loc[i, 'pl'] = True
  11. data['slope_ph'] = data['slope'].where(data['ph'] == True).ffill()
  12. data['slope_pl'] = data['slope'].where(data['pl'] == True).ffill()
  13. data['upper'] = data['ph'].where(data['ph'] == True).ffill() - data['slope_ph']
  14. data['lower'] = data['pl'].where(data['pl'] == True).ffill() + data['slope_pl']

这样,您的 'ph' 和 'pl' 列将在枢轴点处填充为True,而在其他地方填充为NaN(或您使用的任何其他填充值)。如果您希望在枢轴点处使用实际的高/低价格而不是True,您可以将True替换为high[i]和low[i]。

最后,如果您更喜欢使用True/False值而不是True/NaN,您可以使用 data['ph'].fillna(False)data['pl'].fillna(False)

英文:

The reason for the error in your original code is that the length of these lists of indices (which represent the pivot points) does not match the length of your DataFrame.

To correctly assign these pivot points to the DataFrame, you need to insert these pivot points into your DataFrame at the corresponding indices. Here's how to do that:

  1. # initialize new columns with np.nan or any other fill value
  2. data[&#39;ph&#39;] = np.nan
  3. data[&#39;pl&#39;] = np.nan
  4. pivot_highs = find_pivot_highs(high, length)
  5. pivot_lows = find_pivot_lows(low, length)
  6. # assign the value True to the corresponding indices
  7. for i in pivot_highs:
  8. data.loc[i, &#39;ph&#39;] = True
  9. for i in pivot_lows:
  10. data.loc[i, &#39;pl&#39;] = True
  11. data[&#39;slope_ph&#39;] = data[&#39;slope&#39;].where(data[&#39;ph&#39;] == True).ffill()
  12. data[&#39;slope_pl&#39;] = data[&#39;slope&#39;].where(data[&#39;pl&#39;] == True).ffill()
  13. data[&#39;upper&#39;] = data[&#39;ph&#39;].where(data[&#39;ph&#39;] == True).ffill() - data[&#39;slope_ph&#39;]
  14. data[&#39;lower&#39;] = data[&#39;pl&#39;].where(data[&#39;pl&#39;] == True).ffill() + data[&#39;slope_pl&#39;]

This way, your 'ph' and 'pl' columns will be filled with True at the pivot points and NaN (or whatever fill value you used) everywhere else. If you want to use the actual high/low prices at the pivot points instead of True, you could replace True with high[i] and low[i] respectively.

Finally, you may use data[&#39;ph&#39;].fillna(False) and data[&#39;pl&#39;].fillna(False) if you'd prefer to work with True/False values instead of True/NaN.

答案3

得分: 0

以下是代码部分的翻译:

  1. for i, row in data.iterrows():
  2. if row['pl'] == True:
  3. data.loc[i, 'lower_linreg'] = row['close']
  4. data.loc[i, 'lower_stdev'] = row['close']
  5. data.loc[i, 'lower_atr'] = row['close']
  6. else:
  7. data.loc[i, 'lower_linreg'] = data.loc[i-1, 'lower_linreg'] + row['pl_linreg_slope']
  8. data.loc[i, 'lower_stdev'] = data.loc[i-1, 'lower_stdev'] + row['pl_stdev_slope']
  9. data.loc[i, 'lower_atr'] = data.loc[i-1, 'lower_atr'] + row['pl_atr_slope']

错误信息部分未翻译。

英文:
  1. for i, row in data.iterrows():
  2. if row[&#39;pl&#39;] == True:
  3. data.loc[i, &#39;lower_linreg&#39;] = row[&#39;close&#39;]
  4. data.loc[i, &#39;lower_stdev&#39;] = row[&#39;close&#39;]
  5. data.loc[i, &#39;lower_atr&#39;] = row[&#39;close&#39;]
  6. else:
  7. data.loc[i, &#39;lower_linreg&#39;] = data.loc[i-1, &#39;lower_linreg&#39;] + row[&#39;pl_linreg_slope&#39;]
  8. data.loc[i, &#39;lower_stdev&#39;] = data.loc[i-1, &#39;lower_stdev&#39;] + row[&#39;pl_stdev_slope&#39;]
  9. data.loc[i, &#39;lower_atr&#39;] = data.loc[i-1, &#39;lower_atr&#39;] + row[&#39;pl_atr_slope&#39;]

looks likely, but there's an error and it says:

  1. Traceback (most recent call last):
  2. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/indexes/base.py&quot;, line 3803, in get_loc
  3. return self._engine.get_loc(casted_key)
  4. File &quot;pandas/_libs/index.pyx&quot;, line 138, in pandas._libs.index.IndexEngine.get_loc
  5. File &quot;pandas/_libs/index.pyx&quot;, line 165, in pandas._libs.index.IndexEngine.get_loc
  6. File &quot;pandas/_libs/hashtable_class_helper.pxi&quot;, line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  7. File &quot;pandas/_libs/hashtable_class_helper.pxi&quot;, line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
  8. KeyError: &#39;lower_linreg&#39;
  9. The above exception was the direct cause of the following exception:
  10. Traceback (most recent call last):
  11. File &quot;/Users/dev.barbhaya9gmail.com/Library/CloudStorage/OneDrive-IITKanpur/Beyond/BRAINWorks/Internship/Project #304/strategy (pandas-ta).py&quot;, line 89, in &lt;module&gt;
  12. data.loc[i, &#39;lower_linreg&#39;] = data.loc[i-1, &#39;lower_linreg&#39;] + row[&#39;pl_linreg_slope&#39;]
  13. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/indexing.py&quot;, line 1066, in __getitem__
  14. return self.obj._get_value(*key, takeable=self._takeable)
  15. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 3914, in _get_value
  16. series = self._get_item_cache(col)
  17. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 4271, in _get_item_cache
  18. loc = self.columns.get_loc(item)
  19. File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/indexes/base.py&quot;, line 3805, in get_loc
  20. raise KeyError(key) from err
  21. KeyError: &#39;lower_linreg&#39;

huangapple
  • 本文由 发表于 2023年5月25日 22:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定