本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy('Mean Reversion V-F', overlay = true)
//input variables
movingAverage = input.int(title = 'Moving Average (bars)', defval = 28, minval = 1, maxval = 1000)
//riskPercentage = input(title="Amount to Risk (%)", type=input.integer, defval=1, minval=1, maxval=50)
deviation = input.float(title = 'Deviation Increment (%)', defval = 3, minval = 0.01, maxval = 100) / 100
unitsLevel1 = input.float(title = 'Level 1 (units)', defval = 1, minval = 0.0001, maxval = 10000)
unitsLevel2 = input.float(title = 'Level 2 (units)', defval = 2, minval = 0.0001, maxval = 10000)
unitsLevel3 = input.float(title = 'Level 3 (units)', defval = 4, minval = 0.0001, maxval = 10000)
unitsLevel4 = input.float(title = 'Level 4 (units)', defval = 8, minval = 0.0001, maxval = 10000)
unitsLevel5 = input.float(title = 'Level 5 (units)', defval = 16, minval = 0.0001, maxval = 10000)
//moving average
ma = ta.sma(close, movingAverage)
//banding calculations can be change
aH = 1.0 - deviation
aL = aH - deviation
bH = aL
bL = bH - deviation
cH = bL
cL = cH - deviation
dH = cL
dL = dH - deviation
eH = dL
///////////
//// take Profit
take_profit1 = input.float(1.67, title = 'Target Take Profit (%)', step = 0.01, minval = 0.0) / 100
take_profit_level1 = strategy.position_avg_price * (1 + take_profit1)
plot(take_profit_level1, style = plot.style_linebr, linewidth = 2, color = color.green)
plot(strategy.position_avg_price, style = plot.style_linebr, linewidth = 2, color = color.black)
/// trailing take profit % or hull
takeProfitTrailingEnabled = input.bool(defval = true, title = 'Enable Trailing', tooltip = 'Enable or disable the trailing for take profit. WARNING! This feature will repaint. Make sure you use it with "Bar Magnifier" and "Deep Backtesting" for realistic backtest results', group = 'Take Profit')
trailingTakeProfitDistancePerc = input.float(defval = 1.0, title = ' Trailing Distance %', minval = 0.01, maxval = 100, step = 0.01, tooltip = 'The distance as a percentage of the take profit price to keep from the high price after the target is reached when trailing.', group = 'Take Profit') / 100
lengthh = input.int(9, minval=1)
srch = input(close, title="Source")
hullma = ta.wma(2*ta.wma(srch, lengthh/2)-ta.wma(srch, lengthh), math.floor(math.sqrt(lengthh)))
plot(hullma)
// LOGIC
longTrailingTakeProfitStepTicks = take_profit_level1 * trailingTakeProfitDistancePerc / syminfo.mintick
//////exit
if strategy.position_size > 0 and close < hullma
strategy.exit('B1', limit = takeProfitTrailingEnabled ? na : take_profit_level1, comment = "exitbuy", trail_price = takeProfitTrailingEnabled ? take_profit_level1 : na, trail_offset = takeProfitTrailingEnabled ? longTrailingTakeProfitStepTicks : na)
//mode
if close < aH * ma and close > aL * ma and unitsLevel1 * close < strategy.initial_capital + strategy.netprofit - strategy.openprofit and strategy.opentrades == 0
strategy.order('B1', strategy.long, unitsLevel1)
if close < bH * ma and close > bL * ma and unitsLevel2 * close < strategy.initial_capital + strategy.netprofit - strategy.openprofit and strategy.opentrades == 1
strategy.order('B2', strategy.long, unitsLevel2)
if close < cH * ma and close > cL * ma and unitsLevel3 * close < strategy.initial_capital + strategy.netprofit - strategy.openprofit and strategy.opentrades == 2
strategy.order('B3', strategy.long, unitsLevel3)
if close < dH * ma and close > dL * ma and unitsLevel4 * close < strategy.initial_capital + strategy.netprofit - strategy.openprofit and strategy.opentrades == 3
strategy.order('B4', strategy.long, unitsLevel4)
if close < eH * ma and unitsLevel5 * close < strategy.initial_capital + strategy.netprofit - strategy.openprofit and strategy.opentrades == 4
strategy.order('B5', strategy.long, unitsLevel5)
//
l_ma = plot(ma, color = color.new(color.red, 0), linewidth = 3) //moving average center line
l_b1 = plot(aH * ma, color = color.new(color.red, 0), linewidth = 1) //level b1
l_b2 = plot(bH * ma, color = color.new(color.black, 0), linewidth = 1) //level b2
l_b3 = plot(cH * ma, color = color.new(color.black, 0), linewidth = 1) //level b3
l_b4 = plot(dH * ma, color = color.new(color.black, 0), linewidth = 1) //level b4
l_b5 = plot(eH * ma, color = color.new(color.black, 0), linewidth = 1) //level b5
fill(l_ma, l_b1, color = color.new(color.gray, 50))
fill(l_b1, l_b2, color = color.new(color.orange, 90))
fill(l_b2, l_b3, color = color.new(color.orange, 70))
fill(l_b3, l_b4, color = color.new(color.orange, 50))
fill(l_b4, l_b5, color = color.new(color.orange, 30))