Watchlist column for Mobius GlobeX script

fungus12

Member
Hi, I'm trying to make a watchlist column for Mobius's GlobeX script that paints green if the pre-market volume is higher than AvgGBx. I can't really figure out how to logically do this because it's pre-market data. I'm super new to coding so I can't wrap my head around how to check for something that isn't during real trading hours. Here's the code for this script:

Code:
# Globex Volume and Average

# Mobius

# From TASC 2017-06 | Daytrading With Night Volume

# 03.11.2018

 

declare lower;

 

def daysOnChart = if secondsTillTime(0930) == 0

                  then daysOnChart[1] + 1

                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if GlobeX and !GlobeX[1]

            then volume

            else if GlobeX

            then CompoundValue(1, GxVol[1] + volume, volume)

            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]

             then sumVol[1] + GxVol[1]

             else sumVol[1];

plot v = if GlobeX 

         then GxVol 

         else double.nan;

     v.SetPaintingStrategy(PaintingStrategy.Histogram);

plot AvgGbX = HighestALL(if isNaN(close[-1])

                         then sumVol / daysOnChart

                         else double.nan);

# End Code Golbex Volume and Avg

If anyone can help me figure this out I'd greatly appreciate it.
 
So I tried my hand at this but it's not working as expected. Not sure what exactly I did wrong but it's showing "below" for tickers that had Globex > AvgGBX pre-market.

Code:
AddLabel(1, if GlobeX > AvgGBX
             then "Above"
             else if GlobeX < AvgGBX
                  then "Below"
                  else "Below",
             if GlobeX > AvgGBX
             then color.green
             else if GlobeX < AvgGBX
                  then color.red
                  else color.red);

Can someone let me know where I messed up?
 

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

Hi, I'm trying to make a watchlist column for Mobius's GlobeX script that paints green if the pre-market volume is higher than AvgGBx. I can't really figure out how to logically do this because it's pre-market data. I'm super new to coding so I can't wrap my head around how to check for something that isn't during real trading hours. Here's the code for this script:

Code:
# Globex Volume and Average

# Mobius

# From TASC 2017-06 | Daytrading With Night Volume

# 03.11.2018

 

declare lower;

 

def daysOnChart = if secondsTillTime(0930) == 0

                  then daysOnChart[1] + 1

                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if GlobeX and !GlobeX[1]

            then volume

            else if GlobeX

            then CompoundValue(1, GxVol[1] + volume, volume)

            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]

             then sumVol[1] + GxVol[1]

             else sumVol[1];

plot v = if GlobeX

         then GxVol

         else double.nan;

     v.SetPaintingStrategy(PaintingStrategy.Histogram);

plot AvgGbX = HighestALL(if isNaN(close[-1])

                         then sumVol / daysOnChart

                         else double.nan);

# End Code Golbex Volume and Avg

If anyone can help me figure this out I'd greatly appreciate it.

here is a modified lower study that may help.
it adds up the volume in premarket and compares it to AvgGbX and displays it in a bubble.

this version adds up all pre-market volume.
there is another code line, that is disabled, that adds up premarket volume just for each day.

Ruby:
# Mobius_GlobeX_01

# Globex Volume and Average
# Mobius
# From TASC 2017-06 | Daytrading With Night Volume
# 03.11.2018
 

declare lower;

def daysOnChart = if secondsTillTime(0930) == 0
                  then daysOnChart[1] + 1
                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]
             then sumVol[1] + GxVol[1]
             else sumVol[1];

plot v = if GlobeX
         then GxVol
         else double.nan;
     v.SetPaintingStrategy(PaintingStrategy.Histogram);

plot AvgGbX = HighestALL(if isNaN(close[-1])
                         then sumVol / daysOnChart
                         else double.nan);

# End Code Golbex Volume and Avg

# ========================================
# compare to premaket vol

# daytime  times  (EST)
input start = 0930;
input end = 1600;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;

# add up premarket volume, for each day, then rest it
#  reset value if last bar of day (next bar is not daytime)
#def prevol = if daytime[0] and !daytime[-1] then 0 else if !daytime then prevol[1] + volume else prevol[1];

