Secondary Security Low To High Code

SJP07

Member
I piece together this code here that would show arrows if the VIX and the SPX [or any symbol] reach a swing high or swing low point at the same time. I'm not sure why it isn't working correctly. It may be the bn = bar number part of the code. I would appreciate any help!

#declare lower;

################################################################################
################################################################################
# SWING LOW
# define swing low points
input length = 10;
def bn = BarNumber();

################################################################################
################################################################################

def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# identify the very last swing low point
#def lowPointOneBarNumber_delta = HighestAll(if swingLow_delta then bn else 0);
#def lowPointOneValue_delta = if bn == lowPointOneBarNumber_delta then low_delta else lowPointOneValue_delta[1];
#plot low1_delta = if bn < lowPointOneBarNumber_delta then Double.NaN else lowPointOneValue_delta;
#low1_delta.SetDefaultColor(Color.GREEN);
#low1_delta.Hide();

# SWING HIGH
# define swing high points
def swingHigh = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
# identify the very last swing high point
#def highPointOneBarNumber_delta = HighestAll(if swingHigh_delta then bn else 0);
#def highPointOneValue_delta = if bn == highPointOneBarNumber_delta then high_delta else highPointOneValue_delta[1];
#plot high1_delta = if bn < highPointOneBarNumber_delta then Double.NaN else highPointOneValue_delta;
#high1_delta.SetDefaultColor(Color.LIGHT_RED);
#high1_delta.Hide();
# identify the 2nd to last swing high point

#plot Up_delta = if swingLow then low else Double.NaN;
#Up_delta.SetDefaultColor(Color.GREEN);
#Up_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Up_delta.SetLineWeight(1);
#Up_delta.Hide();

#plot Down_delta = if swingHigh then high else Double.NaN;
#Down_delta.SetDefaultColor(Color.GREEN);
#Down_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Down_delta.SetLineWeight(1);
#Down_delta.Hide();
################################################################################
################################################################################

#VIX SWING
input secondarySecurity = "VIX";


def High_VIX = HIGH(secondarySecurity)
;
def Low_VIX = LOW(secondarySecurity);
def Open_VIX = OPEN(secondarySecurity)
;
def Close_VIX = CLOSE(secondarySecurity);

def bn_Vix = Sqr(Correlation(BarNumber(), close_VIX, length));

def lastBar_VIX = HighestAll(if IsNaN(Close_VIX) then 0 else bn_VIX);
def offset_VIX = Min(length - 1, lastBar - bn_VIX);

def swingLow_VIX = Low_VIX < Lowest(Low_VIX[1], length - 1) and Low_VIX == GetValue(Lowest(Low_VIX, length), -offset);

def swingHigh_VIX = High_VIX > Highest(High_VIX[1], length - 1) and High_VIX == GetValue(Highest(High_VIX, length), -offset);

################################################################################
################################################################################


################################################################################
################################################################################

def Op_Buy = swingLow is true && swingHigh_VIX is true;

def Op_Sell = swingHigh_VIX is true && swingLow_VIX is true;

plot Option_Buy = Op_Buy;
Option_Buy.SetLineWeight(1);
Option_Buy.SetDefaultColor(Color.ORANGE);
#Option_Buy.SetDefaultColor(Color.Uptick);
Option_Buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Option_Buy.Hide();


plot Option_Sell = Op_Sell;
Option_Sell.SetLineWeight(1);
Option_Sell.SetDefaultColor(Color.ORANGE);
#Option_Sell.SetDefaultColor(Color.Downtick);
Option_Sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#Option_Sell.Hide();
 
Point #1: Without knowing much about your code, this part can't be helping:

Code:
def Op_Sell = swingHigh_VIX is true && swingLow_VIX is true;


Point #2: please enclose your code in
"code" tags.

Point #3: I see you're using HighestAll to find the last bar, which is NOT RECOMMENDED.
Code:
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastBar_VIX = HighestAll(if IsNaN(Close_VIX) then 0 else bn_VIX);


See below references, HighestAll is NOT RECOMMENDED for finding the LastBar in the Thinkscript Tutorials, and they recommend an alternate method:

Reference:
https://tlc.thinkorswim.com/center/...dvanced/Chapter-12---Past-Offset-and-Prefetch

Apart from its influence on recent values, future offset can be useful in a number of specific tasks. For example, it is widely employed to indicate the level of the last close price across several most recent bars:

Code:
input lineLength = 4;
def lastBar = !IsNaN(close) && IsNaN(close[-1]);
def lastClose = if lastBar then close else lastClose[1];
plot data = if IsNaN(close[-lineLength-1]) then lastClose[-lineLength] else Double.NaN;
data.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
data.SetDefaultColor(Color.BLUE);

