Can't figure out how to remember if I'm short or long

upandacross

New member
VIP
Love ThinkScript but it can be frustrating as well. Here's my problem:

I want to put up a label indicating the last order placed during backtesting. My attempt to use getQuantity(getSymbol()) isn't working. Here's my code:


Code:
def isLong =  getQuantity(getSymbol()) > 0;
def isShort = getQuantity(getSymbol()) < 0;
AddLabel(yes,
         if      isLong  then "Buy"
         else if isShort then "Short"
         else                 "Neutral",
         if      isLong  then Color.DARK_GREEN
         else if isShort then Color.DARK_RED
         else                 Color.GRAY);



With this code, the script blows up - no orders, no FloatingPL, no label - nothing
Without this code, I get orders, FloatingPL, but no label (of course)
 
Last edited:
Solution
@upandacross It sounds like you are working with a strategy? Instead of using GetQuantity(), try substituting the entry and exit variables for your strategy. Something such as def islong = if YourLongEntryCriteriaHere then 1 else if YourLongExitCriteriaHere then 0 else islong[1]; will capture the signal to go long and hold it until the exit point. Then it will reset and wait for the next entry point. The same can be done for the short side.
@upandacross It sounds like you are working with a strategy? Instead of using GetQuantity(), try substituting the entry and exit variables for your strategy. Something such as def islong = if YourLongEntryCriteriaHere then 1 else if YourLongExitCriteriaHere then 0 else islong[1]; will capture the signal to go long and hold it until the exit point. Then it will reset and wait for the next entry point. The same can be done for the short side.
 
Solution
Is
@upandacross It sounds like you are working with a strategy? Instead of using GetQuantity(), try substituting the entry and exit variables for your strategy. Something such as def islong = if YourLongEntryCriteriaHere then 1 else if YourLongExitCriteriaHere then 0 else islong[1]; will capture the signal to go long and hold it until the exit point. Then it will reset and wait for the next entry point. The same can be done for the short side.
Is there is a better way to know pending Long or Short in a strategy without using temporary variables? Thank you!
 
Hi! My GetQuantity() always returns a positive number:

Code:
AddChartBubble(yes, low - 1, getquantity() , Color.GREEN , yes);

It always shows a positive number, even if the strategy is currently has a short position. Has anyone else experienced this?
 

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

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