Repaints Stan Weinstein Trend Indicator For ThinkOrSwim

Repaints
@samer800 -- Can you please help me with the following TV indicator to TOS?

https://www.tradingview.com/v/pMShjARo/

Code:
//@version=5
indicator(title="Stan Weinstein Trend Indicator",overlay=true, timeframe="W")

// Get the ticker symbol of BTCUSD index
i_sym = input.symbol("INDEX:BTCUSD", "Symbol for Relative Strength")

// Retrieve the historical close prices of BTCUSD using request.security
btcusd_close = request.security(i_sym, 'W', close)

// Calculate the ratio between the current chart and BTCUSD
ratio = close / btcusd_close

// Calculate the difference between today's ratio and yesterday's ratio
diff = ratio - ratio[1]

// Calculate the percentage variation
force_relative = (diff / ratio[1]) * 100

//Initialisation des variables
average_volume_8_period = input.float(8, "Period for SMA Volume", 1, 100, 1)
average_volume_8 = ta.ema(volume, int(average_volume_8_period))
volume_condition1 = volume <= average_volume_8

m30_period = input.float(21,"Period for EMA", 1, 100, 1)
m30 = ta.ema(close, int(m30_period))

//Calcul du RSI
rsi_period = input.float(14, "Period for RSI", 1, 100, 1)
rsi = ta.rsi(close, int(rsi_period))
rsi_condition1 = rsi < 50

//Phase 1: Consolidation
tunnel_condition = (high - low) / high <= 0.1
consolidation = volume_condition1 and rsi_condition1 and tunnel_condition

// Initialisation des variables pour phase 2
m30_condition2 = close > m30
volume_condition2 = volume > average_volume_8
resistance_period = input.float(8, "Period for Resistance", 1, 100, 1)
resistance = ta.highest(close, int(resistance_period))
resistance_condition = close >= resistance
m30_trend_condition_phase2 = m30 > m30[1]

// Phase 2: Avancée
avancee = m30_condition2 and m30_trend_condition_phase2 and volume_condition2 and force_relative > 0 and resistance_condition

//Définition des conditions pour l'EMA21
rsi_condition3 = rsi > 50

// Initialisation des variables pour phase 3
proximity_condition = math.abs(close - m30) / close <= 0.05

// Phase 3: Plafonnement
ema_trend = input.float(10, "Period for EMA trend", 1, 100, 1)
plafonnement = rsi_condition3 and proximity_condition and close <= ta.ema(close, int(ema_trend))

// Initialisation des variables pour phase 4
support_period = input.float(8,"Period for Support", 1, 100, 1)
support = ta.lowest(close, int(support_period))
support_condition = close <= support
m30_trend_condition_phase4 = m30 < m30[1]
volume_condition4 = volume > average_volume_8

// Phase 4: Declin
declin = close < m30 and m30_trend_condition_phase4 and volume_condition4 and force_relative < 0 and support_condition

plotshape(consolidation, style = shape.circle, location = location.belowbar, color = color.white, size = size.small)
plotshape(avancee, style = shape.circle, location = location.belowbar, color = color.green, size = size.small)
plotshape(plafonnement, style = shape.circle, location = location.abovebar, color = color.blue, size = size.small)
plotshape(declin, style = shape.circle, location = location.abovebar, color = color.red, size = size.small)
check the below. Select the relative strength symbol from setting as desire.

CSS:
# https://www.tradingview.com/v/pMShjARo/
#//@Ecipi
#indicator(title="Stan Weinstein Trend Indicator",overlay=true, timeframe="W")
# Converted and mod By Sam4Cok@Samer800    - 11/2023
#// Get the ticker symbol of BTCUSD index
input chartTimeframe = AggregationPeriod.HOUR;
input SymbolForRelativeStrength = "/BTC";   # "Symbol for Relative Strength"
input symbolTimeframe = {DAY,Default WEEK, MONTH};
input periodForAverageVolume = 8;           # "Period for SMA Volume"
input periodForEma = 21;                    # "Period for EMA"
input periodForRsi = 14;                    # "Period for RSI"
input periodForEmaTrend = 10;               # "Period for EMA trend"
input resistancePeriod = 8;                 # "Period for Resistance"
input supportPeriod = 8;                    # "Period for Support"

def na = Double.NaN;
def c = close(Period = chartTimeframe);
def h = high(Period = chartTimeframe);
def l = low(Period = chartTimeframe);
def v = volume(Period = chartTimeframe);
#// Retrieve the historical close prices of BTCUSD using request.security
def btcusd_close = close("Symbol" = SymbolForRelativeStrength, Period = symbolTimeframe);

#// Calculate the ratio between the current chart and BTCUSD
def ratio = c / btcusd_close;

#// Calculate the difference between today's ratio and yesterday's ratio
def diff = ratio - ratio[1];

#// Calculate the percentage variation
def force_relative = (diff / ratio[1]) * 100;

#//Initialisation des variables

def average_volume_8 = ExpAverage(v, periodForAverageVolume);
def volume_condition1 = v <= average_volume_8;
def m30 = ExpAverage(c, periodForEma);

#//Calcul du RSI
def rsi = RSI(Price = c, Length = periodForRsi);
def rsi_condition1 = rsi < 50;

#//Phase 1: Consolidation
def tunnel_condition = (high - low) / high <= 0.1;
def consolidation = volume_condition1 and rsi_condition1 and tunnel_condition;

#// Initialisation des variables pour phase 2
def m30_condition2 = c > m30;
def volume_condition2 = v > average_volume_8;
def resistance = Highest(c, resistancePeriod);
def resistance_condition = c >= resistance;
def m30_trend_condition_phase2 = m30 > m30[1];

#// Phase 2: Avancée
def avancee = m30_condition2 and m30_trend_condition_phase2 and volume_condition2 and force_relative > 0 and resistance_condition;

#//Définition des conditions pour l'EMA21
def rsi_condition3 = rsi > 50;
#// Initialisation des variables pour phase 3
def proximity_condition = AbsValue(c - m30) / c <= 0.05;

#// Phase 3: Plafonnement
def emaTrend = ExpAverage(c, periodForEmaTrend);
def plafonnement = rsi_condition3 and proximity_condition and c <= emaTrend;

#// Initialisation des variables pour phase 4
def support = lowest(c, supportPeriod);
def support_condition = c <= support;
def m30_trend_condition_phase4 = m30 < m30[1];
def volume_condition4 = v > average_volume_8;

#// Phase 4: Declin
def declin = c < m30 and m30_trend_condition_phase4 and volume_condition4 and force_relative < 0 and support_condition;

# plots

plot cons = if consolidation and !consolidation[1] then low else na;
plot avan = if avancee and !avancee[1] then low else na;
plot plafon = if plafonnement and !plafonnement[1] then high else na;
plot dec = if declin and !declin[1] then high else na;

cons.SetPaintingStrategy(paintingStrategy.BOOLEAN_WEDGE_DOWN);
avan.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
plafon.SetPaintingStrategy(paintingStrategy.BOOLEAN_WEDGE_UP);
dec.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);

cons.SetDefaultColor(Color.LIME);
avan.SetDefaultColor(Color.GREEN);
plafon.SetDefaultColor(Color.PLUM);
dec.SetDefaultColor(Color.MAGENTA);

#END of CODE
 
This worked perfectly. I changed the default symbol to SPX and adjusted a few things to fit my needs.

Thank you, @samer800, for a quick turnaround and yet another quality code.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
333 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top