我目前正在将此代码用于我的策略。我已经设置了所有警报,但问题是我正在查看何时1天,5天和30天图表%D均低于17.5,因此,每当收到一个警报时,我都必须手动检查其他警报处于。是否有任何方法可以将代码重做到仅在所有三个图表的%D线均低于此值的情况下才将警报发送到的代码?
我相信可以使用security()函数来完成此操作,但我无法完全弄清楚。我的想法是让amd_1,amd_5,amd_30并为所有三个计算D,并在图表中绘制这三个,然后进行设置并发出警报,说明D1 <17.5和D5 <17.5且D30 <17.5,然后发送警报。任何帮助表示赞赏。谢谢!
make fuel_type aspiration num_of_doors body_style drive_wheels engine_location wheel_base length width height curb_weight engine_type num_of_cylinders engine_size fuel_system compression_ratio horsepower peak_rpm city_mpg highway_mpg price
1 alfa-romero gas std two convertible rwd front 88.6 168.8 64.1 48.8 2548 dohc four 130 mpfi 9 111 5000 21 27 13495
2 alfa-romero gas std two convertible rwd front 88.6 168.8 64.1
答案 0 :(得分:0)
由于您的标准非常严格,因此必须更改OS / OB的水平和日期以发出信号。还添加了一些标记和背景代码,因此您可以取消注释以查看不同的条件。您可以选择在较高的时间范围行使用重绘或不重绘:
//@version=4
strategy("stochastic")
length = input(21, minval=1, title="length")
rsilength = input(21, minval=1, title="rsi length")
smoothk = input(4, minval=1, title="smoothk")
smoothd = input(10, minval=1, title="smoothd")
showMarkers = input(true, "Show Markers")
repaint = input(false, "Repaint Higher Timeframes")
showD1 = input(true, "Show 1 Day D")
showD2 = input(true, "Show 5 Day D")
showD3 = input(true, "Show 1 Month D")
obLevel = input(70)
osLevel = input(60)
rsi = rsi(close, rsilength)
sto = stoch(close,highest(length),lowest(length), length)
K = sma(sto,smoothk)
D = sma(K,smoothd)
d1 = repaint ? security(syminfo.tickerid, "1D", D) : security(syminfo.tickerid, "1D", D[1], lookahead = barmerge.lookahead_on)
d2 = repaint ? security(syminfo.tickerid, "5D", D) : security(syminfo.tickerid, "5D", D[1], lookahead = barmerge.lookahead_on)
d3 = repaint ? security(syminfo.tickerid, "M", D) : security(syminfo.tickerid, "30D", D[1], lookahead = barmerge.lookahead_on)
plot(showD1 ? d1 : na, "D 1D", color.aqua)
plot(showD2 ? d2 : na, "D 5D", color.blue)
plot(showD3 ? d3 : na, "D 30D", color.purple)
//plot(rsi, title="rsi", color=color.black)
plot(D, title="%D",color=color.green)
plot(K, title="%K",color=color.red)
hline(obLevel,title="upper limit", color=color.red)
hline(osLevel, title="lower limit",color=color.blue)
//plot(sto, title = "sto",color=color.black)
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 8, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 24, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2000, title = "From Year", minval = 2017)
ToMonth = input(defval = 9, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
// === EXECUTION ===
shares = 10000/close
var inLong = false
buy = false
sell = false
buyTrigger = d1 < osLevel and d2 < osLevel and d3 < osLevel
sellTrigger = d1 > obLevel and d2 > obLevel and d3 > obLevel
if buyTrigger and not inLong and window()
buy := true
inLong := true
else
if sellTrigger and inLong and window()
sell := true
inLong := false
strategy.entry("buy", shares, when = buy) // buy long when "within window of time" AND crossover
strategy.close("buy", when = sell) // sell long when "within window of time" AND crossunder
plotshape(showMarkers and buy, "buy", shape.triangleup, location.bottom, color.green, 0, text = "buy", size = size.tiny)
plotshape(showMarkers and sell, "sell", shape.triangledown, location.top, color.maroon, 0, text = "sell", size = size.tiny)
// plotshape(showMarkers and buyTrigger, "buyTrigger", shape.triangleup, location.bottom, color.green, 0, text = "buyTrigger", size = size.tiny)
// plotshape(showMarkers and sellTrigger, "sellTrigger", shape.triangledown, location.top, color.maroon, 0, text = "sellTrigger", size = size.tiny)
// bgcolor(showMarkers ? buyTrigger ? color.green : sellTrigger ? color.maroon : na : na)
bgcolor(showMarkers ? inLong ? color.green : color.maroon : na)
// bgcolor(showMarkers ? window() ? color.green : color.maroon : na)
这是一个为期一个月的图表,其中禁用了d1和d2图,并且未选中重绘较高时间范围:
与重绘较高时间范围相同的图表。发生两件事:您可以看到d3紫色线适合您当前的TF绿色D线,确认它是正确的。输入/退出的时间较早,因为较高TF行的重新绘制版本不会延迟。