Probability of Expiring Cone Source Code?

Status
Not open for further replies.

YungTraderFromMontana

Well-known member
Last edited by a moderator:
How do you calculate Double.NaN with the period of 100 and pro_range of 68?

The source code is copyrighted and hidden from the public for a reason.
 
open source code from: https://www.tradingview.com/script/Cp49eQt5-Probability-Cones/

Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © joebaus

//@version=5


// ———————————————————— Acknowledgements {

// - Fortunate, a good friend who helped me significantly start my research.

// - Telugu Traders Community, for their video on calculating cones.

// - Savva Shanaev, for his NEDL tutorial on the Laplace distribution.

// - JeylaniB, for his Pine Script V5 extension for VS Studio Code.

// - PineCoders and TradingView, for great Pine documentation and features.

// - @RicardoSantos, for example code and idea of the Select Date feature.

// - Anton Berlin, for his discussions on statistical interpretations.

// }


// ———————————————————— Indicator {

indicator(

  shorttitle        = "Probability Cones",

  title             = "Probability Cones",

  max_lines_count   = 500,

  max_bars_back     = 5000,

  overlay           = true)

// }


// ———————————————————— Constants {

// All tooltip constants are prefixed with "T_".

T_SRC         = "The data source to use for calculating the probability cones."

T_DEV       = "Select the calculation method for the cone deviations."

T_LOOKBACK     = "The number of bars sampled for the probability calculations. Minimum value: 1 and Maximum value: 5000"

T_FORECAST  = "The number of bars to project the probability cones forward. At 70 bars with all cones enabled, TradingView's line limitations will be hit, and the indicator will begin to remove earlier lines. Either keep this input below 70 or hide cones to get extra drawing lines."

T_FILLS     = "The background color highlights between cone lines."

T_BARSBACK  = "The number of bars + or - to position the probability cones."

T_CONELOCK  = "Enables Anchor Type inputs. Keeps the cone origin stationary based on the Anchor Type selected. When disabled, cone origin will use a rolling window placement using the Bars Back to Place Cones input."

T_ANCHOR    = "Select the method to anchor the probability cone origin."

T_DATE      = "Keeps the cone origin stationary at the selected date."

T_HTF       = "Sets the cone's origin to the beginning of a specified timeframe. Updates the cones automatically when a new period begins."

T_OFFSET    = "The number of bars + or - to position cones after being anchored. Only effects the Higher Timeframe Anchor Type."

T_MEAN      = "Select the desired mean for the cones. The Auto setting will use the default, recommended means for the current Deviation input. By default: Standard uses Average, Laplace and Absolute Deviation use Median."

T_MEANDRIFT = "Adds or removes the drift to the cones caused by the mean's increasing or decreasing value in the future. Turning \"Use Mean Drift\" off projects forward only the initial value of the mean, and shifts the values of the cone's deviation lines relative to the mean."


// All group constants are prefixed with "G_".

G_SETTINGS    = "Cone Settings"

G_ANCHOR    = "Anchor Settings "

G_MEAN        = "Mean Settings"

G_CONE1_SETTINGS    = "First Cone's Settings"

G_CONE2_SETTINGS    = "Second Cone's Settings"

G_CONE3_SETTINGS    = "Third Cone's Settings"


// Constants for i_deviation

DEV_STANDARD    = "Standard"

DEV_LAPLACE     = "Laplace Stdev"

DEV_MAD         = "Absolute"


// Constants for i_anchorType

AT_BAR  = "Bar"

AT_DATE = "Select Date"

AT_HTF  = "Higher Timeframe"


// Constants for i_mu

MU_AUTO     = "Auto"

MU_AVERAGE  = "Average"

MU_MEDIAN   = "Median"


// Constants for f_lineStyle()

LINE_STYLE_SOLID    = "Solid"

LINE_STYLE_DASHED   = "Dashed"

LINE_STYLE_DOTTED   = "Dotted"


// Inline constants.

INLINE_CONE_DEV     = "Cone Deviations"

INLINE_CONE_STYLE   = "Cone Line Styles"

INLINE_CONE_COLOR   = "Cone Line Colors"

INLINE_CONE_WIDTH   = "Cone Line Widths"

INLINE_FILL_COLOR   = "Fill Color"

INLINE_FILL_BOOL    = "Fill Bool"

INLINE_MEAN_1       = "Mean Inline Settings 1"

INLINE_MEAN_2       = "Mean Inline Settings 2"

// }


// ———————————————————— Inputs {

// Here is how the cone in variables are written:


// Cone Order:  "first", "second", or "third" cone.

// Position:    Upper "Up" or lower "Dn" line of the cone.

// Attribute:   "Color", "Width", etc.

// All inputs prefixed with "i_", all arrays prefixed with "a_".


// E.g. i_firstUpColor is the input for the first cone's, upper line, color.


// ———————————————————— General Settings Inputs {

i_src        = input.source(

  defval    = close,

  title     = "Source",

  tooltip   = T_SRC,

  group     = G_SETTINGS)


i_deviation = input.string(

  defval    = DEV_STANDARD,

  title     = "Deviation",

  options   = [DEV_STANDARD,

               DEV_LAPLACE,

               DEV_MAD],

  tooltip   = T_DEV,

  group     = G_SETTINGS)


