Expected Move, Probability Cone For ThinkOrSwim

halcyonguy

Expert
VIP
Lifetime
Expected moved, probability cone

enter a date and it draws a parabola and calculates an expected move, using this equation.
expected move = price * iv * ( sqrt( days to expire / 365))
http://www.nishatrades.com/blog/mat...ected-move-using-a-probability-analysis-chart


if the chart time is more than day, the date has to be,
..if week , then a monday
..if month, then the 1st trading day of the month

Ruby:
# expected_move_cone_01

# expected move parabola
# cone starts on last bar
# halcyonguy
# 22-04-09

# http://www.nishatrades.com/blog/mathematically-calculating-expected-move-using-a-probability-analysis-chart
# nisha trades , described std dev
# expected move = price * iv * ( sqrt( days to expire / 365))
#----------------------------------
def price = close;
def iv2 = imp_volatility();
def iv = round(iv2,2);

def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(price), 0, bn));
def endbn = highestall(bn);
def lastbar = if lastbn == bn then 1 else 0;
# def lastbar = !isnan(close[0]) and isnan(close[-1]);

# expansion area, bars after last bar to right edge of chart
def expan_bars = (endbn - lastbn );
def expan_area = if (bn >= lastbn and bn <= endbn) then 1 else 0;

def lastcls = if bn < lastbn then na else if bn == lastbn then price else lastcls[1];
def lastiv = if bn < lastbn then na else if bn == lastbn then iv else lastiv[1];

def newday = if getday() != getday()[1] then 1 else 0;

#--------------------------------------------

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
def daybarqty = roundup(390 / chartmin, 0);
def daysperbar = 1/daybarqty;

# for EM formula, need a qty of days that starts at 0 and counts up to the qty of days to target date
def cone_days_cnt_up = if bn == 1 then na else if lastbar then 0 else cone_days_cnt_up[1] + daysperbar;

# --------------------------------------------

# date of a trading day
input target_date_yyyymmdd = 20220520;
def dat = target_date_yyyymmdd;
def is_target_date = if (GetYYYYMMDD() == dat and newday) then 1 else 0;

# mobius
def data = dat;
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);

#  this fixes the  CountTradingDays() error , when it was in a normal if-then
def dayz5;
if GetYYYYMMDD() > dat then {
 dayz5 = 0;
} else if lastbn == bn then {
 dayz5 = CountTradingDays(GetYYYYMMDD(), dat );
}  else {
 dayz5 = dayz5[1];
}

# -------------------------------------

input vertical_line_target_date = yes;
addverticalline(vertical_line_target_date and is_target_date, "  " + (month + " / " + day), color.yellow);

input em_factor = 1.0;
def em = round(em_factor * lastcls * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
def em_per = round((em/lastcls)*100, 1);

# get EM value on lastbar, so it will be displayed in a label
def em2b = if bn == 1 then 0 else if is_target_date then em else em2b[1];
def em2 = if isNaN(close[-1000])  then em2b[-1000]   else Double.NaN;

# top half parabola
plot ztop = if expan_area then (lastcls + em) else na;
ztop.setdefaultcolor(color.yellow);

# bottom half parabola
plot zbot = if expan_area then (lastcls - em) else na;
zbot.setdefaultcolor(color.yellow);

input cone_horz_line = yes;
plot z2 = if (cone_horz_line and expan_area) then lastcls else na;
z2.setdefaultcolor(color.gray);

# -------------------------------------
# target date price bubbles
input show_price_bubbles = yes;
def vert = 0.99;
addchartbubble(show_price_bubbles and is_target_date, ztop, ztop , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, lastcls * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, zbot, zbot, color.yellow, no);

input show_date_bubble = yes;
addchartbubble(show_date_bubble and is_target_date, ztop*0.99, (month + "/" + day) , color.yellow, no);

# ===========================================
# labels
addlabel(1, "IV  " + iv, color.orange);

input iv_label = yes;
AddLabel(iv_label, " " , Color.black);

input chart_stats_labels = no;
AddLabel(chart_stats_labels, "chartmin " + chartmin, Color.MAGENTA);
AddLabel(chart_stats_labels, "bars per day " + daybarqty, Color.MAGENTA);

input show_labels = yes;
addLabel(show_labels, "target date: " + month + "/" + day + "/" + AsPrice(year), color.yellow);
addlabel(show_labels, "trading days to target date  " + dayz5, color.yellow);

addlabel(show_labels, "EM  " + "$" + em2 , color.orange);
#


WMT 30min 90day 400 expan bars
XneDDKo.jpg

hal_cone
 
Weekly IV Implied Move Probability Cone
Ruby:
# IV Implied Move Probability Cone
# Mobius
# V01.03.2021
####################################################
# mods by zoyd V04.09.2022
# Added weekly implied move
# Added realized volatility traces
# Added option for 0DTE (was SPY only)
# if series IV not available use Black-Scholes
# Recommended aggregation is 5 min and 12 RV samples
####################################################
# Credit for code snippet/weekly Implied move
# TheoTrade_Weely_Expected_Move
# Copyright TheoTrade LLC
# V08.11.2017
####################################################
input ShowBubbles = no;
input meanType = {default Daily, OpenRange};
input DTE0 = no;
input RealEq = {default RogersSatchell, YangZhang, Simple};
input RVlength = 12;
def c = close;
def x = BarNumber();
def nan = Double.NaN;
def active = SecondsFromTime(0930) >= 0 and
             SecondsTillTime(1000) <= 0;
def DOW = GetDayOfWeek(GetYYYYMMDD());

#get close from prev Friday
def Monday = DOW == 1;
def Friday = DOW == 5;
def FridayC = if Friday then close(period = AggregationPeriod.DAY) else FridayC[1];
def lastFridayC = if Monday then FridayC else lastFridayC[1];

def ORH = if active
          then high(period = AggregationPeriod.THIRTY_MIN)
          else ORH[1];
def ORL = if active
          then low(period = AggregationPeriod.THIRTY_MIN)
          else ORL[1];

def t = if SecondsFromTime(0930) > 0
        then SecondsTillTime(1600) / 23400
        else 1;
def DTE;

def IV = if !IsNaN(SeriesVolatility(series = 1))
         then SeriesVolatility(series = 1)
         else if IsNaN(SeriesVolatility(series = 1)) and
                 !IsNaN(SeriesVolatility(series = 2))
         then SeriesVolatility(series = 2)
         else if IsNaN(SeriesVolatility(series = 1)) and
                 IsNaN(SeriesVolatility(series = 2)) and
                !IsNaN(SeriesVolatility(series = 3))
         then SeriesVolatility(series = 3)
         else if !isnan(imp_volatility()) then imp_volatility() else IV[1];

if DOW == 1
{
    DTE = if DTE0 then t else 5 - DOW;
}
else if DOW == 2
{
    DTE = if DTE0 then 1 + t else 5 - DOW;
}
else if DOW == 3
{
    DTE = if DTE0 then t else 5 - DOW;
}
else if DOW == 4
{
    DTE = if DTE0 then 1 + t else 5 - DOW;
}
else if DOW == 5
{
    DTE = t;
}
else
{
    DTE = DTE[1];
}

def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
          GetTime() <= RegularTradingEnd(GetYYYYMMDD());
def x1 = if RTH and !RTH[1]
        then x
        else x1[1];

#borrowed from theo-daily weekly EM
#comment about bandwidth:
# " em_bandwith allow for narrow or wider ranges to be tested. 1.0 is normal.
# for 0.5 Std Dev, then em_bandwidth = 0.5 Entered in % changed to decimal.
# That is, allows for ajustment so that analysis can be made of Iron Fly expected #performance."
input em_skew_percent = 85;
input em_bandwidth = 100;

def ImpMove = if DTE > 0
              then (c * IV * Sqrt(DTE) / Sqrt(365)) * em_skew_percent / 100
              else (c * IV * 1 / Sqrt(365)) * em_skew_percent / 100 * em_bandwidth / 100;

def ImpMoveW = if Monday
              then imp_volatility(period = AggregationPeriod.DAY) * lastFridayC * Sqrt(6) / Sqrt(365) * em_skew_percent / 100 * em_bandwidth / 100
              else ImpMoveW[1];

def factor = AggregationPeriod.DAY/(GetAggregationPeriod()*RVlength);
def RVMove;
switch (RealEq) {
    case RogersSatchell:
        RVMove = expaverage(Sqrt(Sum(Log(high / close) * Log(high / open) + Log(low / close) * Log(low / open), RVlength)) * c * sqrt(factor),RVlength);
    case YangZhang:
        RVMove = expaverage(Sqrt(Sum(Sqr(Log(open / close[1])), RVlength) + Sum(1 / 2 *     Sqr(Log(high / low )), RVlength) -
Sum( (2 * Log(2) - 1) * Sqr(Log(close / open)), RVlength) ) * c * sqrt(factor),RVlength);
    case Simple:
        RVMove = expaverage(Sqrt(Sum(Sqr(Log(c / c[1])),RVLength)) * c * sqrt(factor), RVLength);
}
def RV_Y =  RVMove * Sqrt(365) / c ;
def meanW = if Monday then lastFridayC else if meanW[1] then meanW[1] else c;

plot mean;
switch (meanType)
    {
case Daily:
    mean = (Max(high(period = "DAY"), close(period = "DAY")[1]) +
                 Min(low(period = "DAY"), close(period = "DAY")[1])) / 2;
case OpenRange:
    mean = (ORH + ORL) / 2;
}
mean.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
mean.SetLineWeight(2);
mean.SetDefaultColor(Color.DARK_GRAY);

plot Upper = if RTH and x >= HighestAll(x1)
             then mean + Round(ImpMove / TickSize(), 0) * TickSize()
             else Double.NaN;
plot UpperW = if RTH and x >= HighestAll(x1)
             then meanW + Round(ImpMoveW / TickSize(), 0) * TickSize()
             else Double.NaN;
plot UpperRV = if RTH and x >= HighestAll(x1)
             then mean + Round(RVMove / TickSize(), 0) * TickSize()
             else Double.NaN;
Upper.SetPaintingStrategy(PaintingStrategy.LINE);
Upper.SetDefaultColor(Color.GRAY);
Upper.HideTitle();
UpperW.SetPaintingStrategy(PaintingStrategy.LINE);
UpperW.SetDefaultColor(Color.LIGHT_GRAY);
UpperW.HideTitle();
UpperRV.SetPaintingStrategy(PaintingStrategy.LINE);
UpperRV.SetDefaultColor(Color.LIGHT_ORANGE);
UpperRV.HideTitle();
AddChartBubble(ShowBubbles and x == HighestAll(x), Upper, "ImpMove", Upper.TakeValueColor());
AddChartBubble(ShowBubbles and x == HighestAll(x), UpperW, "IMWeekly", UpperW.TakeValueColor());
AddChartBubble(ShowBubbles and x == HighestAll(x), UpperRV, "RVMove", UpperRV.TakeValueColor());

plot MeanHigh = if RTH and x >= HighestAll(x1)
                then mean + Round((ImpMove * .682) / TickSize(), 0) * TickSize()
                else Double.NaN;
MeanHigh.SetPaintingStrategy(PaintingStrategy.LINE);
MeanHigh.SetDefaultColor(Color.DARK_GRAY);
MeanHigh.HideTitle();
MeanHigh.SetLineWeight(3);
AddChartBubble(ShowBubbles and x == HighestAll(x), MeanHigh, "ProbMove", MeanHigh.TakeValueColor());
AddCloud(Upper, MeanHigh, CreateColor(212, 175, 55), CreateColor(212, 175, 55));
plot Lower = if RTH and x >= HighestAll(x1)
             then mean - Round(ImpMove / TickSize(), 0) * TickSize()
             else Double.NaN;
plot LowerRV = if RTH and x >= HighestAll(x1)
             then mean - Round(RVMove / TickSize(), 0) * TickSize()
             else Double.NaN;
plot LowerW = if RTH and x >= HighestAll(x1)
             then meanW - Round(ImpMoveW / TickSize(), 0) * TickSize()
             else Double.NaN;
Lower.SetPaintingStrategy(PaintingStrategy.LINE);
Lower.SetDefaultColor(Color.GRAY);
Lower.HideTitle();
AddChartBubble(ShowBubbles and x == HighestAll(x), Lower, "ImpMove", Lower.TakeValueColor(), no);
LowerRV.SetPaintingStrategy(PaintingStrategy.LINE);
LowerRV.SetDefaultColor(Color.LIGHT_ORANGE);
LowerRV.HideTitle();
AddChartBubble(ShowBubbles and x == HighestAll(x), LowerRV, "RVMove", LowerRV.TakeValueColor(), no);
LowerW.SetPaintingStrategy(PaintingStrategy.LINE);
LowerW.SetDefaultColor(Color.LIGHT_GRAY);
LowerW.HideTitle();
AddChartBubble(ShowBubbles and x == HighestAll(x), LowerW, "IMWeekly", LowerW.TakeValueColor(), no);

plot MeanLow = if RTH and x >= HighestAll(x1)
               then mean - Round((ImpMove * .682) / TickSize(), 0) * TickSize()
               else Double.NaN;
MeanLow.SetPaintingStrategy(PaintingStrategy.LINE);
MeanLow.SetDefaultColor(Color.DARK_GRAY);
MeanLow.HideTitle();
MeanLow.SetLineWeight(3);
AddChartBubble(ShowBubbles and x == HighestAll(x), MeanLow, "ProbMove", MeanLow.TakeValueColor(), no);
AddCloud(Lower, MeanLow, CreateColor(212, 175, 55), CreateColor(212, 175, 55));

AddLabel(1, "IV = " + AsPercent(IV) +
          "  Todays IM = " + AsDollars(ImpMove) +
          "  PM = " + AsDollars(Round((ImpMove * .682) / TickSize(), 0) * TickSize()) +
          "  RV = " + AsPercent(RV_Y) +
          "  Daily RV = " + AsDollars(Round((RVMove) / TickSize(), 0) * TickSize()) +
          "  IMWeekly = " + AsDollars(Round((ImpMoveW) / TickSize(), 0) * TickSize()) + "        "  ,
         Color.LIGHT_GRAY);
# End Code IV Cone
 
Last edited:
Expected moved, probability cone

enter a date and it draws a parabola and calculates an expected move, using this equation.
expected move = price * iv * ( sqrt( days to expire / 365))
http://www.nishatrades.com/blog/mat...ected-move-using-a-probability-analysis-chart


if the chart time is more than day, the date has to be,
..if week , then a monday
..if month, then the 1st trading day of the month

Ruby:
# expected_move_cone_01

# expected move parabola
# cone starts on last bar
# halcyonguy
# 22-04-09

# http://www.nishatrades.com/blog/mathematically-calculating-expected-move-using-a-probability-analysis-chart
# nisha trades , described std dev
# expected move = price * iv * ( sqrt( days to expire / 365))
#----------------------------------
def price = close;
def iv2 = imp_volatility();
def iv = round(iv2,2);

def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(price), 0, bn));
def endbn = highestall(bn);
def lastbar = if lastbn == bn then 1 else 0;
# def lastbar = !isnan(close[0]) and isnan(close[-1]);

# expansion area, bars after last bar to right edge of chart
def expan_bars = (endbn - lastbn );
def expan_area = if (bn >= lastbn and bn <= endbn) then 1 else 0;

def lastcls = if bn < lastbn then na else if bn == lastbn then price else lastcls[1];
def lastiv = if bn < lastbn then na else if bn == lastbn then iv else lastiv[1];

def newday = if getday() != getday()[1] then 1 else 0;

#--------------------------------------------

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
def daybarqty = roundup(390 / chartmin, 0);
def daysperbar = 1/daybarqty;

# for EM formula, need a qty of days that starts at 0 and counts up to the qty of days to target date
def cone_days_cnt_up = if bn == 1 then na else if lastbar then 0 else cone_days_cnt_up[1] + daysperbar;

# --------------------------------------------

# date of a trading day
input target_date_yyyymmdd = 20220520;
def dat = target_date_yyyymmdd;
def is_target_date = if (GetYYYYMMDD() == dat and newday) then 1 else 0;

# mobius
def data = dat;
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);

#  this fixes the  CountTradingDays() error , when it was in a normal if-then
def dayz5;
if GetYYYYMMDD() > dat then {
 dayz5 = 0;
} else if lastbn == bn then {
 dayz5 = CountTradingDays(GetYYYYMMDD(), dat );
}  else {
 dayz5 = dayz5[1];
}

# -------------------------------------

input vertical_line_target_date = yes;
addverticalline(vertical_line_target_date and is_target_date, "  " + (month + " / " + day), color.yellow);

input em_factor = 1.0;
def em = round(em_factor * lastcls * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
def em_per = round((em/lastcls)*100, 1);

# get EM value on lastbar, so it will be displayed in a label
def em2b = if bn == 1 then 0 else if is_target_date then em else em2b[1];
def em2 = if isNaN(close[-1000])  then em2b[-1000]   else Double.NaN;

# top half parabola
plot ztop = if expan_area then (lastcls + em) else na;
ztop.setdefaultcolor(color.yellow);

# bottom half parabola
plot zbot = if expan_area then (lastcls - em) else na;
zbot.setdefaultcolor(color.yellow);

input cone_horz_line = yes;
plot z2 = if (cone_horz_line and expan_area) then lastcls else na;
z2.setdefaultcolor(color.gray);

# -------------------------------------
# target date price bubbles
input show_price_bubbles = yes;
def vert = 0.99;
addchartbubble(show_price_bubbles and is_target_date, ztop, ztop , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, lastcls * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, zbot, zbot, color.yellow, no);

input show_date_bubble = yes;
addchartbubble(show_date_bubble and is_target_date, ztop*0.99, (month + "/" + day) , color.yellow, no);

# ===========================================
# labels
addlabel(1, "IV  " + iv, color.orange);

input iv_label = yes;
AddLabel(iv_label, " " , Color.black);

input chart_stats_labels = no;
AddLabel(chart_stats_labels, "chartmin " + chartmin, Color.MAGENTA);
AddLabel(chart_stats_labels, "bars per day " + daybarqty, Color.MAGENTA);

input show_labels = yes;
addLabel(show_labels, "target date: " + month + "/" + day + "/" + AsPrice(year), color.yellow);
addlabel(show_labels, "trading days to target date  " + dayz5, color.yellow);

addlabel(show_labels, "EM  " + "$" + em2 , color.orange);
#


WMT 30min 90day 400 expan bars
XneDDKo.jpg

hal_cone
Is it possible to make this probability cone offset? In other words, can we chart what the probability cone would have looked like a certain number of days ago? This would allow for a contrast between the current probability cone and what the cone would have looked like say a week ago, or a month ago, etc.
 
Is it possible to make this probability cone offset? In other words, can we chart what the probability cone would have looked like a certain number of days ago? This would allow for a contrast between the current probability cone and what the cone would have looked like say a week ago, or a month ago, etc.

cone, ver2
can choose the start of cone, a start date or lastbar
can choose a target date


Ruby:
# expected_move_cone_02_date

# expected move parabola , ver2
# cone starts on last bar or a date
# halcyonguy
# 22-05-19

# http://www.nishatrades.com/blog/mathematically-calculating-expected-move-using-a-probability-analysis-chart
# nisha trades , described std dev
# expected move = price * iv * ( sqrt( days to expire / 365))
#----------------------------------
def price = close;
def iv2 = imp_volatility();
def iv = round(iv2,2);

def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(price), 0, bn));
def endbn = highestall(bn);
def lastbar = if lastbn == bn then 1 else 0;
# def lastbar = !isnan(close[0]) and isnan(close[-1]);

# expansion area, bars after last bar to right edge of chart
def expan_bars = (endbn - lastbn );
def expan_area = if (bn >= lastbn and bn <= endbn) then 1 else 0;

def lastcls = if bn < lastbn then na else if bn == lastbn then price else lastcls[1];
# replace lastiv with siv
#def lastiv = if bn < lastbn then na else if bn == lastbn then iv else lastiv[1];
def newday = if getday() != getday()[1] then 1 else 0;

#--------------------------------------------
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
def daybarqty = roundup(390 / chartmin, 0);
def daysperbar = 1/daybarqty;

# --------------------------------------------
input start_bar = { default last_bar, date };

# pick a start date
# cone starts on first bar of a day
#input start_date_yyyymmdd = 20220504;
input start_date_yyyymmdd = 20211123;
def sdat = start_date_yyyymmdd;
def is_start_date = if (GetYYYYMMDD() == sdat and newday) then 1 else 0;

def start_date_price = if is_start_date then close else start_date_price[1];

def syear = Round(sdat/10000, 0);
def smonth = Round((sdat % 10000) / 100, 0);
def sday = (sdat % 100);

# --------------------------
def big = 99999;

# cone start bar, price
def sbar;
def sprice;
def sbn;
switch(start_bar) {
case last_bar:
 sbar = lastbar;
 sprice = lastcls;
 sbn = if bn == 1 then big else if lastbar then bn else sbn[1];
case date:
 sbar = is_start_date;
 sprice = start_date_price;
 sbn = if bn == 1 then big else if is_start_date then bn else sbn[1];
}

def siv = if bn == 1 then na else if bn == sbn then iv else siv[1];

# --------------------------------------------
# date of a trading day
input target_date_yyyymmdd = 20220622;
def tdat = target_date_yyyymmdd;
def is_target_date = if (GetYYYYMMDD() == tdat and newday) then 1 else 0;

# mobius
def tyear = Round(tdat/10000, 0);
def tmonth = Round((tdat % 10000) / 100, 0);
def tday = (tdat % 100);

#  this fixes the  CountTradingDays() error , when it was in a normal if-then
def dayz5;
if GetYYYYMMDD() > tdat then {
 dayz5 = 0;
#} else if lastbn == bn then {
# dayz5 = CountTradingDays(GetYYYYMMDD(), dat );
} else if sbar then {
 dayz5 = CountTradingDays(GetYYYYMMDD(), tdat );
}  else {
 dayz5 = dayz5[1];
}

# -------------------------------------

input vertical_line_target_date = yes;
addverticalline(vertical_line_target_date and is_target_date, "  " + (tmonth + " / " + tday), color.yellow);

# for EM formula, need a qty of days that starts at 0 and counts up to the qty of days to target date
#def cone_days_cnt_up = if bn == 1 then na else if lastbar then 0 else cone_days_cnt_up[1] + daysperbar;
def cone_days_cnt_up = if bn == 1 then na else if sbar then 0 else cone_days_cnt_up[1] + daysperbar;


input em_factor = 1.0;
#def em = round(em_factor * lastcls * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
#def em_per = round((em/lastcls)*100, 1);
#def em = round(em_factor * sprice * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
def em = round(em_factor * sprice * siv * ( sqrt(cone_days_cnt_up/365)), 2);
def em_per = round((em/sprice)*100, 1);

# get EM value on lastbar, so it will be displayed in a label
def em2b = if bn == 1 then 0 else if is_target_date then em else em2b[1];
def em2 = if isNaN(close[-1000]) then em2b[-1000] else Double.NaN;


# top half parabola
#plot ztop = if expan_area then (lastcls + em) else na;
#plot ztop = if expan_area then (sprice + em) else na;
plot ztop = if bn >= sbn then (sprice + em) else na;
ztop.setdefaultcolor(color.yellow);

# bottom half parabola
#plot zbot = if expan_area then (lastcls - em) else na;
#plot zbot = if expan_area then (sprice - em) else na;
plot zbot = if bn >= sbn then (sprice - em) else na;
zbot.setdefaultcolor(color.yellow);

input cone_horz_line = yes;
#plot z2 = if (cone_horz_line and expan_area) then lastcls else na;
#plot z2 = if (cone_horz_line and expan_area) then sprice else na;
plot z2 = if (cone_horz_line and bn > sbn) then sprice else na;
z2.setdefaultcolor(color.gray);

# -------------------------------------
# target date price bubbles
input show_price_bubbles = yes;
def vert = 0.99;
addchartbubble(show_price_bubbles and is_target_date, ztop, ztop , color.yellow, yes);
#addchartbubble(show_price_bubbles and is_target_date, lastcls * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, sprice * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, zbot, zbot, color.yellow, no);

input show_date_bubble = yes;
addchartbubble(show_date_bubble and is_target_date, ztop*0.99, (tmonth + "/" + tday) , color.yellow, no);

# ===========================================
# labels
addlabel(1, "IV  " + iv, color.orange);

input iv_label = yes;
AddLabel(iv_label, " " , Color.black);

input chart_stats_labels = no;
AddLabel(chart_stats_labels, "chartmin " + chartmin, Color.MAGENTA);
AddLabel(chart_stats_labels, "bars per day " + daybarqty, Color.MAGENTA);

input show_labels = yes;
addLabel(show_labels, "start date: " + smonth + "/" + sday + "/" + AsPrice(syear), color.yellow);

addLabel(show_labels, "target date: " + tmonth + "/" + tday + "/" + AsPrice(tyear), color.yellow);
addlabel(show_labels, "trading days to target date  " + dayz5, color.yellow);

addlabel(show_labels, "EM  " + "$" + em2 , color.orange);
#


intc day
GHzDB6e.jpg
 
cone, ver2
can choose the start of cone, a start date or lastbar
can choose a target date


Ruby:
# expected_move_cone_02_date

# expected move parabola , ver2
# cone starts on last bar or a date
# halcyonguy
# 22-05-19

