Can I declare a variable to hold state for later use?

oldyoungguy

New member
VIP
I am painting yellow arrows to signal sell and green arrows for buy. but sometimes there are multiple buys or sells on the row one after another What I wanted is buy, sell and buy, sell again. I am thinking of having a state holder variable tell me if currently there is a buy plotted or not. if yes, then another buy will not allow. The variable will be reset by a sell. Does ThinkScript support this?

I don't know why I cannot paste my image/link here today. I tried many times. so there is the link:
 
Solution
The basic framework would be something like this...
Def Toggle = if Buy then Yes else if Sell then No else Toggle[1];
Plot BuyArrow = Buy and !Toggle;
Plot SellArrow = Sell and Toggle;

You just set a Variable to Variable[1] to hold its state.
The basic framework would be something like this...
Def Toggle = if Buy then Yes else if Sell then No else Toggle[1];
Plot BuyArrow = Buy and !Toggle;
Plot SellArrow = Sell and Toggle;

You just set a Variable to Variable[1] to hold its state.
 
Solution

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

@oldyoungguy
Ruby:
def B_Bar = <Your Buy Condition Here>;
def S_Bar = <Your Sell Condition Here>;

def Buy;
def Sell;
if B_Bar {
    Buy = yes;
    Sell = Double.NaN;
} else if S_Bar {
    Buy = Double.NaN;
    Sell = yes;
} else {
    Buy = Buy[1];
    Sell = Sell[1];
}

plot GU1 = Buy and IsNaN(Buy[1]);
GU1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
GU1.SetDefaultColor(GetColor(6));
GU1.SetLineWeight(3);

plot GX1 = Sell and IsNaN(Sell[1]);
GX1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
GX1.SetDefaultColor(GetColor(5));
GX1.SetLineWeight(3);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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