i_lookBack  = input.int(

  defval    = 500,

  title     = "Bar Lookback",

  minval    = 1,

  maxval    = 5000,

  tooltip   = T_LOOKBACK,

  group     = G_SETTINGS)


i_forecast  = input.int(

  defval    = 30,

  title     = "Bar Forecast",

  minval    = 1,

  tooltip   = T_FORECAST,

  group     = G_SETTINGS)


i_meanDriftBool = input.bool(

  defval    = true,

  title     = "Use Mean Drift",

  tooltip   = T_MEANDRIFT,

  group     = G_SETTINGS)


i_fillBool  = input.bool(

  defval    = true,

  title     = "Show Fills",

  tooltip   = T_FILLS,

  group     = G_SETTINGS)

// }


// ———————————————————— Anchor Settings {

i_barsBack  = input.int(

  defval    = 20,

  title     = "Bars Back to Place Cones",

  tooltip   = T_BARSBACK,

  group     = G_ANCHOR)


i_coneLock  = input.bool(

  defval    = false,

  title     = "Lock Cones to Anchor Type",

  tooltip   = T_CONELOCK,

  group     = G_ANCHOR)


i_anchorType    = input.string(

  defval    = AT_BAR,

  title     = "Anchor Type",

  options   = [AT_BAR,

               AT_DATE,

               AT_HTF],

  tooltip   = T_ANCHOR,

  group     = G_ANCHOR)


// @RicardoSantos contribution

i_anchorDate    = time ==  input.time(

  defval    = timestamp("2022-01-01"),

  title     = "Select Date to Place Cones",

  tooltip   = T_DATE,

  group     = G_ANCHOR)


i_anchorHtf = input.timeframe(

  defval    = "W",

  title     = "Higher Timeframe Anchor",

  tooltip   = T_HTF,

  group     = G_ANCHOR)


i_anchorOffset  = input.int(

  defval    = 1,

  title     = "HTF Anchor Offset",

  tooltip   = T_OFFSET,

  group     = G_ANCHOR)

// }


// ———————————————————— Mean Input Settings {

i_muBool    = input.bool(

  defval    = true,

  title     = "Show Mean",

  group     = G_MEAN)


i_muCalc    = input.string(

  defval    = MU_AUTO,

  title     = "Mean Calculation",

  options   = [MU_AUTO,

               MU_AVERAGE,

               MU_MEDIAN],

  tooltip   = T_MEAN,

  inline    = INLINE_MEAN_1,

  group     = G_MEAN)


i_muColor   = input.color(

  defval    = color.new(color.orange, 0),

  title     = "Color",

  inline    = INLINE_MEAN_2,

  group     = G_MEAN)


i_muWidth   = input.int(

  defval    = 2,

  title     = "Width",

  minval    = 1,

  maxval    = 4,

  inline    = INLINE_MEAN_2,

  group     = G_MEAN)


i_muLineStyle   = input.string(

  defval    = LINE_STYLE_DOTTED,

  title     = "Line Style",

  options   = [LINE_STYLE_SOLID,

               LINE_STYLE_DASHED,

               LINE_STYLE_DOTTED],

  inline    = INLINE_MEAN_2,

  group     = G_MEAN)

// }


// ———————————————————— First Cone's Input Settings {

i_firstConeBool = input.bool(

  defval    = true,

  title     = "Show First Cone",

  group     = G_CONE1_SETTINGS)


i_firstUpColor  = input.color(

  defval    = color.new(color.green, 0),

  title     = "Upper Cone Color",

  inline    = INLINE_CONE_COLOR,

  group     = G_CONE1_SETTINGS)


i_firstDnColor  = input.color(

  defval    = color.new(color.green, 0),

  title     = "Lower Cone Color",

  inline    = INLINE_CONE_COLOR,

  group     = G_CONE1_SETTINGS)


i_firstUpDev    = input.float(

  defval     = 1,

  title        = "Upper Deviation",

  step      = 0.01,

  inline    = INLINE_CONE_DEV,

  group        = G_CONE1_SETTINGS)


// Dn cone deviation inputs are positive, and made negative later.

i_firstDnDev    = input.float(

  defval     = 1,

  title        = "Lower Deviation",

  step      = 0.01,

  inline    = INLINE_CONE_DEV,

  group        = G_CONE1_SETTINGS)


i_firstUpLineStyle  = input.string(

  defval    = LINE_STYLE_DOTTED,

  title     = "Upper Line Style",

  options   = [LINE_STYLE_SOLID,

               LINE_STYLE_DASHED,

               LINE_STYLE_DOTTED],

  inline    = INLINE_CONE_STYLE,

  group     = G_CONE1_SETTINGS)


i_firstDnLineStyle  = input.string(

  defval    = LINE_STYLE_DOTTED,

  title     = "Lower Line Style",

  options   = [LINE_STYLE_SOLID,

               LINE_STYLE_DASHED,

               LINE_STYLE_DOTTED],

  inline    = INLINE_CONE_STYLE,

  group     = G_CONE1_SETTINGS)


