thinkScript GetStrike() returns as NaN

evanevans

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

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

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

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

Similar threads

Top