Is it possible to use a dynamic value as constant in aggregation?

Zac

New member
Example code to demonstrate the problem.
I am trying to get the equivalent of:
Code:
average (AbsValue(hl2[0] - hl2[0]), AbsValue(hl2[1] - hl2[0]), AbsValue(hl2[2] - hl2[0]))
If you try:
Code:
plot b = average (AbsValue(hl2 - hl2[0]), 3);
Then hl2[0] is dynamic, which means it is evaluated as:
Code:
average (AbsValue(hl2[0] - hl2[0]), AbsValue(hl2[1] - hl2[1]), AbsValue(hl2[2] - hl2[2]))
If I can make hl2[0] as constant, then it will work:
Code:
average (AbsValue(hl2 - hl2[0]), 3);
                  ^     ^
            dynamic     static
However I do not see a way to do that.
Also tried a script, hoping that input will force the value to be constant inside the script, but it is still dynamic.
Code:
script c{
    input cons = 0.0;
    plot b = average (AbsValue(hl2 - cons), 100);
}
plot b = c(hl2);
Is there any solution for that?
 
Solution
Its not necessarily dynamic in regard to dynamic versus constant. Dynamic would be something like hl2[x + 1 * y] which is not allowed. To get a dynamic offset you would use GetValue() which would read as; GetValue(hl2,x + 1 * y). The issue is that you're dealing with how the script is iterated on a bar by bar basis. You would need to reference all of the prior bars from the current bar, rather than relying on the iteration as it passes through the chart. Also, hl2 and hl2[0] are the same thing.

Hard coded, it would be something like;

Ruby:
(AbsValue(hl2 - hl2[1]) + AbsValue(hl2 - hl2[2]) + AbsValue(hl2 - hl2[3])) / 3

But you would be much better off with something more like this;

Ruby:
Input Length...
Its not necessarily dynamic in regard to dynamic versus constant. Dynamic would be something like hl2[x + 1 * y] which is not allowed. To get a dynamic offset you would use GetValue() which would read as; GetValue(hl2,x + 1 * y). The issue is that you're dealing with how the script is iterated on a bar by bar basis. You would need to reference all of the prior bars from the current bar, rather than relying on the iteration as it passes through the chart. Also, hl2 and hl2[0] are the same thing.

Hard coded, it would be something like;

Ruby:
(AbsValue(hl2 - hl2[1]) + AbsValue(hl2 - hl2[2]) + AbsValue(hl2 - hl2[3])) / 3

But you would be much better off with something more like this;

Ruby:
Input Length = 3;
def TotalHL =
    fold Index = 0 to Length
    with HL do
    HL + (hl2 - getValue(hl2,Index))
;
plot AvgHL = TotalHL / Length;
 
Solution
Thanks, that solved it.

Is it possible to use fold in a study and use that study as a scan study filter?

Currently the version of the study without the fold works in the scan, and the version with the fold returns no results. On the chart both studies are identical to the last digit.
 

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