Help with Label code

Mightymorphinchris

Active member
Overview
I would like to create a label on a study that will always be one of four colors:
1. Long: Cyan
2. Long Exit: Green
3. Short: Magenta
4. Short Exit: Red

Current Problems:
1. It's not holding the label to the last color that should be shown (ex. if the last signal was Long then stay cyan until Long Exit).
2. Another potential problem once I get over that first hurdle is that Short Exit signal may come after a Long which should be ignored. Like the first problem, if the current label is Long then the only thing that should change the label is a LongExit or Short signal. Likewise if the current label is Short then the only thing that should change the label is ShortExit or Long.

My broken code:
Ruby:
def LongBuy = if Uptrend == 1 and Uptrend[1] == 0 then 1 else 0;
def LongExit = if ExitUpTrend == 1 and ExitUpTrend[1] == 0 then 1 else 0;

def ShortSell = if DownTrend == 1 and DownTrend[1] == 0 then 1 else 0;
def ShortExit = if ExitDownTrend == 1 and ExitDownTrend[1] == 0 then 1 else 0;

AddLabel(yes,
         text = if LongBuy == 1
                then "Long"
                else if LongExit == 1
                then "Exit Long"
                else if ShortSell == 1
                then "Short"
                else "Short Exit",
         color = if LongBuy == 1
                then Color.Cyan
                else if LongExit == 1
                then Color.Green
                else if ShortSell == 1
                then Color.Magenta
                else Color.Red
        );

If someone could help me out it would be greatly appreciated!
 

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

take a look at this study and see if the formulas help you. it keeps track of longs and shorts.
https://usethinkscript.com/threads/label-with-information-related-to-strategy.7902/#post-75825
I had high hopes but it seems I've failed. I'm pretty sure this is what you were talking about in your code and thought that I translated it correctly and it made sense to fit my need but it still doesn't seem to be working correctly.

Here's what I tried:
Ruby:
def LongBuy = if Uptrend == 1 and Uptrend[1] == 0 then 1 else 0;
def LongExit = if ExitUpTrend == 1 and ExitUpTrend[1] == 0 then 1 else 0;

def ShortSell = if DownTrend == 1 and DownTrend[1] == 0 then 1 else 0;
def ShortExit = if ExitDownTrend == 1 and ExitDownTrend[1] == 0 then 1 else 0;

def bn = barnumber();

def LongTrade = if bn == 1 then 0 else if LongBuy then 1 else if LongExit or ShortSell then 0 else LongTrade[1];
def ShortTrade = if bn == 1 then 0 else if ShortSell then 1 else if ShortExit or LongBuy then 0 else ShortTrade[1];
def LongTradeExit = if bn == 1 then 0 else if LongExit and LongTrade[1] then 1 else if LongBuy or ShortSell then 0 else LongTradeExit[1];
def ShortTradeExit = if bn == 1 then 0 else if ShortExit and ShortTrade[1] then 1 else if ShortSell or LongBuy then 0 else ShortTradeExit[1];

AddLabel(yes,
         text = if LongTrade == 1
                then "Long"
                else if LongTradeExit == 1
                then "Exit Long"
                else if ShortTrade == 1
                then "Short"
                else "Short Exit",
         color = if LongTrade == 1
                then Color.Cyan
                else if LongTradeExit == 1
                then Color.Green
                else if ShortTrade == 1
                then Color.Magenta
                else Color.Red
        );
 
Here is a link to something that might answer maintaining persistent values . The Compound expression might be needed. I think I'm having a a similar issue with a strategy I'm using.
Questions about persistent values
There's a lot of good information in that, thanks for sharing. @halcyonguy solution seems to match up perfectly with the answer in that post as well. I don't think I would need to go the compoundValue route but maybe that's what's going on here.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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