本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("LAZY DOG", overlay=true, initial_capital=100000, default_qty_type=strategy.cash, default_qty_value=0, commission_type=strategy.commission.cash_per_contract, commission_value=0.0, slippage=2, process_orders_on_close=true)
// ==========================================
// USER INPUTS & PARAMETERS
// ==========================================
var string G_TIME = "Execution Settings"
entryTimeInput = input.int(1800, title="Initial Entry Time (HHMM)", group=G_TIME)
exitTimeInput = input.int(1728, title="Hard Exit Time (HHMM)", group=G_TIME)
var string G_SIZE = "Position Sizes"
baseLots = input.float(10.0, title="Initial Lot Count (CE & PE)", group=G_SIZE)
lotMultiplier = input.float(10.0, title="Multiplier for Re-entry (10x)", group=G_SIZE)
var string G_OPT = "Options Strike Configuration"
otmPercentInput = input.float(2.5, title="OTM Distance (%)", minval=0.0, group=G_OPT)
basePremiumInput = input.float(100.0, title="Base Initial Premium (USD)", minval=1.0, group=G_OPT)
// ==========================================
// CALCULATIONS & CORE SETUP
// ==========================================
float contractSizePerLot = 0.001
float baseQty = baseLots * contractSizePerLot
float dynamicQty = baseLots * lotMultiplier * contractSizePerLot
// Time Formatting
int currentHHMM = hour * 100 + minute
// Session Engine Windows
bool isEntryTime = (currentHHMM == entryTimeInput)
bool isExitTime = (currentHHMM == exitTimeInput)
// Anchor Spot Tracking via Native Historical Reference Engine
var float initialSpot = na
initialSpot := isEntryTime ? close : initialSpot
// Calculate Theoretical Option Strikes
float strikeCE = initialSpot * (1.0 + (otmPercentInput / 100.0))
float strikePE = initialSpot * (1.0 - (otmPercentInput / 100.0))
// Dynamic Options Premium Matrix
float computedCEPrice = not na(initialSpot) ? math.max(0.5, basePremiumInput + (close - initialSpot)) : na
float computedPEPrice = not na(initialSpot) ? math.max(0.5, basePremiumInput + (initialSpot - close)) : na
// Trailing High Engines
var float maxCEPremiumSeen = 0.0
var float maxPEPremiumSeen = 0.0
maxCEPremiumSeen := isEntryTime ? basePremiumInput : math.max(nz(maxCEPremiumSeen, basePremiumInput), nz(computedCEPrice, 0.0))
maxPEPremiumSeen := isEntryTime ? basePremiumInput : math.max(nz(maxPEPremiumSeen, basePremiumInput), nz(computedPEPrice, 0.0))
// Core Execution Trigger Thresholds
bool ceDoubled = (computedCEPrice >= (basePremiumInput * 2.0))
bool peDoubled = (computedPEPrice >= (basePremiumInput * 2.0))
bool ceTrailingSL = (computedCEPrice <= (maxCEPremiumSeen * 0.5))
bool peTrailingSL = (computedPEPrice <= (maxPEPremiumSeen * 0.5))
// Global Strategy State Machine Tracker
var bool strategyActive = false
strategyActive := isEntryTime ? true : (isExitTime ? false : strategyActive)
// ==========================================
// RISK BRAKE ENGINE (MAX LOSS $25 / MAX PROFIT $50)
// ==========================================
float runningPL = strategy.openprofit
bool riskBrakeTriggered = strategyActive and ((runningPL <= -25.0) or (runningPL >= 50.0))
if riskBrakeTriggered
strategy.close_all(comment="Risk Cap Met - Auto Resetting")
strategyActive := false
// ==========================================
// SIGNAL DEFINITIONS
// ==========================================
bool triggerEntry = (isEntryTime and strategyActive) or (riskBrakeTriggered and (currentHHMM > entryTimeInput) and (currentHHMM < exitTimeInput))
bool triggerExit = isExitTime and strategyActive
bool checkBuySignals = strategyActive and (strategy.position_size > 0)
bool signalPEBuy = checkBuySignals and ceDoubled
bool signalCEBuy = checkBuySignals and peDoubled
bool signalPETSL = checkBuySignals and peTrailingSL
bool signalCETSL = checkBuySignals and ceTrailingSL
// ==========================================
// ANTI-CLUTTER VISUAL SHAPE FILTERS
// ==========================================
bool showStart = triggerEntry and (nz(ta.barssince(triggerEntry), 999) > 0)
bool rawBuy = (signalPEBuy or signalCEBuy)
bool showBuy = rawBuy and (nz(ta.barssince(rawBuy), 999) > 0)
bool rawTsl = (signalPETSL or signalCETSL)
bool showTsl = rawTsl and (nz(ta.barssince(rawTsl), 999) > 0)
bool showExit = triggerExit and (nz(ta.barssince(triggerExit), 999) > 0)
// ==========================================
// STRATEGY ORDERS INTERFACE
// ==========================================
if triggerEntry
strategy.entry("Initial CE", strategy.long, qty=baseQty, comment="START CE")
strategy.entry("Initial PE", strategy.long, qty=baseQty, comment="START PE")
if signalPEBuy
strategy.entry("M_PE", strategy.long, qty=dynamicQty, comment="BUY PE 10x")
if signalPETSL
strategy.close("Initial CE", comment="PE TSL HIT")
strategy.entry("M_CE", strategy.long, qty=dynamicQty, comment="BUY CE 10x")
if signalCEBuy
strategy.entry("M_CE_Alt", strategy.long, qty=dynamicQty, comment="BUY CE 10x")
if signalCETSL
strategy.close("Initial PE", comment="CE TSL HIT")
strategy.entry("M_PE_Alt", strategy.long, qty=dynamicQty, comment="BUY PE 10x")
if triggerExit
strategy.close_all(comment="TIME EXIT")
// ==========================================
// PLOTS & COMPACT CLUTTER-FREE VISUAL SIGNALS
// ==========================================
// Updated to compliant Pine v6 step plots
plot(strategyActive ? strikeCE : na, title="CE Strike Level", color=color.new(color.green, 20), style=plot.style_stepline, linewidth=2)
plot(strategyActive ? strikePE : na, title="PE Strike Level", color=color.new(color.red, 20), style=plot.style_stepline, linewidth=2)
// Fixed: Removed text_color parameters and updated syntax for standard Pine v6 plotchar calls
plotchar(showStart, title="Start Character", char="◆", location=location.belowbar, color=color.blue, size=size.small, text="START")
plotchar(showBuy, title="Buy Character", char="▲", location=location.belowbar, color=color.green, size=size.small, text="BUY")
plotchar(showTsl, title="TSL Character", char="▼", location=location.abovebar, color=color.orange, size=size.small, text="TSL")
plotchar(showExit, title="Exit Character", char="✖", location=location.abovebar, color=color.red, size=size.small, text="EXIT")
// ==========================================
// TELEMETRY PANEL GRID
// ==========================================
var table infoBox = table.new(position.top_right, 2, 4, bgcolor=color.new(color.black, 20), border_color=color.black, border_width=1)
if barstate.islast
table.cell(infoBox, 0, 0, "Metric", text_color=color.white, bgcolor=color.navy)
table.cell(infoBox, 1, 0, "Value", text_color=color.white, bgcolor=color.navy)
table.cell(infoBox, 0, 1, "Simulated CE Premium", text_color=color.white)
table.cell(infoBox, 1, 1, "$" + str.tostring(computedCEPrice, "#.##"), text_color=color.green)
table.cell(infoBox, 0, 2, "Simulated PE Premium", text_color=color.white)
table.cell(infoBox, 1, 2, "$" + str.tostring(computedPEPrice, "#.##"), text_color=color.red)
table.cell(infoBox, 0, 3, "Cumulative Net PnL", text_color=color.white)
table.cell(infoBox, 1, 3, "$" + str.tostring(strategy.netprofit, "#.##"), text_color=strategy.netprofit >= 0 ? color.green : color.red)