# total up all premarket volume
def prevol = if !daytime then prevol[1] + volume else prevol[1];

# if pre-market volume is higher than AvgGBx, draw bubble green
input show_test_bubble = yes;
addchartbubble(show_test_bubble, 0, volume + "\n" + round(prevol,0) + "\n" + round(AvgGbX,0), ( if prevol > AvgGBx then color.green else color.gray) , yes);

#

with chart times of day and longer, AvgGbX is = 0

QGywRR6.jpg
 
here is a modified lower study that may help.
it adds up the volume in premarket and compares it to AvgGbX and displays it in a bubble.

this version adds up all pre-market volume.
there is another code line, that is disabled, that adds up premarket volume just for each day.

Ruby:
# Mobius_GlobeX_01

# Globex Volume and Average
# Mobius
# From TASC 2017-06 | Daytrading With Night Volume
# 03.11.2018
 

declare lower;

def daysOnChart = if secondsTillTime(0930) == 0
                  then daysOnChart[1] + 1
                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]
             then sumVol[1] + GxVol[1]
             else sumVol[1];

plot v = if GlobeX
         then GxVol
         else double.nan;
     v.SetPaintingStrategy(PaintingStrategy.Histogram);

plot AvgGbX = HighestALL(if isNaN(close[-1])
                         then sumVol / daysOnChart
                         else double.nan);

# End Code Golbex Volume and Avg

# ========================================
# compare to premaket vol

# daytime  times  (EST)
input start = 0930;
input end = 1600;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;

# add up premarket volume, for each day, then rest it
#  reset value if last bar of day (next bar is not daytime)
#def prevol = if daytime[0] and !daytime[-1] then 0 else if !daytime then prevol[1] + volume else prevol[1];

# total up all premarket volume
def prevol = if !daytime then prevol[1] + volume else prevol[1];

# if pre-market volume is higher than AvgGBx, draw bubble green
input show_test_bubble = yes;
addchartbubble(show_test_bubble, 0, volume + "\n" + round(prevol,0) + "\n" + round(AvgGbX,0), ( if prevol > AvgGBx then color.green else color.gray) , yes);

#

with chart times of day and longer, AvgGbX is = 0

QGywRR6.jpg

Hm that didn't do the job, but again I'm probably screwing something up. I just replaced the code in the watchlist to your version but I'm getting the same issue.
 
@fungus12 Should be v > AvgGBX and vice versa.
Still not working. For example for ticker ALB it should say "Above" but it doesn't. Here's the code with halcyonguy's code and with v instead of GlobeX:

Code:
# Globex Volume and Average

# Mobius

# From TASC 2017-06 | Daytrading With Night Volume

# 03.11.2018

 


declare lower;


def daysOnChart = if secondsTillTime(0930) == 0

                  then daysOnChart[1] + 1

                  else daysOnChart[1];


def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());


def GxVol = if GlobeX and !GlobeX[1]

            then volume

            else if GlobeX

            then CompoundValue(1, GxVol[1] + volume, volume)

            else GxVol[1];


def sumVol = if !GlobeX and GlobeX[1]

             then sumVol[1] + GxVol[1]

             else sumVol[1];


plot v = if GlobeX

         then GxVol

         else double.nan;

     v.SetPaintingStrategy(PaintingStrategy.Histogram);


plot AvgGbX = HighestALL(if isNaN(close[-1])

                         then sumVol / daysOnChart

                         else double.nan);


# End Code Golbex Volume and Avg


# ========================================

# compare to premaket vol


# daytime  times  (EST)

input start = 0930;

input end = 1600;

def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;


# add up premarket volume, for each day, then rest it

#  reset value if last bar of day (next bar is not daytime)

#def prevol = if daytime[0] and !daytime[-1] then 0 else if !daytime then prevol[1] + volume else prevol[1];


# total up all premarket volume

def prevol = if !daytime then prevol[1] + volume else prevol[1];


# if pre-market volume is higher than AvgGBx, draw bubble green

input show_test_bubble = yes;