i_firstUpWidth  = input.int(

  defval    = 2,

  title     = "Upper Cone Width",

  minval    = 1,

  maxval    = 4,

  inline    = INLINE_CONE_WIDTH,

  group     = G_CONE1_SETTINGS)


i_firstDnWidth  = input.int(

  defval    = 2,

  title     = "Lower Cone Width",

  minval    = 1,

  maxval    = 4,

  inline    = INLINE_CONE_WIDTH,

  group     = G_CONE1_SETTINGS)


i_firstUpFillBool   = input.bool(

  defval    = true,

  title     = "Show Upper Fill",

  inline    = INLINE_FILL_BOOL,

  group     = G_CONE1_SETTINGS)


i_firstDnFillBool   = input.bool(

  defval    = true,

  title     = "Show Lower Fill",

  inline    = INLINE_FILL_BOOL,

  group     = G_CONE1_SETTINGS)


i_firstUpFill   = input.color(

  defval    = color.new(color.green, 95),

  title     = "Upper Fill Color",

  inline    = INLINE_FILL_COLOR,

  group     = G_CONE1_SETTINGS)


i_firstDnFill   = input.color(

  defval    = color.new(color.green, 95),

  title     = "Lower Fill Color",

  inline    = INLINE_FILL_COLOR,

  group     = G_CONE1_SETTINGS)

// }


// ———————————————————— Second Cone's Input Settings {

i_secondConeBool    = input.bool(

  defval    = true,

  title     = "Show Second Cone",

  group     = G_CONE2_SETTINGS)


i_secondUpColor = input.color(

  defval    = color.new(color.blue, 0),

  title     = "Upper Cone Color",

  inline    = INLINE_CONE_COLOR,

  group     = G_CONE2_SETTINGS)


i_secondDnColor = input.color(

  defval    = color.new(color.blue, 0),

  title     = "Lower Cone Color",

  inline    = INLINE_CONE_COLOR,

  group     = G_CONE2_SETTINGS)


i_secondUpDev   = input.float(

  defval     = 2,

  title        = "Upper Deviation",

  step      = 0.01,

  inline    = INLINE_CONE_DEV,

  group        = G_CONE2_SETTINGS)


// Dn cone deviation inputs are positive, and made negative later.

i_secondDnDev   = input.float(

  defval     = 2,

  title        = "Lower Deviation",

  step      = 0.01,

  inline    = INLINE_CONE_DEV,

  group        = G_CONE2_SETTINGS)


i_secondUpLineStyle = input.string(

  defval    = LINE_STYLE_DOTTED,

  title     = "Upper Line Style",

  options   = [LINE_STYLE_SOLID,

               LINE_STYLE_DASHED,

               LINE_STYLE_DOTTED],

  inline    = INLINE_CONE_STYLE,

  group     = G_CONE2_SETTINGS)


i_secondDnLineStyle = input.string(

  defval    = LINE_STYLE_DOTTED,

  title     = "Lower Line Style",

  options   = [LINE_STYLE_SOLID,

               LINE_STYLE_DASHED,

               LINE_STYLE_DOTTED],

  inline    = INLINE_CONE_STYLE,

  group     = G_CONE2_SETTINGS)


i_secondWidthUp = input.int(

  defval    = 2,

  title     = "Upper Cone Width",

  minval    = 1,

  maxval    = 4,

  inline    = INLINE_CONE_WIDTH,

  group     = G_CONE2_SETTINGS)


i_secondDnWidth = input.int(

  defval    = 2,

  title     = "Lower Cone Width",

  minval    = 1,

  maxval    = 4,

  inline    = INLINE_CONE_WIDTH,

  group     = G_CONE2_SETTINGS)


i_secondUpFillBool  = input.bool(

  defval    = true,

  title     = "Show Upper Fill",

  inline    = INLINE_FILL_BOOL,

  group     = G_CONE2_SETTINGS)


i_secondDnFillBool  = input.bool(

  defval    = true,

  title     = "Show Lower Fill",

  inline    = INLINE_FILL_BOOL,

  group     = G_CONE2_SETTINGS)


i_secondUpFill  = input.color(

  defval    = color.new(color.blue, 95),

  title     = "Fill Upper Color",

  inline    = INLINE_FILL_COLOR,

  group     = G_CONE2_SETTINGS)


i_secondDnFill  = input.color(

  defval    = color.new(color.blue, 95),

  title     = "Fill Lower Color",

  inline    = INLINE_FILL_COLOR,

  group     = G_CONE2_SETTINGS)

// }


// ———————————————————— Third Cone's Input Settings {

i_thirdConeBool = input.bool(

  defval    = true,

  title     = "Show Third Cone",

  group     = G_CONE3_SETTINGS)


i_thirdUpColor  = input.color(

  defval    = color.new(color.red, 0),

  title     = "Upper Cone Color",

  inline    = INLINE_CONE_COLOR,

  group     = G_CONE3_SETTINGS)


i_thirdDnColor  = input.color(

  defval    = color.new(color.red, 0),

  title     = "Lower Cone Color",

  inline    = INLINE_CONE_COLOR,

  group     = G_CONE3_SETTINGS)


i_thirdUpDev    = input.float(

  defval     = 3,

  title        = "Upper Deviation",

  step      = 0.01,

  inline    = INLINE_CONE_DEV,

  group        = G_CONE3_SETTINGS)


