Help with Turning off Extended Hours

hboogie

Member
Plus
Hi All

@halcyonguy you really helped me tremendously here with this code.

https://usethinkscript.com/threads/...cant-be-used-in-thinkorswim.13822/post-134848

I've made some tweaks to it to get close to the output I was expecting. I've made no impactful structural changes to the code, but I've noticed that the code doesn't work with extended hours turned off. Presumably it should pickup the first X bars from RTH sessions, I don't see where in the logic it requires extended hours turned on?

Really could use your help, or anyone, on how to get the code to work the same with either extended hours turned on or off.

Best,

H

Here is the latest code..
Code:
# --------------------------------------------------------------
# Relative Volume Code with Price Exhaustion Logic
# hboogie
# Credit to Halcyonguy for logic correction and overhaul
# https://usethinkscript.com/threads/dynamic-zero-line-and-rvol-contraction.17238/
# edits by hboogie on 1-24-2024
# --------------------------------------------------------------

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

def gd = getday();
def lastday = getlastday();
def currentday = gd == lastday;

def lastbar = (!isnan(close) and isnan(close[-1]));


# Define Rvol Thresholds
declare lower;

input rvolMultiplier1 = .2;
input rvolMultiplier2 = .5;
input rvolMultiplier3 = .8;
input rvolMultiplier4 = 1;
input rvolMultiplier5 = 3;



input maPeriod = 21;
input pctvolchg1 = 20;  # Percentage volume change
input pctvolchg2 = 10; # Percentage volume change
input pctvolchg3 = 2; # Percentage volume change
input pctvolchg4 = 5; # Percentage volume change
input pctvolchg5 = 2; # Percentage volume change   

def period_bars = 10;

# find barnumber at day open, each day
#def startBar = secondsFromTime(0930) / secondsFromTime(1);  # 9:30 AM EST
def startBar = if secondsFromTime(0930) == 0 then bn else startbar[1];

def barcnt = if bn == 1 then 1 else if secondsFromTime(0930) == 0 then 1 else barcnt[1] + 1;


def endBar = startBar + period_bars - 1;  # First 10 bars

#def sessionVolume = TotalSum(if BarNumber() >= startBar and BarNumber() <= endBar then volume else 0);

def bars_en = (BarNumber() >= startBar and BarNumber() <= endBar);

# Calculate the total sum of volume from the first 10 bars, of each day ( except current bar
#def sessionVolume = TotalSum(if bars_en and !lastbar then volume else 0);

def sessionVolume =  if bars_en and !lastbar then sessionVolume[1] + volume else sessionVolume[1];
#if bn == 1 then 0 else

# get qty of bars, exclude last bar
def bar_qty = if bars_en and !lastbar then bar_qty[1] + 1 else bar_qty[1];

# rvol average
def rvol_avg = sessionVolume / bar_qty;

# session   v  needs to be divided by qty to get avg
#sessionVolume should be divided by a quantity of bars, to get an average, to be used in this,
#and sessionVolume should not include the volume from current bar

#addchartbubble(1, 0, startBar , color.yellow

# Calculate the relative volume change
#def rvolChange = ((volume - sessionVolume) / sessionVolume) * 100;
#def rvolChange = ((sessionVolume - volume) / sessionVolume) * 100;
def rvolChange = ((rvol_avg - volume) / rvol_avg) * 100;
#def rvolChange = ((rvol_avg - volume) / sessionVolume) * 100;

#def n_bars = 500;
# Calculate the oscillator value
#def oscillatorValue = Min(Max(rvolChange, -n_bars), n_bars);
def oscillatorValue = close - ExpAverage(close, maPeriod);

# Choose the Moving Average type (Exponential)
def maValue = ExpAverage(oscillatorValue, maPeriod);

# Determine spike thresholds based on a % increase
def spikeThreshold1 = rvolChange  < pctvolchg1;
def spikeThreshold2 = rvolChange  > pctvolchg2;
def spikeThreshold3 = rvolChange  > pctvolchg3;
def spikeThreshold4 = rvolChange  < pctvolchg4;
def spikeThreshold5 = rvolChange  > pctvolchg5;


