Indicator · Pine Script v5 · On the chart

Hedge Scalp

by nikita-doronin· code credited to Trade_ND

The Hedge Scalp indicator calculates an Exponential Moving Average (EMA) and generates four long grid levels and one hedged short grid level at user-defined percentage distances below the EMA.

View on GitHub

GitHub stars30on the repository
Pine versionv5runs as is
Lines of code207
Settings17inputs
LicenceCustom licencefile: MPL-2.0
Last update2 years ago7 May 2024

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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

207 lines of Pine Script v5 from backtest/pinescript/1_hedge_scalp.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

1_hedge_scalp.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Trade_ND

//@version=5
indicator('Hedge Scalp', shorttitle='Hedge Scalp', overlay=true)

// Inputs
ema_lenght = input(defval=2, title= "EMA Lenght", tooltip = "Lenght of Initial Grid", group = "EMA Settings:")

grid_1_long_inp = input.float(defval=0.5, title= "1 Grid (in %)", tooltip = "The input for 1st Long grid in %", group = "Grid Settings:")
grid_2_long_inp = input.float(defval=1, title= "2 Grid (in %)", tooltip = "The input for 2nd Long grid in %", group = "Grid Settings:")
grid_3_long_inp = input.float(defval=1.5, title= "3 Grid (in %)", tooltip = "The input for 3rd Long grid in %", group = "Grid Settings:")
grid_4_long_inp = input.float(defval=2, title= "4 Grid (in %)", tooltip = "The input for 4th Long grid in %", group = "Grid Settings:")
grid_5_short_inp = input.float(defval=2.5, title='5 Grid hedged (in %)', tooltip = "The input for 5th Hedged Short grid in %", group = "Grid Settings:")
long_tp_inp = input.float(defval=0.5, title='Long Take Profit (in %)', tooltip = "Take Profit is cacluclated from Average Entry Price", group = "Stop Loss and Take Profit Settings:")
short_tp_inp = input.float(defval=1.6, title='Short Take Profit (in %)', tooltip = "Take Profit is cacluclated from Average Entry Price", group = "Stop Loss and Take Profit Settings:")

deposit = input.int(1000, title = 'Strategy Deposit (USDT)', tooltip = "The input for Strategy Deposit", group = "Equity for etries:")
comission = input.float(0.04, title = "Comission (%)", tooltip = "The input for Comission, already in %", group = "Equity for etries:")
n1 = input.float(2.17, title = 'Long Entry 1 - Equity in % from total', tooltip = "The input for amount equty for 1st grid for Long", group = "Equity for etries:")
n2 = input.float(4.35, title = 'Long Entry 2 - Equity in % from total', tooltip = "The input for amount equty for 2nd grid for Long", group = "Equity for etries:")
n3 = input.float(8.7, title = 'Long Entry 3 - Equity in % from total', tooltip = "The input for amount equty for 3rd grid for Long", group = "Equity for etries:")
n4 = input.float(18.2, title = 'Long Entry 4 - Equity in % from total', tooltip = "The input for amount equty for 4th grid for Long", group = "Equity for etries:")
n5 = input.float(66.67, title = 'Short Entry 5 - Equity in % from total', tooltip = "The input for amount equty for 5th grid for Short", group = "Equity for etries:")

StartDate = input.time(timestamp("1 Jan 2024 00:00"), title="Start Date", group = "Backtest:")
EndDate = input.time(timestamp("1 Jan 2099 01:00"), title="End Date", group = "Backtest:")

// Calculations
ema=ta.ema(close,ema_lenght)

grid_long_1 = ema-ema*(grid_1_long_inp/100)
grid_long_2 = ema-ema*(grid_2_long_inp/100)
grid_long_3 = ema-ema*(grid_3_long_inp/100)
grid_long_4 = ema-ema*(grid_4_long_inp/100)
grid_short_5 = ema-ema*(grid_5_short_inp/100)

// Backtesting range
time_cond = time >= StartDate and time <= EndDate

// Indicator logic
var grid_1_long_taken = false
var grid_2_long_taken = false
var grid_3_long_taken = false
var grid_4_long_taken = false
var grid_5_short_taken = false
var long_tp_taken = false
var short_tp_taken = false

var price_gird_1 = 0.0
var price_gird_2 = 0.0
var price_gird_3 = 0.0
var price_gird_4 = 0.0
var price_gird_5 = 0.0
var price_tp_long = 0.0
var price_tp_short = 0.0

var qty_long_1 = 0.0
var qty_long_2 = 0.0
var qty_long_3 = 0.0
var qty_long_4 = 0.0
var qty_short_5 = 0.0
var qty_long_sum = 0.0
var qty_short_sum = 0.0

var avg_price_long = 0.0
var avg_price_short = 0.0
var n_long = 0
var n_short = 0

var profit = 0.0
var profit_sum = 0.0

