Convert option symbol to underlying symbol ?

Popeious

New member
Is it possible, using thinkscript to take an option position, and parse the underlying equity symbol from it... ?

I am primarily an options trader.

I would like to find a way to change the background color of a chart based on the position P/L being either positive or negative, but... I'm not watching the chart of the option, rather, I am watching the chart of the underlying.

So, as an example; I might have a June Expiry of AAPL (ie: .AAPL240621C180) on but I'm watching the AAPL chart on the 3m1d.

I want to change the background color of that chart to a light-red (hex color defined) if the equity position is at a loss of Loss%, but leave the default background color if the position has SomeProfit% profit, and change the background to a light-green (hex color defined) if the position is over SomeOtherProfit% profitable.

So, example;
Loss%: 50% (so if the loss is greater than 50% turn the background red)
(from -49.9$ to 49.9% the background is default color)
Profit%-A: 50% (if the profit exceed 50% but is less than 200%, set background to dark green)
Profit%-B: 200% (if the profit exceed 200%, set background to light green)

Maybe the script has to say... for the chart (use it's symbol), and see if there are any positions (options with this symbol and/or commons) and if so, figure out if the position is + or -... and based on a 'threshold/ percentage of profit or loss, and decide what color to paint the background .

Is this possible? Or is it a pipe-dream?

I suppose.. we then run into a problem, if there are multiple positions for the same symbol... like AAPL May 100's, June 110's, and AAPL common's. (unless you just add all positions together as a composite; or all options and all commons). But.. whatever.

I'm reviewing 'Conditional Backgrounds'
https://usethinkscript.com/threads/change-charts-background-based-on-thinkscript-condition.3611/
https://usethinkscript.com/threads/condition-based-background-color.17259/
https://usethinkscript.com/threads/sound-alert-and-background-color-change.4004/#post-36909

This Mobius Script draws a line of the Average Position Price for the symbol.
https://thinkscript101.com/thinkorswim-positions-on-chart/

It seems there's a limited number of 'portfolio functions' ; though;
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio/GetAveragePrice
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio/GetOpenPL

Options Profit/Loss Label on chart;
https://usethinkscript.com/threads/option-profit-loss-label-on-chart.10777/#post-94923

Working with Options:
https://usethinkscript.com/threads/labels-of-options-near-the-atm-option.17772/


I haven't worked out the BIG QUESTION... of dealing with Options positions... but... using ChatGTP I did manage to get 'most' of the code to work.. (still working on it... but... and it works great for STOCK positions and not at all for OPTIONS positions).

Code:
# Version 6.5
# ColorizeProfitLossBackground
# User-definable thresholds for BigLoss, LittleLoss, LittleProfit, BigProfit
# and user-definable background colors depending on the PnL of the position according to threshold
# User-definable to show labels or not
# still working on allowing label background color to be user selectable. 

declare lower;

# User input for label visibility
input showLabels = yes;

# User input for label text background colors
input profitLabelBackgroundColor = {"MAGENTA", "CYAN", "PINK", "LIGHT_GRAY", "ORANGE", "RED", "GREEN", default "WHITE", "GREY"};
#Color.WHITE;
input lossLabelBackgroundColor = {"MAGENTA", "CYAN", "PINK", "LIGHT_GRAY", "ORANGE", "RED", "GREEN", default "YELLOW", "GREY"};
#Color.YELLOW;

#DefineGlobalColor("LossLabelColor", lossLabelBackgroundColor);
#DefineGlobalColor("ProfitLabelColor", profitLabelBackgroundColor);

# User input for percentage thresholds
input Profit_threshold_a = 50;  # Threshold for bright green; profitcolor1; very profitable
input Profit_threshold_b = 5;  # Threshold for dark green; profitColor2; profit
input Loss_threshold_c = -5; # Threshold for dark red; losscolor1; loss
input Loss_threshold_d = -25; # Threshold for bright red; losscolor2; very loss

# User input for colors based on percentage thresholds
input brightGreenColorR = 40;
input brightGreenColorG = 99;
input brightGreenColorB = 11;
DefineGlobalColor("profitColor1", CreateColor(brightGreenColorR, brightGreenColorG, brightGreenColorB));
# very profitable

input darkGreenColorR = 23;
input darkGreenColorG = 43;
input darkGreenColorB = 13;
DefineGlobalColor("profitColor2", CreateColor(darkGreenColorR, darkGreenColorG, darkGreenColorB));
# kind of a profit

input blackColorR = 13;
input blackColorG = 0;
input blackColorB = 77;
DefineGlobalColor("blackColor", CreateColor(blackColorR, blackColorG, blackColorB));
# within profit-loss threshold

input darkRedColorR = 74;
input darkRedColorG = 14;
input darkRedColorB = 18;
DefineGlobalColor("lossColor1", CreateColor(darkRedColorR, darkRedColorG, darkRedColorB));
# kind of a loss

input brightRedColorR = 153;
input brightRedColorG = 3;
input brightRedColorB = 13;
DefineGlobalColor("lossColor2", CreateColor(brightRedColorR, brightRedColorG, brightRedColorB));
# very much a loss



# Get average price and quantity from current position
def avgPrice = GetAveragePrice();
def quantity = if !IsNaN(avgPrice) then Round(GetQuantity(),0) else 0;

# Calculate position value
def currentPrice = close;
def positionValue = quantity * currentPrice;

# Calculate P/L
def costBasis = if !IsNaN(avgPrice) then avgPrice else 0;
def pnl = positionValue - (quantity * costBasis);

def positionstate = if pnl < 0 then -1 else if pnl > 1 then 1 else 0;

# Round the calculated profit or loss to 2 decimal points
def roundedPnl = Round(pnl, 0);

# Calculate percentage P/L
def percentPnl = if positionValue != 0 then Round(pnl / positionValue * 100,0)  else 0;

# Define conditions for background color
def bColor = 
    if percentPnl >= Profit_threshold_a                                    then 5 # very profitable
    else if percentPnl <  Profit_threshold_a and percentPnl >= Profit_threshold_b  then  4 # kind of a profit
    else if percentPnl <  Profit_threshold_b and percentPnl >  Loss_threshold_c  then    3 # within profit-loss threshold
    else if percentPnl <= Loss_threshold_c and percentPnl   >= Loss_threshold_d then     2 # kind of a loss
    else if percentPnl <  Loss_threshold_d then                                          1   # very much a loss
    else 0;

# Plot profit or loss, quantity, and percentage P/L as label

AddLabel(showlabels, 
    if pnl < 0 then 
        "Loss: $" + AsPrice(-roundedPnl) + " (" + AsPercent(percentPnl / 100) + ")" + " / Qty: " + AsText(quantity) 
    else if pnl > 0 then
        "Profit: $" + AsPrice(roundedPnl) + " (" + AsPercent(percentPnl / 100) + ")" + " / Qty: " + AsText(ROUND(quantity,0)) 
    else 
        "", 
            if bColor == 1 or bColor == 2 then Color.YELLOW else Color.WHITE);
          # if pnl < 0 then lossLabelBackgroundColor else profitLabelBackgroundColor);

# Define the background color based on thresholds
AssignBackgroundColor(
         if bColor == 5 then CreateColor( brightGreenColorR, brightGreenColorG, brightGreenColorB) else if 
            bColor == 4 then CreateColor( darkGreenColorR, darkGreenColorG, darkGreenColorB) else if 
            bcolor == 2 then CreateColor( darkRedColorR, darkRedColorG, darkRedColorB ) else if 
            bcolor == 1 then CreateColor( brightRedColorR, brightRedColorG, brightRedColorB) else if
            bcolor == 3 AND quantity > 0 then CreateColor( blackColorR, blackColorG, blackColorB) else color.BLACK) ;

2024-03-24_9-43-36 50.jpg
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
478 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