thinkScript last price function "Pricetype.LAST"

yman

Member
In ThinkorSwim, how do you select last price/current price is greater/less than study? I only see high low open close.

Code:
# beginning of code -------------------------------
# puts a label with close/last price on the chart

input price = close;
plot CloseLabel = price;
CloseLabel.SetDefaultColor(color.BLUE);
CloseLabel.hide();
AddChartLabel(yes, concat(CloseLabel, " : Close Price"), CloseLabel.TakeValueColor());

# end of code ---------------------------------

Another example that I found

Code:
close("period" = AggregationPeriod.DAY, "priceType" = PriceType.LAST) from 3 bars ago is less than or equal to SimpleMovingAvg()."SMA" from 3 bars ago
 

godfreym

New member
Hi guys. I am trying to code a custom column. I have two columns on a scan result. One is the Last price of an option and the other is the Open price. I am trying to code a new custom column to highlight the column green when the last price is 10% greater than the Open price.

Here is my code. I'm not sure why TOS wont accept the syntax. Any suggestions:

Code:
def O =OPEN;
def l =Last();
plot scan = (O*1.10);
assignBackgroundColor(if scan>l then color.dark_green else color.black);
 

rad14733

Well-known member
VIP
@godfreym Off the top of me head I think this should work... Note that I use self-documenting code rather than cryptic variable names... Also note that you cannot use Last as a priceType... Close works as Last while a candle/bar is painting...

Ruby:
def percenttChange = 0.10;
def lastPrice = close;
def tradePrice = if !IsNan(EntryPrice()) then EntryPrice() else lastPrice;
def diff = lastPrice - tradePrice;
def pct = if tradePrice != 0 then diff / tradePrice else 0;
plot data = pct * 100;
AssignBackgroundColor(if diff / tradePrice >= percenttChange then Color.DARK_GREEN else Color.BLACK);

I don't have any open positions to test this against at the moment...
 
Last edited:

godfreym

New member
Thanks for taking a look. This may work and will give it a shot. I will let you know. Thanks again.

Shoot. Didn't work. Is there another priceType to use to replace Last? That's weird that you cannot calculate off it when its an actual column
 

rad14733

Well-known member
VIP
@godfreym No, the code above is not working... I did get it working and then messed it back up again... I've been having a terrible time coding today due to stress so I think I'm done for the night... I'll get it working tomorrow morning... I've slept like crap for days, if you can even call it sleep...

Edited to add: I managed to fix the code above... Note that it doesn't account for a negative price change percentage but it could be added...
 
Last edited:

godfreym

New member
Man thats awesome. Thank you so much. I will test is first thing after the markets open. I dont think the negative percentage matters so long as it turns green if its greater than 10%.
 

Similar threads

Top