Is there a funtion in PineScript for calculating the number of bars lasting for the next Upper timeframe resolution bar? - resolution

I'm coding a new pinescript strategy in tradingview. I have 2 resolution timeframes: Upper Time Frame is 4 Days and lower is 65 minutes.
Based on the 65 minutes timeframe (6 candles per day, 24 candles for each 4D candle), is there any way to calculate the number of 65minutes candles lasting for the next 4 days resolution candle?
//#version=3
study("SinceyNew")
res = "4D"
is_newbar(res) =>
t = time(res)
change(t) != 0 ? 1 : 0
plot(is_newbar(res))
color=na
if is_newbar(res)>0
color := black
bgcolor(color)
plot(24-barssince(is_newbar(res)))
Thanks In Advance!
I've this code that tells me the number of bars, the problem that i'm experiencing is that some 4Days bars are not composed by 24 bars of 65 minutes (because of hollydays, etc)

Related

Altering values as a percentage to work with a graph

I have a gauge graph that goes from 0 to 100.
I have divided out my justification points as how they would show on the 0 - 100 graph. -2STDev, -1STDev, Avg, +1STDev. +2 STDev. How would I go about transferring incoming values to a 0 - 100 scale to match the graph?
On the graph of 0 - 100:
16 represents -2STDev
33 represents -1STDev
50 represents Average
66 represents +1 STDEV
83 represents +2 STDEV
My current values that I want to format to a scale of 100 to fit the graph are:
-2STDev = 63.9
-1STDev = 66.8
AVG = 69.6
+1STDev = 72.5
+2STDev = 75.4
How would I go about creating a formula to adjust these to a 0 - 100 scale? Of course my incoming value, will have to also follow this formula to be graphed upon these.
You can use the following long formula:
=IF(E6<C1,E6/C1*B1,IF(E6<C2,(E6-C1)/(C2-C1)*(B2-B1)+B1,IF(E6<C3,(E6-C2)/(C3-C2)*(B3-B2)+B2,IF(E6<C4,(E6-C3)/(C4-C3)*(B4-B3)+B3,IF(E6<C5,(E6-C4)/(C5-C4)*(B5-B4)+B4,(E6-C5)/(100-C5)*(100-B5)+B5)))))
See the location of the data so you can replace for other if required.
Keep in mind this formula does a linear conversion for values inbetween each one of the values you have.

Divide excel column to N equal groups

I have a column with ordinal values. I want to have another column that ranks them in equal groups (relatively to their value).
Example: If I have a score and I want to divide to 5 equal groups:
Score
100
90
80
70
60
50
40
30
20
10
What function do I use in the new column to get this eventually:
Score Group
100 5
90 5
80 4
70 4
60 3
50 3
40 2
30 2
20 1
10 1
Thanks! (I'm guessing the solution is somewhere in mod, row and count - but I couldn't find any good solution for this specific problem)
If you don't care about how the groups are split for groups that aren't evenly divisible, you can use this formula and drag down as far as necessary:
= FLOOR(5*(COUNTA(A:A)-COUNTA(INDEX(A:A,1):INDEX(A:A,ROW())))/COUNTA(A:A),1)+1
Possibly a more efficient solution exists, but this is the first way I thought to do it.
Obviously you'll have to change the references to the A column if you want it in a different column.
See below for working example.

How to display 1 hour 46 minutes in MS Excel cell where both the number count of hours and minutes comes from calculation?

Suppose I sleep for 7 hours and 36 minutes. I have a sleeping threshold of 5 hours and 54 minutes. If I sleep more than threshold time, I get the time difference and If i sleep less than or equal to my sleeping threshold, i get the output as 'You slept Good". To do so, I scaled zero to 60 minutes of actual time to 0 to 1 on a decimal scale, this way 7 hours 36 minutes comes as 7.6 and 5 hours 54 minutes comes as 5.9.
Now, I take their difference which comes out to be 1.7 i.e. I slept extra for 1 hour and 0.7 minutes on 0to1 scale (which equals 1 hour 42 minutes on actual time scale).
So, I used TRUNC function to display 1 h (i.e ! hour); however I can't get my mind working as to how I can display 42 minutes in the same cell.
i.e. my output should be 1h 42 min
This should work:
=IF(B2+C2/60>5.9,LEFT(B2+C2/60-5.9)& "hr "&ROUND(((B2+C2/60-5.9)-VALUE(LEFT(B2+C2/60-5.9)))*60,0)&"min","You slept good")
However, you wouldn't be able to do further calculations, as it is a text.
Also assumes you will not oversleep by more than 10 hrs :)

Time Lookback Calculation

I have two columns:
Column A: Time (seconds from midnight)
Column B: Value (arbitrary value I've created)
How would I find the average value (column B) at any instance in time(row) looking back over the previous x seconds (column A)?
Lets assume A1 = Seconds after midnight
Seconds after midnight Value
0 27
2 29
6 2
16 29
20 19
24 4
34 2
40 1
44 4
54 12
64 12
71 3
81 30
91 21
92 1
93 27
97 12
104 30
112 25
Note that time deltas are variable so I can't just look at the last x rows.
A specific question would be:
In a new column (column C) return the average Value of the last 10 seconds of data.
I have no idea how to do this. Can anyone help? It'd be greatly appreciated.
EDIT:
The output in the first 4 rows of column C would be:
Seconds after midnight Value Result
0 27 No values prior to this one
2 29 27 Average(B2)
6 2 28 Average(B2:B3)
16 29 2 Average(B4)
For each row, I'm taking the current time (16 in the last case)...going back x seconds (10 seconds in this case) and then averaging the values from all the cells in that time range (not including the current value).
My main issue is the time calculation. I don't know how to calculate it as I roll forward in time and I continue to get more instances as we move forward. If we go 10 seconds without any new data point then there would be no output since. If we got one instance in the previous 10 seconds then the output would be that value. If we got 100 instances in the previous 10 seconds, I need to return the average of that 100 instances.
Again, I'd appreciate any help, hints, links. This is driving me crazy.
Try the following formula in cell C3:
=IFERROR(SUMIFS(B:B,A:A,"<"&A3,A:A,">="&A3-10)/COUNTIFS(A:A,"<"&A3,A:A,">="&A3-10),0)
SUMIFS will get the sum of all values for which the seconds are:
less than A3 (i.e. less than 2 in this case)
more or equal to A3-10 (i.e. above -8 in this case)
And this sum is divided by the number of lines found with the same criteria.
If now there is an error (more specifically when the COUNTIFS returns 0, you would get #DIV/0!) you simply get 0 instead of the error.

EXCEL Bar Graph - representing only part of the bar graph

Hi I'm trying to create a bar graph on Excel where I can highlight part of the bar graph. For instance, for the following table,
1 100 20 30
2 100 10 20
3 100 5 15
I would like to have the value 100 represented and then on top of that have only between 20 and 30 highlighted (or shaded/colored/etc in differnt color) - showing that the total is 100 and the range is 20 to 30.
For 2, same thing, have 100 represented first, and only have 10 to 20 highlighted, and so on.
Would any of you guys please help me on how to go about it? I tried stacked bar with no fill and such but it does not fulfill my satisfaction. Thank you for your reply in advance!!! :)
Do you want something like this?
If so, convert your data to this format:
1 20 10 70
2 10 10 80
3 5 10 85
Use a stacked column chart and set the first and the third series to the same color.

Resources