Anchor the Cone on the Chart

Hello,

Starting from this forum code I like to make my own version with a couple of changes :
https://forum.amibroker.com/t/probability-cone/8110/15

Main thing i can't resolve is to anchor the Cone on the Chart. Now when i click on whatever place, the cone is moved to a new bar, So I need it stay anchored at the original position I choosed on the graph.

// Probability Cone 01
// https://forum.amibroker.com/t/probability-cone/8110/15

_SECTION_BEGIN( "Probability Cone" );

// By Lennon 
// Modified and expanded by Milosz Mazurkiewicz
/// @link https://forum.amibroker.com/t/probability-cone/8110/15

PF = ParamField( "Price Field", 3 );
CF = ParamToggle( "Choice Field Hi-Lo", "Field|H-L", 0 );
Vol = Param( "Assumed Volatility", 0.1, 0.01, 5, 0.01 ); //(ideally based on volTyp)
FutureBars = Param( "Number of days ahead", 30, 1, 252 );
StdPer = Param( "Standard Deviation", 2, 1, 5, 0.1 );
DE = Param("Days to expiry", 22, 0, 150, 1); 

// ****** NEW --> Here is my problem ***********
Displacement = ParamToggle( "Displacement", "No|Yes", 1 );


SetChartOptions( 0, chartShowArrows | chartShowDates, chartGridMiddle, 0, 0, FutureBars );
bi = BarIndex();
Point = SelectedValue(bi); // LastValue( bi ); // Point = SelectedValue(bi);
LVB = status( "lastvisiblebar" );
PF = PF[Point];
StMatrix = Matrix( 2, FutureBars );

for( i = 0; i < FutureBars; i++ )
{
    StdDev = StdPer *  PF * Vol * sqrt( i / 365 ) ;

    if( CF )
    {
        StMatrix[0][i] = H[ Point ] + stdDev;
        StMatrix[1][i] = L[ Point ] - stdDev;
    }
    else
    {
        StMatrix[0][i] = PF + stdDev;
        StMatrix[1][i] = PF - stdDev;
    }
}

GfxSetCoordsMode( 1 );
GfxMoveTo( Point, PF[Point] );
GfxSelectFont( "ArialNarrow", 7, 400 );
ColorUp = ColorBlend( colorbrightGreen, colorWhite, 0.9 );
ColorDown = ColorBlend( colorSkyblue, colorWhite, 0.2 );

for( i = 0; i < FutureBars; i++ )
{

    y1 = StMatrix[0][i];
    y2 = StMatrix[1][i];

    GfxSelectPen( colorGreen, 2 );
    GfxSelectSolidBrush( colorGreen );
    GfxLineTo( Point + i, y1 );
    GfxCircle( Point + i, y1, -2 );
	
    if( i % 2 == 0 and  i != 0 )  PlotTextSetFont( "" + Prec( y1, 2 ), "Arial Narrow", 8,  Point + i, y1, colorBlack, ColorUp, 20 );
    
    if (i == DE) GfxSelectPen( colorOrange, 2 );
 
    else GfxSelectPen( colorLightGrey, 1 );
    
    GfxLineTo( Point + i, y2 );

    if( i % 2 == 0 AND i != 0 )  PlotText( "" + Prec( y2, 2 ), Point + i, y2, colorBlack, ColorDown, -20 );

    GfxSelectSolidBrush( colorRed );
    GfxSelectPen( colorRed, 2 );
    GfxCircle( Point + i, y2, -2 );
    GfxLineTo( Max( Point + i - 1, Point ), StMatrix[1][Max( i - 1, 0 )] );
    GfxMoveTo( Point + i, y1 );
    
    if( i > LVB - Point ) break;
}

GfxSelectPen( colorGold, 2 );
GfxMoveTo( Point, PF[Point] );
GfxLineTo( Point + i - 1, PF[Point] );
Plot( C, "", colorDefault, styleCandle );

_SECTION_END();


Best regards,
Carlos



Use static variables:

In your case just store the start index to static variable.

// Storing index at some trigger event
tigger = ....; //(ParamTrigger or mouse/keyboard combination or ...)
if ( trigger ) {
      your_idx = ....

      StaticVarSet("Cone_idx", your_idx);
}

