0% found this document useful (0 votes)
7 views

Dynamic Volatility Strategy

Uploaded by

Samuele Ismalaj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Dynamic Volatility Strategy

Uploaded by

Samuele Ismalaj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//@version=5

strategy("Dynamic Volatility-Based Strategy", overlay=true)

// Input parameters
volatilityLookback = input.int(100, title="Volatility Lookback Bars")
timeframe680 = "680"

// Function to calculate the bar with least volatility


findLeastVolatileBar(_lookback) =>
var float minRange = na
var int minIndex = na
for i = 0 to _lookback - 1
range = high[i] - low[i]
if na(minRange) or range < minRange
minRange := range
minIndex := i
[minRange, minIndex]

// Calculate volatility and the least volatile bar


[minVolatility, minBarIndex] = request.security(syminfo.tickerid, timeframe680,
findLeastVolatileBar(volatilityLookback))

// Stops and TP based on the identified bar


entryHigh = request.security(syminfo.tickerid, timeframe680, high[minBarIndex])
entryLow = request.security(syminfo.tickerid, timeframe680, low[minBarIndex])
range = entryHigh - entryLow
tpMultiplier = 2.0
var float takeProfit = range * tpMultiplier
var float lastDirection = na // 1 for buy, -1 for sell

// Strategy logic
if bar_index == minBarIndex
// Reset take profit to initial TP
takeProfit := range * tpMultiplier

// Entry conditions
if na(lastDirection) or lastDirection == -1
if close >= entryHigh
strategy.entry("Buy", strategy.long, stop=entryHigh)
lastDirection := 1
takeProfit := range * tpMultiplier
else if lastDirection == 1
if close <= entryLow
strategy.entry("Sell", strategy.short, stop=entryLow)
lastDirection := -1
takeProfit := range * tpMultiplier

// Take-profit adjustments
strategy.exit("Exit Buy", "Buy", limit=entryHigh + takeProfit)
strategy.exit("Exit Sell", "Sell", limit=entryLow - takeProfit)

You might also like