# http://www.nishatrades.com/blog/mathematically-calculating-expected-move-using-a-probability-analysis-chart
# nisha trades , described std dev
# expected move = price * iv * ( sqrt( days to expire / 365))
#----------------------------------
def price = close;
def iv2 = imp_volatility();
def iv = round(iv2,2);

def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(price), 0, bn));
def endbn = highestall(bn);
def lastbar = if lastbn == bn then 1 else 0;
# def lastbar = !isnan(close[0]) and isnan(close[-1]);

# expansion area, bars after last bar to right edge of chart
def expan_bars = (endbn - lastbn );
def expan_area = if (bn >= lastbn and bn <= endbn) then 1 else 0;

def lastcls = if bn < lastbn then na else if bn == lastbn then price else lastcls[1];
# replace lastiv with siv
#def lastiv = if bn < lastbn then na else if bn == lastbn then iv else lastiv[1];
def newday = if getday() != getday()[1] then 1 else 0;

#--------------------------------------------
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
def daybarqty = roundup(390 / chartmin, 0);
def daysperbar = 1/daybarqty;

# --------------------------------------------
input start_bar = { default last_bar, date };

# pick a start date
# cone starts on first bar of a day
#input start_date_yyyymmdd = 20220504;
input start_date_yyyymmdd = 20211123;
def sdat = start_date_yyyymmdd;
def is_start_date = if (GetYYYYMMDD() == sdat and newday) then 1 else 0;

def start_date_price = if is_start_date then close else start_date_price[1];

def syear = Round(sdat/10000, 0);
def smonth = Round((sdat % 10000) / 100, 0);
def sday = (sdat % 100);

# --------------------------
def big = 99999;

# cone start bar, price
def sbar;
def sprice;
def sbn;
switch(start_bar) {
case last_bar:
 sbar = lastbar;
 sprice = lastcls;
 sbn = if bn == 1 then big else if lastbar then bn else sbn[1];
case date:
 sbar = is_start_date;
 sprice = start_date_price;
 sbn = if bn == 1 then big else if is_start_date then bn else sbn[1];
}

def siv = if bn == 1 then na else if bn == sbn then iv else siv[1];

# --------------------------------------------
# date of a trading day
input target_date_yyyymmdd = 20220622;
def tdat = target_date_yyyymmdd;
def is_target_date = if (GetYYYYMMDD() == tdat and newday) then 1 else 0;

# mobius
def tyear = Round(tdat/10000, 0);
def tmonth = Round((tdat % 10000) / 100, 0);
def tday = (tdat % 100);

#  this fixes the  CountTradingDays() error , when it was in a normal if-then
def dayz5;
if GetYYYYMMDD() > tdat then {
 dayz5 = 0;
#} else if lastbn == bn then {
# dayz5 = CountTradingDays(GetYYYYMMDD(), dat );
} else if sbar then {
 dayz5 = CountTradingDays(GetYYYYMMDD(), tdat );
}  else {
 dayz5 = dayz5[1];
}

# -------------------------------------

input vertical_line_target_date = yes;
addverticalline(vertical_line_target_date and is_target_date, "  " + (tmonth + " / " + tday), color.yellow);

# for EM formula, need a qty of days that starts at 0 and counts up to the qty of days to target date
#def cone_days_cnt_up = if bn == 1 then na else if lastbar then 0 else cone_days_cnt_up[1] + daysperbar;
def cone_days_cnt_up = if bn == 1 then na else if sbar then 0 else cone_days_cnt_up[1] + daysperbar;


input em_factor = 1.0;
#def em = round(em_factor * lastcls * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
#def em_per = round((em/lastcls)*100, 1);
#def em = round(em_factor * sprice * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
def em = round(em_factor * sprice * siv * ( sqrt(cone_days_cnt_up/365)), 2);
def em_per = round((em/sprice)*100, 1);

# get EM value on lastbar, so it will be displayed in a label
def em2b = if bn == 1 then 0 else if is_target_date then em else em2b[1];
def em2 = if isNaN(close[-1000]) then em2b[-1000] else Double.NaN;


# top half parabola
#plot ztop = if expan_area then (lastcls + em) else na;
#plot ztop = if expan_area then (sprice + em) else na;
plot ztop = if bn >= sbn then (sprice + em) else na;
ztop.setdefaultcolor(color.yellow);

# bottom half parabola
#plot zbot = if expan_area then (lastcls - em) else na;
#plot zbot = if expan_area then (sprice - em) else na;
plot zbot = if bn >= sbn then (sprice - em) else na;
zbot.setdefaultcolor(color.yellow);

input cone_horz_line = yes;
#plot z2 = if (cone_horz_line and expan_area) then lastcls else na;
#plot z2 = if (cone_horz_line and expan_area) then sprice else na;
plot z2 = if (cone_horz_line and bn > sbn) then sprice else na;
z2.setdefaultcolor(color.gray);

# -------------------------------------
# target date price bubbles
input show_price_bubbles = yes;
def vert = 0.99;
addchartbubble(show_price_bubbles and is_target_date, ztop, ztop , color.yellow, yes);
#addchartbubble(show_price_bubbles and is_target_date, lastcls * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, sprice * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, zbot, zbot, color.yellow, no);

input show_date_bubble = yes;
addchartbubble(show_date_bubble and is_target_date, ztop*0.99, (tmonth + "/" + tday) , color.yellow, no);