addchartbubble(show_test_bubble, 0, volume + "\n" + round(prevol,0) + "\n" + round(AvgGbX,0), ( if prevol > AvgGBx then color.green else color.gray) , yes);


#







AddLabel(1, if v > AvgGBX
             then "Above"
             else if v < AvgGBX
                  then "Below"
                  else "Below",
             if v > AvgGBX
             then color.green
             else if v < AvgGBX
                  then color.red
                  else color.red);
 
@fungus12 are you just wanting the total/current Pre-Market/Afterhours volume on your watchlist? Like mentioned on the subject request?
Essentially, I believe all the code is doing is grabbing overnight and pre-market volume and comparing it against an average line which doesnt seem to change. I just want the watchlist to tell me whether the overnight/pre-market volume is above or below this average.
 
I've been trying to figure this thing out for I don't know how long already. So Mobius said to use one plot, and I assumed maybe he was referring to the fact that I used multiple variables or something, so I defined a new variable to be what I need:

Code:
    def trig = if v >= AvgGbX then trig

               else Double.NaN;

Then I used that code in the watchlist and wrote this:

Code:
AddLabel(1, if trig is true
             then "Above"
             else if trig is false
                  then "Below"
                  else "Below",
             if trig is true
             then color.green
             else if trig is false
                  then color.red
                  else color.red);

Then I got this error message:

Cl4YQdq.png


So I just don't get how to get this to work.
 
So I looked back over Mobius' comment to see if I was missing something and I think I get now what he meant by the plot point. He meant you can't have plot points in the original script itself, not the watchlist code. So I changed the plot points to "def" points and this is what I have now:

Code:
# Globex Volume and Average
# Mobius
# From TASC 2017-06 | Daytrading With Night Volume
# 03.11.2018

# Globex Volume and Average
# Mobius
# From TASC 2017-06 | Daytrading With Night Volume
# 03.11.2018

declare lower;
def daysOnChart = if SecondsTillTime(0930) == 0
    then daysOnChart[1] + 1
    else daysOnChart[1];

def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

def GxVol = if GlobeX and !GlobeX[1]
    then volume
    else if GlobeX
    then CompoundValue(1, GxVol[1] + volume, volume)
    else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]
    then sumVol[1] + GxVol[1]
    else sumVol[1];

def v = if GlobeX 
    then GxVol 
    else Double.NaN;

def AvgGbX = HighestAll(if IsNaN(close[-1])
    then sumVol / daysOnChart
    else Double.NaN);

# End Code Golbex Volume and Avg


# ========================================
#Watchlist

AddLabel(1, if v >= AvgGBX
             then "Above"
             else if v < AvgGBX
                  then "Below"
                  else "Below",
             if v >= AvgGBX
             then color.green
             else if v < AvgGBX
                  then color.red
                  else color.red);

Still running into the same issue where I still get "Above" on tickers that are below AvgGBX. Don't know what else to do. @SleepyZ you've been extremely helpful with my other questions, if you ever have time can you take a look at this? Don't know what I'm doing wrong. Thanks in advance.
 
The best way to debug this is use a plot or label to show the values you're working with so you can see if they are as you expect.

plot x = AvgGbx;

For instance, if I don't switch the timeframe of the column to intraday AvgGbx is always zero. Make sure you select an intraday time period for this column and make sure the box is checked for extended hours.

On 1 hour, AvgGbX is infinity. I see the same thing on a chart. Set it to 15m and it shows a number.

Next I'll check v. I set the plot x = v; and everything in my list is NaN because this is not premarket. I can see the same thing on a chart, v has no value. So this code can't work until we start a new day.

I'm assuming you want this to continue working after the open and still be accurate based on the premarket volume. To do that we just need to change v. The following code does that.

The only changes I made to the original script are:
  1. Changed plots to defs
  2. Changed the definition of v so it isn't empty during market hours
  3. Added the label
The code could be made a little cleaner by eliminating v since it's now always equal to GxVol.

Ruby:
def daysOnChart = if secondsTillTime(0930) == 0
                  then daysOnChart[1] + 1
                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]
             then sumVol[1] + GxVol[1]
             else sumVol[1];

