Fractal Trail Algorithm Conversion
Forums › ProRealTime English forum › ProBuilder support › Fractal Trail Algorithm Conversion
- This topic has 1 reply, 1 voice, and was last updated 2 weeks ago by
jogtt.
-
-
02/04/2025 at 1:18 AM #243428
Hi All,
I have found a very interesting Fractal Trail Algorithm based on Williams Fractals. The code is in Pine v5.0 and available for TradingView. You can find a comprehensive description in the following link:
https://es.tradingview.com/script/kAAGPpsC-Fractal-Trail-UAlgo/
I have attempted translating the code into Pro Real Code with the help of Chat GPT. Nonetheless, the results I am getting for the same assets and parameters are different in both TW and PRT as you can see in the images attached, so I reckon I am not properly capturing the indicator’s logic. This is what I have so far:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950// --- Fractal Trail Indicator for ProRealTime (ProBuilder) ---// --- User Inputs ---WilliamsLeftRange = 2 // Number of bars to the leftWilliamsRightRange = 2 // Number of bars to the rightWilliamsStopBuffer = 0 // Stop buffer percentage// --- Fractal Detection ---FractalHigh = 0FractalLow = 0// --- Identify High Fractal (Delayed Confirmation) ---IF High[WilliamsRightRange] >= Highest[WilliamsLeftRange + WilliamsRightRange](High) AND High[WilliamsRightRange] <> High[WilliamsRightRange+1] THENFractalHigh = High[WilliamsRightRange]ENDIF// --- Identify Low Fractal (Delayed Confirmation) ---IF Low[WilliamsRightRange] <= Lowest[WilliamsLeftRange + WilliamsRightRange](Low) AND Low[WilliamsRightRange] <> Low[WilliamsRightRange+1] THENFractalLow = Low[WilliamsRightRange]ENDIF// --- Apply Stop Buffer (Only After Fractal Confirmation) ---FractalLowBuffered = FractalLow * (1 - WilliamsStopBuffer / 100)FractalHighBuffered = FractalHigh * (1 + WilliamsStopBuffer / 100)// --- Persistent Stop Logic ---ONCE ActiveStop = 0ONCE TrendDirection = 0 // 1 = Long, -1 = Short, 0 = Neutral// --- Ensure Stops Persist & Only Move in Favorable Direction ---IF FractalLow > 0 THENIF TrendDirection <> 1 THENActiveStop = FractalLowBuffered // Flip to long stop only if not already longTrendDirection = 1ELSEActiveStop = MAX(ActiveStop, FractalLowBuffered) // Update only if it moves upENDIFENDIFIF FractalHigh > 0 THENIF TrendDirection <> -1 THENActiveStop = FractalHighBuffered // Flip to short stop only if not already shortTrendDirection = -1ELSEActiveStop = MIN(ActiveStop, FractalHighBuffered) // Update only if it moves downENDIFENDIF// --- Plot Only the Active Stop ---RETURN ActiveStop AS "Fractal Trailing Stop"Would anyone be interested in checking the code I have produced or translating the code directly into Pro Real Code?
Many thanks in advance for your support!
02/07/2025 at 3:08 PM #243588Anyone could give me hand with the code conversion, please? For your convenience, I am attaching below the original code from TradingView together with another caption of the indicator:
// This Pine Script™ code is subject to the terms of the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © UAlgo
//@version=5
indicator(title=’Fractal Trail [UAlgo]’, shorttitle=’Fractal Trail [UAlgo]’, overlay=true)bool showWilliamsFractals = input.bool(defval=true, title=’Show All Williams Fractals’, group=”Fractal Trail [UAlgo]”, tooltip=’Places triangle indicators to mark each Williams fractal position’)
bool showWilliamsStops = input.bool(defval=true, title=’Show Williams Trail’, tooltip=’Creates a dynamic trailing stop based on the latest identified fractal’, group=”Fractal Trail [UAlgo]”)
int williamsLeftRange = input.int(defval=2, minval=1, maxval=50, title=’Williams Fractal Range Left/Right’, group=”Fractal Trail [UAlgo]”, tooltip=’Specifies the number of previous bars to analyze for the range. Standard setting is two bars in both directions.’,inline = “range”)
int williamsRightRange = input.int(defval=2, minval=1, maxval=50, title=’ ‘, tooltip=’Determines the number of forward bars to examine for the range. Best practice suggests matching the backward range value.’, group=”Fractal Trail [UAlgo]”,inline = “range”)
float williamsStopBuffer = input.float(defval=0, minval=0, maxval=20, step=0.5, title=’Trail Buffer Percent (%)’, group=”Fractal Trail [UAlgo]”, tooltip=’Sets a percentage-based safety margin between the fractal price and the trailing stop’)
string flipInput = input.string(title=’Trail Invalidation Source’, defval=’Close’, options=[‘Close’, ‘Wick’], tooltip=’Determines whether the trail flips based on candle closing prices or extreme points (high/low)’, group=”Fractal Trail [UAlgo]”)
color upTrailColor = input.color(defval=color.rgb(40, 218, 150), title=’Up/Down Trail Color’, group=”Fractal Trail [UAlgo]”,inline = “trailC”)
color dnTrailColor = input.color(defval=color.new(color.blue, 0), title=’ ‘, group=”Fractal Trail [UAlgo]”,inline = “trailC”)
color fillColorUp = color.new(color.rgb(40, 218, 150), 80)
color fillColorDn = color.new(color.blue, 80)f_IsWilliamsFractal(_leftRange, _rightRange, _type) =>
_isFractal = _type == ‘high’ and high[_rightRange] >= ta.highest(high, _leftRange + _rightRange + 1) or _type == ‘low’ and low[_rightRange] <= ta.lowest(low, _leftRange + _rightRange + 1)
_fractalValue = _isFractal and _type == ‘high’ ? high[_rightRange] : _isFractal and _type == ‘low’ ? low[_rightRange] : na
[_isFractal, _fractalValue][isHighFractal, highFractalPrice] = f_IsWilliamsFractal(williamsLeftRange, williamsRightRange, ‘high’)
[isLowFractal, lowFractalPrice] = f_IsWilliamsFractal(williamsLeftRange, williamsRightRange, ‘low’)isHighFractal := isHighFractal[1] ? false : isHighFractal
isLowFractal := isLowFractal[1] ? false : isLowFractalhighFractalOffset = 0 – williamsRightRange
color white10 = color.new(color.white, 10)
plotshape(isHighFractal and showWilliamsFractals, title=’Shape for Williams High’, style=shape.triangledown, location=location.abovebar, color=white10, size=size.tiny, offset=highFractalOffset)
lowFractalOffset = 0 – williamsRightRange
plotshape(isLowFractal and showWilliamsFractals, title=’Shape for Williams Low’, style=shape.triangleup, location=location.belowbar, color=white10, size=size.tiny, offset=lowFractalOffset)f_addPercentBuffer(_input, _buffer, _direction) =>
_direction == ‘plus’ ? _input * (1 + _buffer / 100) : _direction == ‘minus’ ? _input * (1 – _buffer / 100) : nalowFractalPriceBuffered = f_addPercentBuffer(lowFractalPrice, williamsStopBuffer, ‘minus’)
highFractalPriceBuffered = f_addPercentBuffer(highFractalPrice, williamsStopBuffer, ‘plus’)f_persistAndReset(_trigger, _source) =>
var float _output = 0.0
_output := _trigger ? _source : _output[1]
_outputlongStopPrice = f_persistAndReset(isLowFractal, lowFractalPriceBuffered)
shortStopPrice = f_persistAndReset(isHighFractal, highFractalPriceBuffered)f_trail(_source, _trail, _direction) =>
_direction == ‘down’ and _source >= _trail[1] ? _trail : _direction == ‘up’ and _source <= _trail[1] ? _trail : _sourcevar float longStopPriceTrail = longStopPrice
var float shortStopPriceTrail = shortStopPriceshortStopPriceTrail := f_trail(shortStopPrice, shortStopPriceTrail, ‘down’)
longStopPriceTrail := f_trail(longStopPrice, longStopPriceTrail, ‘up’)f_flip(_flipInput, _longTrail, _shortTrail, _longReset, _shortReset) =>
var bool _flipLongNow = false
var bool _flipShortNow = false
var bool _isLong = true
var bool _isShort = true
float _flipLongSource = _flipInput == ‘Close’ ? close : _flipInput == ‘Wick’ ? high : na
float _flipShortSource = _flipInput == ‘Close’ ? close : _flipInput == ‘Wick’ ? low : na
_flipLongNow := _isShort[1] and _flipLongSource > _shortTrail ? true : false
_flipShortNow := _isLong[1] and _flipShortSource < _longTrail ? true : false
_flipLongNow := _flipShortNow and _flipLongNow and close > _longTrail ? true : _flipShortNow and _flipLongNow and close <= _longTrail ? false : _flipLongNow
_flipShortNow := _flipLongNow and _flipShortNow and close < _shortTrail ? true : _flipShortNow and _flipLongNow and close >= _shortTrail ? false : _flipShortNow
_isLong := _flipLongNow ? true : _flipShortNow ? false : na(_isLong[1]) ? true : _isLong[1]
_isShort := _flipShortNow ? true : _flipLongNow ? false : na(_isShort[1]) ? true : _isShort[1]
_longTrailOutput = _longTrail
_shortTrailOutput = _shortTrail
_longTrailOutput := _isLong and not _isLong[1] ? _longReset : _longTrailOutput
_shortTrailOutput := _isShort and not _isShort[1] ? _shortReset : _shortTrailOutput
float _longTrailPlot = _isLong ? _longTrailOutput : _isLong and _isShort ? _longTrailOutput : na
float _shortTrailPlot = _isShort ? _shortTrailOutput : _isLong and _isShort ? _shortTrailOutput : na
[_longTrailOutput, _shortTrailOutput, _longTrailPlot, _shortTrailPlot]f_getFlipResetWilliamsLong(_longTrail, _buffer, _isLowFractal, _rightRange) =>
_barIndexWhenLastFractalConfirmed = ta.valuewhen(_isLowFractal, bar_index, 0)
_barsSinceLastFractalConfirmed = bar_index – _barIndexWhenLastFractalConfirmed
int _barsToGoBack = _barsSinceLastFractalConfirmed + _rightRange
float _lowestLow = low
for i = 0 to _barsToGoBack by 1
_lowestLow := math.min(low[i], _lowestLow)
_lowestLow
_lowestLowAdjusted = _lowestLow * (1 – _buffer / 100)
_lowestLowAdjustedf_getFlipResetWilliamsShort(_shortTrail, _buffer, _isHighFractal, _leftRange) =>
_barIndexWhenLastFractalConfirmed = ta.valuewhen(_isHighFractal, bar_index, 0)
_barsSinceLastFractalConfirmed = bar_index – _barIndexWhenLastFractalConfirmed
int _barsToGoBack = _barsSinceLastFractalConfirmed + _leftRange
float _highestHigh = high
for i = 0 to _barsToGoBack by 1
_highestHigh := math.max(high[i], _highestHigh)
_highestHigh
_highestHighAdjusted = _highestHigh * (1 + _buffer / 100)
_highestHighAdjustedlongStopPrice := f_getFlipResetWilliamsLong(longStopPrice, williamsStopBuffer, isLowFractal, williamsRightRange)
shortStopPrice := f_getFlipResetWilliamsShort(shortStopPrice, williamsStopBuffer, isHighFractal, williamsLeftRange)[longStopPriceTrailTemp, shortStopPriceTrailTemp, longStopPriceTrailPlot, shortStopPriceTrailPlot] = f_flip(flipInput, longStopPriceTrail, shortStopPriceTrail, longStopPrice, shortStopPrice)
longStopPriceTrail := longStopPriceTrailTemp
shortStopPriceTrail := shortStopPriceTrailTempshortStopPriceTrailPlotDisplay = showWilliamsStops ? shortStopPriceTrailPlot : na
longStopPriceTrailPlotDisplay = showWilliamsStops ? longStopPriceTrailPlot : naplot(shortStopPriceTrailPlotDisplay, ‘Williams Trailing Stop High Price’, color=dnTrailColor, style=plot.style_linebr, linewidth=3)
plot(shortStopPriceTrailPlotDisplay, ‘Williams Trailing Stop High Price Highlight’, color=dnTrailColor, style=plot.style_linebr, linewidth=1)
plot(longStopPriceTrailPlotDisplay, ‘Williams Trailing Stop Low Price’, color=upTrailColor, style=plot.style_linebr, linewidth=3)
plot(longStopPriceTrailPlotDisplay, ‘Williams Trailing Stop Low Price Highlight’, color=upTrailColor, style=plot.style_linebr, linewidth=1)pricefill = plot(hl2, ‘Midline’, color=color.gray, display=display.none)
isUptrend = close < shortStopPriceTrailPlotDisplay
isDowntrend = close > longStopPriceTrailPlotDisplayfillColor = isUptrend ? color.new(fillColorDn, 85) : color.new(fillColorUp, 85)
fillPrice = isUptrend ? shortStopPriceTrailPlotDisplay : longStopPriceTrailPlotDisplayfill(plot(fillPrice, title=’Williams Trailing Stop Fill High’), pricefill, color=fillColor)
alertCrossLongStop = na(longStopPriceTrailPlot) and not na(longStopPriceTrailPlot[1])
alertCrossShortStop = na(shortStopPriceTrailPlot) and not na(shortStopPriceTrailPlot[1])
alertcondition(alertCrossLongStop, title=’Crossed Williams Long Stop’, message=’Alert from Williams Fractal Trailing Stops: \n {{ticker}} price crossed long stop’)
alertcondition(alertCrossShortStop, title=’Crossed Williams Short Stop’, message=’Alert from Williams Fractal Trailing Stops: \n {{ticker}} price crossed short stop’)alertcondition(isHighFractal, title=’High Printed’, message=’Alert from Williams Fractal Trailing Stops: \n {{ticker}} Williams High has been confirmed’)
alertcondition(isLowFractal, title=’Low Printed’, message=’Alert from Williams Fractal Trailing Stops: \n {{ticker}} Williams Low has been confirmed’)Thanks!
-
AuthorPosts
Find exclusive trading pro-tools on