// 1st long grid logic
if not grid_1_long_taken and time_cond and (low<grid_long_1)
    price_gird_1 := grid_long_1[1]
    price_gird_2 := grid_long_2[1]
    price_gird_3 := grid_long_3[1]
    price_gird_4 := grid_long_4[1]
    price_gird_5 := grid_short_5[1]

    n_long := n_long + 1
    qty_long_1 := (deposit*n1/100/price_gird_1)
    qty_long_sum := qty_long_sum + qty_long_1

    avg_price_long := (price_gird_1*qty_long_1)/qty_long_sum
    price_tp_long := avg_price_long+(avg_price_long*long_tp_inp/100)

    grid_1_long_taken := true

// 2nd long grid logic
if grid_1_long_taken and not grid_2_long_taken and time_cond and (low<price_gird_2)
    n_long := n_long + 1
    qty_long_2 := (deposit*n2/100/price_gird_2)
    qty_long_sum := qty_long_sum + qty_long_2

    avg_price_long := ((price_gird_1*qty_long_1)+(price_gird_2*qty_long_2))/qty_long_sum
    price_tp_long := avg_price_long+(avg_price_long*long_tp_inp/100)

    grid_2_long_taken := true

// 3rd long grid logic
if grid_2_long_taken and not grid_3_long_taken and time_cond and (low<price_gird_3)
    n_long := n_long + 1
    qty_long_3 := (deposit*n3/100/price_gird_3)
    qty_long_sum := qty_long_sum + qty_long_3

    avg_price_long := ((price_gird_1*qty_long_1)+(price_gird_2*qty_long_2)+(price_gird_3*qty_long_3))/qty_long_sum
    price_tp_long := avg_price_long+(avg_price_long*long_tp_inp/100)

    grid_3_long_taken := true

// 4th long grid logic
if grid_3_long_taken and not grid_4_long_taken and time_cond and (low<price_gird_4)
    n_long := n_long + 1
    qty_long_4 := (deposit*n4/100/price_gird_4)
    qty_long_sum := qty_long_sum + qty_long_4

    avg_price_long := ((price_gird_1*qty_long_1)+(price_gird_2*qty_long_2)+(price_gird_3*qty_long_3)+(price_gird_4*qty_long_4))/qty_long_sum
    price_tp_long := avg_price_long+(avg_price_long*long_tp_inp/100)

    grid_4_long_taken := true

// 5th short grid logic
if grid_4_long_taken and not grid_5_short_taken and time_cond and (low<price_gird_5)
    n_short := n_short + 1
    qty_short_5 := (deposit*n5/100/price_gird_5)
    qty_short_sum := qty_short_sum + qty_short_5

    avg_price_short := (price_gird_5*qty_short_5)/qty_short_sum
    price_tp_short := avg_price_short-(avg_price_short*short_tp_inp/100)
    grid_5_short_taken := true

// calculate profit
if qty_long_sum > 0 and qty_short_sum > 0 // Calculation for longs and shorts
    profit := math.round((close - avg_price_long)*qty_long_sum + (avg_price_short-close)*qty_short_sum - (avg_price_long*qty_long_sum + avg_price_short*qty_short_sum)*comission*0.01, 2)

if qty_long_sum > 0 and qty_short_sum == 0 // Calculation for longs only (if we have only longs)
    profit := math.round((close - avg_price_long)*qty_long_sum + avg_price_long*qty_long_sum*comission*0.01,2)

if qty_long_sum == 0 and qty_short_sum > 0 // Calculation for short only (if we have only shorts)
    profit := math.round((avg_price_short - close)*qty_short_sum - avg_price_short*qty_short_sum*comission*0.01,2)

// take profit and counters rest
if grid_1_long_taken and not long_tp_taken and not short_tp_taken and time_cond and (low<price_tp_short or high>price_tp_long)
    grid_1_long_taken := false
    grid_2_long_taken := false
    grid_3_long_taken := false
    grid_4_long_taken := false
    grid_5_short_taken := false
    long_tp_taken := false
    short_tp_taken := false

    price_gird_1 := 0.0
    price_gird_1
    price_gird_2 := 0.0
    price_gird_2
    price_gird_3 := 0.0
    price_gird_3
    price_gird_4 := 0.0
    price_gird_4
    price_gird_5 := 0.0
    price_gird_5
    price_tp_long := 0.0
    price_tp_long
    price_tp_short := 0.0
    price_tp_short

    qty_long_1 := 0.0
    qty_long_1
    qty_long_2 := 0.0
    qty_long_2
    qty_long_3 := 0.0
    qty_long_3
    qty_long_4 := 0.0
    qty_long_4
    qty_short_5 := 0.0
    qty_short_5
    qty_long_sum := 0.0
    qty_long_sum
    qty_short_sum := 0.0
    qty_short_sum

    avg_price_long := 0.0
    avg_price_long
    avg_price_short := 0.0
    avg_price_short
    n_long := 0
    n_long
    n_short := 0
    n_short

    profit_sum := profit_sum + profit
    label.new(time, profit_sum, text = "PnL: "+str.tostring(profit_sum), color = color.white, xloc = xloc.bar_time, yloc=yloc.belowbar, size = size.large, style=label.style_label_down)

