Wrong math/logic with different timeframes?

Vision

Member
2019 Donor
Am I right? TOS can't do math or logic on numbers derived from different timeframes? Creating an example:

EMA1(10, 5min) value is 4000
EMA2(10, Day) value is 4100
EMA1 + EMA2 = 4350.

What am I missing?
 

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

With fresh eyes this morning I no longer see the addition problem, but there's a logic problem (EMA(10, 4H) is less than EMA(10, D) but cond1 is returning true). Am looking at the emini.

declare upper;

def cond1 = ExpAverage(close(period = AggregationPeriod.four_hours), 10) > ExpAverage(close(period = AggregationPeriod.DAY), 10);
def cond2 = ExpAverage(close(period = AggregationPeriod.four_hours), 10) < ExpAverage(close(period = AggregationPeriod.DAY), 10);

AddLabel(yes, ExpAverage(close(period = AggregationPeriod.four_hours), 10), Color.WHITE);
AddLabel(yes, ExpAverage(close(period = AggregationPeriod.DAY), 10), Color.WHITE);
AddLabel(yes, cond1, Color.WHITE);
AddLabel(yes, cond2, Color.WHITE);
AddLabel(yes, ExpAverage(close(period = AggregationPeriod.four_hours), 10) + ExpAverage(close(period = AggregationPeriod.DAY), 10), Color.WHITE);
 
With fresh eyes this morning I no longer see the addition problem, but there's a logic problem (EMA(10, 4H) is less than EMA(10, D) but cond1 is returning true). Am looking at the emini.

declare upper;

def cond1 = ExpAverage(close(period = AggregationPeriod.four_hours), 10) > ExpAverage(close(period = AggregationPeriod.DAY), 10);
def cond2 = ExpAverage(close(period = AggregationPeriod.four_hours), 10) < ExpAverage(close(period = AggregationPeriod.DAY), 10);

AddLabel(yes, ExpAverage(close(period = AggregationPeriod.four_hours), 10), Color.WHITE);
AddLabel(yes, ExpAverage(close(period = AggregationPeriod.DAY), 10), Color.WHITE);
AddLabel(yes, cond1, Color.WHITE);
AddLabel(yes, cond2, Color.WHITE);
AddLabel(yes, ExpAverage(close(period = AggregationPeriod.four_hours), 10) + ExpAverage(close(period = AggregationPeriod.DAY), 10), Color.WHITE);


defining 2 different timeframes in 1 formula, like your formula, can cause issues and may not work as expected.

def cond2 = ExpAverage(close(period = AggregationPeriod.four_hours), 10) < ExpAverage(close(period = AggregationPeriod.DAY), 10);

also, you are defining the same functions over and over.

to fix both issues, define each function to a variable.
then reference the variable as needed. that way there is just 1 call for external data ( to calculate an average)

example...
change your cond2 formula to be like this, 3 formulas. then you can reference avg1 and avg2 in other formulas.

def avg1 = ExpAverage(close(period = AggregationPeriod.four_hours), 10);
def avg2 = ExpAverage(close(period = AggregationPeriod.DAY), 10);
def cond2 = avg1 < avg2;

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

this is your study rewritten.
i added inputs variables, to be able to change parameters. i added _4 and _d to the variable names to make it easy to remember which is which.

Code:
declare upper;

input agg1 = AggregationPeriod.four_hours;
input length1 = 10;
input agg2 = AggregationPeriod.day;
input length2 = 10;

def avg1_4 = ExpAverage(close(period = agg1), length1);
def avg2_d = ExpAverage(close(period = agg2), length2);

def cond1 = avg1_4 > avg1_d;
def cond2 = avg1_4 < avg1_d;

AddLabel(yes, avg1_4, Color.WHITE);
AddLabel(yes, avg1_d, Color.WHITE);

AddLabel(yes, cond1, Color.WHITE);
AddLabel(yes, cond2, Color.WHITE);

AddLabel(yes, avg1_4 + avg1_d,  Color.WHITE);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
429 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