actual code for RSI using Wilder average NOT the functions

rloftinbiz

New member
I want to recreate ThinkorSwim’s RSI function using AverageType.WILDERS on a different platform that matches ThinkorSwim’s results. As far as I can tell the close prices of both match historically for any instrument.

Current TOS code:
input RSI_len = 2;
input averageType = AverageType.WILDERS;
def RSI = RSI(length = RSI_len, averageType = averageType);

I can find pieces of the formula/code in TOS. From what I can tell TOS uses a look back period of 7 days for AverageType.WILDERS with ‘1’ as the dividend for Wilder in place of ‘2’ for a standard EMA.

Quotes from learning Center: --------------------------------------------------------------------------------------
“WildersSmoothing
Description
The Wilder's Smoothing study is similar to the Exponential Moving Average with the difference that Wilder's Smoothing uses a smoothing factor of 1/length which makes it respond more slowly to price changes compared to other moving averages.” --------------------
“MovAvgExponential
Description
The Exponential Moving Average (EMA) is a type of moving average that assigns greater weight to the most recent price data. Unlike the simple moving average where all data points have the same weight, the EMA's weighting factors to price data decrease exponentially.
The recursive representation of the EMA formula is the following:
EMA1 = price1;
EMA2 = α*price2 + (1 - α)*EMA1;
EMA3 = α*price3 + (1 - α)*EMA2;
EMAN = α*priceN + (1 - α)*EMAN-1;
where:
• N is the bar number;
• α is a smoothing coefficient equal to 2/(length + 1).”

-------------------------------------------------------------------------------------------------------------------
The is a simple scripting language from ProRealTime and my code to duplicate TOS:
‘[…]’ brackets indicate previous days, increasing numbers increase backwards in days.
-------------------------------------------------------------------------------------------------------------------
price1 = close
LookBack = 7

n0 = price1[0] - price1[1] //1st iteration today minus yesterday's price
if n0 > 0 then
u0 = n0
elsif n0 < 0 then
d0 = n0 * -1
else
u0 = 0
d0 = 0
endif

n1 = price1[1] - price1[2] //2nd iteration yesterday minus day-before-yesterday's price
if n1 > 0 then
u1 = n1
elsif n1 < 0 then
d1 = n1 * -1
else
u1 = 0
d1 = 0
endif

AvgPD = (u0 + u1) / 2 // simple average two-day gain
AvgND = (d0 + d1) / 2 // simple average two-day loss

a = 1/length + 1

// subtract yesterday from today for positive and negative price changes.
for i = 0 to LookBack do
diff = price1[length + i] - price1[length + 1 + i]
if diff > 0 then
diffP = diff
elsif diff < 0 then
diffN = diff * -1 // absolute value
else
diffP = 0
diffN = 0
endif
if i = 0 then // first averaging step
AvgP = AvgPD
AvgN = AvgND
else // subsequent averaging steps
AvgP = a * diffP + (1 - a) * AvgP
AvgN = a * diffN + (1 - a) * AvgN
endif
next

RS = AvgP/AvgN
xRSI = 100-(100/(1+RS))
return xRSI


Unfortunately, the above code does not work. Adjusting the ‘LookBack’ period seems to shift the entire calculation in time with zero ‘LookBack’ (meaning no look-back at all) the closet to TSO. I don’t think what I’m doing is near correct. Unsure why it is so difficult for find out the math or code used. Any help is sincerely appreciated.
 
Solution
I want to recreate ThinkorSwim’s RSI function
Unsure why it is so difficult for find out the math or code used.

Under the study tab, click on the RSI indicator, voilà
There is the code.
ny5frDF.png
If you really want to get down to the nuts and bolts of the RSI indicator, I suggest you buy and study the original source of the RSI, J. Welles Wilder's "New Concepts in Technical Trading Systems (1978). This book and its studies were the original calculations for many studies still used 50 years later. And they were created long before computers were prevalent in many aspects of life, especially trading/investing. He spells out the calculations in simple steps which can be programmed into any system (although all of them are built into all trading platforms now). I have actually programmed most of his indicators into MS Excel, TOS and other platforms. I have done this so I can actually see what is going on inside the box. I suggest that anyone that wants to gain experience try to code more indicators manually. IMHO, Scott
 