// Plots
plot((price_gird_1 > 0 ? price_gird_1 : ema), title = 'Initial EMA', offset=1, color = color.blue)
plot((price_gird_1 > 0 ? price_gird_1 : grid_long_1), title = '1 Gird Long', offset = 1, color = color.green, style = plot.style_linebr)
plot((price_gird_2 > 0 ? price_gird_2 : grid_long_2), title = '2 Gird Long', offset = 1, color = color.green, style = plot.style_linebr)
plot((price_gird_3 > 0 ? price_gird_3 : grid_long_3), title = '3 Gird Long', offset = 1, color = color.green, style = plot.style_linebr)
plot((price_gird_4 > 0 ? price_gird_4 : grid_long_4), title = '4 Gird Long', offset = 1, color = color.green, style = plot.style_linebr)
plot((price_gird_5 > 0 ? price_gird_5 : grid_short_5), title = '5 Gird Short', offset = 1, color = color.red, style = plot.style_linebr)

plot((avg_price_long > 0 ? avg_price_long : na), title = 'AVG Long Entry Price', color = color.green, style = plot.style_linebr, display = display.none)
plot((avg_price_short > 0 ? avg_price_short : na), title = 'AVG Short Entry Price', color = color.red, style = plot.style_linebr, display = display.none)
plot((price_tp_long > 0 ? price_tp_long : na), title = 'Long Take Profit', color = color.green, linewidth = 3, style = plot.style_circles)
plot((price_tp_short > 0 ? price_tp_short : na), title = 'Short Take Profit', color = color.red, linewidth = 3, style = plot.style_circles)

Overview

Hedge Scalp models grid entry execution and average entry take-profit levels for scalp strategies. Settings allow customization of the EMA length, individual percentage grid offsets, long and short take-profit percentages, and position sizing allocations per level. It displays directly on the main price chart.

Details

Published by

nikita-doronin on GitHub

Code credit

Trade_ND, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Moving averages, Support & resistance, Scalping

Markets

Crypto

Timeframes

M1-M5, M15-H1

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
EMA Lenghtint2ema_lenght
1 Grid (in %)float0.5grid_1_long_inp
2 Grid (in %)float1grid_2_long_inp
3 Grid (in %)float1.5grid_3_long_inp
4 Grid (in %)float2grid_4_long_inp
5 Grid hedged (in %)float2.5grid_5_short_inp
Long Take Profit (in %)float0.5long_tp_inp
Short Take Profit (in %)float1.6short_tp_inp
Strategy Deposit (USDT)int1000deposit
Comission (%)float0.04comission
Long Entry 1 - Equity in % from totalfloat2.17n1
Long Entry 2 - Equity in % from totalfloat4.35n2
Show all 17 settings
SettingTypeDefaultVariable
Long Entry 3 - Equity in % from totalfloat8.7n3
Long Entry 4 - Equity in % from totalfloat18.2n4
Short Entry 5 - Equity in % from totalfloat66.67n5
Start Datetimetimestamp("1 Jan 2024 00:00")StartDate
End Datetimetimestamp("1 Jan 2099 01:00")EndDate
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

SPY 0DTE Scalper - 1m

by chris-c-thomas
14 stars

This overlay indicator marks CALLS and PUTS labels on the price chart when a strict boolean gate of price structure, momentum, volatility, breadth, and candle confirmation all pass. It plots an EMA ribbon with cloud fill, session-anchored VWAP and standard-dev

Indicator v6 ScalpingMoving averages
Updated 6 months ago · MIT GitHub repository of SPY 0DTE Scalper - 1m

Scalping PullBack Tool R1.1

by Ahmed-GoCode
5 stars

This indicator plots a Price Action Channel (PAC) based on high, low, and close moving averages alongside fast, medium, and slow trend EMAs directly on the price chart. It detects regular and Bill Williams fractals to track market structure and identifies pull

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Scalping PullBack Tool R1.1
948 stars

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

Indicator v4 Moving averagesSupport & resistance
Updated 10 days ago · GPL-3.0 GitHub repository of Moving Average Channel

This indicator scans NSE-listed stocks for momentum signals using Supertrend, multiple EMAs, MACD, and stochastic oscillator. It supports continuous or flip signal modes and displays matching setups in a customizable table at the bottom left of the chart. The

Indicator v5 Oscillators & momentumMoving averages
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup: Vijay Thakare Option Buying Scalping Setup Scanner

PivotHighLow

by Alorse
330 stars

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

Indicator v4 Support & resistanceOscillators & momentum Repaints
Updated 2 months ago · None stated GitHub repository of PivotHighLow

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers