Option Greeks Calculation Labels for ThinkorSwim

markos

Well-known member
VIP
Posted to TSL 7-23-19
11:45 jeffrey2360: Would be great to be able to grab live greeks in a study the same way we get price. HIstorical data is not that important, but having live ratio is very useful (delta/theta)
11:46 Mobius:
Code:
# Delta Calculation
# For Options with 1 dollar strikes
# Mobius
# V01.07.2019

input DayToExpiry = 2; #hint: Don't mess with this.
input Series_IV = 1;

def IV = SeriesVolatility(series = Series_IV);
def K = Floor(close);
def S = close;
def R = GetInterestRate();
def t = DayToExpiry / 365;
def d1 = (log(S / K) + ((R + (sqr(IV) / 2)) * t)) / (IV * sqrt(t));
script cnd

     {

      input data  = 1;

      def a = AbsValue(data);

      def b1 =  .31938153;

      def b2 = -.356563782;

      def b3 = 1.781477937;

      def b4 = -1.821255978;

      def b5 = 1.330274429;

      def b6 =  .2316419;

      def e = 1 / (1 + b6 * a);

      def i = 1 - 1 / Sqrt(2 * Double.Pi) * Exp(-Power(a, 2) / 2) *

            (b1 * e + b2 * e * e + b3 * Power(e, 3) + b4 * Power(e, 4) + b5 * Power(e, 5));

      plot CND = if data < 0

                then 1 - i

                else i;

     }

def Delta = cnd(d1);
AddLabel(1, "Delta Est = " + Delta, color.white);
# End Code Delta Estimate

11:47 Mobius: That is pretty bloody close
11:47 Mobius: usually within a penny
11:48 jeffrey2360: Oh. I got to study that code. Will it be possible to apply this to 4 options in a single study ?
11:50 Mobius: sure. You'll need to run 4 instances of the study, change the ones for puts from the above which is for calls and alter the other strikes.
11:52 Mobius: And it would help to have a degree in mathematics and coding but you can probably do it. Maybe
 
Last edited:
Posted today on thinkScript Lounge - gift from Mobius.

Code:
# Greeks Calculations (originally just Delta)
# For Options
# Mobius
# V02.07.2019
# Added Gamma, Theta, Vega
# K - Option strike price
# N - Standard normal cumulative distribution function
# r - Risk free interest rate
# IV - Volatility of the underlying
# S - Price of the underlying
# t - Time to option's expiry
# Delta = N(d1)
#           d1 = (ln(S/K) + (r + (sqr(IV)/2))t) / (? (sqrt(t)))
# Gamma  = (d2) / S(IV(sqrt(t)))
# d2 =  e -(sqr(d1) / 2) / sqrt(2*pi)
# Theta = ((S d2))IV) / 2 sqrt(t)) - (rK e(rt)N(d4))
#         where phi(d3) = (exp(-(sqr(x)/2))) / (2 * sqrt(t))
#         where d4 = d1 - IV(sqrt(t))
# Vega = S phi(d1) Sqrt(t)
 
input Strike_Spread = 1.00;
input DayToExpiry = 1;
input Series_IV = 1;

def IV = SeriesVolatility(series = Series_IV);
def K = if close >= Round(close / Strike_Spread, 0) * Strike_Spread
        then Round(close/Strike_Spread, 0) * Strike_Spread
        else (Round(close/Strike_Spread, 0) * Strike_Spread) - Strike_Spread;
def S = close;
def r = GetInterestRate();
def t = DayToExpiry / 365;
def d1 = (Log(S / K) + ((r + (Sqr(IV) / 2)) * t)) / (IV * Sqrt(t));
addLabel(1, "Strike: " + AsDollars(K), color.white);
script N 
    {
    input data  = 1;
    def a = AbsValue(data);
    def b1 =  .31938153;
    def b2 = -.356563782;
    def b3 = 1.781477937;
    def b4 = -1.821255978;
    def b5 = 1.330274429;
    def b6 =  .2316419;
    def e = 1 / (1 + b6 * a);
    def i = 1 - 1 / Sqrt(2 * Double.Pi) * Exp(-Power(a, 2) / 2) * 
           (b1 * e + b2 * e * e + b3 * 
            Power(e, 3) + b4 * Power(e, 4) + b5 * Power(e, 5));
    plot CND = if data < 0
               then 1 - i
               else i;
    }
def Delta = N(d1);
AddLabel(1, "Delta = " + Delta, Color.WHITE);
# End Code Delta
# Gamma
def d2 = Exp(-(Sqr(d1) / 2)) / Sqrt(2 * Double.Pi);
def Gamma = d2 / (S * (IV * Sqrt(t)));
AddLabel(1, "Gamma = " + Gamma, Color.WHITE);
# End Code Gamma
# Theta
def Theta = -(-(S*d2*IV*(.5000)/
             (2*sqrt(t)))-
             (r*(exp(-r*t)*K))*N(d2)+(S*N(d1)*(.5000)))/365;
# (.5000) variant less than .5 e(X/t)
AddLabel(1, "Theta = " + theta, Color.WHITE);
# End Code Theta
# Vega
def Vega = (S*d2*sqrt(t))/100;
AddLabel(1, "Vega = " + Vega, color.white);
# End Code Greeks
 
Posted today on thinkScript Lounge - gift from Mobius.

Code:
# Greeks Calculations (originally just Delta)
# For Options
# Mobius
# V02.07.2019
# Added Gamma, Theta, Vega
# K - Option strike price
# N - Standard normal cumulative distribution function
# r - Risk free interest rate
# IV - Volatility of the underlying
# S - Price of the underlying
# t - Time to option's expiry
# Delta = N(d1)
#           d1 = (ln(S/K) + (r + (sqr(IV)/2))t) / (? (sqrt(t)))
# Gamma  = (d2) / S(IV(sqrt(t)))
# d2 =  e -(sqr(d1) / 2) / sqrt(2*pi)
# Theta = ((S d2))IV) / 2 sqrt(t)) - (rK e(rt)N(d4))
#         where phi(d3) = (exp(-(sqr(x)/2))) / (2 * sqrt(t))
#         where d4 = d1 - IV(sqrt(t))
# Vega = S phi(d1) Sqrt(t)
 
input Strike_Spread = 1.00;
input DayToExpiry = 1;
input Series_IV = 1;

def IV = SeriesVolatility(series = Series_IV);
def K = if close >= Round(close / Strike_Spread, 0) * Strike_Spread
        then Round(close/Strike_Spread, 0) * Strike_Spread
        else (Round(close/Strike_Spread, 0) * Strike_Spread) - Strike_Spread;
def S = close;
def r = GetInterestRate();
def t = DayToExpiry / 365;
def d1 = (Log(S / K) + ((r + (Sqr(IV) / 2)) * t)) / (IV * Sqrt(t));
addLabel(1, "Strike: " + AsDollars(K), color.white);
script N
    {
    input data  = 1;
    def a = AbsValue(data);
    def b1 =  .31938153;
    def b2 = -.356563782;
    def b3 = 1.781477937;
    def b4 = -1.821255978;
    def b5 = 1.330274429;
    def b6 =  .2316419;
    def e = 1 / (1 + b6 * a);
    def i = 1 - 1 / Sqrt(2 * Double.Pi) * Exp(-Power(a, 2) / 2) *
           (b1 * e + b2 * e * e + b3 *
            Power(e, 3) + b4 * Power(e, 4) + b5 * Power(e, 5));
    plot CND = if data < 0
               then 1 - i
               else i;
    }
def Delta = N(d1);
AddLabel(1, "Delta = " + Delta, Color.WHITE);
# End Code Delta
# Gamma
def d2 = Exp(-(Sqr(d1) / 2)) / Sqrt(2 * Double.Pi);
def Gamma = d2 / (S * (IV * Sqrt(t)));
AddLabel(1, "Gamma = " + Gamma, Color.WHITE);
# End Code Gamma
# Theta
def Theta = -(-(S*d2*IV*(.5000)/
             (2*sqrt(t)))-
             (r*(exp(-r*t)*K))*N(d2)+(S*N(d1)*(.5000)))/365;
# (.5000) variant less than .5 e(X/t)
AddLabel(1, "Theta = " + theta, Color.WHITE);
# End Code Theta
# Vega
def Vega = (S*d2*sqrt(t))/100;
AddLabel(1, "Vega = " + Vega, color.white);
# End Code Greeks
Great Coding, very accurate !
I'm a new member to the community and was wondering if there was any way to code
the Greeks for out of the money options.
Mainly trade SPX Broken Wing Butterflies & Iron Condors for Income.

Scripting the Greeks for the OTM options and estimating the price for a particular strike
would enable me to use in formulas provided by the book "Option Strategy, Risk/Return
Ratios" by Brian Johnson. The Author uses an Excel Spreadsheet but doesn't provide
the coding.
Any help would be greatly appreciated .
Many Thanks , HPC
 
Great Coding, very accurate !
I'm a new member to the community and was wondering if there was any way to code
the Greeks for out of the money options.
Mainly trade SPX Broken Wing Butterflies & Iron Condors for Income.

Scripting the Greeks for the OTM options and estimating the price for a particular strike
would enable me to use in formulas provided by the book "Option Strategy, Risk/Return
Ratios" by Brian Johnson. The Author uses an Excel Spreadsheet but doesn't provide
the coding.
Any help would be greatly appreciated .
Many Thanks , HPC
Don't we have all greeks updated real time in Think or Swim for all options?
 
@mc01439 @H.P.C. I have trying to use # Greeks Calculations (originally just Delta) for Options by Mobius V02.07.2019 but getting N/A for all greeks in TOS SPX option chart. You guys have been using this on SPX or SPX options chart? If SPX options chart then how far are you choosing expiration?
 

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