Indicators
SMA / EMA / WMA / HMA #
(series, length)
Various Moving Averages (Simple, Exponential, Weighted, Hull).
SMA 200 Trend Filter Strategy
Demonstrates trend filtering using SMA 200.
# ==================================================
# SMA 200 Trend Filter Strategy
# Demonstrates trend filtering using SMA 200
# ==================================================
# ==================================================
# Entry Logic
# ==================================================
# Calculate the 200-period Simple Moving Average.
#
# SMA 200 is one of the most widely used
# long-term trend indicators.
sma_200 = SMA(close, 200)
# Market is considered bullish when
# price is trading above the SMA 200.
uptrend = close > sma_200
# Buy only when:
#
# 1. Price crosses above SMA 200
# 2. Market is in an uptrend
buy_signal = crossover(close, sma_200) & uptrend
# Enter a long position.
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ==================================================
# Exit Logic
# ==================================================
# Exit when price falls below SMA 200.
#
# This suggests the long-term trend
# may be weakening.
sell_signal = close < sma_200
Exit(
index=1,
logic=sell_signal,
exit_type="MARKET"
)
# ==================================================
# Canvas
# ==================================================
# Calculate SMA 200 again for chart display.
sma_200 = SMA(close, 200)
# Determine trend direction.
uptrend = close > sma_200
# Plot SMA 200 on the chart.
#
# Many traders use this line to determine
# whether a market is bullish or bearish.
draw(
sma_200,
title="SMA 200",
frame=0,
color="yellow"
)
# Mark buy signals.
#
# Green triangles appear when price
# crosses above the SMA 200.
drawshape(
condition=crossover(close, sma_200),
shape="triangle_up",
location="BelowBar",
color="lime",
size="normal"
)
# Mark sell signals.
#
# Red triangles appear when price
# falls below the SMA 200.
drawshape(
condition=close < sma_200,
shape="triangle_down",
location="AboveBar",
color="red",
size="normal"
)
# Display trend information.
marker_text(
frame=0,
text="SMA 200 Trend Filter",
anchor="top_right"
)
# Display strategy statistics.
marker_table(
frame=0,
data=[
["Trend Filter", "SMA 200"],
["Uptrend", uptrend]
],
position="bottom_right"
)
# Show completed trades and PnL.
marker_trades(
frame=0,
show_pnl=True
)
EMA Crossover Strategy
Demonstrates crossover entry and crossunder exit using two EMAs.
# =================================================
# EMA Crossover Strategy
# ==================================================
# ==================================================
# Entry Logic
# ==================================================
# Calculate the fast EMA.
#
# EMA 9 reacts quickly to recent
# price movements.
fast_ema = EMA(close, 9)
# Calculate the slow EMA.
#
# EMA 21 reacts more slowly and helps
# identify the broader trend.
slow_ema = EMA(close, 21)
# Bullish crossover occurs when the
# fast EMA crosses above the slow EMA.
#
# This suggests bullish momentum may
# be starting.
bullish_cross = crossover(fast_ema, slow_ema)
# Enter a long position when a
# bullish crossover occurs.
Entry(
index=1,
logic=bullish_cross,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ==================================================
# Exit Logic
# ==================================================
# Exit when the fast EMA crosses
# below the slow EMA.
#
# This suggests bullish momentum
# is weakening.
bearish_cross = crossunder(fast_ema, slow_ema)
Exit(
index=1,
logic=bearish_cross,
exit_type="MARKET"
)
# ==================================================
# Canvas
# ==================================================
# Calculate the fast EMA.
fast_ema = EMA(close, 9)
# Calculate the slow EMA.
slow_ema = EMA(close, 21)
# Bullish crossover signal.
bullish_cross = crossover(fast_ema, slow_ema)
# Bearish crossover signal.
bearish_cross = crossunder(fast_ema, slow_ema)
# Plot the fast EMA.
#
# Green line follows price more closely.
draw(
fast_ema,
title="EMA 9",
frame=0,
color="green"
)
# Plot the slow EMA.
#
# Red line reacts more slowly and
# filters market noise.
draw(
slow_ema,
title="EMA 21",
frame=0,
color="red"
)
# Draw a green triangle whenever
# a bullish crossover occurs.
drawshape(
condition=bullish_cross,
shape="triangle_up",
location="BelowBar",
color="lime",
size="normal"
)
# Draw a red triangle whenever
# a bearish crossover occurs.
drawshape(
condition=bearish_cross,
shape="triangle_down",
location="AboveBar",
color="red",
size="normal"
)
# Display strategy name.
marker_text(
frame=0,
text="EMA Crossover Strategy",
anchor="top_right"
)
# Show current EMA values.
marker_table(
frame=0,
data=[
["EMA 9", round(fast_ema, 2)],
["EMA 21", round(slow_ema, 2)]
],
position="bottom_right"
)
# Display completed trades and PnL.
marker_trades(
frame=0,
show_pnl=True
)
WMA Rejection Strategy
Demonstrates WMA() based rejection strategy.
# ==================================================
# WMA Rejection Strategy
# Demonstrates WMA()
# ==================================================
# ==================================================
# Entry Logic
# ==================================================
# Calculate the 30-period Weighted Moving Average.
#
# WMA gives more importance to recent prices
# compared to a Simple Moving Average.
wma_30 = WMA(close, 30)
# Price rejection above WMA.
#
# Condition becomes TRUE when the entire candle
# remains above the WMA.
#
# This suggests buyers are defending the trend.
bullish = low > wma_30
# Enter a long position whenever
# the bullish condition is satisfied.
Entry(
index=1,
logic=bullish,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ==================================================
# Exit Logic
# ==================================================
# Exit when price falls below WMA.
#
# This suggests trend strength may be weakening.
sell_signal = close < wma_30
Exit(
index=1,
logic=sell_signal,
exit_type="MARKET"
)
# ==================================================
# Canvas
# ==================================================
# Calculate WMA 30 again for visualization.
wma_30 = WMA(close, 30)
# Plot the WMA on the chart.
#
# The line reacts faster than an SMA
# because recent prices receive more weight.
draw(
wma_30,
title="WMA 30",
frame=0,
color="orange"
)
# Bullish rejection condition.
#
# Entire candle remains above WMA.
bullish = low > wma_30
# Bearish condition.
#
# Price closes below WMA.
bearish = close < wma_30
# Mark bullish rejection candles.
drawshape(
condition=bullish,
shape="triangle_up",
location="BelowBar",
color="lime",
size="normal"
)
# Mark bearish candles.
drawshape(
condition=bearish,
shape="triangle_down",
location="AboveBar",
color="red",
size="normal"
)
# Display strategy name.
marker_text(
frame=0,
text="WMA 30 Rejection Strategy",
anchor="top_right"
)
# Display WMA information.
marker_table(
frame=0,
data=[
["WMA 30", round(wma_30, 2)],
["Bullish", bullish]
],
position="bottom_right"
)
# Display completed trades and PnL.
marker_trades(
frame=0,
show_pnl=True
)
RSI #
(series, period)
Relative Strength Index.
RSI Bullish Momentum Strategy
Buy when RSI crosses above 50, indicating bullish momentum.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate 14-period RSI
rsi_val = RSI(close, 14)
# Buy when RSI crosses above 50, indicating bullish momentum
buy_signal = crossover(rsi_val, 50)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when RSI crosses below 50, indicating bearish momentum
sell_signal = crossunder(rsi_val, 50)
Exit(
index=1,
logic=sell_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot RSI in a separate panel
draw(
rsi_val,
title="RSI (14)",
frame=1,
color="blue",
y_range=[0,100]
)
# Display Entry and Exit markers on the chart
marker_trades(
frame=0,
show_pnl=True
)
RSI Bearish Momentum Strategy
Sell when RSI crosses below 50, indicating bearish momentum.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate 14-period RSI
rsi_val = RSI(close, 14)
# Sell when RSI crosses below 50, indicating bearish momentum
sell_signal = crossunder(rsi_val, 50)
Entry(
index=1,
logic=sell_signal,
timeframe=5,
script=RELIANCE,
action=Sell,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when RSI crosses above 50, indicating bullish momentum
buy_signal = crossover(rsi_val, 50)
Exit(
index=1,
logic=buy_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot RSI in a separate panel
draw(
rsi_val,
title="RSI (14)",
frame=1,
color="blue",
y_range=[0,100]
)
# Display Entry and Exit markers on the chart
marker_trades(
frame=0,
show_pnl=True
)
Volume RSI Strategy
Use volume instead of price to calculate RSI.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate 14-period RSI using Volume instead of Price
rsi_volume = RSI(volume, 14)
# Buy when Volume RSI crosses above 50, indicating increasing buying activity
buy_signal = crossover(rsi_volume, 50)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when Volume RSI crosses below 50, indicating weakening buying activity
sell_signal = crossunder(rsi_volume, 50)
Exit(
index=1,
logic=sell_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot Volume RSI
draw(
rsi_volume,
title="RSI Volume",
frame=1,
color="red"
)
# Overbought level
draw(
80,
title="Overbought",
frame=1,
color="red"
)
# Midline
draw(
50,
title="Midline",
frame=1,
color="white"
)
# Oversold level
draw(
20,
title="Oversold",
frame=1,
color="green"
)
# Display Entry and Exit markers
marker_trades(
frame=0,
show_pnl=True
)
MACD #
(source, fastlen, slowlen, siglen)
Moving Average Convergence Divergence. Returns [macd, signal, hist].
RSI Snapshot Strategy
Capture indicator values exactly when RSI crosses 70.
# ==================================================
# RSI Snapshot Strategy | entry_logic.ta
# ==================================================
ema20 = EMA(close, 20)
rsi14 = RSI(close, 14)
atr14 = ATR(14)
trend_up = close > ema20
rsi_entry = crossover(rsi14, 50)
buy_signal = trend_up & rsi_entry
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ==================================================
# exit_logic.ta
# ==================================================
overbought = crossover(rsi14, 70)
# Capture indicator values exactly when RSI crosses 70
log_snapshot_on_event(
overbought,
close_price=close,
ema20=ema20,
rsi_value=rsi14,
atr_value=atr14,
open_profit=open_pnl,
bars_held=bars_in_trade
)
exit_signal = overbought | DayLastBar
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ==================================================
# canvas.ta
# ==================================================
draw(
ema20,
title="EMA 20",
frame=0,
color="blue"
)
draw(
rsi14,
title="RSI 14",
frame=1,
color="purple"
)
draw(
70,
title="Overbought",
frame=1,
color="red"
)
marker_trades(
frame=0,
show_pnl=True
)
drawshape(
condition=buy_signal,
shape="triangle_up",
location="BelowBar",
color="green",
size="small"
)
MACD Snapshot Strategy
Capture market state at exit using log_snapshot_on_event with MACD.
# ==================================================
# MACD Snapshot Strategy | entry_logic.ta
# ==================================================
# Indicators
macd_line, signal_line, histogram = MACD(close, 12, 26, 9)
ema50 = EMA(close, 50)
# Entry Conditions
trend_up = close > ema50
macd_bull = crossover(macd_line, signal_line)
long_entry = trend_up & macd_bull
Entry(
index=1,
logic=long_entry,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ==================================================
# exit_logic.ta
# ==================================================
# Exit Condition
macd_bear = crossunder(macd_line, signal_line)
# Capture market state at exit
log_snapshot_on_event(
macd_bear,
close_price=close,
macd=macd_line,
signal=signal_line,
histogram=histogram,
ema50=ema50,
pnl=open_pnl,
bars=bars_in_trade
)
Exit(
index=1,
logic=macd_bear,
exit_type="MARKET"
)
# ==================================================
# canvas.ta
# ==================================================
draw(
ema50,
title="EMA 50",
frame=0,
color="orange"
)
draw(
macd_line,
title="MACD",
frame=1,
color="blue"
)
draw(
signal_line,
title="Signal",
frame=1,
color="red"
)
marker_trades(
frame=0,
show_pnl=True
)
drawshape(
condition=long_entry,
shape="triangle_up",
location="BelowBar",
color="green",
size="small"
)
bb #
(series, length, mult)
Bollinger Bands. Returns [mid, upper, lower].
Bollinger Bands Breakout Strategy
Buy when price breaks above the Upper Band.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Bollinger Bands (20 Period, 2 Standard Deviations)
upper_band, middle_band, lower_band = bb(close, 20, 2)
# Buy when price breaks above the Upper Band,
# indicating a bullish breakout
buy_signal = crossover(close, upper_band)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price crosses below the Middle Band,
# indicating bullish momentum is weakening
exit_signal = crossunder(close, middle_band)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot Bollinger Bands
draw(
upper_band,
title="BB Upper",
frame=0,
color="red"
)
draw(
middle_band,
title="BB Middle",
frame=0,
color="white"
)
draw(
lower_band,
title="BB Lower",
frame=0,
color="green"
)
# Highlight the Bollinger Band range
marker_fill(
frame=0,
line1=upper_band,
line2=lower_band,
color="rgba(0,150,255,0.08)"
)
# Mark Buy signals
drawshape(
condition=buy_signal,
shape="triangle_up",
location="BelowBar",
color="lime",
size="small"
)
# Display executed trades
marker_trades(
frame=0,
show_pnl=True
)
Bollinger Bands Reversal Strategy
Buy when price crosses back above the Lower Band.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Bollinger Bands (20 Period, 2 Standard Deviations)
upper_band, middle_band, lower_band = bb(close, 20, 2)
# Buy when price crosses back above the Lower Band,
# indicating a potential reversal from an oversold condition
buy_signal = crossover(close, lower_band)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price reaches the Middle Band,
# which acts as the mean of the Bollinger Bands
exit_signal = crossover(close, middle_band)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ==================================================
# canvas.ta
# Bollinger Bands Indicator
# Displays the Upper, Middle, and Lower Bollinger Bands.
# ==================================================
draw(
upper_band,
title="BB Upper",
frame=0,
color="red"
)
draw(
middle_band,
title="BB Middle",
frame=0,
color="white"
)
draw(
lower_band,
title="BB Lower",
frame=0,
color="green"
)
marker_trades(
frame=0,
show_pnl=True
)
SuperTrend #
(atrPeriod=10, factor=3.0)
SuperTrend indicator. Returns (trend, dir).
SuperTrend Strategy 1
Buy when SuperTrend turns bullish.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate SuperTrend
st_val, st_dir = SuperTrend(atrPeriod=10, factor=3.0)
# Buy when SuperTrend turns bullish
buy_signal = st_dir == 1
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when SuperTrend turns bearish
exit_signal = st_dir == -1
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot SuperTrend
draw(
st_val,
title="SuperTrend",
frame=0,
color="green"
)
# Display Entry and Exit markers
marker_trades(
frame=0,
show_pnl=True
)
SuperTrend Strategy 2
Buy when SuperTrend changes from bearish to bullish.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate SuperTrend
st_val, st_dir = SuperTrend(
atrPeriod=10,
factor=3.0
)
# Buy when SuperTrend changes from bearish to bullish
buy_signal = crossover(st_dir, 0)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when SuperTrend changes from bullish to bearish
exit_signal = crossunder(st_dir, 0)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot SuperTrend
draw(
st_val,
title="SuperTrend",
frame=0,
color="green"
)
# Mark Buy signals
drawshape(
condition=buy_signal,
shape="triangle_up",
location="BelowBar",
color="lime",
size="small"
)
# Strategy name
marker_text(
frame=0,
text="SuperTrend Strategy",
anchor="top_right"
)
# Display Entry and Exit markers
marker_trades(
frame=0,
show_pnl=True
)
VWAP #
(source=hlc3)
Volume-Weighted Average Price.
VWAP Strategy 1
Buy when price crosses above VWAP.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate VWAP
vwap_line = VWAP(ohlc4)
# Buy when price crosses above VWAP,
# indicating bullish momentum
buy_signal = crossover(close, vwap_line)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price crosses below VWAP,
# indicating weakening bullish momentum
exit_signal = crossunder(close, vwap_line)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ==================================================
# canvas.ta
# VWAP Indicator
# Displays Volume Weighted Average Price (VWAP).
# ==================================================
draw(
vwap_line,
title="VWAP",
frame=0,
color="yellow"
)
marker_text(
frame=0,
text="VWAP Indicator",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
VWAP Strategy 2
Buy when price crosses above VWAP.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate VWAP
vwap_val = VWAP(hlc3)
# Buy when price crosses above VWAP,
# indicating bullish momentum
buy_signal = crossover(close, vwap_val)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price crosses below VWAP,
# indicating bearish momentum
exit_signal = crossunder(close, vwap_val)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot VWAP
draw(
vwap_val,
title="VWAP",
frame=0,
color="yellow"
)
# Mark Buy signals
drawshape(
condition=buy_signal,
shape="triangle_up",
location="BelowBar",
color="lime",
size="small"
)
# Display Entry and Exit markers
marker_trades(
frame=0,
show_pnl=True
)
Ichimoku #
(conversionLinePeriod, baseLinePeriod, ...)
Returns [tenkan, kijun, senkou_a, senkou_b, chikou].
Tenkan & Kijun Crossover Strategy
Buy when the Tenkan Line crosses above the Kijun Line.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Ichimoku Cloud
tenkan, kijun, senkou_a, senkou_b, chikou = Ichimoku(
9,
26,
52,
26
)
# Buy when the Tenkan Line crosses above the Kijun Line,
# indicating a bullish trend reversal
buy_signal = crossover(tenkan, kijun)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when the Tenkan Line crosses below the Kijun Line,
# indicating a bearish trend reversal
exit_signal = crossunder(tenkan, kijun)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ==================================================
# canvas.ta
# Ichimoku Cloud Indicator
# Displays all Ichimoku Cloud components.
# ==================================================
draw(
tenkan,
title="Tenkan",
frame=0,
color="red"
)
draw(
kijun,
title="Kijun",
frame=0,
color="blue"
)
draw(
senkou_a,
title="Senkou A",
frame=0,
color="green"
)
draw(
senkou_b,
title="Senkou B",
frame=0,
color="orange"
)
draw(
chikou,
title="Chikou",
frame=0,
color="purple"
)
# Cloud Area
marker_fill(
frame=0,
line1=senkou_a,
line2=senkou_b,
color="rgba(0,255,0,0.08)"
)
marker_text(
frame=0,
text="Ichimoku Cloud",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
Cloud Breakout Strategy
Buy when price breaks above the Ichimoku Cloud.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Ichimoku Cloud
tenkan, kijun, senkou_a, senkou_b, chikou = Ichimoku(
9,
26,
52,
26
)
# Buy when price breaks above the Ichimoku Cloud,
# indicating a strong bullish trend
buy_signal = crossover(close, senkou_a)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price falls below the Kijun Line,
# indicating the bullish trend is weakening
exit_signal = crossunder(close, kijun)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# Ichimoku Cloud Indicator
# Displays all Ichimoku Cloud components.
# ============================================================
draw(
tenkan,
title="Tenkan",
frame=0,
color="red"
)
draw(
kijun,
title="Kijun",
frame=0,
color="blue"
)
draw(
senkou_a,
title="Senkou A",
frame=0,
color="green"
)
draw(
senkou_b,
title="Senkou B",
frame=0,
color="orange"
)
draw(
chikou,
title="Chikou",
frame=0,
color="purple"
)
marker_fill(
frame=0,
line1=senkou_a,
line2=senkou_b,
color="rgba(0,255,0,0.08)"
)
marker_trades(
frame=0,
show_pnl=True
)
ATR / STDDEV #
(length)
Average True Range and Standard Deviation volatility indicators.
High Volatility ATR Strategy
Buy when current ATR is greater than the 50-period ATR.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate ATR
atr_val = ATR(14)
# Buy when current ATR is greater than the 50-period ATR,
# indicating increasing market volatility
buy_signal = atr_val > ATR(50)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when current ATR falls below the 50-period ATR,
# indicating volatility is decreasing
exit_signal = atr_val < ATR(50)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ATR Indicator
# Displays the current ATR and the 50-period ATR.
# ============================================================
draw(
atr_val,
title="ATR (14)",
frame=1,
color="orange"
)
draw(
ATR(50),
title="ATR (50)",
frame=1,
color="blue"
)
marker_trades(
frame=0,
show_pnl=True
)
Volatility Expansion STDDEV Strategy
Buy when volatility increases above its recent average.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Standard Deviation
std_20 = STDDEV(close, 20)
# Buy when volatility increases above its recent average
buy_signal = crossover(std_20, SMA(std_20, 10))
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when volatility falls below its recent average
exit_signal = crossunder(std_20, SMA(std_20, 10))
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# Standard Deviation Indicator
# ============================================================
draw(
std_20,
title="STDDEV 20",
frame=1,
color="orange"
)
draw(
SMA(std_20, 10),
title="STDDEV SMA",
frame=1,
color="blue"
)
marker_text(
frame=1,
text="STDDEV Indicator",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
ATR Trend Filter Strategy
Buy when price crosses above the EMA during high volatility.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate EMA and ATR
ema20 = EMA(close, 20)
atr14 = ATR(14)
# Buy when price crosses above the EMA during high volatility
buy_signal = crossover(close, ema20) & (atr14 > 10)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price falls below the EMA
exit_signal = crossunder(close, ema20)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ATR Trend Filter Strategy
# ============================================================
# EMA
draw(
ema20,
title="EMA 20",
frame=0,
color="blue"
)
# ATR
draw(
atr14,
title="ATR 14",
frame=1,
color="orange"
)
# ATR Threshold
draw(
10,
title="Volatility Threshold",
frame=1,
color="red"
)
# Display trades
marker_trades(
frame=0,
show_pnl=True
)
marker_text(
frame=0,
text="ATR Trend Filter",
anchor="top_right"
)
Stochastic #
(source, high, low, length)
Stochastic Oscillator momentum indicator.
Stochastic Overbought / Oversold Reversal Strategy
Buy when %K crosses above %D in the oversold zone.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Stochastic
k_line, d_line = Stochastic(close, high, low, 14, 3, 3)
# Buy when %K crosses above %D in the oversold zone
buy_zone = (k_line < 20) | (d_line < 20)
buy_signal = crossover(k_line, d_line) & buy_zone
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Calculate Stochastic
k_line, d_line = Stochastic(close, high, low, 14, 3, 3)
# Exit when %K crosses below %D in the overbought zone
sell_zone = (k_line > 80) | (d_line > 80)
exit_signal = crossunder(k_line, d_line) & sell_zone
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Calculate Stochastic
k_line, d_line = Stochastic(close, high, low, 14, 3, 3)
# Overbought / Oversold Levels
ob_line = k_line * 0 + 80
os_line = k_line * 0 + 20
# Plot %K and %D
draw(
k_line,
title="%K Line",
frame=1,
color="#00FFFF"
)
draw(
d_line,
title="%D Line",
frame=1,
color="#FFA500"
)
draw(
ob_line,
title="Overbought",
frame=1,
color="#FF3333"
)
draw(
os_line,
title="Oversold",
frame=1,
color="#33FF33"
)
marker_trades(
frame=0,
show_pnl=True
)
Stochastic Bullish Momentum Strategy
Buy when %K crosses above %D while both are above 50.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Stochastic
k_line, d_line = Stochastic(close, high, low, 14, 3, 3)
# Buy when %K crosses above %D while both are above 50,
# indicating bullish momentum
buy_signal = crossover(k_line, d_line) & (k_line > 50)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when %K crosses below %D or falls below 50,
# indicating weakening momentum
exit_signal = crossunder(k_line, d_line) | (k_line < 50)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot %K and %D
draw(
k_line,
title="%K Line",
frame=1,
color="#00FFFF"
)
draw(
d_line,
title="%D Line",
frame=1,
color="#FFA500"
)
# Midline
draw(
50,
title="Midline",
frame=1,
color="white"
)
# Overbought
draw(
80,
title="Overbought",
frame=1,
color="#FF3333"
)
# Oversold
draw(
20,
title="Oversold",
frame=1,
color="#33FF33"
)
marker_trades(
frame=0,
show_pnl=True
)
DMI #
(diLength=14, adxSmoothing=14)
Directional Movement Index. Returns [di_plus, di_minus, adx].
DMI Crossover Strategy
Buy when +DI crosses above -DI.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate DMI Components
di_plus, di_minus, adx = DMI(14, 14)
# Buy when +DI crosses above -DI,
# indicating buyers are gaining strength
buy_signal = crossover(di_plus, di_minus)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when -DI crosses above +DI,
# indicating sellers are gaining strength
exit_signal = crossover(di_minus, di_plus)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Plot +DI
draw(
di_plus,
title="+DI",
frame=1,
color="green"
)
# Plot -DI
draw(
di_minus,
title="-DI",
frame=1,
color="red"
)
# Plot ADX
draw(
adx,
title="ADX",
frame=1,
color="orange"
)
marker_text(
frame=1,
text="DMI Indicator",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
Strong Trend DMI Strategy
Buy when ADX rises above 25 and DI+ is greater than DI-.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate DMI Components
di_plus, di_minus, adx = DMI(
diLength=14,
adxSmoothing=14
)
# Buy when ADX rises above 25 and DI+ is greater than DI-,
# indicating a strong bullish trend
buy_signal = crossover(adx, 25) & (di_plus > di_minus)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when DI- becomes greater than DI+,
# indicating bearish strength
exit_signal = di_minus > di_plus
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ==================================================
# canvas.ta
# DMI Indicator
# Displays DI+, DI-, and ADX trend strength lines.
# ==================================================
di_plus, di_minus, adx = DMI(
diLength=14,
adxSmoothing=14
)
draw(
di_plus,
title="DI+",
frame=1,
color="green"
)
draw(
di_minus,
title="DI-",
frame=1,
color="red"
)
draw(
adx,
title="ADX",
frame=1,
color="yellow"
)
draw(
25,
title="Trend Strength",
frame=1,
color="white"
)
marker_text(
frame=1,
text="DMI Indicator",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
PSAR #
(start_af=0.02, step_af=0.02, max_af=0.20)
Parabolic Stop and Reverse.
PSAR Trend Reversal Strategy
Buy when PSAR flips below the price.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate PSAR
psar_val = PSAR(0.02, 0.02, 0.20)
# Buy when PSAR flips below the price,
# indicating the start of an uptrend
buy_signal = psar_val < close
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when PSAR moves above the price,
# indicating a possible trend reversal
exit_signal = psar_val > close
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# Parabolic SAR Indicator
# Displays trend reversal levels.
# ============================================================
psar_val = PSAR(0.02, 0.02, 0.20)
draw(
psar_val,
title="PSAR",
frame=0,
color="orange"
)
marker_text(
frame=0,
text="PSAR Indicator",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
PSAR + EMA Trend Filter Strategy
Buy when price crosses above the 20 EMA while remaining above the PSAR.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate PSAR and EMA
psar_val = PSAR(0.02, 0.02, 0.20)
ema20 = EMA(close, 20)
# Buy when price crosses above the 20 EMA
# while remaining above the PSAR
buy_signal = crossover(close, ema20) & (close > psar_val)
Entry(
index=1,
logic=buy_signal,
timeframe=5,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price closes below the PSAR
exit_signal = crossunder(close, psar_val)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# ============================================================
# Calculate PSAR and EMA
psar_val = PSAR(0.02, 0.02, 0.20)
ema20 = EMA(close, 20)
# Plot PSAR
draw(
psar_val,
title="PSAR",
frame=0,
color="orange"
)
# Plot EMA
draw(
ema20,
title="EMA 20",
frame=0,
color="blue"
)
marker_text(
frame=0,
text="PSAR + EMA Strategy",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
CCI #
(source=hlc3, length=20)
Commodity Channel Index.
CCI Oversold Recovery Strategy
Buy when CCI crosses above -100.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate CCI
cci_val = CCI(hlc3, 20)
# Buy when CCI crosses above -100,
# indicating recovery from an oversold condition
buy_signal = crossover(cci_val, -100)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when CCI enters the overbought zone
exit_signal = cci_val > 100
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# CCI Indicator
# ============================================================
# Plot CCI
draw(
cci_val,
title="CCI (20)",
frame=1,
color="orange"
)
# Overbought Level
draw(
100,
title="Overbought",
frame=1,
color="red"
)
# Oversold Level
draw(
-100,
title="Oversold",
frame=1,
color="green"
)
# Zero Line
draw(
0,
title="Zero Line",
frame=1,
color="white"
)
marker_trades(
frame=0,
show_pnl=True
)
CCI Zero Line Crossover Strategy
Buy when CCI crosses above the Zero Line.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate CCI
cci_val = CCI(hlc3, 20)
# Buy when CCI crosses above the Zero Line,
# indicating bullish momentum
buy_signal = crossover(cci_val, 0)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when CCI crosses below the Zero Line,
# indicating bearish momentum
exit_signal = crossunder(cci_val, 0)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# CCI Indicator
# ============================================================
draw(
cci_val,
title="CCI (20)",
frame=1,
color="orange"
)
draw(
100,
title="Overbought",
frame=1,
color="red"
)
draw(
0,
title="Zero Line",
frame=1,
color="white"
)
draw(
-100,
title="Oversold",
frame=1,
color="green"
)
marker_trades(
frame=0,
show_pnl=True
)
PivotPoints #
()
Returns [P, BC, TC, R1, S1, R2, S2, R3, S3].
Pivot Crossover Strategy
Buy when price crosses above the Pivot Point.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Pivot Points
P, BC, TC, R1, S1, R2, S2, R3, S3 = PivotPoints()
# Buy when price crosses above the Pivot Point,
# indicating bullish momentum
buy_signal = crossover(close, P)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price reaches the first resistance level
exit_signal = crossover(close, R1)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# Pivot Point Indicator
# ============================================================
P, BC, TC, R1, S1, R2, S2, R3, S3 = PivotPoints()
draw(
P,
title="Pivot",
frame=0,
color="white"
)
draw(
BC,
title="Bottom Central",
frame=0,
color="cyan"
)
draw(
TC,
title="Top Central",
frame=0,
color="cyan"
)
draw(
R1,
title="Resistance 1",
frame=0,
color="red"
)
draw(
S1,
title="Support 1",
frame=0,
color="green"
)
draw(
R2,
title="Resistance 2",
frame=0,
color="#FF8800"
)
draw(
S2,
title="Support 2",
frame=0,
color="#00AA00"
)
marker_text(
frame=0,
text="Pivot Point Strategy",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
Pivot Support Bounce Strategy
Buy when price bounces above Support 1.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Pivot Points
P, BC, TC, R1, S1, R2, S2, R3, S3 = PivotPoints()
# Buy when price bounces above Support 1,
# indicating buyers are defending the support level
buy_signal = crossover(close, S1)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price reaches the Pivot Point
exit_signal = crossover(close, P)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# Pivot Point Indicator
# ============================================================
P, BC, TC, R1, S1, R2, S2, R3, S3 = PivotPoints()
draw(P, title="Pivot", frame=0, color="white")
draw(BC, title="BC", frame=0, color="cyan")
draw(TC, title="TC", frame=0, color="cyan")
draw(R1, title="R1", frame=0, color="red")
draw(S1, title="S1", frame=0, color="green")
draw(R2, title="R2", frame=0, color="#FF8800")
draw(S2, title="S2", frame=0, color="#00AA00")
draw(R3, title="R3", frame=0, color="#FF4444")
draw(S3, title="S3", frame=0, color="#008800")
marker_text(
frame=0,
text="Pivot Support Bounce",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
DonchianChannels #
(length=20)
Returns [upper, mid, lower].
Donchian Channel Breakout Strategy
Buy when price breaks above the Upper Channel.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Donchian Channels
dc_upper, dc_mid, dc_lower = DonchianChannels(20)
# Buy when price breaks above the Upper Channel,
# indicating a bullish breakout
buy_signal = crossover(close, dc_upper)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price falls below the Middle Channel,
# indicating the trend is weakening
exit_signal = crossunder(close, dc_mid)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# Donchian Channels Indicator
# ============================================================
dc_upper, dc_mid, dc_lower = DonchianChannels(20)
draw(
dc_upper,
title="DC Upper",
frame=0,
color="red"
)
draw(
dc_mid,
title="DC Middle",
frame=0,
color="white"
)
draw(
dc_lower,
title="DC Lower",
frame=0,
color="green"
)
marker_fill(
frame=0,
line1=dc_upper,
line2=dc_lower,
color="rgba(0,150,255,0.08)"
)
marker_text(
frame=0,
text="Donchian Breakout",
anchor="top_right"
)
marker_trades(
frame=0,
show_pnl=True
)
Donchian Midline Strategy
Buy when price crosses above the Middle Channel.
# ============================================================
# entry_logic.ta
# ============================================================
# Calculate Donchian Channels
dc_upper, dc_mid, dc_lower = DonchianChannels(30)
# Buy when price crosses above the Middle Channel,
# indicating bullish momentum
buy_signal = crossover(close, dc_mid)
Entry(
index=1,
logic=buy_signal,
timeframe=15,
script=RELIANCE,
action=Buy,
order_type="MARKET"
)
# ============================================================
# exit_logic.ta
# ============================================================
# Exit when price crosses below the Lower Channel,
# indicating a bearish breakout
exit_signal = crossunder(close, dc_lower)
Exit(
index=1,
logic=exit_signal,
exit_type="MARKET"
)
# ============================================================
# canvas.ta
# Donchian Channels Indicator
# Displays upper, middle and lower channel bands.
# ============================================================
dc_upper, dc_mid, dc_lower = DonchianChannels(30)
draw(
dc_upper,
title="Upper",
frame=0,
color="red"
)
draw(
dc_mid,
title="Middle",
frame=0,
color="white"
)
draw(
dc_lower,
title="Lower",
frame=0,
color="green"
)
marker_trades(
frame=0,
show_pnl=True
)