def v = GxVol;

def AvgGbX = HighestALL(if isNaN(close[-1])
                         then sumVol / daysOnChart
                         else double.nan);

AddLabel(
  yes,
  if v >= AvgGbx then "ABOVE" else "BELOW",
  if v >= AvgGbx then Color.UPTICK else Color.RED
);
 
Last edited:
The best way to debug this is use a plot or label to show the values you're working with so you can see if they are as you expect.

plot x = AvgGbx;

For instance, if I don't switch the timeframe of the column to intraday AvgGbx is always zero. Make sure you select an intraday time period for this column and make sure the box is checked for extended hours.

On 1 hour, AvgGbX is infinity. I see the same thing on a chart. Set it to 15m and it shows a number.

Next I'll check v. I set the plot x = v; and everything in my list is NaN because this is not premarket. I can see the same thing on a chart, v has no value. So this code can't work until we start a new day.

I'm assuming you want this to continue working after the open and still be accurate based on the premarket volume. To do that we just need to change v. The following code does that.

The only changes I made to the original script are:
  1. Changed plots to defs
  2. Changed the definition of v so it isn't empty during market hours
  3. Added the label
The code could be made a little cleaner by eliminating v since it's now always equal to GxVol.

Ruby:
def daysOnChart = if secondsTillTime(0930) == 0
                  then daysOnChart[1] + 1
                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]
             then sumVol[1] + GxVol[1]
             else sumVol[1];

def v = GxVol;

def AvgGbX = HighestALL(if isNaN(close[-1])
                         then sumVol / daysOnChart
                         else double.nan);

AddLabel(
  yes,
  if v >= AvgGbx then "ABOVE" else "BELOW",
  if v >= AvgGbx then Color.UPTICK else Color.RED
);
This is bizarre dude. Firstly thank you for responding. So this works better than before but it's still not entirely accurate and still changes wildly depending on which intraday period you aggregate. For instance, on SNAP the 30m aggregation shows BELOW which is correct, but the 15m aggregation shows ABOVE. Is this because I'm checking this after RTH?
 
This is bizarre dude. Firstly thank you for responding. So this works better than before but it's still not entirely accurate and still changes wildly depending on which intraday period you aggregate. For instance, on SNAP the 30m aggregation shows BELOW which is correct, but the 15m aggregation shows ABOVE. Is this because I'm checking this after RTH?
Yeah, there seems to be some weirdness with some symbols.

I put the original study on a 5D 15m chart and modified v the same way. If I load any symbol with generally good premarket volume everything looks fine. GLD, QQQ, SPY, all look good. KO, PEP, TWTR have good premarket data a lot less often but generally have some and those look okay. PSA, PFPT sometimes have no premarket volume and this is where the problem is. v still has the value from the last day that did have premarket volume because there is no candle that meets the condition of GlobeX and !GlobeX[1] to reset it.

GetDay() != GetDay()[1] would get the volume of the first candle even if it's after the open, so that won't work. We need a time that is guaranteed to actually have a candle on the chart. I updated the code to use 4pm as the reset time and that looks okay on the chart.

NOTE: The field will not work correctly while volume for the symbol is N/A. Once any shares get traded and volume has a numeric value then it should work.

The change is just to the definition of GxVol, as follows:

Ruby:
def GxVol = if secondsTillTime(1600) == 0
            then 0
            else if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

For different results on different timeframes, there's nothing I can do about that one. I think the issue with an Hour chart that caused infinity is because there's not an even number of whole hours in regular trading hours. There's 6.5. And the code is looking for 9:30am which is in the middle of an hour candle so never true. 15m works because 6.5 hours divides evenly by 15m. I think if you use timeframes that don't create partial candles in 6.5 hours it should be mostly okay.

We've seen, many times, values from TOS for one timeframe, such as for volume, don't match another timeframe. We expect we can add together volume for all the bars of the day and come up with the same total as the day chart but it's often not exact and it isn't something we have control over. It's the data they send us. It shouldn't be too far off, though.

