Disply time between bars


Category:
0
0

Hello,

 

I am looking to make a study that plots how long (in minutes) from the last 1 minute candle. I am using a 1 minute chart. I think this can be achieved by plotting the difference between the current time and the most recent one minute candle.

For example, if the last trade on the minute chart happened at 9:45 and the current time is 9:51 then 6 should be plotted.

 

Thanks guys!!

 

Marked as spam
Posted by (Questions: 8, Answers: 5)
Asked on August 3, 2020 10:28 am
588 views
0
Private answer

Sure, you simply count the number of bars. Each bar is one minute long. So the number of bars tells you the number of minutes.

But we can't do anything without knowing the rules for marking that candle at 9:45.

From the title of your question it seems you want some sort of a chart label that displays the count since the last live trade on the ticker symbol loaded on the chart. If that is the case then we actually have two things to address. One is how on earth does the code know that you took a live trade on that instrument. The second is very minor, simply counting bars.

Let me know if I understand the request correctly or if you had something else in mind.

Edit: From the clarification in the comment section below I have a solution. I also updated the title of the question to describe exactly what this code does. This is will make it easier for the rest of our viewers to locate this solution through search.

declare lower;
def data1 = GetTime() / 1000 / 60;
plot data2 = data1 - data1[1];

The line for data1 gets the start time of each bar on the chart, then converts milliseconds to minutes. The line for data2 subtracts the time of the previous bar from the time of the current bar. The code is set to display on the lower subgraph. For a ticker symbol that has gaps in the trade data the line will be jagged and show the number of minutes between bars so you can see the gaps in the trade data. For a ticker symbol that has zero gaps the line will display a constant value across the bottom of the chart.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 3, 2020 11:06 am
0
Thanks for the quick response Pete! Some of the stocks/options I trade don't have volume every minute of the day (the time might be 9:50 but the last bar on the 1 minute chart might be around 9:43). I am not worried about the code knowing whether or not I took a live trade on the instrument. I am just looking for the script to plot the time difference (in minutes) from the current time and the time of the last bar on the 1 minute chart. I am having trouble referencing the current time and the time of the most recent bar. Thanks!
( at August 3, 2020 11:23 am)
0
Thanks for explaining this more clearly. I have updated my answer to include the solution.
( at August 3, 2020 2:33 pm)
0
Thank you so much Pete! Your solution worked perfectly.
( at August 3, 2020 3:28 pm)