EMA clouds Lags too much!!!!!

raj999

New member
Hi everyone,As we know, TradingView's regular session closes at 20:00 EST and reopens at 04:00 EST the next day, while Thinkorswim charts run 24 hours (including the overnight session).I tried modifying the Ripster EMA Clouds script to exclude the overnight session when calculating the EMAs, so they align perfectly with the standard Ripster Clouds on TradingView (which only use RTH +EXT data).However, after applying the modified script on Thinkorswim, the chart lags badly — it keeps loading data for a long time and sometimes takes minutes before the chart finally appears. This is making me a bit worried.
can anyone optimized this code so that it can run with out any lags. here is the code that need modification or optimization?
Code:
# Session-based EMA Clouds
#ORIGINAL RIPSTER_EMA_CLOUDS
#MODIFY BY RAZOO TO ALLIGN WITH TRADING_VIEW EMA CLOUDS
input displace = 0;
def price = hl2;
# Session handling
def seconds = SecondsFromTime(0);
def isOvernight = seconds >= 20*60*60 or seconds < 4*60*60; # 8 PM to 4 AM
def isRegularSession = !isOvernight;

# EMA periods for clouds
input show_cloud1=yes;
input ema1low = 5;
input ema1high = 12;

input show_cloud2=yes;
input ema2low = 8;
input ema2high = 9;

input show_cloud3=yes;
input ema3low = 34;
input ema3high = 50;

input show_cloud4=yes;
input ema6low = 20;
input ema6high = 21;

input show_cloud5=yes;
input ema4low = 120;
input ema4high = 150;

input show_cloud6=yes;
input ema5low = 180;
input ema5high = 200;

# Calculate session-based EMAs using hl2
rec ema5 = if IsNaN(ema5[1]) then ExpAverage(hl2, ema1low) else
if isRegularSession then (2 * hl2 + (ema1low - 1) * ema5[1]) / (ema1low + 1)
else ema5[1];

rec ema13 = if IsNaN(ema13[1]) then ExpAverage(hl2, ema1high) else
if isRegularSession then (2 * hl2 + (ema1high - 1) * ema13[1]) / (ema1high + 1)
else ema13[1];

rec ema8 = if IsNaN(ema8[1]) then ExpAverage(hl2, ema2low) else
if isRegularSession then (2 * hl2 + (ema2low - 1) * ema8[1]) / (ema2low + 1)
else ema8[1];

rec ema9 = if IsNaN(ema9[1]) then ExpAverage(hl2, ema2high) else
if isRegularSession then (2 * hl2 + (ema2high - 1) * ema9[1]) / (ema2high + 1)
else ema9[1];

rec ema34 = if IsNaN(ema34[1]) then ExpAverage(hl2, ema3low) else
if isRegularSession then (2 * hl2 + (ema3low - 1) * ema34[1]) / (ema3low + 1)
else ema34[1];

rec ema50 = if IsNaN(ema50[1]) then ExpAverage(hl2, ema3high) else
if isRegularSession then (2 * hl2 + (ema3high - 1) * ema50[1]) / (ema3high + 1)
else ema50[1];

rec ema20 = if IsNaN(ema20[1]) then ExpAverage(hl2, ema6low) else
if isRegularSession then (2 * hl2 + (ema6low - 1) * ema20[1]) / (ema6low + 1)
else ema20[1];

rec ema21 = if IsNaN(ema21[1]) then ExpAverage(hl2, ema6high) else
if isRegularSession then (2 * hl2 + (ema6high - 1) * ema21[1]) / (ema6high + 1)
else ema21[1];

rec ema120 = if IsNaN(ema120[1]) then ExpAverage(hl2, ema4low) else
if isRegularSession then (2 * hl2 + (ema4low - 1) * ema120[1]) / (ema4low + 1)
else ema120[1];

rec ema150 = if IsNaN(ema150[1]) then ExpAverage(hl2, ema4high) else
if isRegularSession then (2 * hl2 + (ema4high - 1) * ema150[1]) / (ema4high + 1)
else ema150[1];

rec ema180 = if IsNaN(ema180[1]) then ExpAverage(hl2, ema5low) else
if isRegularSession then (2 * hl2 + (ema5low - 1) * ema180[1]) / (ema5low + 1)
else ema180[1];

rec ema200 = if IsNaN(ema200[1]) then ExpAverage(hl2, ema5high) else
if isRegularSession then (2 * hl2 + (ema5high - 1) * ema200[1]) / (ema5high + 1)
else ema200[1];