I updated the code to display a % of the average instead of just above/below. If the volume is very close to the average, above/below doesn't mean much. If volume is 500% of average, that's meaningful. Here's the whole script. You may want to adjust the color thresholds near the bottom to fit your criteria.

Ruby:
def daysOnChart = if secondsTillTime(0930) == 0
                  then daysOnChart[1] + 1
                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if secondsTillTime(1600) == 0 then 0
            else if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]
             then sumVol[1] + GxVol[1]
             else sumVol[1];

def v = GxVol;

def AvgGbX = HighestALL(if isNaN(close[-1])
                         then sumVol / daysOnChart
                         else double.nan);

plot pct = if IsNaN(GxVol) or GxVol == 0 then Double.NaN else Round(GxVol / AvgGbX * 100, 1);
pct.AssignValueColor(
  if pct < 50 then Color.RED
  else if pct < 80 then Color.DARK_ORANGE
  else if pct < 120 then Color.YELLOW
  else if pct < 200 then Color.UPTICK
  else Color.GREEN
);
 
Yeah, there seems to be some weirdness with some symbols.

I put the original study on a 5D 15m chart and modified v the same way. If I load any symbol with generally good premarket volume everything looks fine. GLD, QQQ, SPY, all look good. KO, PEP, TWTR have good premarket data a lot less often but generally have some and those look okay. PSA, PFPT sometimes have no premarket volume and this is where the problem is. v still has the value from the last day that did have premarket volume because there is no candle that meets the condition of GlobeX and !GlobeX[1] to reset it.

GetDay() != GetDay()[1] would get the volume of the first candle even if it's after the open, so that won't work. We need a time that is guaranteed to actually have a candle on the chart. I updated the code to use 4pm as the reset time and that looks okay on the chart.

NOTE: The field will not work correctly while volume for the symbol is N/A. Once any shares get traded and volume has a numeric value then it should work.

The change is just to the definition of GxVol, as follows:

Ruby:
def GxVol = if secondsTillTime(1600) == 0
            then 0
            else if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

For different results on different timeframes, there's nothing I can do about that one. I think the issue with an Hour chart that caused infinity is because there's not an even number of whole hours in regular trading hours. There's 6.5. And the code is looking for 9:30am which is in the middle of an hour candle so never true. 15m works because 6.5 hours divides evenly by 15m. I think if you use timeframes that don't create partial candles in 6.5 hours it should be mostly okay.

We've seen, many times, values from TOS for one timeframe, such as for volume, don't match another timeframe. We expect we can add together volume for all the bars of the day and come up with the same total as the day chart but it's often not exact and it isn't something we have control over. It's the data they send us. It shouldn't be too far off, though.

I updated the code to display a % of the average instead of just above/below. If the volume is very close to the average, above/below doesn't mean much. If volume is 500% of average, that's meaningful. Here's the whole script. You may want to adjust the color thresholds near the bottom to fit your criteria.

Ruby:
def daysOnChart = if secondsTillTime(0930) == 0
                  then daysOnChart[1] + 1
                  else daysOnChart[1];

def GlobeX = getTime() < regularTradingStart(getYYYYMMDD());

def GxVol = if secondsTillTime(1600) == 0 then 0
            else if GlobeX and !GlobeX[1]
            then volume
            else if GlobeX
            then CompoundValue(1, GxVol[1] + volume, volume)
            else GxVol[1];

def sumVol = if !GlobeX and GlobeX[1]
             then sumVol[1] + GxVol[1]
             else sumVol[1];

def v = GxVol;

def AvgGbX = HighestALL(if isNaN(close[-1])
                         then sumVol / daysOnChart
                         else double.nan);

plot pct = if IsNaN(GxVol) or GxVol == 0 then Double.NaN else Round(GxVol / AvgGbX * 100, 1);
pct.AssignValueColor(
  if pct < 50 then Color.RED
  else if pct < 80 then Color.DARK_ORANGE
  else if pct < 120 then Color.YELLOW
  else if pct < 200 then Color.UPTICK
  else Color.GREEN
);
This is awesome work dude, thanks so much. You're right that it isn't 100% accurate but it's very close and that's totally fine. Can't thank you enough man.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
475 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