Regular Session High or Low Crossing Premarket High or Low


Category:
0
0

Hi,

I am looking to plot on a Watchlist column if the RTH Low / High Crossed Pre Market Low / High, and color code. Using some of the code you posted on another study I was able to define and see the premarket levels on the watchlists.

But when I want to compare them to the RTH Low/High, given it’s working on intraday mode with Ext checked, it compares to the actual bar, not the Low /High of the day.

So basically I need to fix how to reference the Low/High of day ONLY since the RTH Start.

Idea is if to plot “BOTH”, if High>ONH and Low<ONL, plot “ONH” if High>ONH, plot “ONL” if Low<ONL, plot “-” if inside ON range. Will add some color coding but I guess I can do that if the rest is working.

Below the code I am using. Thanks for your help.

#PreMarket Session and ONH / ONL
def stageOne = RegularTradingEnd(GetYYYYMMDD());
def premarketStart = stageOne[1] <> stageOne;
def stageTwo = SecondsTillTime(930);
def premarketSession = stageTwo > 0;
def PMH = if premarketStart and premarketSession then high else if high > PMH[1] and premarketSession then high else PMH[1];
def PML = if premarketStart and premarketSession then low else if low < PML[1] and premarketSession then low else PML[1];

#Regular Trading Session LOD / HOD

def stageOneR = RegularTradingEND(GetYYYYMMDD());
def RTHStart = stageOneR[1] <> stageOneR;
def stageTwoR = SecondsFromTime(930);
def RTHSession = stageTwoR > 0;
def HOD = if stageOneR and RTHSession then high else if high > HOD[1] and RTHSession then high else HOD[1];

def LOD = if RTHStart and RTHSession then low else if low < LOD[1] and RTHSession then low else LOD[1];

AddLabel (yes,if HOD > PMH and LOD < PML then “BOTH” else if HOD > PMH then “ONH” else if LOD < PML then “ONL” else “—“);

 

 

 

 

 

RESOLVED
Marked as spam
Posted by (Questions: 4, Answers: 8)
Asked on March 4, 2020 10:38 am
198 views
1
Private answer

Here is the final code. I tested it thoroughly and it's working. Hope I did not miss anything.

# Pre Market High / Low Levels Hit at last once wiht Color Coding.
# Thanks to https://www.hahn-tech.com for helping on how to solve issues.

#PreMarket Session Levels and Start/Ending Times
input PreMarketStartTime = 400;
input PreMarketEndTime = 929;
def PreMarketUntilClose = SecondsTillTime(PreMarketEndTime);
def PreMarketSinceOpen = SecondsFromTime(PreMarketStartTime);
def PreMarketTargetPeriod = PreMarketUntilClose >= 0 and PreMarketSinceOpen > 0 ;

#PreMkt High and Low
rec PreMktHigh = if PreMarketTargetPeriod and !PreMarketTargetPeriod[1] then high else if PreMarketTargetPeriod and high > PreMktHigh[1] then high else PreMktHigh[1];
rec PreMktLow = if PreMarketTargetPeriod and !PreMarketTargetPeriod[1] then low else if PreMarketTargetPeriod and low < PreMktLow[1] and low > 0 then low else PreMktLow[1];

#Regular Trading Session Start/Ending Times
input First_Bar_StartTime = 930;
input First_Bar_EndTime = 1600;
def RTH_TimeUntilClose = SecondsTillTime(First_Bar_EndTime);
def RTH_TimeSinceOpen = SecondsfromTime(First_Bar_StartTime);
def First_Bar = RTH_TimeSinceOpen >= 0 and RTH_TimeUntilClose > 0 ;

#RTH High and Low
rec RTH_High = if First_Bar and !First_Bar [1] then high else if First_Bar and high > RTH_High[1] then high else RTH_High[1];
rec RTH_Low = if First_Bar and !First_Bar [1] then low else if First_Bar and low < RTH_Low[1] and low > 0 then low else RTH_Low[1];

#Definition of variable Outputs
Def Both = RTH_High > PreMktHigh and RTH_Low < PreMktLow;
Def ONH = RTH_High > PreMktHigh;
Def ONL = RTH_Low < PreMktLow;
Def Inside = RTH_High < PreMktHigh and RTH_Low > PreMktLow;

#Plots on Watchlist with Text Color Coding
AddLabel (yes,if Both then "BOTH" else if ONH then "ONH" else if ONL then "ONL" else "---", if Both or ONH then Color.Black else Color.White);

#Background Color Coding
assignbackgroundColor(
if Both then CreateColor(235,175,65)
else if ONH then CreateColor(175,250,175)
else if ONL then Color.Light_Red
else Color.black);

Marked as spam
Posted by (Questions: 4, Answers: 8)
Answered on March 5, 2020 9:13 am
0
Private answer

I am inclined to start from scratch here rather than try to modify your code. The first problem I have though is the use of the following abbreviations: ONH, ONL, ON. Does the ON stand for Overnight?

Using abbreviations in the code makes it much more difficult to read and understand. Especially when the code does not work and the only way to tell what you are trying to do is to read the code.

So spell things out in your variable names. HOD should be highOfDay, LOD should be lowOfDay.... got it? Ok.

The use of the function named RegularTradingEnd() is not going to work here. Simply plot the following as a lower study on a chart and you will see why:

declare lower;
plot data = RegularTradingEnd(GetYYYYMMDD());

I usually will take the time to explain where the code is wrong and teach you how to correct things. But those abbreviations are making that too difficult. I only allow about 15 mins to provide solutions in the Q&A forum. We need code that is very easy to read and understand.

So go back to the charts and rework your code. Clean up those abbreviations so the code is readable and implement SecondsTillTime() and/or SecondsFromTime() functions instead of RegularTradingEnd().

For that, you can examine the code in the following post:

https://www.hahn-tech.com/ans/premarket-highlow-label/

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 4, 2020 12:11 pm
0
Thanks a lot……I figured it out and learnt a great framework to define times between and inside sessions. Appreciate your help. Will post on Solutions to share with the community:
( at March 4, 2020 7:55 pm)
0
Go ahead and post that as a new solution to this post. It will be easier for folks to see the code and copy/paste it into their own platforms.
( at March 5, 2020 7:36 am)