# add a factor to make threshold numbers smaller, close to the vol change
#def f = 1/10000;
# Define Rvol Thresholds
def rvolThreshold1 = rvolMultiplier1 * rvol_avg;
def rvolThreshold2 = rvolMultiplier2 * rvol_avg;
def rvolThreshold3 = rvolMultiplier3 * rvol_avg;
def rvolThreshold4 = rvolMultiplier4 * rvol_avg;
def rvolThreshold5 = rvolMultiplier5 * rvol_avg;
#def rvolThreshold1 = rvolMultiplier1 * rvol_avg * f;
#def rvolThreshold2 = rvolMultiplier2 * rvol_avg * f;
#def rvolThreshold3 = rvolMultiplier3 * rvol_avg * f;
#def rvolThreshold4 = rvolMultiplier4 * rvol_avg * f;
#def rvolThreshold5 = rvolMultiplier5 * rvol_avg * f;



# Define logic for volume contraction
def volumeContraction = rvolChange  < (0.4 * AbsValue(rvolChange[1]));

# Plot the zero line
plot ZeroLine = 0;
ZeroLine.SetPaintingStrategy(PaintingStrategy.LINE);
ZeroLine.SetDefaultColor(Color.GRAY);

# Plot colored points for the spike thresholds
plot SpikePoints1 = if spikeThreshold1 then 0 else double.nan;
SpikePoints1.SetPaintingStrategy(PaintingStrategy.POINTS);
SpikePoints1.AssignValueColor(Color.GREEN);
SpikePoints1.setlineweight(3);
#SpikePoints1.hidebubble();

# Plot points
plot SpikePoints2 = if spikeThreshold2 and volumeContraction then 0 else double.nan;
SpikePoints2.SetPaintingStrategy(PaintingStrategy.POINTS);
SpikePoints2.AssignValueColor(Color.MAGENTA);
SpikePoints2.setlineweight(3);
SpikePoints2.hidebubble();

# Plot colored points for the spike thresholds
plot SpikePoints3 = if spikeThreshold3 and volumeContraction then 0 else double.nan;
SpikePoints3.SetPaintingStrategy(PaintingStrategy.POINTS);
SpikePoints3.AssignValueColor(Color.RED);
SpikePoints3.setlineweight(3);
SpikePoints3.hidebubble();

# Plot colored points for the spike thresholds
plot SpikePoints4 = if spikeThreshold4 then 0 else double.nan;
SpikePoints4.SetPaintingStrategy(PaintingStrategy.POINTS);
SpikePoints4.AssignValueColor(Color.YELLOW);
SpikePoints4.setlineweight(3);
SpikePoints4.hidebubble();

# Plot colored points for the spike thresholds
plot SpikePoints5 = if spikeThreshold5 and volumeContraction then 0 else double.nan;
SpikePoints5.SetPaintingStrategy(PaintingStrategy.POINTS);
SpikePoints5.AssignValueColor(Color.CYAN);
SpikePoints5.setlineweight(3);
SpikePoints5.hidebubble();

def absrvol = absvalue(rvolChange);

# Plot the oscillator in the lower pane (Exponential MA)
plot rel_vol_osc = maValue;
rel_vol_osc.AssignValueColor(
         if absrvol >= rvolThreshold5 then Color.RED
    else if absrvol >= rvolThreshold4 then Color.LIGHT_ORANGE
    else if absrvol >= rvolThreshold3 then Color.CYAN
    else if absrvol >= rvolThreshold2 then Color.VIOLET
    else if absrvol >= rvolThreshold1 then Color.GREEN
    else Color.WHITE
);

rel_vol_osc.SetLineWeight(1);
rel_vol_osc.HideTitle();

###################
## Elder Impulse Logic
###################
input length = 13;

def EMA = MovAvgExponential(length=length)."AvgExp";


def impulseSignal = if (EMA < EMA[1])
then 0
else if (EMA > EMA[1])
then 1
else -1;

assignpriceColor(if impulseSignal == 1 then Color.White else if impulseSignal == 0 then Color.MAGENTA else Color.Yellow);

These are the inputs I'm using to provide results that are ok. I hit a wall with the spikepoints but I'm close enough at the moment. Really want to figure out how to get this to work with extended hours turned off.

1706190629356.png
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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