Strategy · Pine Script v5 · Own pane

Vol-Targeted Position Size Strategy

by Robot-Wealth· code credited to robotkris

This strategy calculates a target position size based on exponentially weighted moving average volatility forecasts.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code40
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago26 Mar 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 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

40 lines of Pine Script v5 from VolTargeting/vol_targeted_pos_size_strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

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

//@version=5
strategy("Vol-Targeted Position Size Strategy", overlay = false)
import TradingView/ta/7
import robotkris/VolTargeting/2 as VolTargeting

balance = strategy.initial_capital
volEstimationWdw = input(30, "Volatility estimation window length")
targetVolContribution = input(20, "Target volatility contribution, %")
tradeBuffer = input(10, "Size of no-trade region as % of target position size")

// calculate vol forecast as ewma of volatility
volForecast = VolTargeting.ewmaVol(volEstimationWdw)

// calculate target position size
targetPos = VolTargeting.volTargetedSize(balance, targetVolContribution, volForecast)
upperBarrier = targetPos+targetPos*0.01*tradeBuffer
lowerBarrier = targetPos-targetPos*0.01*tradeBuffer

// current position
float currentPosition = strategy.position_size

// trade logic
delta = math.floor(math.abs(currentPosition - targetPos))
if currentPosition > upperBarrier
    // current position is above trade buffer - rebalance back to target
    strategy.order('position', direction = strategy.short, qty = delta)
else if currentPosition < lowerBarrier
    // current position is below trade buffer - rebalance back to target
    strategy.order('position', direction = strategy.long, qty = delta)

// plot current position
plot(currentPosition, "current pos", color = color.rgb(37, 188, 0), style = plot.style_stepline, linewidth = 2)
// plot target position size and no-trade region
plot(targetPos, title = "Target position", color = color.rgb(66, 56, 245), style = plot.style_stepline)
barrier1 = plot(upperBarrier, "Top of no-trade region", color = color.rgb(245, 126, 56, 50))
barrier2 = plot(lowerBarrier, "Bottom of no-trade region", color = color.rgb(245, 126, 56, 50))
fill(barrier1, barrier2, color = color.rgb(245, 126, 56, 75))

Overview

Vol-Targeted Position Size Strategy plots the current position size, target position size, and a configurable trade buffer region in a separate indicator pane. When the current position breaches the upper or lower buffer boundaries, the strategy automatically rebalances the open quantity back toward the target size using market orders. Key inputs control the volatility estimation window, target annual volatility contribution, and trade buffer percentage. The strategy relies solely on dynamic position sizing and rebalancing, featuring no traditional stop-loss or take-profit exits.

Details

Published by

Robot-Wealth on GitHub

Code credit

robotkris, as written in the script's header

Type

Strategy, Pine Script v5

Draws

Separate pane below the chart

Category

Volatility & bands

Markets

Any symbol

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
Volatility estimation window lengthint30volEstimationWdw
Target volatility contribution, %int20targetVolContribution
Size of no-trade region as % of target position sizeint10tradeBuffer
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

330 stars

A momentum strategy combining the TTM Squeeze oscillator with an RSI filter. It calculates linear regression momentum relative to Bollinger Bands and Keltner Channels alongside an RSI oscillator. Long signals trigger when momentum rises from below zero while R

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of TTM Squeeze [Alorse]

This tutorial strategy demonstrates lower-timeframe multi-timeframe functionality in Pine Script using request.security_lower_tf. It evaluates synthetic lower-timeframe 1-minute close prices within each chart bar to calculate the intra-bar price range percenta

Strategy v6 Volatility & bands
Updated today · Apache-2.0 GitHub repository of PineForge MTF tutorial — lower-TF intra-bar range

2xTP, SL to Entry

by hasnocool
106 stars

This strategy executes mean-reversion trades based on Bollinger Bands and the Stochastic Oscillator. Long orders trigger when price dips below the lower band alongside a bullish Stochastic crossover, with an optional oversold threshold. Short orders execute on

Strategy v5 Mean reversionOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 2xTP, SL to Entry

EMA Ribbon MACD

by iamrichardD
57 stars

This strategy combines an EMA ribbon with MACD confirmation and plots in a separate pane. It enters when ribbon alignment, optional MACD zero or signal crosses, volume, smart-money, and multi-timeframe filters agree, then exits with percentage stop loss and ta

Strategy v6 TrendMoving averages
Updated 1 year ago · Apache-2.0 GitHub repository of EMA Ribbon MACD
25 stars

This Pine Script strategy calculates Bayesian probabilities of price breaking up or down relative to Bollinger Bands and a Simple Moving Average. It plots these breakout and breakdown probability lines as oscillators in a separate pane. Long entries trigger wh

Strategy v4 Oscillators & momentumVolatility & bands
Updated 6 years ago · None stated GitHub repository of Bayesian BBSMA Oscillator Strategy

Greeffer BB Strategy

by SammyEnigma
18 stars

This strategy fades Bollinger Band extremes using the close. It enters short when price crosses above the upper band and the 14-period RSI is above 70, and enters long when price crosses below the lower band and RSI is below 30. It closes half of an open short

Strategy v4 Volatility & bandsOscillators & momentum
Updated 6 years ago · None stated GitHub repository of Greeffer BB Strategy

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