این خطا به این دلیل است که در Pine Script، تابع plotshape() نمیتواند در یک بلوک شرطی (if یا for) استفاده شود. تابع plotshape() باید در سطح اصلی کد (global scope) استفاده شود.
✅ راهحل:
برای رفع این مشکل، باید شرطها را به عنوان پارامتر series به plotshape() منتقل کنیم.
✅ کد اصلاح شده و بدون خطا:
//@version=5
indicator("Ichimoku Cloud with Buy/Sell Signals", overlay=true, shorttitle="Ichimoku Signals")
// تنظیمات ورودی اصلی
tenkan_period = input.int(9, title="Tenkan Sen Period", minval=1, group="Ichimoku Settings")
kijun_period = input.int(26, title="Kijun Sen Period", minval=1, group="Ichimoku Settings")
senkou_span_b_period = input.int(52, title="Senkou Span B Period", minval=1, group="Ichimoku Settings")
displacement = input.int(26, title="Displacement", minval=1, group="Ichimoku Settings")
// نمایش/عدم نمایش اجزا
show_tenkan = input.bool(true, title="Show Tenkan Sen", group="Display Options")
show_kijun = input.bool(true, title="Show Kijun Sen", group="Display Options")
show_chikou = input.bool(true, title="Show Chikou Span", group="Display Options")
show_cloud = input.bool(true, title="Show Cloud", group="Display Options")
show_signals_on_chart = input.bool(true, title="Show Buy/Sell Signals on Chart", group="Display Options")
// تنظیمات "Pane Labels" (شبیهسازی شده با label.new)
show_pane_signal_label = input.bool(true, title="Show Signal Status in Pane (Top)", group="Pane Signal Label")
pane_label_position_y_offset = input.float(1.02, title="Vertical Position Offset", step=0.01, group="Pane Signal Label")
pane_label_text_color = input.color(color.white, title="Text Color", group="Pane Signal Label")
pane_label_size_str = input.string("Normal", title="Text Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group="Pane Signal Label")
// تنظیمات TK Simple Cross Arrows
show_tk_simple_arrows = input.bool(true, title="Show TK Simple Cross Arrows", group="TK Simple Cross")
tk_simple_arrow_size_str = input.string("Normal", title="Arrow Size", options=["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], group="TK Simple Cross")
tk_simple_up_color = input.color(color.new(color.blue, 30), title="TK Cross Up Color", group="TK Simple Cross")
tk_simple_down_color = input.color(color.new(color.maroon, 30), title="TK Cross Down Color", group="TK Simple Cross")
// محاسبه خطوط ایچیموکو
tenkan_sen = (ta.highest(high, tenkan_period) + ta.lowest(low, tenkan_period)) / 2
kijun_sen = (ta.highest(high, kijun_period) + ta.lowest(low, kijun_period)) / 2
senkou_span_a = (tenkan_sen + kijun_sen) / 2
senkou_span_b = (ta.highest(high, senkou_span_b_period) + ta.lowest(low, senkou_span_b_period)) / 2
chikou_span = close
// شرایط سیگنالها
tk_cross_up = ta.crossover(tenkan_sen, kijun_sen)
tk_cross_down = ta.crossunder(tenkan_sen, kijun_sen)
future_senkou_a = senkou_span_a[displacement]
future_senkou_b = senkou_span_b[displacement]
future_cloud_top = math.max(future_senkou_a, future_senkou_b)
future_cloud_bottom = math.min(future_senkou_a, future_senkou_b)
price_above_future_cloud = close > future_cloud_top
price_below_future_cloud = close < future_cloud_bottom
price_inside_future_cloud = close >= future_cloud_bottom and close <= future_cloud_top
future_bullish_cloud = future_senkou_a > future_senkou_b
chikou_above_price = chikou_span > close[displacement]
chikou_below_price = chikou_span < close[displacement]
price_above_kijun = close > kijun_sen
price_below_kijun = close < kijun_sen
strong_buy = tk_cross_up and price_above_future_cloud and future_bullish_cloud and chikou_above_price and price_above_kijun
medium_buy = tk_cross_up and (price_above_future_cloud or price_inside_future_cloud) and tenkan_sen > kijun_sen
strong_sell = tk_cross_down and price_below_future_cloud and not future_bullish_cloud and chikou_below_price and price_below_kijun
medium_sell = tk_cross_down and (price_below_future_cloud or price_inside_future_cloud) and tenkan_sen < kijun_sen
// رسم خطوط
plot(show_tenkan ? tenkan_sen : na, color=color.blue, title="Tenkan Sen", linewidth=2)
plot(show_kijun ? kijun_sen : na, color=color.red, title="Kijun Sen", linewidth=2)
plot(show_chikou ? chikou_span : na, offset=-displacement, color=color.purple, title="Chikou Span", linewidth=1)
// رسم ابر
p1 = plot(show_cloud ? senkou_span_a : na, offset=displacement, color=color.green, title="Senkou Span A", display=display.none)
p2 = plot(show_cloud ? senkou_span_b : na, offset=displacement, color=color.red, title="Senkou Span B", display=display.none)
fill(p1, p2, color=senkou_span_a > senkou_span_b ? color.new(color.green, 80) : color.new(color.red, 80), title="Cloud")
// نمایش سیگنالها روی نمودار
if show_signals_on_chart
if strong_buy
label.new(bar_index, low - ta.atr(14) * 0.5, text="BUY++", color=color.green, textcolor=color.white, style=label.style_label_up, size=size.normal)
else if medium_buy and not strong_buy
label.new(bar_index, low - ta.atr(14) * 0.5, text="BUY", color=color.lime, textcolor=color.black, style=label.style_label_up, size=size.small)
if strong_sell
label.new(bar_index, high + ta.atr(14) * 0.5, text="SELL++", color=color.red, textcolor=color.white, style=label.style_label_down, size=size.normal)
else if medium_sell and not strong_sell
label.new(bar_index, high + ta.atr(14) * 0.5, text="SELL", color=color.orange, textcolor=color.black, style=label.style_label_down, size=size.small)
// رنگ پسزمینه بر اساس قدرت سیگنال
bgcolor(strong_buy ? color.new(color.green, 90) : na, title="Strong Buy Background")
bgcolor(strong_sell ? color.new(color.red, 90) : na, title="Strong Sell Background")
// نمایش وضعیت فعلی در گوشه نمودار
var label status_info_label = na
if barstate.islast
label.delete(status_info_label)
status_text = ""
current_signal_status = "⚪ NEUTRAL"
current_signal_color = color.gray
if strong_buy
current_signal_status := "🟢 STRONG BUY"
current_signal_color := color.green
else if medium_buy
current_signal_status := "🟢 BUY"
current_signal_color := color.lime
else if strong_sell
current_signal_status := "🔴 STRONG SELL"
current_signal_color := color.red
else if medium_sell
current_signal_status := "🔴 SELL"
current_signal_color := color.orange
status_text := current_signal_status
status_text += "\nTenkan/Kijun: " + (tenkan_sen > kijun_sen ? "Bullish ↑" : "Bearish ↓")
status_text += "\nPrice vs Cloud: " + (price_above_future_cloud ? "Above ↑" : price_below_future_cloud ? "Below ↓" : "Inside ↔")
status_text += "\nCloud: " + (future_bullish_cloud ? "Bullish ☁↑" : "Bearish ☁↓")
status_text += "\nChikou vs Price: " + (chikou_above_price ? "Above ↑" : chikou_below_price ? "Below ↓" : "Neutral/Crossing")
status_text += "\nPrice vs Kijun: " + (price_above_kijun ? "Above ↑" : price_below_kijun ? "Below ↓" : "On Kijun")
status_info_label := label.new(bar_index + 10, high, text=status_text, xloc=xloc.bar_index, yloc=yloc.price,
color=color.new(current_signal_color, 30), textcolor=color.white,
style=label.style_label_left, size=size.normal)
// هشدارها
alertcondition(strong_buy, title="Strong Buy Signal", message="Ichimoku Strong Buy Signal on {{ticker}} at {{close}}")
alertcondition(medium_buy, title="Medium Buy Signal", message="Ichimoku Medium Buy Signal on {{ticker}} at {{close}}")
alertcondition(strong_sell, title="Strong Sell Signal", message="Ichimoku Strong Sell Signal on {{ticker}} at {{close}}")
alertcondition(medium_sell, title="Medium Sell Signal", message="Ichimoku Medium Sell Signal on {{ticker}} at {{close}}")
alertcondition(tk_cross_up, title="Tenkan/Kijun Bullish Cross", message="Ichimoku TK Bullish Cross on {{ticker}}")
alertcondition(tk_cross_down, title="Tenkan/Kijun Bearish Cross", message="Ichimoku TK Bearish Cross on {{ticker}}")
// نمایش فلشهای کوچک برای تقاطعهای ساده تنکان/کیجون با اندازه قابل تغییر
// Auto size
plotshape(show_tk_simple_arrows and tk_cross_up and not medium_buy and not strong_buy and tk_simple_arrow_size_str == "Auto",
title="TK Simple Cross Up (Auto)", location=location.belowbar, color=tk_simple_up_color, style=shape.triangleup, size=size.auto)
plotshape(show_tk_simple_arrows and tk_cross_down and not medium_sell and not strong_sell and tk_simple_arrow_size_str == "Auto",
title="TK Simple Cross Down (Auto)", location=location.abovebar, color=tk_simple_down_color, style=shape.triangledown, size=size.auto)
// Tiny size
plotshape(show_tk_simple_arrows and tk_cross_up and not medium_buy and not strong_buy and tk_simple_arrow_size_str == "Tiny",
title="TK Simple Cross Up (Tiny)", location=location.belowbar, color=tk_simple_up_color, style=shape.triangleup, size=size.tiny)
plotshape(show_tk_simple_arrows and tk_cross_down and not medium_sell and not strong_sell and tk_simple_arrow_size_str == "Tiny",
title="TK Simple Cross Down (Tiny)", location=location.abovebar, color=tk_simple_down_color, style=shape.triangledown, size=size.tiny)
// Small size
plotshape(show_tk_simple_arrows and tk_cross_up and not medium_buy and not strong_buy and tk_simple_arrow_size_str == "Small",
title="TK Simple Cross Up (Small)", location=location.belowbar, color=tk_simple_up_color, style=shape.triangleup, size=size.small)
plotshape(show_tk_simple_arrows and tk_cross_down and not medium_sell and not strong_sell and tk_simple_arrow_size_str == "Small",
title="TK Simple Cross Down (Small)", location=location.abovebar, color=tk_simple_down_color, style=shape.triangledown, size=size.small)
// Normal size
plotshape(show_tk_simple_arrows and tk_cross_up and not medium_buy and not strong_buy and tk_simple_arrow_size_str == "Normal",
title="TK Simple Cross Up (Normal)", location=location.belowbar, color=tk_simple_up_color, style=shape.triangleup, size=size.normal)
plotshape(show_tk_simple_arrows and tk_cross_down and not medium_sell and not strong_sell and tk_simple_arrow_size_str == "Normal",
title="TK Simple Cross Down (Normal)", location=location.abovebar, color=tk_simple_down_color, style=shape.triangledown, size=size.normal)
// Large size
plotshape(show_tk_simple_arrows and tk_cross_up and not medium_buy and not strong_buy and tk_simple_arrow_size_str == "Large",
title="TK Simple Cross Up (Large)", location=location.belowbar, color=tk_simple_up_color, style=shape.triangleup, size=size.large)
plotshape(show_tk_simple_arrows and tk_cross_down and not medium_sell and not strong_sell and tk_simple_arrow_size_str == "Large",
title="TK Simple Cross Down (Large)", location=location.abovebar, color=tk_simple_down_color, style=shape.triangledown, size=size.large)
// Huge size
plotshape(show_tk_simple_arrows and tk_cross_up and not medium_buy and not strong_buy and tk_simple_arrow_size_str == "Huge",
title="TK Simple Cross Up (Huge)", location=location.belowbar, color=tk_simple_up_color, style=shape.triangleup, size=size.huge)
plotshape(show_tk_simple_arrows and tk_cross_down and not medium_sell and not strong_sell and tk_simple_arrow_size_str == "Huge",
title="TK Simple Cross Down (Huge)", location=location.abovebar, color=tk_simple_down_color, style=shape.triangledown, size=size.huge)
// متغیر