Skip to content

Instantly share code, notes, and snippets.

@NVentimiglia
Created June 25, 2021 04:16
Show Gist options
  • Save NVentimiglia/139b7abebdf95b25601ed0902fb2d65a to your computer and use it in GitHub Desktop.
Save NVentimiglia/139b7abebdf95b25601ed0902fb2d65a to your computer and use it in GitHub Desktop.
ThinkScript OptionVolume : Histogram of near the money option volume
#NIC_OptionVolume
# @NicTheMajestic
# ThinkScript Histogram of near the money option volume
# Requires setting of option expiry
declare lower;
input optionExpiry = "210716";
def price = Round(open, 0);
# CALLS
def c0 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 0)));
def cv0 = if IsNaN(c0) then 0 else c0;
def c1 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 1)));
def cv1 = if IsNaN(c1) then 0 else c1;
def c2 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 2)));
def cv2 = if IsNaN(c2) then 0 else c2;
def c3 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 3)));
def cv3 = if IsNaN(c3) then 0 else c3;
def c4 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 4)));
def cv4 = if IsNaN(c4) then 0 else c4;
def c5 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 5)));
def cv5 = if IsNaN(c5) then 0 else c5;
def c6 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 6)));
def cv6 = if IsNaN(c6) then 0 else c6;
def c7 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 7)));
def cv7 = if IsNaN(c7) then 0 else c7;
def c8 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 8)));
def cv8 = if IsNaN(c8) then 0 else c8;
def c9 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 9)));
def cv9 = if IsNaN(c9) then 0 else c9;
def c10 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 10)));
def cv10 = if IsNaN(c10) then 0 else c10;
def c11 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 11)));
def cv11 = if IsNaN(c11) then 0 else c11;
def c12 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 12)));
def cv12 = if IsNaN(c12) then 0 else c12;
def c13 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 13)));
def cv13 = if IsNaN(c13) then 0 else c13;
def c14 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 14)));
def cv14 = if IsNaN(c14) then 0 else c14;
def c15 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 15)));
def cv15 = if IsNaN(c15) then 0 else c15;
def c16 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 16)));
def cv16 = if IsNaN(c16) then 0 else c16;
def c17 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 17)));
def cv17 = if IsNaN(c17) then 0 else c17;
def c18 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 18)));
def cv18 = if IsNaN(c18) then 0 else c18;
def c19 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 19)));
def cv19 = if IsNaN(c19) then 0 else c19;
def c20 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("C", price + 20)));
def cv20 = if IsNaN(c20) then 0 else c20;
plot calls = cv0 + cv1 + cv2 + cv3 + cv4 + cv5 + cv6 + cv7 + cv8 + cv9 + cv10 + cv11 + cv12 + cv13 + cv14 + cv15 + cv16 + cv17 + cv18 + cv19 + cv20;
calls.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
calls.SetDefaultColor(GetColor(6));
# PUTS
def p0 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 0)));
def pv0 = if IsNaN(p0) then 0 else p0;
def p1 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 1)));
def pv1 = if IsNaN(p1) then 0 else p1;
def p2 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 2)));
def pv2 = if IsNaN(p2) then 0 else p2;
def p3 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 3)));
def pv3 = if IsNaN(p3) then 0 else p3;
def p4 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 4)));
def pv4 = if IsNaN(p4) then 0 else p4;
def p5 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 5)));
def pv5 = if IsNaN(p5) then 0 else p5;
def p6 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 6)));
def pv6 = if IsNaN(p6) then 0 else p6;
def p7 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 7)));
def pv7 = if IsNaN(p7) then 0 else p7;
def p8 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 8)));
def pv8 = if IsNaN(p8) then 0 else p8;
def p9 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 9)));
def pv9 = if IsNaN(p9) then 0 else p9;
def p10 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 10)));
def pv10 = if IsNaN(p10) then 0 else p10;
def p11 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 11)));
def pv11 = if IsNaN(p11) then 0 else p11;
def p12 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 12)));
def pv12 = if IsNaN(p12) then 0 else p12;
def p13 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 13)));
def pv13 = if IsNaN(p13) then 0 else p13;
def p14 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 14)));
def pv14 = if IsNaN(p14) then 0 else p14;
def p15 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 15)));
def pv15 = if IsNaN(p15) then 0 else p15;
def p16 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 16)));
def pv16 = if IsNaN(p16) then 0 else p16;
def p17 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 17)));
def pv17 = if IsNaN(p17) then 0 else p17;
def p18 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 18)));
def pv18 = if IsNaN(p18) then 0 else p18;
def p19 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 19)));
def pv19 = if IsNaN(p19) then 0 else p19;
def p20 = volume(Concat("."+GetUnderlyingSymbol()+ optionExpiry, Concat("P", price - 20)));
def pv20 = if IsNaN(p20) then 0 else p20;
plot puts = pv0 + pv1 + pv2 + pv3 + pv4 + pv5 + pv6 + pv7 + pv8 + pv9 + pv10 + pv11 + pv12 + pv13 + pv14 + pv15 + pv16 + pv17 + pv18 + pv19 + pv20;
puts.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
puts.SetDefaultColor(GetColor(5));
//https://dotnetfiddle.net/3vVm8X
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("#NIC_OptionVolume");
Console.WriteLine("");
Console.WriteLine("#@NicTheMajestic");
Console.WriteLine("#Histogram of near the money option volume");
Console.WriteLine("#Requires setting of option expiry");
Console.WriteLine("");
Console.WriteLine("declare lower;");
Console.WriteLine("input optionExpiry = \"210716\";");
Console.WriteLine("def price = Round(open, 0);");
Console.WriteLine("");
Console.WriteLine("# CALLS");
for(int i = 0; i < 21; i++)
{
Console.WriteLine(string.Format("def c{0} = volume(Concat(\".\"+GetUnderlyingSymbol()+ optionExpiry, Concat(\"{1}\", price + {0})));", i, "C"));
Console.WriteLine(string.Format("def cv{0} = if IsNaN(c{0}) then 0 else c{0};", i));
}
Console.WriteLine("");
Console.Write("plot calls = ");
for(int i = 0; i < 20; i++)
{
Console.Write(string.Format("cv{0} + ", i));
}
Console.Write(string.Format("cv{0};", 20));
Console.WriteLine("");
Console.WriteLine("calls.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);");
Console.WriteLine("calls.SetDefaultColor(GetColor(6));");
Console.WriteLine("");
Console.WriteLine("# PUTS");
for(int i = 0; i < 21; i++)
{
Console.WriteLine(string.Format("def p{0} = volume(Concat(\".\"+GetUnderlyingSymbol()+ optionExpiry, Concat(\"{1}\", price - {0})));", i, "P"));
Console.WriteLine(string.Format("def pv{0} = if IsNaN(p{0}) then 0 else p{0};", i));
}
Console.WriteLine("");
Console.Write("plot puts = ");
for(int i = 0; i < 20; i++)
{
Console.Write(string.Format("pv{0} + ", i));
}
Console.Write(string.Format("pv{0};", 20));
Console.WriteLine("");
Console.WriteLine("puts.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);");
Console.WriteLine("puts.SetDefaultColor(GetColor(5));");
Console.WriteLine("");
}
}
@NVentimiglia
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment