Position Size for ATR Trailing Stop

rthomson · study · 42 行 · 点赞 217 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

////////////////////////////////////////////////////////////
// Position Size for ATR Trailing Stop Strategy, originally by Sylvain Vervoort 
// The related article is copyrighted material from Stocks & Commodities Jun 2009 
// Developed by River to add Wick Protection to HPotter's version, and then calculate position size based on the distance between stop and entry
////////////////////////////////////////////////////////////
study(title="Position Size for ATR Trailing Stop", overlay = false)
nATRPeriod = input(5)
nATRMultip = input(4)
length = input(30, "#Periods of Wick Protection", minval=2, type=integer)
bType = input(0, "Max [1] or Avg Wick Protection [0]", minval=0, maxval=1, type=integer)
equity = input(4000, "Equity on Account", minval=1, type=integer)
riskpercent = input(1, "Risk Percentage of Equity", minval=0.1, maxval=100, type=float)
 
avgupperwick = sma(close[1] <= open[1] ? high[1] - open[1] : high[1] - close[1], length)
maxupperwick = highest(close[1] <= open[1] ? high[1] - open[1] : high[1] - close[1], length)
avglowerwick = sma(close[1] > open[1] ? open[1] - low[1] : close[1] - low[1], length)
maxlowerwick = highest(close[1] > open[1] ? open[1] - low[1] : close[1] - low[1], length)
upperwick = bType == 0 ? avgupperwick : maxupperwick
lowerwick = bType == 0 ? avglowerwick : maxlowerwick
 
xATR = atr(nATRPeriod)
nLoss = nATRMultip * xATR 
upperband = iff(high < nz(upperband[1], 0) and high[1] < nz(upperband[1], 0), min(nz(upperband[1]), high + nLoss + upperwick), high + nLoss + upperwick)
lowerband = iff(low > nz(lowerband[1], 0) and low[1] > nz(lowerband[1], 0), max(nz(lowerband[1]), low - nLoss - lowerwick), low - nLoss - lowerwick) 
xATRTrailingStop = iff(low > nz(xATRTrailingStop[1], 0) and low[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), low - nLoss - lowerwick),
                    iff(high < nz(xATRTrailingStop[1], 0) and high[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), high + nLoss + upperwick), 
//                        iff(low <= nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), high + nLoss + upperwick, iff(high >= nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), low - nLoss - lowerwick,0))))
                        iff(low <= nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), upperband[1], iff(high >= nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), lowerband[1],0))))
 
distancetoshortstop = (upperband - close) / close
distancetolongstop = (close - lowerband) / close
riskamount = equity * (riskpercent/100)
shortpositionsize = riskamount / distancetoshortstop
longpositionsize = riskamount / distancetolongstop
 
plot(longpositionsize, color=green, title="Long Size")
plot(shortpositionsize, color=red, title="Short Size")
plot(equity, title="x1", color=black, linewidth=1)
plot(equity*2, title="x2", color=black, linewidth=1)
plot(equity*3, title="x3", color=black, linewidth=1)
plot(equity*4, title="x4", color=black, linewidth=1)
plot(equity*5, title="x5", color=black, linewidth=1)

← 返回列表