# ===========================================
# labels
addlabel(1, "IV  " + iv, color.orange);

input iv_label = yes;
AddLabel(iv_label, " " , Color.black);

input chart_stats_labels = no;
AddLabel(chart_stats_labels, "chartmin " + chartmin, Color.MAGENTA);
AddLabel(chart_stats_labels, "bars per day " + daybarqty, Color.MAGENTA);

input show_labels = yes;
addLabel(show_labels, "start date: " + smonth + "/" + sday + "/" + AsPrice(syear), color.yellow);

addLabel(show_labels, "target date: " + tmonth + "/" + tday + "/" + AsPrice(tyear), color.yellow);
addlabel(show_labels, "trading days to target date  " + dayz5, color.yellow);

addlabel(show_labels, "EM  " + "$" + em2 , color.orange);
#


intc day
GHzDB6e.jpg
Awesome! Thank you so much!
 
@halcyonguy
Any chance a watchlist column could be created for the probability cone to be converted to a watchlist column so it just show the upper and lower expected move depending on the expected move factor?
 
Last edited by a moderator:
Expected moved, probability cone

enter a date and it draws a parabola and calculates an expected move, using this equation.
expected move = price * iv * ( sqrt( days to expire / 365))
http://www.nishatrades.com/blog/mat...ected-move-using-a-probability-analysis-chart


if the chart time is more than day, the date has to be,
..if week , then a monday
..if month, then the 1st trading day of the month

Ruby:
# expected_move_cone_01

# expected move parabola
# cone starts on last bar
# halcyonguy
# 22-04-09

# http://www.nishatrades.com/blog/mathematically-calculating-expected-move-using-a-probability-analysis-chart
# nisha trades , described std dev
# expected move = price * iv * ( sqrt( days to expire / 365))
#----------------------------------
def price = close;
def iv2 = imp_volatility();
def iv = round(iv2,2);

def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(price), 0, bn));
def endbn = highestall(bn);
def lastbar = if lastbn == bn then 1 else 0;
# def lastbar = !isnan(close[0]) and isnan(close[-1]);

# expansion area, bars after last bar to right edge of chart
def expan_bars = (endbn - lastbn );
def expan_area = if (bn >= lastbn and bn <= endbn) then 1 else 0;

def lastcls = if bn < lastbn then na else if bn == lastbn then price else lastcls[1];
def lastiv = if bn < lastbn then na else if bn == lastbn then iv else lastiv[1];

def newday = if getday() != getday()[1] then 1 else 0;

#--------------------------------------------

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
def daybarqty = roundup(390 / chartmin, 0);
def daysperbar = 1/daybarqty;

# for EM formula, need a qty of days that starts at 0 and counts up to the qty of days to target date
def cone_days_cnt_up = if bn == 1 then na else if lastbar then 0 else cone_days_cnt_up[1] + daysperbar;

# --------------------------------------------

# date of a trading day
input target_date_yyyymmdd = 20220520;
def dat = target_date_yyyymmdd;
def is_target_date = if (GetYYYYMMDD() == dat and newday) then 1 else 0;

# mobius
def data = dat;
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);

#  this fixes the  CountTradingDays() error , when it was in a normal if-then
def dayz5;
if GetYYYYMMDD() > dat then {
 dayz5 = 0;
} else if lastbn == bn then {
 dayz5 = CountTradingDays(GetYYYYMMDD(), dat );
}  else {
 dayz5 = dayz5[1];
}

# -------------------------------------

input vertical_line_target_date = yes;
addverticalline(vertical_line_target_date and is_target_date, "  " + (month + " / " + day), color.yellow);

input em_factor = 1.0;
def em = round(em_factor * lastcls * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
def em_per = round((em/lastcls)*100, 1);

# get EM value on lastbar, so it will be displayed in a label
def em2b = if bn == 1 then 0 else if is_target_date then em else em2b[1];
def em2 = if isNaN(close[-1000])  then em2b[-1000]   else Double.NaN;

# top half parabola
plot ztop = if expan_area then (lastcls + em) else na;
ztop.setdefaultcolor(color.yellow);

# bottom half parabola
plot zbot = if expan_area then (lastcls - em) else na;
zbot.setdefaultcolor(color.yellow);

input cone_horz_line = yes;
plot z2 = if (cone_horz_line and expan_area) then lastcls else na;
z2.setdefaultcolor(color.gray);

# -------------------------------------
# target date price bubbles
input show_price_bubbles = yes;
def vert = 0.99;
addchartbubble(show_price_bubbles and is_target_date, ztop, ztop , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, lastcls * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, zbot, zbot, color.yellow, no);

input show_date_bubble = yes;
addchartbubble(show_date_bubble and is_target_date, ztop*0.99, (month + "/" + day) , color.yellow, no);

# ===========================================
# labels
addlabel(1, "IV  " + iv, color.orange);

input iv_label = yes;
AddLabel(iv_label, " " , Color.black);

input chart_stats_labels = no;
AddLabel(chart_stats_labels, "chartmin " + chartmin, Color.MAGENTA);
AddLabel(chart_stats_labels, "bars per day " + daybarqty, Color.MAGENTA);

input show_labels = yes;
addLabel(show_labels, "target date: " + month + "/" + day + "/" + AsPrice(year), color.yellow);
addlabel(show_labels, "trading days to target date  " + dayz5, color.yellow);

addlabel(show_labels, "EM  " + "$" + em2 , color.orange);
#


WMT 30min 90day 400 expan bars
View attachment 14260
hal_cone
Hello,
EM always returns a $0 value for me. I picked a 7/14/2023 target date (6 days to target) but EM is $0. What am I doing wrong?
Thank you.
 
Expected moved, probability cone

enter a date and it draws a parabola and calculates an expected move, using this equation.
expected move = price * iv * ( sqrt( days to expire / 365))
http://www.nishatrades.com/blog/mat...ected-move-using-a-probability-analysis-chart


if the chart time is more than day, the date has to be,
..if week , then a monday
..if month, then the 1st trading day of the month

Ruby:
# expected_move_cone_01

# expected move parabola
# cone starts on last bar
# halcyonguy
# 22-04-09

# http://www.nishatrades.com/blog/mathematically-calculating-expected-move-using-a-probability-analysis-chart
# nisha trades , described std dev
# expected move = price * iv * ( sqrt( days to expire / 365))
#----------------------------------
def price = close;
def iv2 = imp_volatility();
def iv = round(iv2,2);

def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(price), 0, bn));
def endbn = highestall(bn);
def lastbar = if lastbn == bn then 1 else 0;
# def lastbar = !isnan(close[0]) and isnan(close[-1]);

