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 strategy, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.
Source Code
114 lines of Pine Script v5 from leobot/hedge-mode-strategy.pine, published under CC0-1.0. Keep the author's credit and licence lines when you reuse it.
hedge-mode-strategy.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 Strategy calculates an upper, lower, and median Donchian band alongside 240-minute EMAs fetched specifically for BTCUSDT. When price breaches the outer channel boundaries, the script triggers formatted JSON alert messages designed for external bot execution. Inputs allow users to toggle trade direction, adjust Donchian channel lookback periods, and specify order quantities. The strategy does not execute built-in strategy exits, relying entirely on alert triggers.
Configurable Donchian Channel breakout levels
Multi-timeframe 240-minute EMA trend overlays
Automated JSON webhook alert formatting for external bots
Directional filter toggle for long or short execution
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.
This strategy plots running pivot highs as red R circles and pivot lows as blue S circles directly on the price chart. It computes an EMA 3 and a Cto trend line from close prices, then generates long signals on upward EMA-Cto crossovers combined with price pos
This strategy executes trades based on crossovers between a 12-period and a 26-period exponential moving average (EMA). Long positions can be filtered by pivot resistance breakouts and RSI boundary ranges. For trade management, it provides two configurable par
This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the
This strategy executes trades on short-term cryptocurrency charts using a combination of pivot support and resistance levels and a multi-pole smoothed trend filter. Entries trigger when price crosses the smoothed trend line and confirms above or below a short-
This strategy trades longs and shorts on the price chart. Long entries require a close above the prior Donchian high, a close above a long-term SMA, and RSI above a threshold, with size set as a percentage of equity. Long exits use a stop at the prior Donchian
This strategy plots Donchian channels, trailing stops, and profit targets directly on the price chart. It enters breakout trades using a 55-bar Donchian channel and sizes positions according to an ATR-based risk percentage per unit. The system adds units as pr