// Dn cone deviation inputs are positive, made negative later.

i_thirdDnDev    = input.float(

  defval     = 3,

  title        = "Lower Deviation",

  step      = 0.01,

  inline    = INLINE_CONE_DEV,

  group        = G_CONE3_SETTINGS)


i_thirdUpLineStyle  = input.string(

  defval    = LINE_STYLE_DOTTED,

  title     = "Upper Line Style",

  options   = [LINE_STYLE_SOLID,

               LINE_STYLE_DASHED,

               LINE_STYLE_DOTTED],

  inline    = INLINE_CONE_STYLE,

  group     = G_CONE3_SETTINGS)


i_thirdDnLineStyle  = input.string(

  defval    = LINE_STYLE_DOTTED,

  title     = "Lower Line Style",

  options   = [LINE_STYLE_SOLID,

               LINE_STYLE_DASHED,

               LINE_STYLE_DOTTED],

  inline    = INLINE_CONE_STYLE,

  group     = G_CONE3_SETTINGS)


i_thirdUpWidth  = input.int(

  defval    = 2,

  title     = "Upper Cone Width",

  minval    = 1,

  maxval    = 4,

  inline    = INLINE_CONE_WIDTH,

  group     = G_CONE3_SETTINGS)


i_thirdDnWidth  = input.int(

  defval    = 2,

  title     = "Lower Cone Width",

  minval    = 1,

  maxval    = 4,

  inline    = INLINE_CONE_WIDTH,

  group     = G_CONE3_SETTINGS)


i_thirdUpFillBool   = input.bool(

  defval    = true,

  title     = "Show Upper Fill",

  inline    = INLINE_FILL_BOOL,

  group     = G_CONE3_SETTINGS)


i_thirdDnFillBool   = input.bool(

  defval    = true,

  title     = "Show Lower Fill",

  inline    = INLINE_FILL_BOOL,

  group     = G_CONE3_SETTINGS)


i_thirdUpFill   = input.color(

  defval    = color.new(color.red, 95),

  title     = "Fill Upper Color",

  inline    = INLINE_FILL_COLOR,

  group     = G_CONE3_SETTINGS)


i_thirdDnFill   = input.color(

  defval    = color.new(color.red, 95),

  title     = "Fill Lower Color",

  inline    = INLINE_FILL_COLOR,

  group     = G_CONE3_SETTINGS)

// }


// }


// ———————————————————— Calculations {


// ———————————————————— Array Index Variables {

// coneForecast adds 1 to i_forecast.

// This prevents an array from being a length making the forecast 1 bar short.

coneForecast = i_forecast + 1


// coneForecastIndex stops repetitition in "for" loops.

// While i_forecast is equivalent, this variable is explicit for indexing.

coneForecastIndex = coneForecast - 1

// }


// ———————————————————— Cone Origin Anchor Variables {

// Barlock variable

barLock = ta.valuewhen(barstate.isnew == true, bar_index, 0)


// Date Select, anchor variable

// @RicardoSantos contribution

var int dateAnchor = na


// Last time i_anchorDate was true, get bar_index and set to dateAnchor.

if i_anchorDate == true

    dateAnchor := bar_index


// Get the close of the prior, historical higher timeframe.

// barmerge.lookahead_on resolves historical cone anchor repaints.

higherTimeframe = request.security(

  symbol        = syminfo.tickerid,

  timeframe     = i_anchorHtf,

  expression    = close[1],

  lookahead     = barmerge.lookahead_on)


// Find the numbers of barssince the start a new candle based on i_anchorHtf.

htfAnchor = ta.barssince(higherTimeframe[1] != higherTimeframe)


// If i_coneLock is enabled, use the respective anchorBarIndex bar_index input.

// Otherwise, default back to the i_barsBack input.

anchorBar = switch i_coneLock

    true    => bar_index - i_barsBack - (bar_index - barLock[1] - 1)

    false   => bar_index - i_barsBack


anchorDate    = switch i_coneLock

    true    => dateAnchor

    false   => bar_index - i_barsBack


anchorHtf = switch i_coneLock

    true    => bar_index - htfAnchor - i_anchorOffset

    false   => bar_index - i_barsBack


// What bar index to start displaying the cones. Used for f_coneLine().

anchorBarIndex    = switch i_anchorType

    AT_BAR  => anchorBar

    AT_DATE => anchorDate

    AT_HTF  => anchorHtf


// If i_coneLock is enabled, use the respective i_anchorType subscript input.

// Otherwise, default back to the i_barsBack input.

anchorBarSub  = switch i_coneLock

    true    => i_barsBack + (bar_index - barLock[1] - 1)

    false   => i_barsBack


anchorDateSub = switch i_coneLock

    true    => bar_index - dateAnchor

    false   => i_barsBack


anchorHtfSub  = switch i_coneLock

    true    => htfAnchor + i_anchorOffset

    false   => i_barsBack


// Get the equivalent anchorBarIndex value to use as a subscript[] for variables.

// Used in "Devation and Mean Switches", and "Cone Value Array" sections.

anchorBarIndexSub   = switch i_anchorType

    AT_BAR  => anchorBarSub

    AT_DATE => anchorDateSub

    AT_HTF  => anchorHtfSub

