thinkScript GetStrike() returns as NaN

evanevans

Active member
In the following code, I'm working with the GetStrike function but I get the result of NaN for the else side of the final label statement below. Why? I can't for the life of me seem to write a workaround.

Code:
def InPosition = If GetQuantity()<>0 then 1 else 0;
def IsOption = if GetStrike()>0 then 1 else 0;
def Quantity = GetQuantity();

AddLabel(InPosition, Quantity, If Quantity>0 then Color.LIGHT_GREEN else Color.PINK);

AddLabel(InPosition, AsDollars((If IsOption then Quantity*100 else Quantity)*Close), Color.LIGHT_GRAY);
 
Try replacing the def Quantity line with this:
Code:
def Quantity = if isNaN(GetQuantity()) then 0 else GetQuantity();

also you don't need if statements in the def InPosition or def IsOption line

Code:
def InPosition = GetQuantity() < 0 or GetQuantity() > 0;
def IsOption = GetStrike() > 0;

is the same thing, if true it'll return 1, if false it'll return a 0
 
It's amazing how it doesn't work. It's like dumbfounding me. Can you take another look at what I've done my friend @Welkin?

Code:
# OpenPL_Labels
# Evan Evans
# ©2020

def InPosition = GetQuantity()<>0;
def IsOption = GetStrike()>0;
def Quantity = If isNaN(GetQuantity()) then 0 else GetQuantity();

AddLabel(InPosition, Quantity, If Quantity>0 then Color.LIGHT_GREEN else Color.PINK);

AddLabel(InPosition, AsDollars(Quantity*(If IsOption then 100 else 1)*Close), Color.LIGHT_GRAY);

AddLabel(InPosition, (If GetOpenPL()>0 then "+" else "-") + AsDollars(GetOpenPL()), If GetOpenPL()>0 then Color.LIGHT_GREEN else Color.PINK);

AddLabel(InPosition, Round(GetOpenPL()/(GetAveragePrice()*(Quantity*(If IsOption then 100 else 1)))*100,1) + "%", If GetOpenPL()>0 then Color.LIGHT_GREEN else Color.PINK);

ssa1ZfU.png


On stock it will show NaN. On options it works.

EDIT:
I also made this change, to no effect. No difference:
Code:
# OpenPL_Labels
# Evan Evans
# ©2020

def Quantity = If isNaN(GetQuantity()) then 0 else GetQuantity();
def InPosition = Quantity<>0;
def IsOption = GetStrike()>0;
def Multiplier = If IsOption then 100 else 1;

AddLabel(InPosition, Quantity, If Quantity>0 then Color.LIGHT_GREEN else Color.PINK);

AddLabel(InPosition, AsDollars(Quantity*Multiplier*Close), Color.LIGHT_GRAY);

AddLabel(InPosition, (If GetOpenPL()>0 then "+" else "-") + AsDollars(GetOpenPL()), If GetOpenPL()>0 then Color.LIGHT_GREEN else Color.PINK);

AddLabel(InPosition, Round(GetOpenPL()/(GetAveragePrice()*(Quantity*Multiplier))*100,1) + "%", If GetOpenPL()>0 then Color.LIGHT_GREEN else Color.PINK);
 
Last edited:
Wow. This appeared to fix it
Code:
def Multiplier = If isNaN(IsOption) then 1 else If IsOption then 100 else 1;

I say wow, because I am proud of myself for figuring that out :)
 
It looks like you are trying to mix a numeric value of 100 with a boolean of 1 or 0 and that is most likely why you are getting the error... 💡

Edited to add: 1 or 0, yes or no, and true or false are booleans...
 
I'm trying to backtest a strategy with TOS OnDemand. The script contains GetQuantity() to get the current open position quantity, but even though I've made a trade and see the position size in the watchlist column, I'm not getting a value in my ThinkScript code. The code is as simple as this:
Code:
AddLabel(GetQuantity() != 0, "Qty: " + GetQuantity(), if GetQuantity() > 0 then color.GREEN else color.RED);

Also, even though I have "Show Trades" set on my chart, the chart isn't showing the trades I make in OnDemand.

Are there limitations as to what I can do in OnDemand mode?
 
sometimes with tos you think it doesnt work, but if you keep the study loaded it ends up working, its almost as if TOS has to approve the script. or atleast thats my explanation since it runs on their server. ive had scripts not work and next few days they start working fine. then i alter once peice of code and it stops working only to work again a few days later. i dont know why it does that but my theory above is all i can think of.
 
sometimes with tos you think it doesnt work, but if you keep the study loaded it ends up working, its almost as if TOS has to approve the script. or atleast thats my explanation since it runs on their server. ive had scripts not work and next few days they start working fine. then i alter once peice of code and it stops working only to work again a few days later. i dont know why it does that but my theory above is all i can think of.
Mine always work but there are times when I have to go into Edit Studies and click Apply and Ok again... Sometimes more than once... But a few do lag in painting for more than a few seconds when initially loading and when that happens it might be the timeframe... I keep my charts at the shortest timeframes needed and load the least number of studies/indicators... And if they aren't actively being use they aren't in the list for that chart...
 
I'm trying to backtest a strategy with TOS OnDemand. The script contains GetQuantity() to get the current open position quantity, but even though I've made a trade and see the position size in the watchlist column, I'm not getting a value in my ThinkScript code. The code is as simple as this:
Code:
AddLabel(GetQuantity() != 0, "Qty: " + GetQuantity(), if GetQuantity() > 0 then color.GREEN else color.RED);

Also, even though I have "Show Trades" set on my chart, the chart isn't showing the trades I make in OnDemand.

Are there limitations as to what I can do in OnDemand mode?
Do You have a solution to this problem?
 

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