Tutorial: Hold A Condition True Until... For ThinkOrSwim

E_Sophiea

New member
I appreciate any help!

For a bigger project (combining multiple indicators), I need to be able to reference a long or short signal in an indicator from an prior time. The signal is a cross of a momentum indicator (such as RSI or MoneyFlowIndex) from above Overbought to below the Overbought line, or from below Oversold to back above Oversold. So the value of the indicator in both cases is between the Overbought and Oversold lines, and the only "identifier" that it's long or short is which direction it most recently crossed.

The time from the prior signal is always changing, so I can't tell it to look back a certain number of periods. I can't find any logic in ThinkScript to tell it to look for the most recent signal.

Any thoughts on how to make a clean way to do this? The logic is easy if "long" is only when it's oversold, but I can't seem to find any logic to define "long" as the entire time after crossing above oversold but not yet having gone above overbought.

Another way of thinking of this: I want the indicator to have a value of long or short, that stays that value until the opposite signal is registered.

Reason: I want to reference other momentum indicators as being long or short in a combined indicator.

Thanks in advance!
 
This is both a very common question and need for many.
Using this format you can make a 'switch' of sorts for each condition you wish to hold true or false.

def LE = < Your Long Entry Condition1 >
def LX = < Your Long Exit Condition1 >

def Long;
if LE {
Long = 1;
} else if LX {
Long = 0;
} else {
Long = Long[1];
}

Add as many 'switches' as you like, just be sure to change all variable names.
 
Thanks! I'll try that.
Here's what I did that seems to work as well:

def LastBullSignal = if RSI crosses above OverSold then GetYYYYMMDD() else LastBullSignal[1];
def LastBearSignal = if RSI crosses below OverBought then GetYYYYMMDD() else LastBearSignal[1];
def RSIBullish = LastBullSignal > LastBearSignal;
def RSIBearish = LastBearSignal > LastBullSignal;

RSI.AssignValueColor(If LastBullSignal > LastBearSignal then COLOR.GREEN else COLOR.RED );
The limitation is that you need the chart to be long enough that it sees both a bull and bear signal.
Credit for this goes to BabigDiggyD
 
Last edited:
This is both a very common question and need for many.
Using this format you can make a 'switch' of sorts for each condition you wish to hold true or false.

def LE = < Your Long Entry Condition1 >
def LX = < Your Long Exit Condition1 >

def Long;
if LE {
Long = 1;
} else if LX {
Long = 0;
} else {
Long = Long[1];
}

Add as many 'switches' as you like, just be sure to change all variable names.
Thanks for this! I can't get it to work, though. I'm not understanding your use of semicolons and {} I think?
Here's what I tried and Thinkscript doesn't like it:

def LastBullSignal = if RSI crosses above OverSold then GetYYYYMMDD() else LastBullSignal[1];
def LastBearSignal = if RSI crosses below OverBought then GetYYYYMMDD() else LastBearSignal[1];
def RSIBullish = LastBullSignal > LastBearSignal;
def RSIBearish = LastBearSignal > LastBullSignal;

def RSILong(if RSIBullish then RSILong = 1 else RSILong = -1);
 
Thanks for this! I can't get it to work, though. I'm not understanding your use of semicolons and {} I think?
Here's what I tried and Thinkscript doesn't like it:

def LastBullSignal = if RSI crosses above OverSold then GetYYYYMMDD() else LastBullSignal[1];
def LastBearSignal = if RSI crosses below OverBought then GetYYYYMMDD() else LastBearSignal[1];
def RSIBullish = LastBullSignal > LastBearSignal;
def RSIBearish = LastBearSignal > LastBullSignal;

def RSILong(if RSIBullish then RSILong = 1 else RSILong = -1);

do you really need to find a date? or just compare when 2 signals happened?
i changed the first 2 variables , to use barnumber instead of a date.
i added a colored label to display bull or bear.


your last line is wrong. it should be,
def
variable name
=
formula
i rewrote it as def rsi_sig = ...


Code:
def na = double.nen;
def bn = barnumber();
def LastBullSignal = if RSI crosses above OverSold then bn else LastBullSignal[1];
def LastBearSignal = if RSI crosses below OverBought then bn else LastBearSignal[1];
def RSIBullish = LastBullSignal > LastBearSignal;
def RSIBearish = LastBearSignal > LastBullSignal;


# retain the most recent bull/bear signal , as a 1 or -1.
def RSI_sig = if bn == 1 then 0
else if RSIBullish then 1
else if RSIBearish then -1
else RSI_sig[1];

addlabel(1, (if RSI_sig > 0 then "Bull" else if RSI_sig < 0 then "Bear" else "-"), (if RSI_sig > 0 then color.green else if RSI_sig < 0 then color.red else color.gray));
#


i think the above code will work for you.
here is another way to look back and find when a signal happened.
valuewhen() pine code converted
https://usethinkscript.com/threads/...tos-script-not-pine-studies.11424/#post-99310
 

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