Merge
Merge
Merge
// Renko Calculation
if na(renkoLevel)
renkoLevel := close
brickDirection := true
brickDirection := false
MagicTrend := ta.cci(src, period) >= 0 ? (upT < nz(MagicTrend[1]) ? nz(MagicTrend[1]) : upT) : (downT > nz(MagicTrend[1]) ? nz(MagicTrend[1]) : downT)
trendColor = ta.cci(src, period) >= 0 ? #0022FC : #FC0400
// Alerts
//@version=5
strategy('Strategy tester', overlay=true, initial_capital=1000000, default_qty_type=strategy.percent_of_equity)
// Input parameters
atrLength = input(14, title='ATR Length') // ATR calculation length
atrMultiplierTP = input(10, title='ATR Multiplier (Take Profit)') // Take Profit multiplier
atrMultiplierSL = input(50, title='ATR Multiplier (Stop Loss)') // Stop Loss multiplier
// Calculate ATR
atrValue = ta.atr(atrLength)
length = 5
highestHigh = ta.highest(high, length)
lowestLow = ta.lowest(low, length)
length2 = 5
stopLossLow = ta.lowest(low, length)
stoplossHigh = ta.highest(high, length)
// Calculate EMA
ema = ta.ema(close, len1)
emaStep = request.security(syminfo.tickerid, res, ema[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
emaSmooth = request.security(syminfo.tickerid, res, ema[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_on)[barstate.isrealtime ? 0 :
1]
// Draw EMA
plot(smooth ? emaSmooth : emaStep, color=col ? (close > emaStep ? color.green : color.red) : color.black, linewidth=2, title="HTF EMA")
// banker
// CALCULATION --------------------------------------------------------------------------------------------------------{
//@variable: HMA of hl2 (high+low)/2 as the source of trend analysis
series float src1 = ta.hma(hl2, 25)
//@variable: simple moving average of high-low as a volatility measure
series float vlt = ta.sma(high - low, 100)
//@variable: array to store the moving average values over the period
var AVG = array.new<float>(period, float(na))
if close < AVG.last() // If the current close is lower than the last stored value in AVG
AVG.push(high + vlt) // Add the high price plus volatility to the array
//@variable: trend color based on the relationship between the source and the average
color trend_color = src1 > avg
? (avg == avg[1] or avg < avg[1] ? color.new(up1, 40) : up1)
: (avg == avg[1] or avg > avg[1] ? color.new(dn1, 40) : dn1)
// PLOTTING -----------------------------------------------------------------------------------------------------------{
// Displaying a final label indicating the trend direction if not showing deviation
if barstate.islast and not show_dev
label.delete(label.new(bar_index + 5, 0,
close > avg ? "Trend Up" : "Trend Down",
style = label.style_label_left,
color = color(na),
textcolor = chart.fg_color)[1]
)
// signals
//}
//functions
xrf(values, length) =>
r_val = float(na)
if length >= 1
for i = 0 to length by 1
if na(r_val) or not na(values[i])
r_val := values[i]
r_val
r_val
xsa(src,len,wei) =>
sumf = 0.0
ma = 0.0
out = 0.0
sumf := nz(sumf[1]) - nz(src[len]) + src
ma := na(src[len]) ? na : sumf/len
out := na(out[1]) ? ma : (src*wei+out[1]*(len-wei))/len
out
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
smoothrng
smrng1 = smoothrng(source, per1, mult1)
smrng2 = smoothrng(source, per2, mult2)
smrng = (smrng1 + smrng2) / 2
// Range Filter
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
rngfilt
filt = rngfilt(source, smrng)
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
longCond = bool(na)
shortCond = bool(na)
longCond := source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0
shortCond := source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
// Plotting
if ta.crossunder(fundtrend, 90)
strategy.close("long")
if ta.crossover(fundtrend, 10)
strategy.close("short")
// Stop-loss level
var float stop_loss_price = na
if not na(longEntryPrice) and strategy.position_size > 0
// Initial stop-loss at a % below the entry price
// Move stop-loss to breakeven once 1% equity gain is reached
if close >= target_price
stop_loss_price := longEntryPrice
//strategy.exit("long", stop= stop_loss_price, comment_loss="lost stop long")
// shortEntryPrice := close
// stop_loss_in_ticks = (atrValue*atrMultiplierSL) / syminfo.mintick
// if stoplossHigh > high[1]
// stop_loss_in_ticks = (stoplossHigh - close) /syminfo.mintick
//take_profit_in_ticks = (atrValue*atrMultiplierTP)/syminfo.mintick
//strategy.order("short", strategy.short, qty=strategy.initial_capital * 0.01)
//strategy.exit("short", loss = stop_loss_in_ticks, comment_loss = "stoploss", profit = take_profit_in_ticks, comment_profit =
"takeprofit")
// Parameters
// Number of candles to look back