r - 从 Pine 到 R 上下摆动
问题描述
我正在尝试将 Swing High 和 Low 函数从 Pine 转换为 R,但我无法真正了解 Pine 代码背后的逻辑。
基本上,这个函数在高价和低价的时间序列数据库上循环,并产生:
摆动高点:作为价格低于先前摆动高点后的最高价格的位置,或者作为在给定的先前时间段内达到新高的新价格的位置。
然后移动寻找
摆动低点:作为价格高于前一个摆动低点之后的最低值或作为在给定的前一个时间段内达到新低的新价格的位置。
有人熟悉 R 中的类似功能吗?
这是 Pine 中的函数:
//@version=3
study("Swings", overlay=true)
barsback = input(7, title='Bars back to check for a swing')
swing_detection(index)=>
swing_high = false
swing_low = false
start = (index*2) - 1 // -1 so we have an even number of
swing_point_high = high[index]
swing_point_low = low[index]
//Swing Highs
for i = 0 to start
swing_high := true
if i < index
if high[i] > swing_point_high
swing_high := false
break
// Have to do checks before pivot and after separately because we can get
// two highs of the same value in a row. Notice the > and >= difference
if i > index
if high[i] >= swing_point_high
swing_high := false
break
//Swing lows
for i = 0 to start
swing_low := true
if i < index
if low[i] < swing_point_low
swing_low := false
break
// Have to do checks before pivot and after seperately because we can get
// two lows of the same value in a row. Notice the > and >= difference
if i > index
if low[i] <= swing_point_low
swing_low := false
break
[swing_high, swing_low]
// Check for a swing
[swing_high, swing_low] = swing_detection(barsback)
// Plotting
plotshape(swing_high, style=shape.arrowdown, location=location.abovebar, color=red, text='SH', offset=-barsback)
plotshape(swing_low, style=shape.arrowup, location=location.belowbar, color=green, text='SL', offset=-barsback)
这是一个示例数据:
library(quantmod)
DT <- getSymbols('rdwr', auto.assign=FALSE)
DT=data.frame(DT$RDWR.Low, DT$RDWR.High)
colnames(DT)=c("Low","High")
解决方案
我创建了一个使用chartSeries
quantmod 包的函数。如果您想使用每小时或每分钟的数据,您可以调整低点的时间段。该功能并未真正优化或检查错误的输入,但它可以工作。
library(quantmod)
ADM <- getSymbols("ADM",
from = "2018-01-01",
to = "2018-07-01",
auto.assign = FALSE)
# create function for calculating the swings defaulting to checking lows for 7 time periods.
add_swing_high_low <- function(x, n = 7){
# find rolling low
x_low <- rollapply(Lo(x), n, min)
y_low <- ifelse(x_low == Lo(x), 1, 0)
# calculate lows while checking that the next 2 higher lows are indeed higher
z_low <- ifelse(x_low < lag(Lo(x),-1) &
x_low < lag(Lo(x),-2) &
lag(Lo(x),-1) < lag(Lo(x),-2) &
y_low == 1,
1,
0)
swing_low <- ifelse(z_low == 1, Lo(x), NA)
# find rolling high
x_high <- rollapply(Hi(x), n, max)
y_high <- ifelse(x_high == Hi(x), 1, 0)
z_high <- ifelse(x_high > lag(Hi(x),-1) &
x_high > lag(Hi(x),-2) &
lag(Hi(x),-1) > lag(Hi(x),-2) &
y_high == 1,
1,
0)
swing_high <- ifelse(z_high == 1, Hi(ADM), NA)
# set colours
swings <- ifelse(!is.na(swing_low), swing_low, ifelse(!is.na(swing_high), swing_high, NA))
swing_cols <- ifelse(swings == quantmod::Lo(ADM), "green", NA)
swing_cols <- ifelse(swings == quantmod::Hi(ADM), "red", swing_cols)
# set pch values to triangle and inverted triangle.
swing_pch <- ifelse(swing_cols == "green", 24,
ifelse(swing_cols == "red", 25, NA))
# add points to chart
addPoints(1:nrow(swings), swings, col = swing_cols, pch = swing_pch, cex = 0.75, on = 1)
}
chartSeries(ADM)
add_swing_high_low(ADM, n = 7)
推荐阅读
- linux - 如何阅读直到分隔符?
- python - 如何从交互式解释器调用函数并为 argparse 传递参数?
- javascript - 从 Dart 转换为 Javascript 时,Dart2JS 无法运行异步代码
- php - 选择查询执行两次
- java - 如何解决这个“不使用 PreparedStatement 方法”的问题?
- spring - 如何动态更改自动装配的spring bean的属性?
- ios - 如何在 Swift 中基于图像创建进度条
- sql - 如何排除 sql 子查询的计数返回 0 的行
- types - 如何在保留其签名的同时将一个模块包含在另一个模块中?
- lua - 如何在 Lua 中压缩 3 维数组?