// }


// ———————————————————— Log Returns Variables {

logReturns      = math.log(i_src / i_src[1])

logReturnsMed   = ta.median(logReturns, i_lookBack)

logReturnsAvg   = ta.sma(logReturns, i_lookBack)

logReturnsDev   = ta.stdev(logReturns, i_lookBack)

// }


// ———————————————————— Mean Switches {

// Based on i_deviation, select the mean.

meanAuto    = switch i_deviation

    DEV_STANDARD    => logReturnsAvg[anchorBarIndexSub]

    DEV_LAPLACE     => logReturnsMed[anchorBarIndexSub]

    DEV_MAD         => logReturnsMed[anchorBarIndexSub]


// Based on the mean input, return autoMean or a manually selected mean.

meanSwitch  = switch i_muCalc

    MU_AUTO         => meanAuto

    MU_AVERAGE      => logReturnsAvg

    MU_MEDIAN       => logReturnsMed

// }


// ———————————————————— Absolute Deviation Calculation {

a_absDev    = array.new_float(i_lookBack, na)


for i = 0 to i_lookBack - 1

    _absDev = math.abs(logReturns - meanSwitch)  // Absolute Deviation Calculation

    array.set(id = a_absDev, index = i, value = _absDev)


absDev  = array.avg(id = a_absDev)   // Absolute Deviation

// }


// ———————————————————— Laplace Standard Deviation Calculation {

laplaceVar = 2 * math.pow(absDev, 2)

laplaceStdev = math.sqrt(laplaceVar)

// }


// ———————————————————— Devation Switches {

devSelectUp = switch i_deviation

    DEV_STANDARD    => logReturnsDev[anchorBarIndexSub]

    DEV_LAPLACE     => laplaceStdev[anchorBarIndexSub]

    DEV_MAD         => absDev[anchorBarIndexSub]


devSelectDn = switch i_deviation

    DEV_STANDARD    => logReturnsDev[anchorBarIndexSub]

    DEV_LAPLACE     => laplaceStdev[anchorBarIndexSub]

    DEV_MAD         => absDev[anchorBarIndexSub]

// }


// ———————————————————— a_conePeriod and a_conePeriodSqrt {

// Holds an increasing int series: 1, 2, 3... up to coneForecast

// This counts the number of bars to forecast the cones forward.

a_conePeriod = array.new_int(size = coneForecast, initial_value = na)


// Holds square root of a_conePeriod.

// When multiplied by a deviation, generates the a cone line's curve.

a_conePeriodSqrt = array.new_float(size = coneForecast, initial_value = na)


for i = 0 to coneForecastIndex

    

    int _conePeriod = na

    _conePeriod  := i + 1

    array.set(id = a_conePeriod, index = i, value = _conePeriod)

    

    _conePeriodSqrt = math.sqrt(number = _conePeriod)

    array.set(id = a_conePeriodSqrt, index = i, value = _conePeriodSqrt)

// }


// ———————————————————— conePeriod and DevSqrt Arrays {

a_conePeriodAvg = array.new_float(size = coneForecast, initial_value = na)


// Arrays for the three cones.

a_firstUpDevSqrt    = array.new_float(size = coneForecast, initial_value = na)

a_firstDnDevSqrt    = array.new_float(size = coneForecast, initial_value = na)


a_secondUpDevSqrt   = array.new_float(size = coneForecast, initial_value = na)

a_secondDnDevSqrt   = array.new_float(size = coneForecast, initial_value = na)


a_thirdUpDevSqrt    = array.new_float(size = coneForecast, initial_value = na)

a_thirdDnDevSqrt    = array.new_float(size = coneForecast, initial_value = na)


for i = 0 to coneForecastIndex


    // When i_meanDriftBool is true, multiply the mean by a_conePeriod at i

    // If false, set the initial mean value, from i to coneForecastIndex, to a_conePeriodAvg

    _conePeriodAvg = switch i_meanDriftBool

        true    => meanSwitch * array.get(id = a_conePeriod, index = i)

        false   => meanSwitch

    array.set(id = a_conePeriodAvg, index = i, value = _conePeriodAvg)


    // Multiply the stdev of log returns by the sqrt of the cone period.

    _firstUpDevSqrt = i_firstUpDev * devSelectUp * array.get(id = a_conePeriodSqrt, index = i)

    array.set(id = a_firstUpDevSqrt,   index = i, value = _firstUpDevSqrt)


    _firstDnDevSqrt = i_firstDnDev * devSelectDn * array.get(id = a_conePeriodSqrt, index = i)

    array.set(id = a_firstDnDevSqrt,   index = i, value = _firstDnDevSqrt)


    _secondUpDevSqrt = i_secondUpDev * devSelectUp * array.get(id = a_conePeriodSqrt, index = i)

    array.set(id = a_secondUpDevSqrt,  index = i, value = _secondUpDevSqrt)


    _secondDnDevSqrt = i_secondDnDev * devSelectDn * array.get(id = a_conePeriodSqrt, index = i)

    array.set(id = a_secondDnDevSqrt,  index = i, value = _secondDnDevSqrt)


    _thirdUpDevSqrt = i_thirdUpDev * devSelectUp * array.get(id = a_conePeriodSqrt, index = i)

    array.set(id = a_thirdUpDevSqrt,   index = i, value = _thirdUpDevSqrt)


    _thirdDnDevSqrt = i_thirdDnDev * devSelectDn * array.get(id = a_conePeriodSqrt, index = i)

    array.set(id = a_thirdDnDevSqrt,   index = i, value = _thirdDnDevSqrt)

