- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 DatetimeIndex 和 ohlcv 股票报价列的 pandas Dataframe。我想提取满足特定阈值的价格波动/趋势:大于 0.3 美元的上升趋势/趋势和超过 -0.3 美元的下跌趋势/趋势。
df[:10]
close high low open volume
2014-05-09 09:30:00-04:00 187.5600 187.73 187.54 187.700 1922600
2014-05-09 09:31:00-04:00 187.4900 187.56 187.42 187.550 534400
2014-05-09 09:32:00-04:00 187.4200 187.51 187.35 187.490 224800
2014-05-09 09:33:00-04:00 187.5500 187.58 187.39 187.400 303700
2014-05-09 09:34:00-04:00 187.6700 187.67 187.53 187.560 438200
2014-05-09 09:35:00-04:00 187.6000 187.71 187.56 187.680 296400
2014-05-09 09:36:00-04:00 187.4100 187.67 187.38 187.600 329900
2014-05-09 09:37:00-04:00 187.3100 187.44 187.28 187.400 404000
2014-05-09 09:38:00-04:00 187.2600 187.37 187.26 187.300 912800
2014-05-09 09:39:00-04:00 187.2200 187.28 187.12 187.250 607700
在研究了 pandas 文档之后,Dataframe.apply() 似乎是一种方法,但我在构建函数时遇到了困难。由于我的编码能力总体上有限,所以我需要一些帮助。
global row_nr
row_nr = 1
def extract_swings()
if row_nr == 1 : pivot = row.open ; row_nr += 1
else : if (row.high-pivot) >= 0.3 : ????
... ????
df['swings'] = df.apply(extract_swings, axis=1)
结果应该是这样的:
df['swings'][:10]
2014-05-09 09:30:00-04:00 NaN
2014-05-09 09:31:00-04:00 NaN
2014-05-09 09:32:00-04:00 -0.35
2014-05-09 09:33:00-04:00 NaN
2014-05-09 09:34:00-04:00 NaN
2014-05-09 09:35:00-04:00 0.36
2014-05-09 09:36:00-04:00 NaN
2014-05-09 09:37:00-04:00 NaN
2014-05-09 09:38:00-04:00 NaN
2014-05-09 09:39:00-04:00 -0.59
更新:为了避免混淆,请求的函数应该如何通过数据框:
close high low open volume
2014-05-09 09:30:00-04:00 187.5600 187.73 187.54 187.700 1922600
# this is the first line, first minute and we well take row.open 187.70 as \
# the starting point or first pivot
2014-05-09 09:31:00-04:00 187.4900 187.56 187.42 187.550 534400
# next minute we check if either (row.high - pivot) >= 0.3 or \
# (row.low-pivot) <= -0.3. Neither is true so nothing to do here.
2014-05-09 09:32:00-04:00 187.4200 187.51 187.35 187.490 224800
# next minute same check ... we see that row.low-pivot = -0.35. \
# We consider 187.35 a second pivot and the diff value -0.35 a first trend down
2014-05-09 09:33:00-04:00 187.5500 187.58 187.39 187.400 303700
# next minute we check if the identified trend/swing down goes further \
# down by having a row.low lower than previous row.low. If we would \
# have found here a new lower row.low that would be the second pivot \
# and we would forget about 187.35 as being a pivot ... and so on. \
# We don't see that on this row, instead we see prices are higher than \
# previous row, so we start checking the diff for a potential up trend \
# starting from second pivot 187.35. As long as we do not encounter a \
# higher high with over 0.3 above last pivot we are still within the identified down trend.
2014-05-09 09:34:00-04:00 187.6700 187.67 187.53 187.560 438200
# we don't see a lower low to reconsider the second pivot neither \
# a (row.high- second_pivot) >= 0.3
2014-05-09 09:35:00-04:00 187.6000 187.71 187.56 187.680 296400
# here we see (row.high- second_pivot) = 0.36. We consider 187.71 as \
# a third_pivot and the diff value 0.36 as an up trend (from second pivot to here)
2014-05-09 09:36:00-04:00 187.4100 187.67 187.38 187.600 329900
# next minute we check if the identified trend/swing up goes further up \
# by having a row.high higher than third pivot. If we would have found here \
# a new higher row.high that would be the third pivot and we would forget \
# about 187.71 as being a pivot ... and so on. We don't see that on this row,\
# instead we see prices are lower than previous row, so we start \
# checking the diff for a potential down trend starting from third \
# pivot 187.71. As long as we do not encounter a lower low with \
# over 0.3 below last pivot we are still within the identified up trend.
2014-05-09 09:37:00-04:00 187.3100 187.44 187.28 187.400 404000
# we find here a (row.low - third_pivot) = 0.43 so we have identified \
# a new down trend starting from third pivot and now we have a potential\
# fourth pivot 187.28
2014-05-09 09:38:00-04:00 187.2600 187.37 187.26 187.300 912800
# we find here a lower low so we don't consider 187.28 the fourth \
# pivot anymore but this lower low 187.26
2014-05-09 09:39:00-04:00 187.2200 187.28 187.12 187.250 607700
# we find here a lower low so we don't consider 187.26 the fourth pivot anymore \
# but this lower low 187.12. Being this the lowest low we consider this one \
# to be the fourth pivot and the diff 187.12-187.71=-0.59 as a downtrend with that value
最佳答案
这有点棘手,因为在找到下一个潜在支点之前,您不能将一个点标记为支点(即,如果您处于上升趋势中,则在找到足够低的低点之前不能说它已经完成)。
这段代码可以解决问题 - 为方便起见,我已将您的数据放在 tmpData.txt 文件中,并获得了所需的结果。请检查
def get_pivots():
data = pd.DataFrame.from_csv('tmpData.txt')
data['swings'] = np.nan
pivot = data.irow(0).open
last_pivot_id = 0
up_down = 0
diff = .3
for i in range(0, len(data)):
row = data.irow(i)
# We don't have a trend yet
if up_down == 0:
if row.low < pivot - diff:
data.ix[i, 'swings'] = row.low - pivot
pivot, last_pivot_id = row.low, i
up_down = -1
elif row.high > pivot + diff:
data.ix[i, 'swings'] = row.high - pivot
pivot, last_pivot_id = row.high, i
up_down = 1
# Current trend is up
elif up_down == 1:
# If got higher than last pivot, update the swing
if row.high > pivot:
# Remove the last pivot, as it wasn't a real one
data.ix[i, 'swings'] = data.ix[last_pivot_id, 'swings'] + (row.high - data.ix[last_pivot_id, 'high'])
data.ix[last_pivot_id, 'swings'] = np.nan
pivot, last_pivot_id = row.high, i
elif row.low < pivot - diff:
data.ix[i, 'swings'] = row.low - pivot
pivot, last_pivot_id = row.low, i
# Change the trend indicator
up_down = -1
# Current trend is down
elif up_down == -1:
# If got lower than last pivot, update the swing
if row.low < pivot:
# Remove the last pivot, as it wasn't a real one
data.ix[i, 'swings'] = data.ix[last_pivot_id, 'swings'] + (row.low - data.ix[last_pivot_id, 'low'])
data.ix[last_pivot_id, 'swings'] = np.nan
pivot, last_pivot_id = row.low, i
elif row.high > pivot - diff:
data.ix[i, 'swings'] = row.high - pivot
pivot, last_pivot_id = row.high, i
# Change the trend indicator
up_down = 1
print data
输出:
date close high low open volume swings
2014-05-09 13:30:00 187.56 187.73 187.54 187.70 1922600 NaN
2014-05-09 13:31:00 187.49 187.56 187.42 187.55 534400 NaN
2014-05-09 13:32:00 187.42 187.51 187.35 187.49 224800 -0.35
2014-05-09 13:33:00 187.55 187.58 187.39 187.40 303700 NaN
2014-05-09 13:34:00 187.67 187.67 187.53 187.56 438200 NaN
2014-05-09 13:35:00 187.60 187.71 187.56 187.68 296400 0.36
2014-05-09 13:36:00 187.41 187.67 187.38 187.60 329900 NaN
2014-05-09 13:37:00 187.31 187.44 187.28 187.40 404000 NaN
2014-05-09 13:38:00 187.26 187.37 187.26 187.30 912800 NaN
2014-05-09 13:39:00 187.22 187.28 187.12 187.25 607700 -0.59
关于python - 用股票报价识别 Pandas 数据框中的价格波动/趋势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23614259/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!