Plot horizontal line at specific time


Category:
0
0

Hi Pete, hope this question is short and takes much less than 15 min.

I am drawing a line at a specific time that I can see in any timeframe chart. For some reason, the price is not correct, nor it prints in the same location/price when changing timeframes. Please find the code below.

Thank you!!!

input pointInTime = 0945;

def regularSessionHours = RegularTradingStart(GetYYYYMMDD()) <= GetTime();
def isAppointedHour = if secondsFromTime(pointInTime) >= 0 then 1 else 0;
def priceAtTime = if isAppointedHour == 1 and isAppointedHour[1] == 0 then close else if isAppointedHour == 1 then priceAtTime[1] else double.nan;

plot priceLine = if regularSessionHours then priceAtTime else Double.NaN;
priceline.SetDefaultColor(Color.ORANGE);
priceline.SetLineWeight(2);
priceline.SetStyle(Curve.LONG_DASH);

Marked as spam
Posted by (Questions: 2, Answers: 2)
Asked on November 10, 2020 7:35 pm
139 views
0
Private answer

Your code is performing exactly has you have described. The time stamp on each bar is set to the start time. So it's quite obvious that if you change the time frame from 1 hour, to 30 min, to 15 min, to 5 min... you will get entirely different values. Because the time you are specifying is the start time while the price you are specifying is the end time (close). You did not really explain your goals here so I have nothing more to offer.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on November 10, 2020 8:10 pm
0
My goal was to have the exact price at a specific time that shows in the same place in the chart regardless of the timeframe chart chosen. Ex. @9:45:00am ES was 3525.5, I would like to see a horizontal line at 3525.5 on the 5 min, 30 min, or any timeframe chart.
( at November 10, 2020 8:28 pm)
0
Well then you need to use a time that is matched to the end time of the bar for whatever time frame you have selected. I explained that the time stamp on each bar is set to the START time. If you want the price as of 9:45 am you will have to adjust the time input based on whatever time frame you have selected. If you select a 5 min time time frame you must select close of the 9:40 bar. If you select the 15 min time frame you must select the close price of the 9:30 bar. The 30 min time frame will never permit you to derive a price at 9:45 because it only provides prices at the top and bottom of every hour. There is nothing wrong with your code. You simply need to understand how the time component of each bar is handled on Thinkorswim. You can also consider changing your code so that you get the open price of the target bar instead of the close. If you change the code to use the open, then you can get the time component to work more directly.
( at November 10, 2020 10:04 pm)
0
...but you still must understand that no matter which solution you choose the 30 min time frame will never be able to get you the price at 9:45. The only time frames you use are those that evenly divide into 45. Which leaves you the following time frames: 1 min, 3 min, 5 min, 15 min. That's it. Any other time frame will not be possible.
( at November 10, 2020 10:04 pm)
0
Got it, thank you, I had the feeling it was related to the bars...one of the many limitations on tos...Thank you again!!!
( at November 11, 2020 7:17 am)