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);
 

Welkin

Active member
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
 

evanevans

Active member
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:

evanevans

Active member
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 :)
 

rad14733

Well-known member
VIP
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...
 

epete

Member
VIP
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?
 

XeoNoX

Well-known member
VIP
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.
 

rad14733

Well-known member
VIP
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...
 

LukaszC

New member
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?
 

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.
Top