-2

我正在尝试将 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")
4

2 回答 2

0

我创建了一个使用chartSeriesquantmod 包的函数。如果您想使用每小时或每分钟的数据,您可以调整低点的时间段。该功能并未真正优化或检查错误的输入,但它可以工作。

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)

在此处输入图像描述

于 2021-07-09T13:52:00.603 回答
0

我试图复制与 Pine 中的 Swings High 和 Low 函数相同的逻辑(如此所述),我得出了以下函数。我认为它可以清理一下,但它似乎复制了我使用 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)
}
于 2021-07-09T22:48:31.180 回答