Avg Gain/Loss for 30-Min bars for current day


Category:
0
0

Hi Pete,

Is there a universal way to use the same verbiage looking only for the current day with any script? If not, could you please help me again so that I don’t have to use the lookback?

Thanks again!

input lookback = 13;

def pctGain = ((close – close[1]) / close[1]) * 100;
plot a = average(pctGain, lookback);
a.assignValueColor(if a < 0 then color.red else color.green);

Marked as spam
Posted by (Questions: 20, Answers: 27)
Asked on February 3, 2020 9:12 am
63 views
0
Private answer

It seems you are referring to a previous solution:

https://www.hahn-tech.com/ans/count-green-bars-only-current-day/

So please make sure to include a link for context. There are thousands of pairs of eyeballs viewing your thread. Let's make it easier for them to follow along.

The answer to your question is yes. There is a "universal" way to capture this data for only the current day and not require the use of the lookback period. That universal way is called recursion. You can Google that if you want to learn what that means.

However in this particular case we have a problem. That lookback period is being used to compute an average. What exactly did you have in mind here? The behavior of this code is going to change if we remove that lookback period and replace it with something else. Right now the code computes the 13 period simple moving average of the percent change from one bar's close to the next.

So what does it look like when we take that out and replace it with only the current day's values? In case it's not obvious, I will explain. On bar one of current day, there is no percent difference from the previous bar, because there is no previous bar. So the computed value for the first bar of the day is zero. Let's move on the bar number two for the current day. Now we have a value to compute. Since we are on bar number 2 we can take that result and divide by 2. What happens on bar number 3? We have two values to compute, we add them together and then divide by 3. For each new bar on the chart for the current day the average we compute changes. (the values become diluted over time)

In summary, how do you propose we modify the behavior without destroying the way your average percent change is being computed?

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on February 3, 2020 11:28 am
0
Pete, It looks like my script is not the best route to go. Are you able to write a more streamline script? I understand the dilution after adding an additional bar but I'm looking for a running average after each 30-min bar whether it's positive or negative. Thank you.
( at February 3, 2020 5:22 pm)
0
Well then there are no changes required. That's exactly what your existing code is accomplishing.
( at February 4, 2020 8:36 am)
0
There are thirteen 30-min bars in a trading day. When I use this for options, does it go back to the previous day if there isn't trading volume for 30 minutes or longer? This is what I'm trying to avoid so that's why I asked if it was possible to only have it look at the current day.
( at February 4, 2020 9:32 am)
0
I'm not sure how else to explain this any more clearly. It's impossible to compute a 13 period moving average for only the current day UNLESS you ignore the first 13 bars of the current day and only compute the value on bar number 14. Since there is no bar number 14 on a signal day 30 min chart, your entire request is impossible.
( at February 4, 2020 11:23 am)