Calculate percent change during extended hours


Category:
0
0

I’m trying to calculate the Extended Hours %Change (from 4PM to now, assuming we are in an extended hours market, until 9:30AM, from which the %Change number won’t change anymore until the close of the session at 4PM) using a 5 minutes aggregation. I’ve run this code but it’s not making the calculation right. Are there any advise? Thanks in advance.


def timeOpen = if SecondsTillTime(0925) >= 0 then close else timeOpen[1];
def timeClose = if SecondsFromTime(1600) == 0 then open else timeClose[1];
def ID = (timeOpen-timeClose)/timeClose;
addLabel(yes, AsPercent(IDC), if IDC>0 then CreateColor(0, 195, 49) else if IDC<0 then CreateColor(237, 87, 87) else color.WHITE);

RESOLVED
Marked as spam
Posted by (Questions: 7, Answers: 9)
Asked on June 11, 2020 5:36 pm
111 views
1
Private answer

I borrowed some code from a previous post in the Watch Lists topic. This requires the input named "endTime" to be adjusted based on the time frame selected for the chart. The last bar of the regular trading session is stamped with the start time of the bar. So for a 15 min chart the value needs to be 1545. If you change the chart to 5 min the "endTime" input needs to be adjusted to 1555.

def endTime = 1545;
def regularSessionEnd = SecondsTillTime(endTime) == 0;
rec dailyClose = if regularSessionEnd then close else dailyClose[1];
def percentChange = (close / dailyClose - 1);
AddLabel(yes, AsPercent(percentChange), if percentChange >= 0 then Color.GREEN else Color.RED);

In this code the percent change will continue to update throughout the regular trading session just as it does during extended trading session. I am way over budget on time for this solution and this is all I can afford to offer.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 11, 2020 6:21 pm
0
Pete, thanks a lot. I appreciate your time spent on this. I have fine tuned the solution thanks to your response and will post it below in the solution box.
( at June 11, 2020 7:10 pm)
1
Private answer

Solution attached to avoid formatting problems with HTML

Attachments:
Marked as spam
Posted by (Questions: 7, Answers: 9)
Answered on June 11, 2020 7:12 pm