# Plot displaced EMAs
plot AvgExp5 = ema5[-displace];
plot AvgExp13 = ema13[-displace];
plot AvgExp8 = ema8[-displace];
plot AvgExp9 = ema9[-displace];
plot AvgExp34 = ema34[-displace];
plot AvgExp50 = ema50[-displace];
plot AvgExp20 = ema20[-displace];
plot AvgExp21 = ema21[-displace];
plot AvgExp120 = ema120[-displace];
plot AvgExp150 = ema150[-displace];
plot AvgExp180 = ema180[-displace];
plot AvgExp200 = ema200[-displace];



# Add clouds using plotted EMAs with conditional display
AddCloud(if show_cloud1 then AvgExp5 else Double.NaN, if show_cloud1 then AvgExp13 else Double.NaN, Color.GREEN, Color.RED, no);
AddCloud(if show_cloud2 then AvgExp8 else Double.NaN, if show_cloud2 then AvgExp9 else Double.NaN, Color.Green, Color.Red, no);
AddCloud(if show_cloud3 then AvgExp34 else Double.NaN, if show_cloud3 then AvgExp50 else Double.NaN, Color.Cyan, Color.Orange, no);
AddCloud(if show_cloud4 then AvgExp20 else Double.NaN, if show_cloud4 then AvgExp21 else Double.NaN, Color.RED, Color.RED, no);
AddCloud(if show_cloud5 then AvgExp120 else Double.NaN, if show_cloud5 then AvgExp150 else Double.NaN, Color.LIGHT_ORANGE, Color.PLUM, no);
AddCloud(if show_cloud6 then AvgExp180 else Double.NaN, if show_cloud6 then AvgExp200 else Double.NaN, Color.CYAN, Color.Pink, no);

Thank you in advance!!!
 
Last edited by a moderator:
Hi everyone,As we know, TradingView's regular session closes at 20:00 EST and reopens at 04:00 EST the next day, while Thinkorswim charts run 24 hours (including the overnight session).I tried modifying the Ripster EMA Clouds script to exclude the overnight session when calculating the EMAs, so they align perfectly with the standard Ripster Clouds on TradingView (which only use RTH +EXT data).However, after applying the modified script on Thinkorswim, the chart lags badly — it keeps loading data for a long time and sometimes takes minutes before the chart finally appears. This is making me a bit worried.
can anyone optimized this code so that it can run with out any lags. here is the code that need modification or optimization?
Code:
# Session-based EMA Clouds
#ORIGINAL RIPSTER_EMA_CLOUDS
#MODIFY BY RAZOO TO ALLIGN WITH TRADING_VIEW EMA CLOUDS
input displace = 0;
def price = hl2;
# Session handling
def seconds = SecondsFromTime(0);
def isOvernight = seconds >= 20*60*60 or seconds < 4*60*60; # 8 PM to 4 AM
def isRegularSession = !isOvernight;

# EMA periods for clouds
input show_cloud1=yes;
input ema1low = 5;
input ema1high = 12;

input show_cloud2=yes;
input ema2low = 8;
input ema2high = 9;

input show_cloud3=yes;
input ema3low = 34;
input ema3high = 50;

input show_cloud4=yes;
input ema6low = 20;
input ema6high = 21;

input show_cloud5=yes;
input ema4low = 120;
input ema4high = 150;

input show_cloud6=yes;
input ema5low = 180;
input ema5high = 200;

# Calculate session-based EMAs using hl2
rec ema5 = if IsNaN(ema5[1]) then ExpAverage(hl2, ema1low) else
if isRegularSession then (2 * hl2 + (ema1low - 1) * ema5[1]) / (ema1low + 1)
else ema5[1];

rec ema13 = if IsNaN(ema13[1]) then ExpAverage(hl2, ema1high) else
if isRegularSession then (2 * hl2 + (ema1high - 1) * ema13[1]) / (ema1high + 1)
else ema13[1];

rec ema8 = if IsNaN(ema8[1]) then ExpAverage(hl2, ema2low) else
if isRegularSession then (2 * hl2 + (ema2low - 1) * ema8[1]) / (ema2low + 1)
else ema8[1];

rec ema9 = if IsNaN(ema9[1]) then ExpAverage(hl2, ema2high) else
if isRegularSession then (2 * hl2 + (ema2high - 1) * ema9[1]) / (ema2high + 1)
else ema9[1];

rec ema34 = if IsNaN(ema34[1]) then ExpAverage(hl2, ema3low) else
if isRegularSession then (2 * hl2 + (ema3low - 1) * ema34[1]) / (ema3low + 1)
else ema34[1];

rec ema50 = if IsNaN(ema50[1]) then ExpAverage(hl2, ema3high) else
if isRegularSession then (2 * hl2 + (ema3high - 1) * ema50[1]) / (ema3high + 1)
else ema50[1];

rec ema20 = if IsNaN(ema20[1]) then ExpAverage(hl2, ema6low) else
if isRegularSession then (2 * hl2 + (ema6low - 1) * ema20[1]) / (ema6low + 1)
else ema20[1];

