OPEN-SOURCE SCRIPT
celenni

//version=6
strategy("Cruce SMA 5/20 – v6 (const TF, gap en puntos SOLO cortos, next bar open, 1 trade/ventana, anti-flip)",
overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 10,
pyramiding = 0)
// === CONSTANTES ===
const string TF = "15" // fija el timeframe de cálculo (ej. "5","15","30","60","120","240","D")
const string SYM_ALLOWED = "QQQ" // símbolo permitido
// === Inputs ===
confirmOnClose = input.bool(true, "Confirmar señal al cierre (evita repaint)")
maxGapPtsShort = input.float(0.50, "Máx gap permitido en CORTOS (puntos)", 0.0, 1e6)
lenFast = input.int(5, "SMA rápida", 1)
lenSlow = input.int(20, "SMA lenta", 2)
tpPts = input.float(20.0, "Take Profit (puntos)", 0.01)
slPts = input.float(5.0, "Stop Loss (puntos)", 0.01)
// Ventanas (NY)
useSessions = input.bool(true, "Usar ventanas NY")
sess1 = input.session("1000-1130", "Ventana 1 (NY)")
sess2 = input.session("1330-1600", "Ventana 2 (NY)")
flatOutside = input.bool(true, "Cerrar posición al salir de la ventana")
// === Utilidades ===
isAllowedSymbol() =>
(syminfo.ticker == SYM_ALLOWED) or str.contains(str.upper(syminfo.ticker), str.upper(SYM_ALLOWED))
// === Series MTF (cálculo en TF) ===
closeTF = request.security(syminfo.tickerid, TF, close, barmerge.gaps_off, barmerge.lookahead_off)
smaFast = ta.sma(closeTF, lenFast)
smaSlow = ta.sma(closeTF, lenSlow)
// Señales MTF sin repaint
longSignalTF = request.security(syminfo.tickerid, TF,
ta.crossover(ta.sma(close, lenFast), ta.sma(close, lenSlow)),
barmerge.gaps_off, barmerge.lookahead_off)
shortSignalTF = request.security(syminfo.tickerid, TF,
ta.crossunder(ta.sma(close, lenFast), ta.sma(close, lenSlow)),
barmerge.gaps_off, barmerge.lookahead_off)
// === Sesiones (evaluadas en el TF del gráfico, zona NY) ===
inSess1 = useSessions ? not na(time(timeframe.period, sess1, "America/New_York")) : true
inSess2 = useSessions ? not na(time(timeframe.period, sess2, "America/New_York")) : true
inSession = inSess1 or inSess2
// Inicio de ventanas y contadores (1 trade por ventana)
var bool wasIn1 = false, wasIn2 = false
win1Start = inSess1 and not wasIn1
win2Start = inSess2 and not wasIn2
wasIn1 := inSess1
wasIn2 := inSess2
var int tradesWin1 = 0, tradesWin2 = 0
if win1Start
tradesWin1 := 0
if win2Start
tradesWin2 := 0
justOpened = strategy.position_size != 0 and strategy.position_size[1] == 0
if justOpened
if inSess1
tradesWin1 += 1
if inSess2
tradesWin2 += 1
canTakeMore =
(inSess1 and tradesWin1 < 1) or
(inSess2 and tradesWin2 < 1) or
(not useSessions)
// === Filtro NO-GAP SOLO para CORTOS (en PUNTOS) ===
// Compara OPEN actual vs CLOSE previo; se evalúa en la barra donde se EJECUTA (apertura actual).
gapPts = math.abs(open - close[1])
shortGapOK = maxGapPtsShort <= 0 ? true : (gapPts <= maxGapPtsShort)
// === Anti-flip y gating ===
isFlat = strategy.position_size == 0
canSignal = (not confirmOnClose or barstate.isconfirmed)
canTrade = isAllowedSymbol() and inSession and canTakeMore and canSignal
// === ENTRADAS (se colocan al cierre; se llenan en la apertura siguiente) ===
// Largos: sin filtro de gap
if canTrade and isFlat and longSignalTF
strategy.entry("Long", strategy.long)
// Cortos: requieren shortGapOK
if canTrade and isFlat and shortSignalTF and shortGapOK
strategy.entry("Short", strategy.short)
// === TP/SL en puntos ===
if strategy.position_size > 0
e = strategy.position_avg_price
strategy.exit("TP/SL Long", from_entry="Long", limit=e + tpPts, stop=e - slPts)
if strategy.position_size < 0
e = strategy.position_avg_price
strategy.exit("TP/SL Short", from_entry="Short", limit=e - tpPts, stop=e + slPts)
// === Cierre fuera de sesión ===
if flatOutside and not inSession and strategy.position_size != 0
strategy.close_all("Fuera de sesión")
// === Visual ===
plot(smaFast, color=color.new(color.teal, 0), title="SMA 5 ("+TF+")")
plot(smaSlow, color=color.new(color.orange, 0), title="SMA 20 ("+TF+")")
plotshape(longSignalTF and canTrade and isFlat, title="Compra", style=shape.triangleup,
location=location.belowbar, color=color.new(color.teal,0), size=size.tiny, text="Long")
plotshape(shortSignalTF and canTrade and isFlat and shortGapOK, title="Venta", style=shape.triangledown,
location=location.abovebar, color=color.new(color.red,0), size=size.tiny, text="Short")
strategy("Cruce SMA 5/20 – v6 (const TF, gap en puntos SOLO cortos, next bar open, 1 trade/ventana, anti-flip)",
overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 10,
pyramiding = 0)
// === CONSTANTES ===
const string TF = "15" // fija el timeframe de cálculo (ej. "5","15","30","60","120","240","D")
const string SYM_ALLOWED = "QQQ" // símbolo permitido
// === Inputs ===
confirmOnClose = input.bool(true, "Confirmar señal al cierre (evita repaint)")
maxGapPtsShort = input.float(0.50, "Máx gap permitido en CORTOS (puntos)", 0.0, 1e6)
lenFast = input.int(5, "SMA rápida", 1)
lenSlow = input.int(20, "SMA lenta", 2)
tpPts = input.float(20.0, "Take Profit (puntos)", 0.01)
slPts = input.float(5.0, "Stop Loss (puntos)", 0.01)
// Ventanas (NY)
useSessions = input.bool(true, "Usar ventanas NY")
sess1 = input.session("1000-1130", "Ventana 1 (NY)")
sess2 = input.session("1330-1600", "Ventana 2 (NY)")
flatOutside = input.bool(true, "Cerrar posición al salir de la ventana")
// === Utilidades ===
isAllowedSymbol() =>
(syminfo.ticker == SYM_ALLOWED) or str.contains(str.upper(syminfo.ticker), str.upper(SYM_ALLOWED))
// === Series MTF (cálculo en TF) ===
closeTF = request.security(syminfo.tickerid, TF, close, barmerge.gaps_off, barmerge.lookahead_off)
smaFast = ta.sma(closeTF, lenFast)
smaSlow = ta.sma(closeTF, lenSlow)
// Señales MTF sin repaint
longSignalTF = request.security(syminfo.tickerid, TF,
ta.crossover(ta.sma(close, lenFast), ta.sma(close, lenSlow)),
barmerge.gaps_off, barmerge.lookahead_off)
shortSignalTF = request.security(syminfo.tickerid, TF,
ta.crossunder(ta.sma(close, lenFast), ta.sma(close, lenSlow)),
barmerge.gaps_off, barmerge.lookahead_off)
// === Sesiones (evaluadas en el TF del gráfico, zona NY) ===
inSess1 = useSessions ? not na(time(timeframe.period, sess1, "America/New_York")) : true
inSess2 = useSessions ? not na(time(timeframe.period, sess2, "America/New_York")) : true
inSession = inSess1 or inSess2
// Inicio de ventanas y contadores (1 trade por ventana)
var bool wasIn1 = false, wasIn2 = false
win1Start = inSess1 and not wasIn1
win2Start = inSess2 and not wasIn2
wasIn1 := inSess1
wasIn2 := inSess2
var int tradesWin1 = 0, tradesWin2 = 0
if win1Start
tradesWin1 := 0
if win2Start
tradesWin2 := 0
justOpened = strategy.position_size != 0 and strategy.position_size[1] == 0
if justOpened
if inSess1
tradesWin1 += 1
if inSess2
tradesWin2 += 1
canTakeMore =
(inSess1 and tradesWin1 < 1) or
(inSess2 and tradesWin2 < 1) or
(not useSessions)
// === Filtro NO-GAP SOLO para CORTOS (en PUNTOS) ===
// Compara OPEN actual vs CLOSE previo; se evalúa en la barra donde se EJECUTA (apertura actual).
gapPts = math.abs(open - close[1])
shortGapOK = maxGapPtsShort <= 0 ? true : (gapPts <= maxGapPtsShort)
// === Anti-flip y gating ===
isFlat = strategy.position_size == 0
canSignal = (not confirmOnClose or barstate.isconfirmed)
canTrade = isAllowedSymbol() and inSession and canTakeMore and canSignal
// === ENTRADAS (se colocan al cierre; se llenan en la apertura siguiente) ===
// Largos: sin filtro de gap
if canTrade and isFlat and longSignalTF
strategy.entry("Long", strategy.long)
// Cortos: requieren shortGapOK
if canTrade and isFlat and shortSignalTF and shortGapOK
strategy.entry("Short", strategy.short)
// === TP/SL en puntos ===
if strategy.position_size > 0
e = strategy.position_avg_price
strategy.exit("TP/SL Long", from_entry="Long", limit=e + tpPts, stop=e - slPts)
if strategy.position_size < 0
e = strategy.position_avg_price
strategy.exit("TP/SL Short", from_entry="Short", limit=e - tpPts, stop=e + slPts)
// === Cierre fuera de sesión ===
if flatOutside and not inSession and strategy.position_size != 0
strategy.close_all("Fuera de sesión")
// === Visual ===
plot(smaFast, color=color.new(color.teal, 0), title="SMA 5 ("+TF+")")
plot(smaSlow, color=color.new(color.orange, 0), title="SMA 20 ("+TF+")")
plotshape(longSignalTF and canTrade and isFlat, title="Compra", style=shape.triangleup,
location=location.belowbar, color=color.new(color.teal,0), size=size.tiny, text="Long")
plotshape(shortSignalTF and canTrade and isFlat and shortGapOK, title="Venta", style=shape.triangledown,
location=location.abovebar, color=color.new(color.red,0), size=size.tiny, text="Short")
สคริปต์โอเพนซอร์ซ
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้มันเป็นโอเพ่นซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันการทำงานของสคริปต์ได้ ขอแสดงความชื่นชมผู้เขียน! แม้ว่าคุณจะสามารถใช้งานได้ฟรี แต่อย่าลืมว่าการเผยแพร่โค้ดซ้ำนั้นจะต้องเป็นไปตามกฎระเบียบการใช้งานของเรา
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ ข้อกำหนดการใช้งาน
สคริปต์โอเพนซอร์ซ
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้มันเป็นโอเพ่นซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันการทำงานของสคริปต์ได้ ขอแสดงความชื่นชมผู้เขียน! แม้ว่าคุณจะสามารถใช้งานได้ฟรี แต่อย่าลืมว่าการเผยแพร่โค้ดซ้ำนั้นจะต้องเป็นไปตามกฎระเบียบการใช้งานของเรา
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ ข้อกำหนดการใช้งาน