// }


// ———————————————————— Percent Arrays {

a_firstUpPercent    = array.new_float(size = coneForecast, initial_value = na)

a_firstDnPercent    = array.new_float(size = coneForecast, initial_value = na)


a_secondUpPercent   = array.new_float(size = coneForecast, initial_value = na)

a_secondDnPercent   = array.new_float(size = coneForecast, initial_value = na)


a_thirdUpPercent    = array.new_float(size = coneForecast, initial_value = na)

a_thirdDnPercent    = array.new_float(size = coneForecast, initial_value = na)


for i = 0 to coneForecastIndex

    // If i_meanDriftBool is true, a_conePeriodAvg equals meanSwitch for i to coneForecastIndex


    _firstUpPercent =   array.get(id = a_conePeriodAvg, index = i) +

                      array.get(id = a_firstUpDevSqrt, index = i)

    array.set(id = a_firstUpPercent, index = i, value = _firstUpPercent)


    _firstDnPercent =   array.get(id = a_conePeriodAvg, index = i) -

                      array.get(id = a_firstDnDevSqrt, index = i)

    array.set(id = a_firstDnPercent, index = i, value = _firstDnPercent)


    _secondUpPercent =   array.get(id = a_conePeriodAvg, index = i) +

                      array.get(id = a_secondUpDevSqrt, index = i)

    array.set(id = a_secondUpPercent, index = i, value = _secondUpPercent)


    _secondDnPercent =   array.get(id = a_conePeriodAvg, index = i) -

                      array.get(id = a_secondDnDevSqrt, index = i)

    array.set(id = a_secondDnPercent, index = i, value = _secondDnPercent)


    _thirdUpPercent =   array.get(id = a_conePeriodAvg, index = i) +

                      array.get(id = a_thirdUpDevSqrt, index = i)

    array.set(id = a_thirdUpPercent, index = i, value = _thirdUpPercent)


    _thirdDnPercent =   array.get(id = a_conePeriodAvg, index = i) -

                      array.get(id = a_thirdDnDevSqrt, index = i)

    array.set(id = a_thirdDnPercent, index = i, value = _thirdDnPercent)

// }


// ———————————————————— Cone Value Arrays {

a_meanValue     = array.new_float(size = coneForecast, initial_value = na)


a_firstUpValue  = array.new_float(size = coneForecast, initial_value = na)

a_firstDnValue  = array.new_float(size = coneForecast, initial_value = na)


a_secondUpValue = array.new_float(size = coneForecast, initial_value = na)

a_secondDnValue = array.new_float(size = coneForecast, initial_value = na)


a_thirdUpValue  = array.new_float(size = coneForecast, initial_value = na)

a_thirdDnValue  = array.new_float(size = coneForecast, initial_value = na)


for i = 0 to coneForecastIndex


    _a_meanValue = i_src[anchorBarIndexSub] * math.exp( array.get(id = a_conePeriodAvg, index = i) )

    array.set(id = a_meanValue, index = i, value = _a_meanValue)


    _firstUpConeValue = i_src[anchorBarIndexSub] * math.exp( array.get(id = a_firstUpPercent, index = i) )

    array.set(id = a_firstUpValue, index = i, value = _firstUpConeValue)

    

    _firstDnConeValue = i_src[anchorBarIndexSub] * math.exp( array.get(id = a_firstDnPercent, index = i) )

    array.set(id = a_firstDnValue, index = i, value = _firstDnConeValue)


    _secondUpConeValue = i_src[anchorBarIndexSub] * math.exp( array.get(id = a_secondUpPercent, index = i) )

    array.set(id = a_secondUpValue, index = i, value = _secondUpConeValue)


    _secondDnConeValue = i_src[anchorBarIndexSub] * math.exp( array.get(id = a_secondDnPercent, index = i) )

    array.set(id = a_secondDnValue, index = i, value = _secondDnConeValue)


    _thirdUpConeValue = i_src[anchorBarIndexSub] * math.exp( array.get(id = a_thirdUpPercent, index = i) )

    array.set(id = a_thirdUpValue, index = i, value = _thirdUpConeValue)


    _thirdDnConeValue = i_src[anchorBarIndexSub] * math.exp( array.get(id = a_thirdDnPercent, index = i) )

    array.set(id = a_thirdDnValue, index = i, value = _thirdDnConeValue)

// }


// }


// ———————————————————— Lines and Fills {


// ———————————————————— Draw Functions {


// ———————————————————— Line Style Function {

f_lineStyle(_lineStyle) =>

    // @param _lineStyle, user input line style variable.

    _output = switch _lineStyle

        LINE_STYLE_SOLID    => line.style_solid

        LINE_STYLE_DASHED   => line.style_dashed

        LINE_STYLE_DOTTED   => line.style_dotted

// }


// ———————————————————— Cone Line Function {

