本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("Pucci Trend EMA-SMA Crossover with Tolerance", overlay=true, shorttitle="Pucci Trend Xover")
// © fpucci
// Created: May 13, 2025
// Description: Trading signals based on EMA/SMA crossover with tolerance.
// Signals are generated when EMA crosses SMA with a configurable tolerance,
// with optional filtering based on price position (highs/lows) relative to EMA.
// State management (0=wait, 1=buy, 2=sell)
var int state = 0
// Input parameters
maPeriod = input.int(50, "Moving Average Period (7-300)", minval=7, maxval=300)
tolerance = input.int(15, "Tolerance (0-250 basis points)", minval=0, maxval=250)
usePriceFilter = input.bool(true, "Use Price Filter",
tooltip="When enabled, requires price highs/lows to be above/below EMA for signals")
// Calculate moving averages
emaPlot = ta.ema(close, maPeriod)
smaPlot = ta.sma(close, maPeriod)
// Calculate tolerance factor (convert basis points to decimal)
toleranceFactor = tolerance / 1000.0
// Trading conditions with price filter
buyCondition = (emaPlot > (smaPlot * (1 + toleranceFactor))) and
(not usePriceFilter or (high < emaPlot or low < emaPlot))
if buyCondition
buyCondition := state != 1
state := 1
sellCondition = (smaPlot > (emaPlot * (1 + toleranceFactor))) and
(not usePriceFilter or (high > emaPlot or low > emaPlot))
if sellCondition
sellCondition := state != 2
state := 2
// Plot moving averages
plot(emaPlot, title="EMA", color=color.new(color.teal, 0), linewidth=1)
plot(smaPlot, title="SMA", color=color.new(color.maroon, 0), linewidth=1)
// Plot trading signals
plotshape(buyCondition, title="Buy Signal", text="B", style=shape.labelup,
location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(sellCondition, title="Sell Signal", text="S", style=shape.labeldown,
location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)