#// Indicator for TOS
#// © fyntrade
#indicator("IBD PowerTrend", overlay=false)
# Converted by Sam4Cok@Samer800 - 07/2024 - request from useThinkScript.com member
Declare lower;
input source = close;
input fastMovAvg = 21;
input slowMovAvg = 50;
input fastMovAvgType = AverageType.EXPONENTIAL;
input slowMovAvgType = AverageType.SIMPLE;
def na = Double.NaN;
def last = isNaN(close);
#// Calculate moving averages
def ema21 = MovingAverage(fastMovAvgType, source, fastMovAvg);
def sma50 = MovingAverage(slowMovAvgType, source, slowMovAvg);
#// Conditions for starting a power trend
#// Condition 1: The low is above the 21-day EMA for at least 10 days
def condition1 = lowest(low, 10) >= lowest(ema21, 10);
#// Condition 2: The 21-day EMA is above the 50-day SMA for at least five days
def barssince = if ema21 < sma50 then 0 else barssince[1] + 1;
def condition2 = barssince >= 5;
#// Condition 3: The 50-day SMA is in an uptrend (one day is sufficient)
def condition3 = sma50 > sma50[1];
#// Condition 4: The market closes up for the day
def condition4 = close > close[1];
#// Combine all conditions
def start_trend = condition1 and condition2 and condition3 and condition4;
#// End power trend conditions
def end_condition1 = (sma50 > ema21) and (sma50[1] <= ema21[1]);
def end_condition2 = close < (highest(high, 63) * 0.9) and close < sma50;
def end_trend = end_condition1 or end_condition2;
#// Maintain trend status
def trend = if (start_trend) then yes else
if (end_trend) then no else trend[1];
#// Plot a line in a separate pane to indicate the power trend status
plot TrendLine = if trend then 0 else na;
plot zeroLine = if last then na else 0; # "PowerTrend Status"
TrendLine.SetLineWeight(3);
TrendLine.SetDefaultColor(Color.GREEN);
TrendLine.SetPaintingStrategy(PaintingStrategy.POINTS);
zeroLine.SetDefaultColor(Color.DARK_GRAY);
#-- END of CODE