test_pine_script.pine
Copy
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © haricherry1988
//@version=5
indicator("Multi-Timeframe Trade Signal Indicator", overlay=true, max_labels_count=500)
// ============================================================================
// INPUT PARAMETERS
// ============================================================================
// Timeframe Selection
tf1 = input.timeframe("5", "Timeframe 1 (Day Trade)", group="Timeframes")
tf2 = input.timeframe("15", "Timeframe 2 (Day Trade)", group="Timeframes")
tf3 = input.timeframe("60", "Timeframe 3 (Swing)", group="Timeframes")
tf4 = input.timeframe("D", "Timeframe 4 (Position)", group="Timeframes")
// Indicator Parameters
rsi_length = input.int(14, "RSI Length", group="Indicators")
rsi_overbought = input.int(70, "RSI Overbought", group="Indicators")
rsi_oversold = input.int(30, "RSI Oversold", group="Indicators")
macd_fast = input.int(12, "MACD Fast", group="Indicators")
macd_slow = input.int(26, "MACD Slow", group="Indicators")
macd_signal = input.int(9, "MACD Signal", group="Indicators")
ema_fast = input.int(9, "EMA Fast", group="Indicators")
ema_slow = input.int(21, "EMA Slow", group="Indicators")
ema_trend = input.int(50, "EMA Trend", group="Indicators")
bb_length = input.int(20, "Bollinger Bands Length", group="Indicators")
bb_mult = input.float(2.0, "Bollinger Bands Multiplier", group="Indicators")
// Display Options
show_signals = input.bool(true, "Show Buy/Sell Signals", group="Display")
show_table = input.bool(true, "Show Signal Table", group="Display")
show_trade_suggestions = input.bool(true, "Show Trade Suggestions", group="Display")
// Trade Parameters
stop_loss_pct_input = input.float(2.0, "Stop Loss %", minval=0.5, maxval=10.0, step=0.5, group="Trade Suggestions", tooltip="Stop loss as percentage of entry price")
profit_target_pct_input = input.float(4.0, "Take Profit %", minval=1.0, maxval=20.0, step=0.5, group="Trade Suggestions", tooltip="Profit target for 1:2 risk-reward")
// ============================================================================
// SIGNAL CALCULATION FUNCTION
// ============================================================================
calculateSignals() =>
// RSI
rsi = ta.rsi(close, rsi_length)
rsi_buy = rsi < rsi_oversold
rsi_sell = rsi > rsi_overbought
// MACD
[macd_line, signal_line, _] = ta.macd(close, macd_fast, macd_slow, macd_signal)
macd_buy = ta.crossover(macd_line, signal_line)
macd_sell = ta.crossunder(macd_line, signal_line)
macd_bullish = macd_line > signal_line
// EMA
ema_f = ta.ema(close, ema_fast)
ema_s = ta.ema(close, ema_slow)
ema_t = ta.ema(close, ema_trend)
ema_buy = ta.crossover(ema_f, ema_s) and close > ema_t
ema_sell = ta.crossunder(ema_f, ema_s) and close < ema_t
ema_uptrend = ema_f > ema_s and close > ema_t
// Bollinger Bands
bb_basis = ta.sma(close, bb_length)
bb_dev = bb_mult * ta.stdev(close, bb_length)
bb_upper = bb_basis + bb_dev
bb_lower = bb_basis - bb_dev
bb_buy = close < bb_lower
bb_sell = close > bb_upper
// Volume
vol_avg = ta.sma(volume, 20)
vol_spike = volume > vol_avg * 1.5
// Combined Signals
buy_score = (rsi_buy ? 1 : 0) + (macd_buy ? 1 : 0) + (ema_buy ? 1 : 0) + (bb_buy ? 1 : 0)
sell_score = (rsi_sell ? 1 : 0) + (macd_sell ? 1 : 0) + (ema_sell ? 1 : 0) + (bb_sell ? 1 : 0)
strong_buy = buy_score >= 3
buy_signal = buy_score >= 2
strong_sell = sell_score >= 3
sell_signal = sell_score >= 2
// Trend determination
trend = ema_uptrend and macd_bullish ? "BULLISH" : not ema_uptrend and not macd_bullish ? "BEARISH" : "NEUTRAL"
[strong_buy, buy_signal, strong_sell, sell_signal, trend, rsi, vol_spike, buy_score, sell_score]
// ============================================================================
// MULTI-TIMEFRAME ANALYSIS
// ============================================================================
// Current Timeframe
[curr_strong_buy, curr_buy, curr_strong_sell, curr_sell, curr_trend, curr_rsi, curr_vol_spike, curr_buy_score, curr_sell_score] = calculateSignals()
// Timeframe 1
[tf1_strong_buy, tf1_buy, tf1_strong_sell, tf1_sell, tf1_trend, tf1_rsi, _, _, _] = request.security(syminfo.tickerid, tf1, calculateSignals())
// Timeframe 2
[tf2_strong_buy, tf2_buy, tf2_strong_sell, tf2_sell, tf2_trend, tf2_rsi, _, _, _] = request.security(syminfo.tickerid, tf2, calculateSignals())
// Timeframe 3
[tf3_strong_buy, tf3_buy, tf3_strong_sell, tf3_sell, tf3_trend, tf3_rsi, _, _, _] = request.security(syminfo.tickerid, tf3, calculateSignals())
// Timeframe 4
[tf4_strong_buy, tf4_buy, tf4_strong_sell, tf4_sell, tf4_trend, tf4_rsi, _, _, _] = request.security(syminfo.tickerid, tf4, calculateSignals())
// ============================================================================
// PLOT SIGNALS ON CHART
// ============================================================================
// Strong Buy Signal
plotshape(show_signals and curr_strong_buy, "Strong Buy", shape.labelup, location.belowbar, color.new(color.green, 0), text="STRONG BUY", textcolor=color.white, size=size.normal)
// Buy Signal
plotshape(show_signals and curr_buy and not curr_strong_buy, "Buy", shape.triangleup, location.belowbar, color.new(color.lime, 0), text="BUY", size=size.small)
// Strong Sell Signal
plotshape(show_signals and curr_strong_sell, "Strong Sell", shape.labeldown, location.abovebar, color.new(color.red, 0), text="STRONG SELL", textcolor=color.white, size=size.normal)
// Sell Signal
plotshape(show_signals and curr_sell and not curr_strong_sell, "Sell", shape.triangledown, location.abovebar, color.new(color.orange, 0), text="SELL", size=size.small)
// Plot EMAs
plot(ta.ema(close, ema_fast), "EMA Fast", color.new(color.blue, 0), linewidth=1)
plot(ta.ema(close, ema_slow), "EMA Slow", color.new(color.orange, 0), linewidth=1)
plot(ta.ema(close, ema_trend), "EMA Trend", color.new(color.purple, 0), linewidth=2)
// ============================================================================
// SIGNAL TABLE
// ============================================================================
getSignalText(strong_buy, buy, strong_sell, sell) =>
strong_buy ? "🟢 STRONG BUY" : buy ? "🟢 BUY" : strong_sell ? "🔴 STRONG SELL" : sell ? "🔴 SELL" : "⚪ NEUTRAL"
getSignalColor(strong_buy, buy, strong_sell, sell) =>
strong_buy ? color.new(color.green, 70) : buy ? color.new(color.lime, 80) : strong_sell ? color.new(color.red, 70) : sell ? color.new(color.orange, 80) : color.new(color.gray, 90)
if show_table
var table signal_table = table.new(position.top_right, 3, 6, border_width=2, border_color=color.gray)
// Headers
table.cell(signal_table, 0, 0, "Timeframe", bgcolor=color.new(color.blue, 80), text_color=color.white, text_size=size.normal)
table.cell(signal_table, 1, 0, "Signal", bgcolor=color.new(color.blue, 80), text_color=color.white, text_size=size.normal)
table.cell(signal_table, 2, 0, "Trend", bgcolor=color.new(color.blue, 80), text_color=color.white, text_size=size.normal)
// Current Timeframe
table.cell(signal_table, 0, 1, timeframe.period, bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(signal_table, 1, 1, getSignalText(curr_strong_buy, curr_buy, curr_strong_sell, curr_sell), bgcolor=getSignalColor(curr_strong_buy, curr_buy, curr_strong_sell, curr_sell), text_size=size.small)
table.cell(signal_table, 2, 1, curr_trend, bgcolor=curr_trend == "BULLISH" ? color.new(color.green, 80) : curr_trend == "BEARISH" ? color.new(color.red, 80) : color.new(color.gray, 90), text_size=size.small)
// TF1
table.cell(signal_table, 0, 2, tf1, bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(signal_table, 1, 2, getSignalText(tf1_strong_buy, tf1_buy, tf1_strong_sell, tf1_sell), bgcolor=getSignalColor(tf1_strong_buy, tf1_buy, tf1_strong_sell, tf1_sell), text_size=size.small)
table.cell(signal_table, 2, 2, tf1_trend, bgcolor=tf1_trend == "BULLISH" ? color.new(color.green, 80) : tf1_trend == "BEARISH" ? color.new(color.red, 80) : color.new(color.gray, 90), text_size=size.small)
// TF2
table.cell(signal_table, 0, 3, tf2, bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(signal_table, 1, 3, getSignalText(tf2_strong_buy, tf2_buy, tf2_strong_sell, tf2_sell), bgcolor=getSignalColor(tf2_strong_buy, tf2_buy, tf2_strong_sell, tf2_sell), text_size=size.small)
table.cell(signal_table, 2, 3, tf2_trend, bgcolor=tf2_trend == "BULLISH" ? color.new(color.green, 80) : tf2_trend == "BEARISH" ? color.new(color.red, 80) : color.new(color.gray, 90), text_size=size.small)
// TF3
table.cell(signal_table, 0, 4, tf3, bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(signal_table, 1, 4, getSignalText(tf3_strong_buy, tf3_buy, tf3_strong_sell, tf3_sell), bgcolor=getSignalColor(tf3_strong_buy, tf3_buy, tf3_strong_sell, tf3_sell), text_size=size.small)
table.cell(signal_table, 2, 4, tf3_trend, bgcolor=tf3_trend == "BULLISH" ? color.new(color.green, 80) : tf3_trend == "BEARISH" ? color.new(color.red, 80) : color.new(color.gray, 90), text_size=size.small)
// TF4
table.cell(signal_table, 0, 5, tf4, bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(signal_table, 1, 5, getSignalText(tf4_strong_buy, tf4_buy, tf4_strong_sell, tf4_sell), bgcolor=getSignalColor(tf4_strong_buy, tf4_buy, tf4_strong_sell, tf4_sell), text_size=size.small)
table.cell(signal_table, 2, 5, tf4_trend, bgcolor=tf4_trend == "BULLISH" ? color.new(color.green, 80) : tf4_trend == "BEARISH" ? color.new(color.red, 80) : color.new(color.gray, 90), text_size=size.small)
// ============================================================================
// TRADE SUGGESTIONS
// ============================================================================
if show_trade_suggestions
var table trade_table = table.new(position.bottom_right, 2, 12, border_width=2, border_color=color.gray)
// Calculate price levels
atr = ta.atr(14)
current_price = close
// Entry calculations (use configurable inputs)
stop_loss_pct = stop_loss_pct_input
profit_target_pct = profit_target_pct_input
stop_loss_buy = current_price * (1 - stop_loss_pct / 100)
take_profit_buy = current_price * (1 + profit_target_pct / 100)
stop_loss_sell = current_price * (1 + stop_loss_pct / 100)
take_profit_sell = current_price * (1 - profit_target_pct / 100)
// Options calculations (rough estimates)
otm_call_strike = math.round(current_price * 1.02 / 5) * 5 // 2% OTM, rounded to nearest $5
otm_put_strike = math.round(current_price * 0.98 / 5) * 5 // 2% OTM
atm_call_strike = math.round(current_price / 5) * 5
atm_put_strike = math.round(current_price / 5) * 5
// LEAP strikes for longer term (6M: 3% OTM, 1Y: 5% OTM)
leap_6m_call_strike = math.round(current_price * 1.03 / 5) * 5
leap_6m_put_strike = math.round(current_price * 0.97 / 5) * 5
leap_1y_call_strike = math.round(current_price * 1.05 / 5) * 5
leap_1y_put_strike = math.round(current_price * 0.95 / 5) * 5
// Determine primary action
primary_action = curr_strong_buy or (curr_buy and tf3_buy) ? "BUY" : curr_strong_sell or (curr_sell and tf3_sell) ? "SELL" : "HOLD"
// Headers (Pine Script doesn't support colspan - use two cells with same styling)
table.cell(trade_table, 0, 0, "💡 TRADE SUGGESTIONS", bgcolor=color.new(color.blue, 70), text_color=color.white, text_size=size.normal)
table.cell(trade_table, 1, 0, "", bgcolor=color.new(color.blue, 70), text_color=color.white, text_size=size.normal)
// Primary Action
table.cell(trade_table, 0, 1, "Primary Action:", bgcolor=color.new(color.gray, 90), text_size=size.small)
action_color = primary_action == "BUY" ? color.new(color.green, 80) : primary_action == "SELL" ? color.new(color.red, 80) : color.new(color.gray, 90)
table.cell(trade_table, 1, 1, primary_action, bgcolor=action_color, text_size=size.small)
// Stock Entry
table.cell(trade_table, 0, 2, "Stock Entry:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 2, str.tostring(current_price, "#.##"), bgcolor=color.new(color.gray, 95), text_size=size.small)
if primary_action == "BUY"
// Stop Loss & Take Profit
table.cell(trade_table, 0, 3, "Stop Loss (Value):", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 3, str.tostring(stop_loss_buy, "#.##") + " (" + str.tostring(stop_loss_pct, "#.#") + "%)", bgcolor=color.new(color.red, 90), text_size=size.small)
table.cell(trade_table, 0, 4, "Take Profit:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 4, str.tostring(take_profit_buy, "#.##"), bgcolor=color.new(color.green, 90), text_size=size.small)
// Day Trade Option
table.cell(trade_table, 0, 5, "Day Trade (0-2 DTE):", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 5, "Buy ATM Call $" + str.tostring(atm_call_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// Swing Trade Option
table.cell(trade_table, 0, 6, "Swing Trade (7-14 DTE):", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 6, "Buy OTM Call $" + str.tostring(otm_call_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// 6 Month LEAP
table.cell(trade_table, 0, 7, "6 Month LEAP:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 7, "Buy OTM Call $" + str.tostring(leap_6m_call_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// 1 Year LEAP
table.cell(trade_table, 0, 8, "1 Year LEAP:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 8, "Buy OTM Call $" + str.tostring(leap_1y_call_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// Risk-Reward
table.cell(trade_table, 0, 9, "Risk:Reward:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 9, "1:2", bgcolor=color.new(color.green, 90), text_size=size.small)
else if primary_action == "SELL"
// Stop Loss & Take Profit
table.cell(trade_table, 0, 3, "Stop Loss (Value):", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 3, str.tostring(stop_loss_sell, "#.##") + " (" + str.tostring(stop_loss_pct, "#.#") + "%)", bgcolor=color.new(color.red, 90), text_size=size.small)
table.cell(trade_table, 0, 4, "Take Profit:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 4, str.tostring(take_profit_sell, "#.##"), bgcolor=color.new(color.green, 90), text_size=size.small)
// Day Trade Option
table.cell(trade_table, 0, 5, "Day Trade (0-2 DTE):", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 5, "Buy ATM Put $" + str.tostring(atm_put_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// Swing Trade Option
table.cell(trade_table, 0, 6, "Swing Trade (7-14 DTE):", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 6, "Buy OTM Put $" + str.tostring(otm_put_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// 6 Month LEAP
table.cell(trade_table, 0, 7, "6 Month LEAP:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 7, "Buy OTM Put $" + str.tostring(leap_6m_put_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// 1 Year LEAP
table.cell(trade_table, 0, 8, "1 Year LEAP:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 8, "Buy OTM Put $" + str.tostring(leap_1y_put_strike), bgcolor=color.new(color.gray, 95), text_size=size.small)
// Risk-Reward
table.cell(trade_table, 0, 9, "Risk:Reward:", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 9, "1:2", bgcolor=color.new(color.green, 90), text_size=size.small)
else
table.cell(trade_table, 0, 3, "No clear signal", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 1, 3, "", bgcolor=color.new(color.gray, 90), text_size=size.small)
table.cell(trade_table, 0, 4, "Wait for confirmation", bgcolor=color.new(color.gray, 95), text_size=size.small)
table.cell(trade_table, 1, 4, "", bgcolor=color.new(color.gray, 95), text_size=size.small)
// ============================================================================
// ALERTS
// ============================================================================
alertcondition(curr_strong_buy, "Strong Buy Signal", "Strong Buy Signal on {{ticker}}")
alertcondition(curr_strong_sell, "Strong Sell Signal", "Strong Sell Signal on {{ticker}}")
alertcondition(curr_buy, "Buy Signal", "Buy Signal on {{ticker}}")
alertcondition(curr_sell, "Sell Signal", "Sell Signal on {{ticker}}")
Show all 293 lines