f_coneLine(_a_coneValue, _color, _style, _width, _coneToggle) =>


    // @param _a_coneValue, the array holding the cone's forecast values.

    // @param _color, line color variable.

    // @param _style, line style variable.

    // @param _width, line width variable.

    // @param _coneToggle, bool input for this line.


    var _a_coneLine = array.new_line(size = coneForecast, initial_value = na)


    // Offset "i" to get the next _a_coneValue to use as y2 in _coneLine.

    _a_x2Period     = array.new_int(size =  coneForecast, initial_value = na)


    // If the respective cone and line input toggles are on, display the line.

    if _coneToggle == true


        // For loop to generate offset "i" array in _a_x2Period

        for i = 0 to coneForecastIndex - 1  // Index is one shorter than "i".

            _x2Period = i + 1               // Create offset index value from "i".

            array.set(id = _a_x2Period, index = i, value = _x2Period)


        for i = 0 to coneForecastIndex


            _coneX1  = anchorBarIndex + i

            _coneX2  = anchorBarIndex + array.get(id = _a_x2Period, index = i)


            line _coneLine = na


            _coneLine   := line.new(

              x1        = _coneX1,

              y1        = array.get(id = _a_coneValue, index = i ),

              x2        = _coneX2,

              y2        = array.get(id = _a_coneValue, index = array.get(id = _a_x2Period, index = i) ),

              xloc      = xloc.bar_index,

              extend    = extend.none,

              color     = _color,

              style     = _style,

              width     = _width)

            

            array.push(id = _a_coneLine, value = _coneLine)


            if array.size(id = _a_coneLine) > coneForecast

                _ln = array.shift(id = _a_coneLine)

                line.delete(id = _ln)   


    _a_coneLine

// }


// ———————————————————— Cone Fill Function {

f_coneFill(_a_line1, _a_line2, _color, _i_fillBool) =>


    // @param _a_line1, is the first line's array for the fill function.

    // @param _a_line2, is the second line's array for the fill function.

    // @param _color, color variable.

    // @param _i_fillBool, bool input for this fill.


    _a_linefill = array.new_linefill()


    if i_fillBool == true       // i_fillBool is a global variable.

        if _i_fillBool == true  // _i_fillBool is a local variable.

            for i = 0 to coneForecastIndex

        

                line _line1 = na

                line _line2 = na

                

                _line1 := array.get(id = _a_line1, index = i)

                _line2 := array.get(id = _a_line2, index = i)

        

                _linefill = linefill.new(_line1, _line2, _color)

        

                array.push(id = _a_linefill, value = _linefill)

        

                if array.size(id = _a_linefill) > coneForecast

                    _ln = array.shift(id = _a_linefill)

                    linefill.delete(id = _ln)   

// }


// }


// ———————————————————— Draw Indicator {


// ———————————————————— Draw Lines {

meanLine    = f_coneLine(

  a_meanValue,

  i_muColor,

  f_lineStyle(i_muLineStyle),

  i_muWidth,

  i_muBool)


firstUpLine = f_coneLine(

  a_firstUpValue,

  i_firstUpColor,

  f_lineStyle(i_firstUpLineStyle),

  i_firstUpWidth,

  i_firstConeBool)


firstDnLine = f_coneLine(

  a_firstDnValue,

  i_firstDnColor,

  f_lineStyle(i_firstDnLineStyle),

  i_firstDnWidth,

  i_firstConeBool)


secondUpLine = f_coneLine(

  a_secondUpValue,

  i_secondUpColor,

  f_lineStyle(i_secondUpLineStyle),

  i_secondWidthUp,

  i_secondConeBool)


secondDnLine = f_coneLine(

  a_secondDnValue,

  i_secondDnColor,

  f_lineStyle(i_secondDnLineStyle),

  i_secondDnWidth,

  i_secondConeBool)


thirdUpLine = f_coneLine(

  a_thirdUpValue,

  i_thirdUpColor,

  f_lineStyle(i_thirdUpLineStyle),

  i_thirdUpWidth,

  i_thirdConeBool)


thirdDnLine = f_coneLine(

  a_thirdDnValue,

  i_thirdDnColor,

  f_lineStyle(i_thirdDnLineStyle),

  i_thirdDnWidth,

  i_thirdConeBool)

// }


// ———————————————————— Draw Fills

f_coneFill(meanLine, firstUpLine, i_firstUpFill, i_firstUpFillBool)

f_coneFill(meanLine, firstDnLine, i_firstDnFill, i_firstDnFillBool)

f_coneFill(firstUpLine, secondUpLine, i_secondUpFill, i_secondUpFillBool)

f_coneFill(firstDnLine, secondDnLine, i_secondDnFill, i_secondDnFillBool)

f_coneFill(secondUpLine, thirdUpLine, i_thirdUpFill, i_thirdUpFillBool)

f_coneFill(secondDnLine, thirdDnLine, i_thirdDnFill, i_thirdDnFillBool)

// }


// }
 
Last edited:
@Snorg

thank you for posting that.
but it seems overly complicated