This script records the last close price and plots it as a line across the last 4 bars, the number being customizable via the input parameter. In case you’re wondering what those IsNaN and !IsNaN are, refer to the Appendix D. Here, these constants are used for finding the last bar on chart. We strongly recommend that you use the “IsNaN + future offset” method for detection of the last bar, not the popular HighestAll method:

Code:
def isLastBar = BarNumber() == HighestAll(if !IsNaN(close) then BarNumber() else Double.NEGATIVE_INFINITY);
plot LastBarSignal = isLastBar;
LastBarSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);

The script above makes the study dependent on all bars of chart, which may result in productivity problems. Besides, the re-calculation mode will be set to once per bar instead of once per quote, which may also affect the output in an unwanted way. Should you need to fix the last bar in your script, use the future offset version:

Code:
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
plot LastBarSignal = isLastBar;
LastBarSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
 
Last edited:
I piece together this code here that would show arrows if the VIX and the SPX [or any symbol] reach a swing high or swing low point at the same time. I'm not sure why it isn't working correctly. It may be the bn = bar number part of the code. I would appreciate any help!

#declare lower;

################################################################################
################################################################################
# SWING LOW
# define swing low points
input length = 10;
def bn = BarNumber();

################################################################################
################################################################################

def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# identify the very last swing low point
#def lowPointOneBarNumber_delta = HighestAll(if swingLow_delta then bn else 0);
#def lowPointOneValue_delta = if bn == lowPointOneBarNumber_delta then low_delta else lowPointOneValue_delta[1];
#plot low1_delta = if bn < lowPointOneBarNumber_delta then Double.NaN else lowPointOneValue_delta;
#low1_delta.SetDefaultColor(Color.GREEN);
#low1_delta.Hide();

# SWING HIGH
# define swing high points
def swingHigh = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
# identify the very last swing high point
#def highPointOneBarNumber_delta = HighestAll(if swingHigh_delta then bn else 0);
#def highPointOneValue_delta = if bn == highPointOneBarNumber_delta then high_delta else highPointOneValue_delta[1];
#plot high1_delta = if bn < highPointOneBarNumber_delta then Double.NaN else highPointOneValue_delta;
#high1_delta.SetDefaultColor(Color.LIGHT_RED);
#high1_delta.Hide();
# identify the 2nd to last swing high point

#plot Up_delta = if swingLow then low else Double.NaN;
#Up_delta.SetDefaultColor(Color.GREEN);
#Up_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Up_delta.SetLineWeight(1);
#Up_delta.Hide();

#plot Down_delta = if swingHigh then high else Double.NaN;
#Down_delta.SetDefaultColor(Color.GREEN);
#Down_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Down_delta.SetLineWeight(1);
#Down_delta.Hide();
################################################################################
################################################################################

#VIX SWING
input secondarySecurity = "VIX";


def High_VIX = HIGH(secondarySecurity)
;
def Low_VIX = LOW(secondarySecurity);
def Open_VIX = OPEN(secondarySecurity)
;
def Close_VIX = CLOSE(secondarySecurity);

def bn_Vix = Sqr(Correlation(BarNumber(), close_VIX, length));

def lastBar_VIX = HighestAll(if IsNaN(Close_VIX) then 0 else bn_VIX);
def offset_VIX = Min(length - 1, lastBar - bn_VIX);

def swingLow_VIX = Low_VIX < Lowest(Low_VIX[1], length - 1) and Low_VIX == GetValue(Lowest(Low_VIX, length), -offset);

def swingHigh_VIX = High_VIX > Highest(High_VIX[1], length - 1) and High_VIX == GetValue(Highest(High_VIX, length), -offset);

################################################################################
################################################################################


################################################################################
################################################################################

def Op_Buy = swingLow is true && swingHigh_VIX is true;

def Op_Sell = swingHigh_VIX is true && swingLow_VIX is true;

plot Option_Buy = Op_Buy;
Option_Buy.SetLineWeight(1);
Option_Buy.SetDefaultColor(Color.ORANGE);
#Option_Buy.SetDefaultColor(Color.Uptick);
Option_Buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Option_Buy.Hide();


plot Option_Sell = Op_Sell;
Option_Sell.SetLineWeight(1);
Option_Sell.SetDefaultColor(Color.ORANGE);
#Option_Sell.SetDefaultColor(Color.Downtick);
Option_Sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#Option_Sell.Hide();
The way you have that written you would need to use a chart with SPX. Also, while I was working on this I see Tradecombine found one problem, that is fixed in the code below with the Op_Sell.