I want to recreate ThinkorSwim’s RSI function using AverageType.WILDERS on a different platform that matches ThinkorSwim’s results. As far as I can tell the close prices of both match historically for any instrument.

Current TOS code:
input RSI_len = 2;
input averageType = AverageType.WILDERS;
def RSI = RSI(length = RSI_len, averageType = averageType);

I can find pieces of the formula/code in TOS. From what I can tell TOS uses a look back period of 7 days for AverageType.WILDERS with ‘1’ as the dividend for Wilder in place of ‘2’ for a standard EMA.

Quotes from learning Center: --------------------------------------------------------------------------------------
“WildersSmoothing
Description
The Wilder's Smoothing study is similar to the Exponential Moving Average with the difference that Wilder's Smoothing uses a smoothing factor of 1/length which makes it respond more slowly to price changes compared to other moving averages.” --------------------
“MovAvgExponential
Description
The Exponential Moving Average (EMA) is a type of moving average that assigns greater weight to the most recent price data. Unlike the simple moving average where all data points have the same weight, the EMA's weighting factors to price data decrease exponentially.
The recursive representation of the EMA formula is the following:
EMA1 = price1;
EMA2 = α*price2 + (1 - α)*EMA1;
EMA3 = α*price3 + (1 - α)*EMA2;
EMAN = α*priceN + (1 - α)*EMAN-1;
where:
• N is the bar number;
• α is a smoothing coefficient equal to 2/(length + 1).”

-------------------------------------------------------------------------------------------------------------------
The is a simple scripting language from ProRealTime and my code to duplicate TOS:
‘[…]’ brackets indicate previous days, increasing numbers increase backwards in days.
-------------------------------------------------------------------------------------------------------------------
price1 = close
LookBack = 7

n0 = price1[0] - price1[1] //1st iteration today minus yesterday's price
if n0 > 0 then
u0 = n0
elsif n0 < 0 then
d0 = n0 * -1
else
u0 = 0
d0 = 0
endif

n1 = price1[1] - price1[2] //2nd iteration yesterday minus day-before-yesterday's price
if n1 > 0 then
u1 = n1
elsif n1 < 0 then
d1 = n1 * -1
else
u1 = 0
d1 = 0
endif

AvgPD = (u0 + u1) / 2 // simple average two-day gain
AvgND = (d0 + d1) / 2 // simple average two-day loss

a = 1/length + 1

// subtract yesterday from today for positive and negative price changes.
for i = 0 to LookBack do
diff = price1[length + i] - price1[length + 1 + i]
if diff > 0 then
diffP = diff
elsif diff < 0 then
diffN = diff * -1 // absolute value
else
diffP = 0
diffN = 0
endif
if i = 0 then // first averaging step
AvgP = AvgPD
AvgN = AvgND
else // subsequent averaging steps
AvgP = a * diffP + (1 - a) * AvgP
AvgN = a * diffN + (1 - a) * AvgN
endif
next

RS = AvgP/AvgN
xRSI = 100-(100/(1+RS))
return xRSI


Unfortunately, the above code does not work. Adjusting the ‘LookBack’ period seems to shift the entire calculation in time with zero ‘LookBack’ (meaning no look-back at all) the closet to TSO. I don’t think what I’m doing is near correct. Unsure why it is so difficult for find out the math or code used. Any help is sincerely appreciated.
check below code if it makes any help

CSS:
input length = 14;
input price1 = close;

def n0 = price1[0] - price1[1];
def u0 = if n0>0 then n0 else
         if n0<0 then -n0 else 0;
def a = 1 / length;
def NetChgAvg = a * n0 + (1 - a) * NetChgAvg[1];
def TotChgAvg = a * u0 + (1 - a) * TotChgAvg[1];
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
 

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
497 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