try to apply this
http://www.nishatrades.com/blog/mat...ected-move-using-a-probability-analysis-chart
Thanks - expected move calculation. Problem with expected move is that it uses IV ( as we know means option pricing ). Stocks without options or thinly traded options have no IVs / suspect IV's. This example is for a 1 std dev move ( not outwardly stated ). SD factor should be included in EM calcuation.
 
Thanks - expected move calculation. Problem with expected move is that it uses IV ( as we know means option pricing ). Stocks without options or thinly traded options have no IVs / suspect IV's. This example is for a 1 std dev move ( not outwardly stated ). SD factor should be included in EM calcuation.

EDIT
a fixed version of expected move cone

https://usethinkscript.com/threads/expected-moved-probability-cone.10891/

-----------------------------------
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/imp-volatility
https://www.investopedia.com/terms/i/iv.asp#:~:text=Implied volatility is the market's,factors for calculating implied volatility.
 
Last edited:
i made my version of a cone, just to be able to draw a sidways parabola.
i don't care about standard deviations or bell curves.
sometimes i like to figure out how to do something, then save it away and maybe a future project will have a need for it.
maybe it will be a strarting point for someone elses project.

one day i came across the cone study and saw that it was hidden.
ProbabilityOfExpiringCone

so i looked for formulas that might be able to draw a sideways parabola, and found this.
http://www.nishatrades.com/blog/mat...ected-move-using-a-probability-analysis-chart
expected move = price * iv * ( sqrt( days to expire / 365))

this finds a value for a future date.
is this a stistically valid value? i don't know.

i guess you are after something that finds a date for a specific future value ( 68% ?)
maybe this could be altered?

i used imp_volatility() to read a value for the stock , and used it in the formula.
maybe that isn't valid? but this IV has values that change as price moves around.

load the below code and take a look

Code:
declare lower;
plot iv2 = imp_volatility();


this draws a sideways parabola, from the last bar, into the future.
a date is entered and future values are calculated.
are they accurate? i don't know. do they mean anything? i don't know.

this is cleaned up a little, but it's still sloppy

Ruby:
# cone_prob_00b
# draw a sideways parabola , cone of probability?
# halcyonguy
# 21-12

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

def price = close;
def iv2 = imp_volatility();
def iv = round(iv2,2);
# addchartbubble(1, low, iv2, color.yellow, no);

# draw bubbles on last bar and after
def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(price), 0, bn));
def endbn = highestall(bn);
def exp = (endbn - lastbn );

input test3_labels = yes;
addlabel(test3_labels, "IV " + iv, color.yellow);
addlabel(test3_labels, "last bar # " + lastbn, color.yellow);
addlabel(test3_labels, "bar rt edge " + endbn, color.yellow);
addlabel(test3_labels, "exp area " + exp, color.yellow);


# expansion area bars, ( after last bar)
def expbn = ( bn >= lastbn and bn <= endbn);
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];

input test1 = no;
addchartbubble(test1 and expbn, lastcls, "x" , color.cyan, no);

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

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

input expire_date_yyyymmdd = 20220414;
def dat = expire_date_yyyymmdd;

# days from last bar to expire date
#def dayz = 22;

# days to a a date, includes weekends
def dayz = DaysTillDate(expire_date_yyyymmdd);
def dayz2 = if expbn then (bn - lastbn) else na;

# ------------
def expirebn = (dayz == dayz2);
addverticalline(expirebn, dat, color.cyan);

# draw arc just to expire date
def dayz3 = if dayz2 <= dayz then dayz2 else na;

#def em = price * iv * ( sqrt( dayz / 365));
def em = round( lastcls * lastiv * ( sqrt( dayz2 / 365)), 2);
def em_per = round((em/lastcls)*100, 1);


addlabel(1, "days to expire " + dayz, color.yellow);
#addlabel(1, "EM  " + em, color.yellow);

input test2_bubble_vars = no;
addchartbubble(test2_bubble_vars and expbn, lastcls, "L " + lastcls + "\nIV " + lastiv + "\nD " + dayz2 + "\nEM " + em , color.cyan, no);

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


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

# expire price bubbles
def vert = 0.99;
#addchartbubble( expirebn , lastcls - em, ztop + "\n" + em + "\n" + lastcls + "\n" + em + "\n" + zbot , color.yellow, yes);
addchartbubble( expirebn , lastcls + em, ztop , color.yellow, yes);
#addchartbubble( expirebn , lastcls - em, em + "\n" + em_per + "% of" + "\n" + lastcls , color.yellow, yes);
#addchartbubble( expirebn , lastcls * vert, em + " / " + em_per + "% of / " +  lastcls , color.yellow, yes);
addchartbubble( expirebn , lastcls * vert, "+-" + em + " / " + em_per + "% of / " +  lastcls , color.yellow, yes);
addchartbubble( expirebn , lastcls - em, zbot , color.yellow, no);
#


PM 90D 2hr chart 3/23/22
M4gydYr.jpg



https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/imp-volatility

https://www.investopedia.com/terms/i/iv.asp#:~:text=Implied volatility is the market's,factors for calculating implied volatility.
Thanks.

The reason I prefer historical volatility is because there is no option pricing included in the calculation. Implied volatility (IV) uses options data.

In any event, these are just targets so I am getting that worn down feeling and may go with your IV plot.


Much appreciated.
 
Status
Not open for further replies.

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