static_idx = Nz(StaticVarGet("Cone_idx"), SelectedValue(BarIndex());

// Draw elements of matrix values stored at static index
// ....

2 Likes

Thanks @fxshrat for your quick reply.
I'm going to implement it and I'll tell you.

Best regards,
Carlos

As addition.... actually you should store DateTime at trigger not bar index. And then use
Lookup() function to retrieve Barindex() element at stored datetime.

Or without using trigger and without static variables you just might use ParamDate() function (-> if intraday <- then plus ParamTime() and DateTimeConvert() functions) and then LookUp() function to get bar element.

Another way is using some look-back bar period set via Param() function.

8

4 Likes

Hi @fxshrat,

I don't know them very well and I'm a little lost.
Is it possible to post a concrete example to guide me?

Thanks again.
And very good that gif

C'mon...

/// Setting start index by date/time
/// @link https://forum.amibroker.com/t/anchor-the-cone-on-the-chart/21838/6
startDate = ParamDate("Start Date", Now(1));
startTime = ParamTime("Start Time", "09:00:00");
bi = BarIndex();
start_dt = DateTimeConvert(2, startdate, starttime);
lk_bi = Lookup(bi, start_dt, -1);
Plot( C, "Price", colorDefault, styleBar );
GfxSetCoordsMode(3);
GfxSelectPen( colorRed, 1, 0 );	
GfxMoveTo(lk_bi, 0);
GfxLineTo(lk_bi, Status("pxheight"));

8

4 Likes

Thanks @fxshrat for the example.
Now it's time to study.

Nice stuff gents!
-S

Here is the result:

// Probability Cone 01
// https://forum.amibroker.com/t/probability-cone/8110/15

_SECTION_BEGIN( "Probability Cone" );
/// By Lennon 
/// Modified and expanded by Milosz Mazurkiewicz
/// @link https://forum.amibroker.com/t/probability-cone/8110/15
/// Code addition to set start index via DateTime by fxshrat@gmail.com
/// @link https://forum.amibroker.com/t/anchor-the-cone-on-the-chart/21838/6

PF = ParamField( "Price Field", 3 );
CF = ParamToggle( "Choice Field Hi-Lo", "Field|H-L", 0 );
Vol = Param( "Assumed Volatility", 0.1, 0.01, 5, 0.01 ); //(ideally based on volTyp)
FutureBars = Param( "Number of days ahead", 30, 1, 252 );
StdPer = Param( "Standard Deviation", 2, 1, 5, 0.1 );
DE = Param("Days to expiry", 22, 0, 150, 1); 

SetChartOptions( 0, chartShowArrows | chartShowDates, chartGridMiddle, 0, 0, FutureBars );

/// ** NEW ** #######################################################
/// Code addition to set start index via datetime
startDate = ParamDate("Start Date", Now(1));
bi = BarIndex();
start_dt = DateTimeConvert(2, startdate);
lk_bi = Lookup(bi, start_dt, -1);
// ##################################################################

Point = lk_bi; // SelectedValue(bi); // LastValue( bi ); // Point = SelectedValue(bi);
LVB = status( "lastvisiblebar" );

PF = PF[Point];
StMatrix = Matrix( 2, FutureBars );

for( i = 0; i < FutureBars; i++ )
{
    StdDev = StdPer *  PF * Vol * sqrt( i / 365 ) ;

    if( CF )
    {
        StMatrix[0][i] = H[ Point ] + stdDev;
        StMatrix[1][i] = L[ Point ] - stdDev;
    }
    else
    {
        StMatrix[0][i] = PF + stdDev;
        StMatrix[1][i] = PF - stdDev;
    }
}

GfxSetCoordsMode( 1 );
GfxMoveTo( Point, PF[Point] );
GfxSelectFont( "ArialNarrow", 7, 400 );
ColorUp = ColorBlend( colorbrightGreen, colorWhite, 0.9 );
ColorDown = ColorBlend( colorSkyblue, colorWhite, 0.2 );

for( i = 0; i < FutureBars; i++ )
{

    y1 = StMatrix[0][i];
    y2 = StMatrix[1][i];

    GfxSelectPen( colorGreen, 2 );
    GfxSelectSolidBrush( colorGreen );
    GfxLineTo( Point + i, y1 );
    GfxCircle( Point + i, y1, -2 );
	
    if( i % 2 == 0 and  i != 0 )  PlotTextSetFont( "" + Prec( y1, 2 ), "Arial Narrow", 8,  Point + i, y1, colorBlack, ColorUp, 20 );
    
    if (i == DE) GfxSelectPen( colorOrange, 2 );
 
    else GfxSelectPen( colorLightGrey, 1 );
    
    GfxLineTo( Point + i, y2 );

    if( i % 2 == 0 AND i != 0 )  PlotText( "" + Prec( y2, 2 ), Point + i, y2, colorBlack, ColorDown, -20 );

    GfxSelectSolidBrush( colorRed );
    GfxSelectPen( colorRed, 2 );
    GfxCircle( Point + i, y2, -2 );
    GfxLineTo( Max( Point + i - 1, Point ), StMatrix[1][Max( i - 1, 0 )] );
    GfxMoveTo( Point + i, y1 );
    
    if( i > LVB - Point ) break;
}

GfxSelectPen( colorGold, 2 );
GfxMoveTo( Point, PF[Point] );
GfxLineTo( Point + i - 1, PF[Point] );
Plot( C, "", colorDefault, styleCandle );

_SECTION_END();

Thanks @fxshrat for your simple and useful solution.

Best regards,
Carlos

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.