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

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

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:

我想创建一个算法它将帮助我指出交易中的突破点并相应地制定我的策略

我的代码
```python
import pandas as pd
import pandas_ta as ta
import matplotlib.pyplot as plt
import numpy as np
from statistics import variance

# 将价格数据加载到pandas DataFrame中
data = pd.read_csv('data.csv')
close = data['close']
high = data['high']
low = data['low']

def find_pivot_highs(data, length):
    pivot_highs = []
    for i in range(length, len(data) - length):
        if (
            data[i] > max(data[i - length : i])
            and data[i] > max(data[i + 1 : i + length + 1])
        ):
            pivot_highs.append(i)
    return pivot_highs

def find_pivot_lows(data, length):
    pivot_lows = []
    for i in range(length, len(data) - length):
        if (
            data[i] < min(data[i - length : i])
            and data[i] < min(data[i + 1 : i + length + 1])
        ):
            pivot_lows.append(i)
    return pivot_lows

length = 14
k = 1.0
show = False
n = range(len(close))

# 使用pandas-ta计算指标
data['atr'] = ta.atr(high, low, close, length)
data['stddev'] = ta.stdev(close, length)
data['sma1'] = ta.sma(close, length)
data['sma2'] = ta.sma(close * n, length)
data['var'] = variance(close)
data['n'] = data.index

data['slope'] = 0.0

data['slope_atr'] = data['atr'] / length * k
data['slope_stdev'] = data['stddev'] / length * k
data['slope_linereg'] = (data['sma1'] - data['sma2'] * data['n']) / data['var'] / 2 * k

# 寻找顶点高点和低点
data['ph'] = find_pivot_highs(high, length)
data['pl'] = find_pivot_lows(low, length)

data['slope_ph'] = data['slope_atr'].where(data.index.isin(data['ph'])).ffill()
data['slope_pl'] = data['slope_atr'].where(data.index.isin(data['pl'])).ffill()

data['upper'] = data['ph'].where(data.index.isin(data['ph'])).ffill() - data['slope_ph']
data['lower'] = data['pl'].where(data.index.isin(data['pl'])).ffill() + data['slope_pl']

你遇到的错误信息是:

File "/Users/dev.barbhaya9gmail.com/Library/CloudStorage/OneDrive-IITKanpur/Beyond/BRAINWorks/Internship/Project #304/strategy (pandas-ta).py", line 60, in <module>
    data['ph'] = find_pivot_highs(high, length)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py", line 3977, in __setitem__
    self._set_item(key, value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py", line 4171, in _set_item
    value = self._sanitize_column(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py", line 4904, in _sanitize_column
    com.require_length_match(value, self.index)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/common.py", line 561, in require_length_match
    raise ValueError(
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:

import pandas as pd
import pandas_ta as ta
import matplotlib.pyplot as plt
import numpy as np
from statistics import variance
# Load your price data into a pandas DataFrame
data = pd.read_csv(&#39;data.csv&#39;)
close = data[&#39;close&#39;]
high = data[&#39;high&#39;]
low = data[&#39;low&#39;]
def find_pivot_highs(data, length):
pivot_highs = []
for i in range(length, len(data) - length):
if (
data[i] &gt; max(data[i - length : i])
and data[i] &gt; max(data[i + 1 : i + length + 1])
):
pivot_highs.append(i)
return pivot_highs
def find_pivot_lows(data, length):
pivot_lows = []
for i in range(length, len(data) - length):
if (
data[i] &lt; min(data[i - length : i])
and data[i] &lt; min(data[i + 1 : i + length + 1])
):
pivot_lows.append(i)
return pivot_lows
length = 14
k = 1.0
#method = &#39;Atr&#39;
show = False
n = range(len(close))
# Calculate the indicators using pandas-ta
data[&#39;atr&#39;] = ta.atr(high, low, close, length)
data[&#39;stddev&#39;] = ta.stdev(close, length)
data[&#39;sma1&#39;] = ta.sma(close, length)
data[&#39;sma2&#39;] = ta.sma(close * n, length)
data[&#39;var&#39;] = variance(close)
data[&#39;n&#39;] = data.index
data[&#39;slope&#39;] = 0.0
#data[&#39;slope&#39;] = np.where(method == &#39;Atr&#39;, data[&#39;atr&#39;] / length * k,
#                         np.where(method == &#39;Stdev&#39;, data[&#39;stddev&#39;] / length * k,
#                                  method == &#39;Linreg&#39;, data[&#39;sma1&#39;] - data[&#39;sma2&#39;] * data[&#39;n&#39;] / data[&#39;var&#39;] / 2 * k))
data[&#39;slope_atr&#39;] = data[&#39;atr&#39;] / length * k
data[&#39;slope_stdev&#39;] = data[&#39;stddev&#39;] / length * k
data[&#39;slope_linereg&#39;] = (data[&#39;sma1&#39;] - data[&#39;sma2&#39;] * data[&#39;n&#39;]) / data[&#39;var&#39;] / 2 * k
# Find pivot highs and lows
data[&#39;ph&#39;] = find_pivot_highs(high, length)
data[&#39;pl&#39;] = find_pivot_lows(low, length)
data[&#39;slope_ph&#39;] = data[&#39;slope_atr&#39;].where(data.index.isin(data[&#39;ph&#39;])).ffill()
data[&#39;slope_pl&#39;] = data[&#39;slope_atr&#39;].where(data.index.isin(data[&#39;pl&#39;])).ffill()
data[&#39;upper&#39;] = data[&#39;ph&#39;].where(data.index.isin(data[&#39;ph&#39;])).ffill() - data[&#39;slope_ph&#39;]
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:

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;
data[&#39;ph&#39;] = find_pivot_highs(high, length)
File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 3977, in __setitem__
self._set_item(key, value)
File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 4171, in _set_item
value = self._sanitize_column(value)
File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 4904, in _sanitize_column
com.require_length_match(value, self.index)
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
raise ValueError(
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

英文:
# Find pivot highs and lows
data[&#39;ph&#39;] = np.nan
data[&#39;pl&#39;] = np.nan
pivot_highs = find_pivot_highs(high, length)
pivot_lows = find_pivot_lows(low, length)
# assign the value True to the corresponding indices
for i in pivot_highs:
data.loc[i, &#39;ph&#39;] = True
for i in pivot_lows:
data.loc[i, &#39;pl&#39;] = True
for i, row in data.iterrows():
if row[&#39;ph&#39;] == True:
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
data.loc[i, &#39;ph_stdev_slope&#39;] = row[&#39;stddev&#39;] / length * k
data.loc[i, &#39;ph_atr_slope&#39;] = row[&#39;atr&#39;] / length * k
for i, row in data.iterrows():
if row[&#39;pl&#39;] == True:
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
data.loc[i, &#39;pl_stdev_slope&#39;] = row[&#39;stddev&#39;] / length * k
data.loc[i, &#39;pl_atr_slope&#39;] = row[&#39;atr&#39;] / length * k

答案2

得分: 0

以下是您要翻译的内容:

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

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

# 使用np.nan或其他填充值初始化新列
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

data['slope_ph'] = data['slope'].where(data['ph'] == True).ffill()
data['slope_pl'] = data['slope'].where(data['pl'] == True).ffill()

data['upper'] = data['ph'].where(data['ph'] == True).ffill() - data['slope_ph']
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:

# initialize new columns with np.nan or any other fill value
data[&#39;ph&#39;] = np.nan
data[&#39;pl&#39;] = np.nan
pivot_highs = find_pivot_highs(high, length)
pivot_lows = find_pivot_lows(low, length)
# assign the value True to the corresponding indices
for i in pivot_highs:
data.loc[i, &#39;ph&#39;] = True
for i in pivot_lows:
data.loc[i, &#39;pl&#39;] = True
data[&#39;slope_ph&#39;] = data[&#39;slope&#39;].where(data[&#39;ph&#39;] == True).ffill()
data[&#39;slope_pl&#39;] = data[&#39;slope&#39;].where(data[&#39;pl&#39;] == True).ffill()
data[&#39;upper&#39;] = data[&#39;ph&#39;].where(data[&#39;ph&#39;] == True).ffill() - data[&#39;slope_ph&#39;]
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

以下是代码部分的翻译:

for i, row in data.iterrows():
    if row['pl'] == True:
        data.loc[i, 'lower_linreg'] = row['close']
        data.loc[i, 'lower_stdev'] = row['close']
        data.loc[i, 'lower_atr'] = row['close']
    else:
        data.loc[i, 'lower_linreg'] = data.loc[i-1, 'lower_linreg'] + row['pl_linreg_slope']
        data.loc[i, 'lower_stdev'] = data.loc[i-1, 'lower_stdev'] + row['pl_stdev_slope']
        data.loc[i, 'lower_atr'] = data.loc[i-1, 'lower_atr'] + row['pl_atr_slope']

错误信息部分未翻译。

英文:
for i, row in data.iterrows():
if row[&#39;pl&#39;] == True:
data.loc[i, &#39;lower_linreg&#39;] = row[&#39;close&#39;]
data.loc[i, &#39;lower_stdev&#39;] = row[&#39;close&#39;]
data.loc[i, &#39;lower_atr&#39;] = row[&#39;close&#39;]
else:
data.loc[i, &#39;lower_linreg&#39;] = data.loc[i-1, &#39;lower_linreg&#39;] + row[&#39;pl_linreg_slope&#39;]
data.loc[i, &#39;lower_stdev&#39;] = data.loc[i-1, &#39;lower_stdev&#39;] + row[&#39;pl_stdev_slope&#39;]
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:

Traceback (most recent call last):
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
return self._engine.get_loc(casted_key)
File &quot;pandas/_libs/index.pyx&quot;, line 138, in pandas._libs.index.IndexEngine.get_loc
File &quot;pandas/_libs/index.pyx&quot;, line 165, in pandas._libs.index.IndexEngine.get_loc
File &quot;pandas/_libs/hashtable_class_helper.pxi&quot;, line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
File &quot;pandas/_libs/hashtable_class_helper.pxi&quot;, line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: &#39;lower_linreg&#39;
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
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;
data.loc[i, &#39;lower_linreg&#39;] = data.loc[i-1, &#39;lower_linreg&#39;] + row[&#39;pl_linreg_slope&#39;]
File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/indexing.py&quot;, line 1066, in __getitem__
return self.obj._get_value(*key, takeable=self._takeable)
File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py&quot;, line 3914, in _get_value
series = self._get_item_cache(col)
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
loc = self.columns.get_loc(item)
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
raise KeyError(key) from err
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:

确定