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