Untitled

 avatar
unknown
plain_text
a month ago
2.6 kB
4
Indexable
//@version=5
strategy('RSN TREND STR', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// Length parameter
length = input.int(20, title="Length")

// Source input
src = input(close, title="Source")

// Exponent parameter with step of 0.1
powValue = input.float(2.0, title="Pow Value", step=0.1, minval=0.1, maxval=10)

// Fixed percentage change threshold (0.1%)
percentageThreshold = 0.001  // Hardcoded to 0.1%

// Bar color parameters
showBarColor = input(true, title="Bar Color")
upBarColor = input.color(color.green, title="Up Bar Color")
downBarColor = input.color(color.red, title="Down Bar Color")

//--dS--
ama = 0.0
hh = math.max(math.sign(ta.change(ta.highest(length))), 0)
ll = math.max(math.sign(ta.change(ta.lowest(length)) * -1), 0)

// Calculate tc with user-adjustable powValue
tc = math.pow(ta.sma(hh or ll ? 1 : 0, length), powValue)

// AMA calculation
ama := nz(ama[1] + tc * (src - ama[1]), src)

// Define global variable for line color
var color lineColor = na

// Variable to check uptrend or downtrend
var bool isUptrend = na

// Calculate price change percentage
priceChangePercent = (ama - ama[1]) / ama[1]

// Change line color based on price change percentage
if priceChangePercent > percentageThreshold
    if isUptrend != true
        lineColor := color.green  // Change color when transitioning from downtrend to uptrend
    isUptrend := true
else if priceChangePercent < -percentageThreshold
    if isUptrend != false
        lineColor := color.red  // Change color when transitioning from uptrend to downtrend
    isUptrend := false
else
    lineColor := lineColor[1]  // Keep the previous color during sideways movement

// Plot the AMA
plot(ama, 'Plot', color=lineColor, linewidth=2)

// Set bar colors based on trend
barcolor(showBarColor ? (isUptrend ? upBarColor : downBarColor) : na)

// Strategy entry and exit conditions
if isUptrend
    strategy.entry("Buy", strategy.long)

if not isUptrend
    strategy.close("Buy")

// Start and end date for the strategy
startDate = timestamp(2020, 1, 1, 0, 0)
endDate = timestamp(2025, 1, 1, 0, 0)
if (time < startDate) or (time > endDate)
    strategy.cancel_all()

// Take Profit based on a user-adjustable value (in steps of 0.1)
takeProfitPercent = input.float(1.0, title="Take Profit Percentage", step=0.1) / 100

// Take profit logic
takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercent)
if (strategy.position_size > 0) and (close >= takeProfitLevel)
    strategy.close("Buy", comment="Take Profit")
Editor is loading...
Leave a Comment