1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
114 lines of Pine Script v5 from leobot/hedge-mode-indicator.pine, published under CC0-1.0. Keep the author's credit and licence lines when you reuse it.
hedge-mode-indicator.pine
// @Greeffer Hedge Mode Study
// @version=5
// S T U D Y
indicator(shorttitle="Hedge Study", title="Greeffer's Hedge Mode Study", overlay=true)
// S T R A T E G Y
//strategy(
// "Greeffer's Hedge Mode Strategy",
// "Hedge Strategy",
// overlay=true,
// pyramiding=1,
// default_qty_type=strategy.percent_of_equity,
// default_qty_value=200,
// initial_capital=5000,
// commission_type=strategy.commission.percent,
// commission_value=0.01,
// margin_long=1./20*50,
// margin_short=1./20*50
// )
// I N P U T S
longOrShort = input(title="Long", defval=true, tooltip="Uncheck to Short") //<-------- todo: get rid of this var
//var longOrShort = true
entryAmount = input(title="Entry Amount", defval=0.001) //<---------------todo: handle position size in azure
exitAmount = input(title="Exit Amount", defval=0.001) //<----------------todo: handle position size in azure
donchianupper = input(title="upper donchian channel period", defval=1440)
donchianlower = input(title="lower donchian channel period", defval=1440)
// V A R S
symbol = "BTCUSDT" //symbol = "ETHUSDT"
short = false
long = false
plots = 2000 // plot this many bars back
// E M A s
// plots = 2500 // plot this many bars back
ema08 = ta.ema(close, 8)
ema17 = ta.ema(close,17)
ema72 = ta.ema(close,72)
red = request.security("BTCUSDT", "240", ema08)
orange = request.security("BTCUSDT", "240", ema17)
yellow = request.security("BTCUSDT", "240", ema72)
plot(red,title="EMA 08", color=color.red, linewidth=1, style=plot.style_line) //, show_last=plots
plot(orange, title="EMA 17", color=color.orange, linewidth=2, style=plot.style_line)
plot(yellow,title="EMA 72", color=color.yellow, linewidth=3, style=plot.style_line)
// D O N C H I A N C H A N N E L S
lower = ta.lowest(donchianlower)
upper = ta.highest(donchianupper)
middle= (upper-lower)/2+lower
plot(middle, title='Middle', color=color.green, linewidth=1)
plot(upper, title='Upper', color=color.orange, linewidth=1)
plot(lower, title='Lower', color=color.orange, linewidth=1)
// B O L L I N G E R B A N D S
//length = input(20, minval=1)
//src = input(close, title="Source")
//mult = input(4.0, minval=0.001, maxval=50, title="StdDev")
//basis = sma(src, length)
//dev = mult * stdev(src, length)
//bbUpper = basis + dev
//bbLower = basis - dev
//bbw = (upper-lower)/basis
// E N T R Y / E X I T C o n d i t i o n s
// L o n g :
if close>=upper and longOrShort==true
long:=true
else
long:=false
if close<=lower and longOrShort==true
long:=true
else
long:=false
// S h o r t
if close>=upper and longOrShort==false
short:=true
else
short:=false
if close<=lower and longOrShort==false
short:=true
else
short:=false
// O R D E R S :
// L o n g
if long==true and longOrShort==true
alert(message='{"side":"longHedge","stopPrice":"'+str.tostring(math.round(lower))+'","middle":"'+str.tostring(math.round(middle))+'","symbol":"'+symbol+'","close":"'+str.tostring(close)+'","amount":"'+str.tostring(entryAmount)+'"}', freq=alert.freq_once_per_bar_close)
if short==true and longOrShort==true
alert(message='{"side":"closeLong","middle":"'+str.tostring(math.round(middle))+'","symbol":"'+symbol+'","close":"'+str.tostring(close)+'","amount":"'+str.tostring(exitAmount)+'"}', freq=alert.freq_once_per_bar_close)
// S h o r t
if short==true and longOrShort==false
alert(message='{"side":"shortHedge","stopPrice":"'+str.tostring(math.round(upper))+'","middle":"'+str.tostring(math.round(middle))+'","symbol":"'+symbol+'","close":"'+str.tostring(close)+'","amount":"'+str.tostring(entryAmount)+'"}', freq=alert.freq_once_per_bar_close)
if long==true and longOrShort==false
alert(message='{"side":"closeShort","middle":"'+str.tostring(math.round(middle))+'","symbol":"'+symbol+'","close":"'+str.tostring(close)+'","amount":"'+str.tostring(exitAmount)+'"}', freq=alert.freq_once_per_bar_close)
Overview
Greeffer's Hedge Mode Study calculates EMAs from 8, 17 and 72 periods using higher timeframe data from 240 minutes. The script monitors price action against the upper and lower Donchian bands with default periods of 1440 bars each. When price crosses the upper band or lower band depending on the long or short mode input it generates alerts for entry and exit signals. The main settings allow switching between long and short hedge modes and adjusting entry and exit amounts. It sends JSON formatted alerts containing stop prices, middle band values, symbol and amounts to an external trading system.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Long
bool
true
longOrShort
Entry Amount
float
0.001
entryAmount
Exit Amount
float
0.001
exitAmount
upper donchian channel period
int
1440
donchianupper
lower donchian channel period
int
1440
donchianlower
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us
This indicator calculates pivot high and pivot low points on the price chart based on configurable left and right bar counts. Confirmed pivot highs and lows are plotted with negative bar offsets directly on the price chart using line and cross styles. Addition
This overlay indicator marks call and put setups from EMAs, RSI, MACD, and a SPY volume ratio. A call needs higher highs, RSI above its minimum, rising MACD, above-average proxy volume, and a rising trend EMA; a put reverses those tests. Early markers use an E
The Midnight VWAP indicator calculates and overlays two distinct Volume Weighted Average Price (VWAP) lines on the price chart based on customizable session timeframes. It tracks an overnight or pre-market session alongside regular trading hours, resetting cal
This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts
This indicator displays moving average deduction reference points directly on the price chart. It plots dynamic labels on historical bars corresponding to short, medium, and long lookback periods, defaulted to 20, 60, and 120 bars. Each text label displays the