【问题标题】:Swing high and low from Pine to R从 Pine 到 R 上下摆动
【发布时间】:2021-09-19 09:57:44
【问题描述】:

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

【问题讨论】:

  • 你真的,真的需要根据你提供的输入数据显示你期望的输出。

标签: r quantmod ibrokers ttr


【解决方案1】:

我从 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,
                  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)

【讨论】:

  • 谢谢。纵观情节,我注意到有一些挥杆失误。
  • @Camilo,就在第三个绿色摆动低点之后,摆动不被计算在内,因为它属于滚动的 7 天低点计数。第 4 个红色高点之后的高点波动,第一个不是波动,因为第二个高点高于第一个低点,之后的第二个可能的高点不符合 7 天滚动高点。否则需要对公式进行更好的分解。
  • 我实际上是在观察前两个绿色三角形之间的低位摆动......有趣的是,第二个摆动高点被选中,尽管它似乎与低位摆动具有相似的条件没有被选中……顺便说一句,这个包做得很好。
  • @Camilo,低点看起来就在那里,但是低点之后的第二根柱线低于低点之后的第一根柱线,这意味着它不应被视为摆动低点。跨度>
【解决方案2】:

我尝试复制与 Pine 中的 Swings High 和 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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2013-03-30
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多