Strategy · Pine Script v5 · On the chart

Greeffer's Hedge Mode Strategy

by modster

This strategy overlays Donchian Channels and higher-timeframe exponential moving averages on the price chart to generate trading signals.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code114
Settings5inputs
LicenceCC0-1.0
Last update4 years ago16 Mar 2022

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 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.

Details

Published by

modster on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Breakout, Moving averages, Trend

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
LongbooltruelongOrShort
Entry Amountfloat0.001entryAmount
Exit Amountfloat0.001exitAmount
upper donchian channel periodint1440donchianupper
lower donchian channel periodint1440donchianlower
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.

Similar Pine Scripts

257 stars

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

Strategy v6 TrendMoving averages Repaints
Updated 6 weeks ago · None stated GitHub repository of Best Chart 1m Strategy Candle Bar

12/26-IT Strategy

by hasnocool
106 stars

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

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 12/26-IT Strategy

均线突破交易系统

by tangly1024
76 stars

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

Strategy v5 TrendMoving averages
Updated 4 months ago · None stated GitHub repository of 均线突破交易系统
43 stars

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-

Strategy v5 TrendBreakout Repaints
Updated 2 years ago · None stated GitHub repository of SOL 1m - Strategy Candle Bar

ADA4H Optuna Strategy

by skywalker0803r
42 stars

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

Strategy v5 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of ADA4H Optuna Strategy
16 stars

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

Strategy v6 TrendBreakout
Updated 11 months ago · None stated GitHub repository of Seykota Alt 10: Profit Targets

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers