本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("L.I.M - 5m Execution & Risk Engine", overlay = true, max_labels_count = 500, max_lines_count = 500)
// ==========================================
// SETTINGS & INPUTS
// ==========================================
tradeMode = input.string("Both", title="Trade Mode", options=["Long Only", "Short Only", "Both"], group="Signal Settings")
compareRef = input.string("Previous Close", title="Break Level Reference", options=["Previous Close", "Previous Body (Open/Close)", "Previous High/Low (Wicks)"], group="Signal Settings")
// Time & Session Inputs
useTimeFilter = input.bool(true, title="Enable Session Time Window", group="Time & Date Filters")
sessionInput = input.session("1630-2300", title="Session Time (HHMM-HHMM)", group="Time & Date Filters")
sessionTz = input.string("Asia/Jerusalem", title="Session Timezone", group="Time & Date Filters")
// Days Lookback Input
useLookback = input.bool(true, title="Limit Historical Days", group="Time & Date Filters")
lookbackDays = input.int(5, title="Lookback Days Count", minval=1, group="Time & Date Filters")
// Trigger / Broken Level Line Settings
showBreakLine = input.bool(true, title="Draw Broken Level Line", group="Trigger Level Line Settings")
breakLongColor = input.color(color.green, title="Long Level Line Color", group="Trigger Level Line Settings")
breakShortColor = input.color(color.red, title="Short Level Line Color", group="Trigger Level Line Settings")
breakStyleStr = input.string("Solid", title="Level Line Style", options=["Solid", "Dashed", "Dotted"], group="Trigger Level Line Settings")
breakWidth = input.int(2, title="Level Line Width", minval=1, maxval=4, group="Trigger Level Line Settings")
breakLineLength = input.int(15, title="Level Line Extension (Bars)", minval=1, group="Trigger Level Line Settings")
showBreakLevelPrice = input.bool(true, title="Show Price Label on Level Line", group="Trigger Level Line Settings")
// Unified Anchor Setting for SL & TP
riskAnchor = input.string("Entry (Candle Close)", title="Calculate SL & TP Points From", options=["Entry (Candle Close)", "Candle High/Low"], group="Risk & Target Base Anchor")
// Stop Loss Settings
showSLLines = input.bool(true, title="Draw Stop Loss Lines", group="Stop Loss Settings")
sl1_pts = input.float(10.0, title="Stop Loss 1 (Points)", minval=0.1, group="Stop Loss Settings")
sl2_pts = input.float(15.0, title="Stop Loss 2 (Points)", minval=0.1, group="Stop Loss Settings")
slLineLength = input.int(15, title="SL Line Extension (Bars)", minval=1, group="Stop Loss Settings")
// Stop Loss Line Customization
sl1StyleStr = input.string("Dashed", title="SL 1 Line Style", options=["Solid", "Dashed", "Dotted"], group="Stop Loss Customization")
sl2StyleStr = input.string("Dotted", title="SL 2 Line Style", options=["Solid", "Dashed", "Dotted"], group="Stop Loss Customization")
showSLPrices = input.bool(true, title="Show Price Label Next to SL Lines", group="Stop Loss Customization")
sl1Color = input.color(color.orange, title="10 Pt SL Line Color", group="Stop Loss Customization")
sl2Color = input.color(color.maroon, title="15 Pt SL Line Color", group="Stop Loss Customization")
// Take Profit Settings
showTPLines = input.bool(true, title="Draw Take Profit Lines", group="Take Profit Settings")
tp1_pts = input.float(10.0, title="Take Profit 1 (Points)", minval=0.1, group="Take Profit Settings")
tp2_pts = input.float(20.0, title="Take Profit 2 (Points)", minval=0.1, group="Take Profit Settings")
tpLineLength = input.int(15, title="TP Line Extension (Bars)", minval=1, group="Take Profit Settings")
// Take Profit Line Customization
tp1StyleStr = input.string("Dashed", title="TP 1 Line Style", options=["Solid", "Dashed", "Dotted"], group="Take Profit Customization")
tp2StyleStr = input.string("Dotted", title="TP 2 Line Style", options=["Solid", "Dashed", "Dotted"], group="Take Profit Customization")
showTPPrices = input.bool(true, title="Show Price Label Next to TP Lines", group="Take Profit Customization")
tp1Color = input.color(color.teal, title="TP 1 Line Color", group="Take Profit Customization")
tp2Color = input.color(color.blue, title="TP 2 Line Color", group="Take Profit Customization")
// General Text & Visual Settings
labelSizeStr = input.string("Small", title="All Price Labels Text Size", options=["Tiny", "Small", "Normal", "Large"], group="Visuals")
longColor = input.color(color.green, title="Long Signal Color", group="Visuals")
shortColor = input.color(color.red, title="Short Signal Color", group="Visuals")
showBarColor = input.bool(true, title="Color Signal Candles", group="Visuals")
// ==========================================
// HELPER FUNCTIONS
// ==========================================
f_getLineStyle(styleStr) =>
switch styleStr
"Solid" => line.style_solid
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
=> line.style_dashed
f_getLabelSize(sizeStr) =>
switch sizeStr
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
=> size.small
sl1Style = f_getLineStyle(sl1StyleStr)
sl2Style = f_getLineStyle(sl2StyleStr)
tp1Style = f_getLineStyle(tp1StyleStr)
tp2Style = f_getLineStyle(tp2StyleStr)
breakStyle = f_getLineStyle(breakStyleStr)
lblSize = f_getLabelSize(labelSizeStr)
// ==========================================
// TIME & LOOKBACK FILTER LOGIC
// ==========================================
inSession = not na(time(timeframe.period, sessionInput + ":1234567", sessionTz))
timeValid = useTimeFilter ? inSession : true
msInDay = 86400000
daysValid = useLookback ? (time >= (timenow - lookbackDays * msInDay)) : true
filterPassed = timeValid and daysValid
// ==========================================
// CANDLE DEFINITIONS
// ==========================================
isGreen = close > open
isRed = close < open
float longTargetLevel = switch compareRef
"Previous Close" => close[1]
"Previous Body (Open/Close)" => math.max(open[1], close[1])
"Previous High/Low (Wicks)" => high[1]
float shortTargetLevel = switch compareRef
"Previous Close" => close[1]
"Previous Body (Open/Close)" => math.min(open[1], close[1])
"Previous High/Low (Wicks)" => low[1]
// ==========================================
// SIGNAL LOGIC
// ==========================================
showLongs = (tradeMode == "Long Only" or tradeMode == "Both")
showShorts = (tradeMode == "Short Only" or tradeMode == "Both")
longCondition = filterPassed and showLongs and isGreen and isRed[1] and (close > longTargetLevel)
shortCondition = filterPassed and showShorts and isRed and isGreen[1] and (close < shortTargetLevel)
// ==========================================
// BROKEN LEVEL LINE DRAWING LOGIC
// ==========================================
if showBreakLine
if longCondition
line.new(x1=bar_index - 1, y1=longTargetLevel, x2=bar_index + breakLineLength, y2=longTargetLevel, color=breakLongColor, width=breakWidth, style=breakStyle)
if showBreakLevelPrice
label.new(x=bar_index + breakLineLength, y=longTargetLevel, text=str.tostring(longTargetLevel, "#.00"), color=color.new(color.white, 100), textcolor=breakLongColor, style=label.style_label_left, size=lblSize)
if shortCondition
line.new(x1=bar_index - 1, y1=shortTargetLevel, x2=bar_index + breakLineLength, y2=shortTargetLevel, color=breakShortColor, width=breakWidth, style=breakStyle)
if showBreakLevelPrice
label.new(x=bar_index + breakLineLength, y=shortTargetLevel, text=str.tostring(shortTargetLevel, "#.00"), color=color.new(color.white, 100), textcolor=breakShortColor, style=label.style_label_left, size=lblSize)
// ==========================================
// STOP LOSS & TAKE PROFIT DRAWING LOGIC
// ==========================================
if longCondition
// Shared Base Anchor for Long (Entry Close or Low)
float baseLongRef = (riskAnchor == "Entry (Candle Close)") ? close : low
// Stop Loss Lines
if showSLLines
float longSL1 = baseLongRef - sl1_pts
float longSL2 = baseLongRef - sl2_pts
line.new(x1=bar_index, y1=longSL1, x2=bar_index + slLineLength, y2=longSL1, color=sl1Color, width=2, style=sl1Style)
line.new(x1=bar_index, y1=longSL2, x2=bar_index + slLineLength, y2=longSL2, color=sl2Color, width=2, style=sl2Style)
if showSLPrices
label.new(x=bar_index + slLineLength, y=longSL1, text=str.tostring(longSL1, "#.00"), color=color.new(color.white, 100), textcolor=sl1Color, style=label.style_label_left, size=lblSize)
label.new(x=bar_index + slLineLength, y=longSL2, text=str.tostring(longSL2, "#.00"), color=color.new(color.white, 100), textcolor=sl2Color, style=label.style_label_left, size=lblSize)
// Take Profit Lines
if showTPLines
float longTP1 = baseLongRef + tp1_pts
float longTP2 = baseLongRef + tp2_pts
line.new(x1=bar_index, y1=longTP1, x2=bar_index + tpLineLength, y2=longTP1, color=tp1Color, width=2, style=tp1Style)
line.new(x1=bar_index, y1=longTP2, x2=bar_index + tpLineLength, y2=longTP2, color=tp2Color, width=2, style=tp2Style)
if showTPPrices
label.new(x=bar_index + tpLineLength, y=longTP1, text=str.tostring(longTP1, "#.00"), color=color.new(color.white, 100), textcolor=tp1Color, style=label.style_label_left, size=lblSize)
label.new(x=bar_index + tpLineLength, y=longTP2, text=str.tostring(longTP2, "#.00"), color=color.new(color.white, 100), textcolor=tp2Color, style=label.style_label_left, size=lblSize)
if shortCondition
// Shared Base Anchor for Short (Entry Close or High)
float baseShortRef = (riskAnchor == "Entry (Candle Close)") ? close : high
// Stop Loss Lines
if showSLLines
float shortSL1 = baseShortRef + sl1_pts
float shortSL2 = baseShortRef + sl2_pts
line.new(x1=bar_index, y1=shortSL1, x2=bar_index + slLineLength, y2=shortSL1, color=sl1Color, width=2, style=sl1Style)
line.new(x1=bar_index, y1=shortSL2, x2=bar_index + slLineLength, y2=shortSL2, color=sl2Color, width=2, style=sl2Style)
if showSLPrices
label.new(x=bar_index + slLineLength, y=shortSL1, text=str.tostring(shortSL1, "#.00"), color=color.new(color.white, 100), textcolor=sl1Color, style=label.style_label_left, size=lblSize)
label.new(x=bar_index + slLineLength, y=shortSL2, text=str.tostring(shortSL2, "#.00"), color=color.new(color.white, 100), textcolor=sl2Color, style=label.style_label_left, size=lblSize)
// Take Profit Lines
if showTPLines
float shortTP1 = baseShortRef - tp1_pts
float shortTP2 = baseShortRef - tp2_pts
line.new(x1=bar_index, y1=shortTP1, x2=bar_index + tpLineLength, y2=shortTP1, color=tp1Color, width=2, style=tp1Style)
line.new(x1=bar_index, y1=shortTP2, x2=bar_index + tpLineLength, y2=shortTP2, color=tp2Color, width=2, style=tp2Style)
if showTPPrices
label.new(x=bar_index + tpLineLength, y=shortTP1, text=str.tostring(shortTP1, "#.00"), color=color.new(color.white, 100), textcolor=tp1Color, style=label.style_label_left, size=lblSize)
label.new(x=bar_index + tpLineLength, y=shortTP2, text=str.tostring(shortTP2, "#.00"), color=color.new(color.white, 100), textcolor=tp2Color, style=label.style_label_left, size=lblSize)
// ==========================================
// PLOTTING & VISUALS
// ==========================================
plotshape(longCondition, title="Long Entry Signal", style=shape.triangleup, location=location.belowbar, color=longColor, size=size.small, text="LONG")
plotshape(shortCondition, title="Short Entry Signal", style=shape.triangledown, location=location.abovebar, color=shortColor, size=size.small, text="SHORT")
barcolor(showBarColor ? (longCondition ? longColor : shortCondition ? shortColor : na) : na)
// ==========================================
// ALERTS
// ==========================================
alertcondition(longCondition, title="5m Long Signal Alert", message="5m Long Signal Triggered at {{close}}")
alertcondition(shortCondition, title="5m Short Signal Alert", message="5m Short Signal Triggered at {{close}}")