NexusFi: Find Your Edge


Home Menu

 





Script to add a moving average


Discussion in ThinkOrSwim

Updated
    1. trending_up 2,412 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 1 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
TradingForALiving
Miami Florida US
 
Posts: 16 since Oct 2015
Thanks Given: 6
Thanks Received: 1

Can someone please show me how to add a 50 bar simple moving average to the following relative strength script that is found in TOS:


declare lower;

input CorrelationWithSecurity = "SPX";
def close2 = close(CorrelationWithSecurity);

plot RS = if close2 == 0 then 0 else close/close2;
RS.setDefaultColor(GetColor(6));

def sr = CompoundValue("historical data" = RS, "visible data" = if isNaN(sr[1]) then RS else sr[1]);
plot SRatio = sr;
SRatio.setDefaultColor(GetColor(5));


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Thanks Mike. Godspeed.
The Elite Circle
May 26 Deadline Dies at 7.5% as US Strikes Iran During C …
Prediction Markets & Event Contracts
ATFX Suspends Prop Trading Unit ATFunded -- Full Review …
Funded Trading Evaluation Firms
More Than Capable: Hegseths War Warning Validates $114M …
Prediction Markets & Event Contracts
Iran Deal "In Review" at 87% on Deadline Day - …
Prediction Markets & Event Contracts
 
  #2 (permalink)
 
TrendFollower1's Avatar
 TrendFollower1 
Austin, TX
 
Experience: Advanced
Platform: ThinkOrSwim
Trading: Currency, index, commodity futures
Posts: 9 since Mar 2012
Thanks Given: 32
Thanks Received: 8

Hi, this seems to work. You can adjust the length and type of moving average if needed. The additional code is shown in bold.

Best of luck to you.

# RelativeStrength_with_MA

declare lower;

input length = 50;
input averageType = AverageType.Simple;
input CorrelationWithSecurity = "SPX";
def close2 = close(CorrelationWithSecurity);

plot RS = if close2 == 0 then 0 else close/close2;
plot RSavg = MovingAverage(averageType, RS, length);

RS.setDefaultColor(GetColor(6));
RSavg.setDefaultColor(Color.RED);

def sr = CompoundValue("historical data" = RS, "visible data" = if isNaN(sr[1]) then RS else sr[1]);
plot SRatio = sr;
SRatio.setDefaultColor(GetColor(5));


Reply With Quote
Thanked by:




Last Updated on July 27, 2021


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts