Help with this pre market low breakout code

olufemig

New member
i have the below code to scan for tickers that show 3m candle breakout below pre market lows. for me, it doesnt return anything and wondering what could be wrong

Code:
# Advanced Premarket Low Breakdown Scan

input preMarketStart = 0900;
input marketOpen = 1430;
input volumeMultiplier = 1.5;

def isPremarket = SecondsFromTime(preMarketStart) >= 0 and SecondsTillTime(marketOpen) > 0;
def isRegular = SecondsFromTime(marketOpen) >= 0;

# Track Premarket Low
def pmLow = CompoundValue(1,
    if isPremarket then
        if IsNaN(pmLow[1]) then low
        else Min(low, pmLow[1])
    else pmLow[1],
low);

# Average volume
def avgVol = Average(volume, 20);

# Detect first cross below PM low
def breakDown = close crosses below pmLow;

# Volume confirmation
def highVol = volume > avgVol * volumeMultiplier;

# Final scan condition
plot scan = isRegular and breakDown and highVol;

top.png
 
Solution
it is near impossible to debug a scan code.
when i have problems, i load the code on a chart (upper or lower).
then i can add lines and bubbles and see what is happening.


why do you have these values for pre and open?
#input preMarketStart = 0900;
#input marketOpen = 1430;

pre starts at 4am
market opens at 9:30am
https://www.fidelity.com/learning-center/smart-money/stock-market-hours



the pmlow formula doesn't always reset on a new day.
each day it should start at the low of first bar of pre.

this line isn't true, because pmlow[1] is not an error. so it only resets pmlow when a candle goes lower than the previous pmlow.
if IsNaN(pmLow[1]) then low


new code
def pmLow = if (bn == 1 or newday) then low
else if isPremarket...
it is near impossible to debug a scan code.
when i have problems, i load the code on a chart (upper or lower).
then i can add lines and bubbles and see what is happening.


why do you have these values for pre and open?
#input preMarketStart = 0900;
#input marketOpen = 1430;

pre starts at 4am
market opens at 9:30am
https://www.fidelity.com/learning-center/smart-money/stock-market-hours



the pmlow formula doesn't always reset on a new day.
each day it should start at the low of first bar of pre.

this line isn't true, because pmlow[1] is not an error. so it only resets pmlow when a candle goes lower than the previous pmlow.
if IsNaN(pmLow[1]) then low


new code
def pmLow = if (bn == 1 or newday) then low
else if isPremarket then Min(low, pmLow[1])
else pmLow[1];



i loaded the code as an upper study
i added some lines
i calculated the highest and lowest, so i could draw a line in the middle of the chart.
this line has a pulse when scan is true.


experiment with this
then copy back to scan code and remove the extra lines and bubbles

the blue line with the pulse is the scan variable

Code:
#breakout_below_pre_low
#https://usethinkscript.com/threads/help-with-this-pre-market-low-breakout-code.22209/
#Help with this pre market low breakout code


#declare lower;

def na = double.nan;
def bn = barnumber();


def day = getday();
def newday = day != day[1];

#input preMarketStart = 0900;
#input marketOpen = 1430;

#input preMarketStart = 0400;
input preMarketStart = 0100;
input marketOpen = 0930;

input volumeMultiplier = 1.5;

def isPremarket = SecondsFromTime(preMarketStart) >= 0 and SecondsTillTime(marketOpen) > 0;
def isRegular = SecondsFromTime(marketOpen) >= 0;

# Track Premarket Low
#def pmLow = CompoundValue(1,
#    if isPremarket then
#        if IsNaN(pmLow[1]) then low
#        else Min(low, pmLow[1])
#    else pmLow[1],
#low);

def pmLow = if (bn == 1 or newday) then low
  else if isPremarket then Min(low, pmLow[1])
  else pmLow[1];




# Average volume
def avgVol = Average(volume, 20);

# Detect first cross below PM low
def breakDown = close crosses below pmLow;

# Volume confirmation
def highVol = volume > avgVol * volumeMultiplier;

# Final scan condition
#plot scan = isRegular and breakDown and highVol;


#--------------------------------------


# premarket bubbles
addchartbubble(0 and isPremarket, high, "P", color.cyan, 1);


#def firstlow = if bn == 1 then low else firstlow[1];

def n = 800;
def big = 99999;
def hi;
def lo;
if bn == 1 then {
 hi = high;
 lo = low;
} else if bn == 2 then {
hi = fold a = 1 to n
  with b = hi[1]
  while !isnan(getvalue(close,-a))
  do max(b, getvalue(high,-a));
lo = fold c = 1 to n
  with d = lo[1]
  while !isnan(getvalue(close,-c))
  do min(d, getvalue(low,-c));
} else {
 hi = hi[1];
 lo = lo[1];
}

# create a price level in the middle of the chart, to draw the pulse line,  true/false of scan
def mid = (hi + lo)/2;

#plot zh = hi;
#plot zl = lo;


# chg plot to be on top of upper chart
def pulse = 2;
#plot scan = if (isRegular and breakDown and highVol) then (firstlow + 2) else firstlow;

plot scan = if (isRegular and breakDown and highVol) then (mid + pulse) else mid;



# plot a line along lowest lows
plot zlow = pmlow;


addchartbubble(0, pmlow,
pmlow
, color.cyan, 0);


addchartbubble(0, mid,
bn + "\n" +
hi + "\n" +
mid + "\n" +
lo
, color.cyan, 0);

#
 

Attachments

  • img1.png
    img1.png
    47.6 KB · Views: 24
Last edited:
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1043 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top