rec ema21 = if IsNaN(ema21[1]) then ExpAverage(hl2, ema6high) else
if isRegularSession then (2 * hl2 + (ema6high - 1) * ema21[1]) / (ema6high + 1)
else ema21[1];

rec ema120 = if IsNaN(ema120[1]) then ExpAverage(hl2, ema4low) else
if isRegularSession then (2 * hl2 + (ema4low - 1) * ema120[1]) / (ema4low + 1)
else ema120[1];

rec ema150 = if IsNaN(ema150[1]) then ExpAverage(hl2, ema4high) else
if isRegularSession then (2 * hl2 + (ema4high - 1) * ema150[1]) / (ema4high + 1)
else ema150[1];

rec ema180 = if IsNaN(ema180[1]) then ExpAverage(hl2, ema5low) else
if isRegularSession then (2 * hl2 + (ema5low - 1) * ema180[1]) / (ema5low + 1)
else ema180[1];

rec ema200 = if IsNaN(ema200[1]) then ExpAverage(hl2, ema5high) else
if isRegularSession then (2 * hl2 + (ema5high - 1) * ema200[1]) / (ema5high + 1)
else ema200[1];

# Plot displaced EMAs
plot AvgExp5 = ema5[-displace];
plot AvgExp13 = ema13[-displace];
plot AvgExp8 = ema8[-displace];
plot AvgExp9 = ema9[-displace];
plot AvgExp34 = ema34[-displace];
plot AvgExp50 = ema50[-displace];
plot AvgExp20 = ema20[-displace];
plot AvgExp21 = ema21[-displace];
plot AvgExp120 = ema120[-displace];
plot AvgExp150 = ema150[-displace];
plot AvgExp180 = ema180[-displace];
plot AvgExp200 = ema200[-displace];



# Add clouds using plotted EMAs with conditional display
AddCloud(if show_cloud1 then AvgExp5 else Double.NaN, if show_cloud1 then AvgExp13 else Double.NaN, Color.GREEN, Color.RED, no);
AddCloud(if show_cloud2 then AvgExp8 else Double.NaN, if show_cloud2 then AvgExp9 else Double.NaN, Color.Green, Color.Red, no);
AddCloud(if show_cloud3 then AvgExp34 else Double.NaN, if show_cloud3 then AvgExp50 else Double.NaN, Color.Cyan, Color.Orange, no);
AddCloud(if show_cloud4 then AvgExp20 else Double.NaN, if show_cloud4 then AvgExp21 else Double.NaN, Color.RED, Color.RED, no);
AddCloud(if show_cloud5 then AvgExp120 else Double.NaN, if show_cloud5 then AvgExp150 else Double.NaN, Color.LIGHT_ORANGE, Color.PLUM, no);
AddCloud(if show_cloud6 then AvgExp180 else Double.NaN, if show_cloud6 then AvgExp200 else Double.NaN, Color.CYAN, Color.Pink, no);

Thank you in advance!!!


untested ideas,

to speed up code,
..reduce the quanity of external function calls (double.nan, open, high, low, close, )
..restructure long if thens


instead of 12 if thens,
have 1 big if then, with 3 segments, for the 3 possible outcomes of the if thens.

you don't need 2 if thens in the cloud functions. both use the same condition. if just 1 price parameter is nan, then the cloud won't draw. i tend to use an if then for the top price and the desired value for the bottom price.


this is true on barnumber 1
IsNaN(ema5[1])
so
if barnumber() == 1 then
x = ExpAverage(hl2, ema1low)


---------------------------------------

an example with partial code


def na = double.nan;
def bn = barnumber();
def ema5;
def ema13;
def ema8;
def ema9;

(def the other 8 vars )

if bn == 1 then {

ema5 = ExpAverage(hl2, ema1low);
ema13 = ExpAverage(hl2, ema1high);
ema8 = ExpAverage(hl2, ema2low);
ema9 = ExpAverage(hl2, ema2high);

(the other 8 vars)

} else if isRegularSession then {

ema5 = (2 * hl2 + (ema1low - 1) * ema5[1]) / (ema1low + 1);
ema13 = (2 * hl2 + (ema2low - 1) * ema8[1]) / (ema2low + 1);
ema8 = (2 * hl2 + (ema2low - 1) * ema8[1]) / (ema2low + 1);
ema9 = (2 * hl2 + (ema2high - 1) * ema9[1]) / (ema2high + 1);

(the other 8 vars)

} else {

ema5 = ema5[1];
ema13 = ema13[1];
ema8 = ema8[1];
ema9 = ema9[1];

(the other 8 vars)

}

...............

AddCloud(if show_cloud1 then AvgExp5 else na, AvgExp13, Color.GREEN, Color.RED, no);

(the other 11 clouds)
 

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