【问题标题】:How do I find out where one line crosses another with pandas/matplotlib?如何使用 pandas/matplotlib 找出一条线与另一条线相交的位置?
【发布时间】:2021-10-27 15:35:28
【问题描述】:
【问题讨论】:
标签:
pandas
numpy
matplotlib
【解决方案1】:
我最终尝试了很多不同的东西。最初我真的不确定如何解决这个问题,因为我对编码还很陌生。在做了一些研究之后,我找到了一个解决方案,它通过应用一个函数来创建一个新列,然后找到满足特定条件的列区域的索引。
#Making a function to apply to df
trade_pos = ''
dip = ''
def apply_function(df):
global trade_pos
global dip
if df.Drawdown == 0 and trade_pos == 'open':
trade_pos = 'closed'
dip = 'false'
return 'Exit'
elif df.Drawdown == 0:
trade_pos = 'closed'
dip = 'false'
return 0
elif df.Drawdown >= 2 and df.Drawdown < 5 and trade_pos == 'closed' and dip != 'true':
trade_pos = 'open'
return 'Entry'
elif df.Drawdown >=5 and trade_pos == 'open':
trade_pos = 'closed'
dip = 'true'
return 'Exit'
else:
return 0
#Applying function to df
df['Strategy'] = df.apply(apply_function, axis=1)
Finding dates of crossover with caveats
entry = df[df['Strategy'] == 'Entry']
entry = list(entry.index)
#We finish with a list of dates that meet the criteria of the original post
希望对遇到类似问题的人有所帮助。