如何解决从 Pine 到 R 上下摆动
我正在尝试将 Swing High and 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")
解决方法
我创建了一个与 quantmod 包中的 chartSeries
一起使用的函数。如果您想使用每小时或分钟数据,您可以调整低点查看的时间段。该函数并未真正优化或检查错误的输入,但它可以工作。
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,0)
swing_low <- ifelse(z_low == 1,Lo(x),NA)
# find rolling high
x_high <- rollapply(Hi(x),max)
y_high <- ifelse(x_high == Hi(x),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,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)
,
我尝试复制与 Pine 中的 Swings High and Low 函数相同的逻辑(如 here 所述),然后我得出了以下函数。我认为它可以稍微清理一下,但它似乎复制了我使用 Pine 函数获得的结果。
SwingsHL <-function (Low,High,barsback){
#Create container for results
swing_high = c()
swing_low = c()
#Loops through all candles,leaving room for the search window at the start and end of the series
for (Index in (barsback+1): (length(Low)-barsback)){
# Select high and low values at position Index
HighVal = High[Index]
LowVal = Low[Index]
#Define the size of the window to check for swings
LeftWindow= c((Index-barsback):(Index-1))
RightWindow= c((Index+1):(Index+barsback))
#find extreme values to left and right
MaxHighValToLeft=max(High[LeftWindow])[1]
MaxHighValToRight=max(High[RightWindow])[1]
MinLowValToLeft=min(Low[LeftWindow])[1]
MinLowValToRight=min(Low[RightWindow])[1]
#check if value at position index is larger than values to left or right
###----Swing Highs
if(HighVal>MaxHighValToLeft & HighVal>=MaxHighValToRight){swing_high[Index] = TRUE; } else{swing_high[Index] = FALSE; }
###----Swing Lows
if(LowVal<MinLowValToLeft & LowVal<=MinLowValToRight){swing_low[Index] = TRUE; } else {swing_low[Index] = FALSE; }
}
Results=data.frame(swing_high,swing_low)
Results[(Index+1):(Index+barsback),]=NA
colnames(Results)=c("SHigh","SLows")
return (Results)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。