This code identifies SPX as the first symbol and VIX as the second. You should be able to use this on charts for most symbols, not just SPX and VIX.

The image below shows the SPX/VIX match of swings on AAL.

Capture.jpg

Ruby:
#declare lower;

################################################################################
################################################################################
# SWING LOW
# define swing low points
input length = 10;
def bn = BarNumber();
input sym = "SPX";

################################################################################
################################################################################

def lastBar = HighestAll(if IsNaN(close(sym)) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

def swingLow = low(sym) < Lowest(low(sym)[1], length - 1) and low(sym) == GetValue(Lowest(low(sym), length), -offset);
# identify the very last swing low point
#def lowPointOneBarNumber_delta = HighestAll(if swingLow_delta then bn else 0);
#def lowPointOneValue_delta = if bn == lowPointOneBarNumber_delta then low_delta else lowPointOneValue_delta[1];
#plot low1_delta = if bn < lowPointOneBarNumber_delta then Double.NaN else lowPointOneValue_delta;
#low1_delta.SetDefaultColor(Color.GREEN);
#low1_delta.Hide();

# SWING HIGH
# define swing high points
def swingHigh = high(sym) > Highest(high(sym)[1], length - 1) and high(sym) == GetValue(Highest(high(sym), length), -offset);
# identify the very last swing high point
#def highPointOneBarNumber_delta = HighestAll(if swingHigh_delta then bn else 0);
#def highPointOneValue_delta = if bn == highPointOneBarNumber_delta then high_delta else highPointOneValue_delta[1];
#plot high1_delta = if bn < highPointOneBarNumber_delta then Double.NaN else highPointOneValue_delta;
#high1_delta.SetDefaultColor(Color.LIGHT_RED);
#high1_delta.Hide();
# identify the 2nd to last swing high point

#plot Up_delta = if swingLow then low else Double.NaN;
#Up_delta.SetDefaultColor(Color.GREEN);
#Up_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Up_delta.SetLineWeight(1);
#Up_delta.Hide();

#plot Down_delta = if swingHigh then high else Double.NaN;
#Down_delta.SetDefaultColor(Color.GREEN);
#Down_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Down_delta.SetLineWeight(1);
#Down_delta.Hide();
################################################################################
################################################################################

#VIX SWING
input secondarySecurity = "VIX";


def High_VIX = HIGH(secondarySecurity)
;
def Low_VIX = LOW(secondarySecurity);
def Open_VIX = OPEN(secondarySecurity)
;
def Close_VIX = CLOSE(secondarySecurity);

def bn_Vix = Sqr(Correlation(BarNumber(), close_VIX, length));

def lastBar_VIX = HighestAll(if IsNaN(Close_VIX) then 0 else bn_VIX);
def offset_VIX = Min(length - 1, lastBar - bn_VIX);

def swingLow_VIX = Low_VIX < Lowest(Low_VIX[1], length - 1) and Low_VIX == GetValue(Lowest(Low_VIX, length), -offset);

def swingHigh_VIX = High_VIX > Highest(High_VIX[1], length - 1) and High_VIX == GetValue(Highest(High_VIX, length), -offset);

################################################################################
################################################################################


################################################################################
################################################################################

def Op_Buy = swinglow is true && swingHigh_VIX is true;

def Op_Sell = swingHigh is true && swinglow_VIX is true;

plot Option_Buy = Op_Buy;
Option_Buy.SetLineWeight(1);
Option_Buy.SetDefaultColor(Color.ORANGE);
#Option_Buy.SetDefaultColor(Color.Uptick);
Option_Buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Option_Buy.Hide();


plot Option_Sell = Op_Sell;
Option_Sell.SetLineWeight(1);
Option_Sell.SetDefaultColor(Color.ORANGE);
#Option_Sell.SetDefaultColor(Color.Downtick);
Option_Sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#Option_Sell.Hide();
 
The way you have that written you would need to use a chart with SPX. Also, while I was working on this I see Tradecombine found one problem, that is fixed in the code below with the Op_Sell.

This code identifies SPX as the first symbol and VIX as the second. You should be able to use this on charts for most symbols, not just SPX and VIX.

Themage below shows the SPX/VIX match of swings on AAL.
Hello , this looks pretty awesome never thought of this! I just placed it in my studies just now & I see the buy signals but nothing matching for sell , could it be there was no correlation for sell signals?
 
Hello , this looks pretty awesome never thought of this! I just placed it in my studies just now & I see the buy signals but nothing matching for sell , could it be there was no correlation for sell signals?

Yeah, I think there must not be any correlations on the chart timeframe and symbol you selected as the following image from today's activity shows both buy and sell arrows

Screenshot-2022-09-30-190402.png
 
The way you have that written you would need to use a chart with SPX. Also, while I was working on this I see Tradecombine found one problem, that is fixed in the code below with the Op_Sell.

This code identifies SPX as the first symbol and VIX as the second. You should be able to use this on charts for most symbols, not just SPX and VIX.

The image below shows the SPX/VIX match of swings on AAL.
Can you insert a sound signal when an arrow appears???
 
Can you insert a sound signal when an arrow appears???

Sound alert added to code below

Ruby:
#declare lower;

################################################################################
################################################################################
# SWING LOW
# define swing low points
input length = 10;
def bn = BarNumber();
input sym = "SPX";

################################################################################
################################################################################

def lastBar = HighestAll(if IsNaN(close(sym)) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

def swingLow = low(sym) < Lowest(low(sym)[1], length - 1) and low(sym) == GetValue(Lowest(low(sym), length), -offset);
# identify the very last swing low point
#def lowPointOneBarNumber_delta = HighestAll(if swingLow_delta then bn else 0);
#def lowPointOneValue_delta = if bn == lowPointOneBarNumber_delta then low_delta else lowPointOneValue_delta[1];
#plot low1_delta = if bn < lowPointOneBarNumber_delta then Double.NaN else lowPointOneValue_delta;
#low1_delta.SetDefaultColor(Color.GREEN);
#low1_delta.Hide();

# SWING HIGH
# define swing high points
def swingHigh = high(sym) > Highest(high(sym)[1], length - 1) and high(sym) == GetValue(Highest(high(sym), length), -offset);
# identify the very last swing high point
#def highPointOneBarNumber_delta = HighestAll(if swingHigh_delta then bn else 0);
#def highPointOneValue_delta = if bn == highPointOneBarNumber_delta then high_delta else highPointOneValue_delta[1];
#plot high1_delta = if bn < highPointOneBarNumber_delta then Double.NaN else highPointOneValue_delta;
#high1_delta.SetDefaultColor(Color.LIGHT_RED);
#high1_delta.Hide();
# identify the 2nd to last swing high point

#plot Up_delta = if swingLow then low else Double.NaN;
#Up_delta.SetDefaultColor(Color.GREEN);
#Up_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Up_delta.SetLineWeight(1);
#Up_delta.Hide();

#plot Down_delta = if swingHigh then high else Double.NaN;
#Down_delta.SetDefaultColor(Color.GREEN);
#Down_delta.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Down_delta.SetLineWeight(1);
#Down_delta.Hide();
################################################################################
################################################################################

#VIX SWING
input secondarySecurity = "VIX";


def High_VIX = high(secondarySecurity)
;
def Low_VIX = low(secondarySecurity);
def Open_VIX = open(secondarySecurity)
;
def Close_VIX = close(secondarySecurity);

def bn_Vix = Sqr(Correlation(BarNumber(), Close_VIX, length));

def lastBar_VIX = HighestAll(if IsNaN(Close_VIX) then 0 else bn_Vix);
def offset_VIX = Min(length - 1, lastBar - bn_Vix);

def swingLow_VIX = Low_VIX < Lowest(Low_VIX[1], length - 1) and Low_VIX == GetValue(Lowest(Low_VIX, length), -offset);

def swingHigh_VIX = High_VIX > Highest(High_VIX[1], length - 1) and High_VIX == GetValue(Highest(High_VIX, length), -offset);

################################################################################
################################################################################


################################################################################
################################################################################

def Op_Buy = swingLow is true && swingHigh_VIX is true;

def Op_Sell = swingHigh is true && swingLow_VIX is true;

plot Option_Buy = Op_Buy;
Option_Buy.SetLineWeight(1);
Option_Buy.SetDefaultColor(Color.ORANGE);
#Option_Buy.SetDefaultColor(Color.Uptick);
Option_Buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Option_Buy.Hide();


plot Option_Sell = Op_Sell;
Option_Sell.SetLineWeight(1);
Option_Sell.SetDefaultColor(Color.ORANGE);
#Option_Sell.SetDefaultColor(Color.Downtick);
Option_Sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#Option_Sell.Hide();

input alerts = yes;
Alert(Op_Sell or Op_Buy, if Op_Sell then "SELL" else "BUY", Alert.BAR, Sound.Chimes);
 

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
456 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