# expansion area, bars after last bar to right edge of chart
def expan_bars = (endbn - lastbn );
def expan_area = if (bn >= lastbn and bn <= endbn) then 1 else 0;

def lastcls = if bn < lastbn then na else if bn == lastbn then price else lastcls[1];
def lastiv = if bn < lastbn then na else if bn == lastbn then iv else lastiv[1];

def newday = if getday() != getday()[1] then 1 else 0;

#--------------------------------------------

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
def daybarqty = roundup(390 / chartmin, 0);
def daysperbar = 1/daybarqty;

# for EM formula, need a qty of days that starts at 0 and counts up to the qty of days to target date
def cone_days_cnt_up = if bn == 1 then na else if lastbar then 0 else cone_days_cnt_up[1] + daysperbar;

# --------------------------------------------

# date of a trading day
input target_date_yyyymmdd = 20220520;
def dat = target_date_yyyymmdd;
def is_target_date = if (GetYYYYMMDD() == dat and newday) then 1 else 0;

# mobius
def data = dat;
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);

#  this fixes the  CountTradingDays() error , when it was in a normal if-then
def dayz5;
if GetYYYYMMDD() > dat then {
 dayz5 = 0;
} else if lastbn == bn then {
 dayz5 = CountTradingDays(GetYYYYMMDD(), dat );
}  else {
 dayz5 = dayz5[1];
}

# -------------------------------------

input vertical_line_target_date = yes;
addverticalline(vertical_line_target_date and is_target_date, "  " + (month + " / " + day), color.yellow);

input em_factor = 1.0;
def em = round(em_factor * lastcls * lastiv * ( sqrt(cone_days_cnt_up/365)), 2);
def em_per = round((em/lastcls)*100, 1);

# get EM value on lastbar, so it will be displayed in a label
def em2b = if bn == 1 then 0 else if is_target_date then em else em2b[1];
def em2 = if isNaN(close[-1000])  then em2b[-1000]   else Double.NaN;

# top half parabola
plot ztop = if expan_area then (lastcls + em) else na;
ztop.setdefaultcolor(color.yellow);

# bottom half parabola
plot zbot = if expan_area then (lastcls - em) else na;
zbot.setdefaultcolor(color.yellow);

input cone_horz_line = yes;
plot z2 = if (cone_horz_line and expan_area) then lastcls else na;
z2.setdefaultcolor(color.gray);

# -------------------------------------
# target date price bubbles
input show_price_bubbles = yes;
def vert = 0.99;
addchartbubble(show_price_bubbles and is_target_date, ztop, ztop , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, lastcls * vert, "$" + em + "\n" + em_per + "%" , color.yellow, yes);
addchartbubble(show_price_bubbles and is_target_date, zbot, zbot, color.yellow, no);

input show_date_bubble = yes;
addchartbubble(show_date_bubble and is_target_date, ztop*0.99, (month + "/" + day) , color.yellow, no);

# ===========================================
# labels
addlabel(1, "IV  " + iv, color.orange);

input iv_label = yes;
AddLabel(iv_label, " " , Color.black);

input chart_stats_labels = no;
AddLabel(chart_stats_labels, "chartmin " + chartmin, Color.MAGENTA);
AddLabel(chart_stats_labels, "bars per day " + daybarqty, Color.MAGENTA);

input show_labels = yes;
addLabel(show_labels, "target date: " + month + "/" + day + "/" + AsPrice(year), color.yellow);
addlabel(show_labels, "trading days to target date  " + dayz5, color.yellow);

addlabel(show_labels, "EM  " + "$" + em2 , color.orange);
#


WMT 30min 90day 400 expan bars
View attachment 14260
hal_cone
if one wanted a default of 45 days, how could they tweak the script? thanks in advance
 

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