本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=4
study("ATR Trailing Stop by ceyhun", overlay=true)
/////////notes////////////////////////////////////////
// This is based on the ATR trailing stop indicator //
// width addition of two levels of stops and //
// different interpretation. //
// This is a fast-reacting system and is better //
// suited for higher volatility markets //
//////////////////////////////////////////////////////
SC = input(close, "Source", input.source)
// Fast Trail //
AP1 = input(5, "fast ATR period", input.integer) // ATR Period
AF1 = input(0.5, "fast ATR multiplier", input.float) // ATR Factor
SL1 = AF1 * atr(AP1) // Stop Loss
Trail1 = 0.0
Trail1 := iff(SC > nz(Trail1[1], 0) and SC[1] > nz(Trail1[1], 0), max(nz(Trail1[1], 0), SC - SL1), iff(SC < nz(Trail1[1], 0) and SC[1] < nz(Trail1[1], 0), min(nz(Trail1[1], 0), SC + SL1), iff(SC > nz(Trail1[1], 0), SC - SL1, SC + SL1)))
// Slow Trail //
AP2 = input(10, "slow ATR perod", input.integer) // ATR Period
AF2 = input(2, "slow ATR multiplier", input.float) // ATR Factor
SL2 = AF2 * atr(AP2) // Stop Loss
Trail2 = 0.0
Trail2 := iff(SC > nz(Trail2[1], 0) and SC[1] > nz(Trail2[1], 0), max(nz(Trail2[1], 0), SC - SL2), iff(SC < nz(Trail2[1], 0) and SC[1] < nz(Trail2[1], 0), min(nz(Trail2[1], 0), SC + SL2), iff(SC > nz(Trail2[1], 0), SC - SL2, SC + SL2)))
// Bar color for trade signal //
Green = Trail1 > Trail2 and close > Trail2 and low > Trail2
Blue = Trail1 > Trail2 and close > Trail2 and low < Trail2
Red = Trail2 > Trail1 and close < Trail2 and high < Trail2
Yellow = Trail2 > Trail1 and close < Trail2 and high > Trail2
// Signals //
Bull = barssince(Green) < barssince(Red)
Bear = barssince(Red) < barssince(Green)
Buy = crossover(Trail1, Trail2)
Sell = crossunder(Trail1, Trail2)
TS1 = plot(Trail1, "fast trail", style=plot.style_line,color=Trail1 > Trail2 ? color.blue : color.yellow, linewidth=2)
TS2 = plot(Trail2, "slow trail", style=plot.style_line,color=Trail1 > Trail2 ? color.green : color.red, linewidth=2)
fill(TS1, TS2, Bull ? color.green : color.red, transp=90)
plotcolor = input(false, "Paint color on chart", input.bool)
plotbuysell = input(true, "Plot Buy/Sell arrows", input.bool)
bcl = iff(plotcolor == 1, Blue ? color.blue : Green ? color.lime : Yellow ? color.yellow : Red ? color.red : color.white, na)
barcolor(bcl)
plotshape(Buy and plotbuysell == 1, "Buy", shape.arrowup, location.belowbar, color.green, text="Buy")
plotshape(Sell and plotbuysell == 1, "Sell", shape.arrowdown, location.abovebar, color.red, text="Sell")
alertcondition(Buy, "Buy Signal", "Buy ATR Trailing Stop")
alertcondition(Sell, "Sell Signal", "Sell ATR Trailing Stop")
//InfoPanel
f_draw_infopanel(_x, _y, _line, _text)=>
_rep_text = ""
for _l = 0 to _line
_rep_text := _rep_text + "\n"
_rep_text := _rep_text + _text
var label _la = na
label.delete(_la)
_la := label.new(
x=_x, y=_y,
text=_rep_text, xloc=xloc.bar_time, yloc=yloc.price,
color=color.black, style=label.style_labelup, textcolor=color.white, size=size.small)
//)
lapos_x = timenow + round(change(time)*35)
lapos_y = highest(35)
price=input(true,"Price Line")
Buyprice = valuewhen(Buy, close,0)
Buyprice_text= "" + tostring(Buyprice, "##.##") + " " + (close>Buyprice? "▲" : na)
plot(price ? Buyprice:na,color=color.blue,title="Buy Price",style=plot.style_circles)
ProfitLong = 100 * ((close - Buyprice) / Buyprice)
ProfitLong_text= "" + tostring(ProfitLong, "##.##") + " " + (ProfitLong>20 ? "▲" : "▼")
Sellprice = valuewhen(Sell, close,0)
Sellprice_text= "" + tostring(Sellprice, "##.##") + " " + (close<Sellprice ? "▼" :na)
plot(price ? Sellprice:na,color=color.red,title="Sell Price",style=plot.style_circles)
ProfitShort = 100 * ((Sellprice - close) / Sellprice)
ProfitShort_text= "" + tostring(ProfitShort, "##.##") + " " + (ProfitShort>20 ? "▲" : "▼")
f_draw_infopanel(lapos_x, lapos_y, 20, "╚══════════════════════════════════════╝")
f_draw_infopanel(lapos_x, lapos_y, 18, " " + ProfitShort_text + " ")
f_draw_infopanel(lapos_x, lapos_y, 16, "―══ " + "Profit Short %20" + " ══―")
f_draw_infopanel(lapos_x, lapos_y, 14, " " + Sellprice_text + " ")
f_draw_infopanel(lapos_x, lapos_y, 12, "―══ " + "Sell Price" + " ══―")
f_draw_infopanel(lapos_x, lapos_y, 8, " " + ProfitLong_text + " ")
f_draw_infopanel(lapos_x, lapos_y, 6, "―══ " + "Profit Long %20" + " ══―")
f_draw_infopanel(lapos_x, lapos_y, 4, " " + Buyprice_text + " ")
f_draw_infopanel(lapos_x, lapos_y, 2, "―══ " + "Buy Price" + " ══―")
f_draw_infopanel(lapos_x, lapos_y, 0, "╔══════════════════════════════════════╗")