Gaints

Gaints

//@version=5
strategy("TESTING", overlay=true)

// Input settings
source = input(defval=close, title='Source')
per1 = input.int(defval=27, minval=1, title='Fast period')
mult1 = input.float(defval=1.6, minval=0.1, title='Fast range')
per2 = input.int(defval=55, minval=1, title='Slow period')
mult2 = input.float(defval=2, minval=0.1, title='Slow range')
skipThresholdPercent = input.float(defval=1.3, title='Skip Threshold (%)')
stopLossPercent = input.float(defval=1.0, minval=0.0, step=0.1, title='Stop Loss Percentage')

// Smooth range calculation
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x ), t)
ta.ema(avrng, wper) * m

smrng1 = smoothrng(source, per1, mult1)
smrng2 = smoothrng(source, per2, mult2)
smrng = (smrng1 + smrng2) / 2

// Range filtering
rngfilt(x, r) =>
var float rngfilt = na
rngfilt := x > nz(rngfilt ) ? x - r < nz(rngfilt ) ? nz(rngfilt ) : x - r : x + r > nz(rngfilt ) ? nz(rngfilt ) : x + r
rngfilt

filt = rngfilt(source, smrng)

// Trend detection
STR = filt + smrng
STS = filt - smrng

FUB = 0.0
FUB := STR < nz(FUB ) or close > nz(FUB ) ? STR : nz(FUB )
FLB = 0.0
FLB := STS > nz(FLB ) or close < nz(FLB ) ? STS : nz(FLB )
TRF = 0.0
TRF := nz(TRF ) == FUB and close = FUB ? FLB : nz(TRF ) == FLB and close >= FLB ? FLB : nz(TRF ) == FLB and close threshold)
label.new(bar_index, close, "Skipped Sell Entry", color=color.red, textcolor=color.white, style=label.style_label_up)
else
strategy.entry("Sell", strategy.short)
inShortPosition := true

if (longCondition)
if (distanceToTRF > threshold)
label.new(bar_index, close, "Skipped Buy Entry", color=color.red, textcolor=color.white, style=label.style_label_down)
else
strategy.entry("Buy", strategy.long)
inShortPosition := false // Exit short position when entering long

// Set Stop Loss and Take Profit
if (strategy.position_size > 0)
stopLossLong = close * (1 - stopLossPercent / 100)
takeProfitLong = close * (1 + stopLossPercent / 100)
strategy.exit("Sell on Stop Loss/TP", from_entry="Buy", stop=stopLossLong, limit=takeProfitLong)

if (strategy.position_size < 0)
stopLossShort = close * (1 + stopLossPercent / 100)
takeProfitShort = close * (1 - stopLossPercent / 100)
strategy.exit("Cover on Stop Loss/TP", from_entry="Sell", stop=stopLossShort, limit=takeProfitShort)

// Exit short if a long signal is skipped
if (inShortPosition and longCondition and distanceToTRF > threshold)
strategy.close("Sell") // Close the short position at market price

// Plotting signals
plotshape(longCondition and distanceToTRF

Read More

Share:

Latest News