Simple Coding Help - No Promise.

Hi Senior Mmber and Happy Sir

Can you please let me know how to plot the below trailing stop loss in Amibroker

Trailng stop : Highest low(taking highest low of every bar) - atr(20)
 
Last edited:
Hey Guys...! This is a very simple stupid AFL sintaxis-logic question, but Im a little bit rusty to digg quite inside. I would like to "ReWrite" the NVI() PVI() functions, in order to change some of the internals, in this case the "Close ROC" originally propossed by Fosback.

In my attempt, I want to use AvgPrice range, my initial formula is based on an algorithm found on TradingViews, given funcitons used as well on AFL Ami's! Here is the code that currently give errors:

Code:
_SECTION_BEGIN("Modded Negative Volume Index");
function xNVI()
{
	xROC = ROC(Avg,1);
	xNVI = IIf(V < Ref(V,-1), Nz(Ref(xNVI,-1),0) + xROC, Nz(Ref(xNVI,-1),0));
	return xNVI	;
}
Plot( xNVI(), _DEFAULT_NAME(), ParamColor("Color", colorCycle ), ParamStyle("Style")  );

_SECTION_END();
Thaks in Advance! :thumb:
 

trash

Well-Known Member
Hey Guys...! This is a very simple stupid AFL sintaxis-logic question, but Im a little bit rusty to digg quite inside. I would like to "ReWrite" the NVI() PVI() functions, in order to change some of the internals, in this case the "Close ROC" originally propossed by Fosback.

In my attempt, I want to use AvgPrice range, my initial formula is based on an algorithm found on TradingViews, given funcitons used as well on AFL Ami's! Here is the code that currently give errors:

Code:
_SECTION_BEGIN("Modded Negative Volume Index");
function xNVI()
{
	xROC = ROC(Avg,1);
	xNVI = IIf(V < Ref(V,-1), Nz(Ref(xNVI,-1),0) + xROC, Nz(Ref(xNVI,-1),0));
	return xNVI	;
}
Plot( xNVI(), _DEFAULT_NAME(), ParamColor("Color", colorCycle ), ParamStyle("Style")  );

_SECTION_END();
Thaks in Advance! :thumb:
Ref() is not for self referencing! It is an array function. It is applied on whole array at once.

For self referencing equations you may use loop.

Code:
_SECTION_BEGIN( "Modded Negative Volume Index" );
// fixed self referencing  by trash
function xNVI(array)
{
    xROC = ROC( array, 1 );
    Vcond = ROC( V, 1 ) < 0;

    _NVI = 0; // initialize _NVI

    for ( i = 1; i < BarCount; i++ )
    {
        prevNVI = _NVI[ i - 1 ]; // previous value of _NVI

        if ( Vcond[i] )
            _NVI[ i ] = prevNVI + xROC[ i ];
        else
            _NVI[ i ] = prevNVI;
    }

    return _NVI + 100;
}

Plot( xNVI(Avg), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style" ) );
_SECTION_END();
 
Last edited:

trash

Well-Known Member
Hey Guys...! This is a very simple stupid AFL sintaxis-logic question, but Im a little bit rusty to digg quite inside. I would like to "ReWrite" the NVI() PVI() functions, in order to change some of the internals, in this case the "Close ROC" originally propossed by Fosback.

In my attempt, I want to use AvgPrice range, my initial formula is based on an algorithm found on TradingViews, given funcitons used as well on AFL Ami's! Here is the code that currently give errors:

Code:
_SECTION_BEGIN("Modded Negative Volume Index");
function xNVI()
{
	xROC = ROC(Avg,1);
	xNVI = IIf(V < Ref(V,-1), Nz(Ref(xNVI,-1),0) + xROC, Nz(Ref(xNVI,-1),0));
	return xNVI	;
}
Plot( xNVI(), _DEFAULT_NAME(), ParamColor("Color", colorCycle ), ParamStyle("Style")  );

_SECTION_END();
Thaks in Advance! :thumb:
But you could also use this approach shown by Tomasz http://www.amibroker.com/kb/2014/11/04/indicators-based-on-user-values/
to use a custom array other than Close for functions that work on Close by default.

In your case it would be

Code:
_SECTION_BEGIN( "Modded Negative Volume Index" );
//http://www.amibroker.com/kb/2014/11/04/indicators-based-on-user-values/

procedure SaveRestorePrices( DoSave )
{
  global SaveO, SaveH, SaveL, SaveC, SaveV;
  
  if( DoSave )
  {
     SaveO = Open;
     SaveH = High;
     SaveL = Low;
     SaveC = Close;
     SaveV = Volume;
  }
  else
  {
    Open = SaveO;
    High = SaveH;
    Low = SaveL;
    Close = SaveC;
    Volume = SaveV;
  }
}

// save OHLCV arrays
SaveRestorePrices( True );

// calculate our array
customArray = Avg;

// override built-in array(s)
Close = customArray;

// calculate our function, MACD and Signal in this case
Plot( NVI(), "NVI", colorRed );

// restore OHLCV arrays
SaveRestorePrices( False );
_SECTION_END();
 
Ref() is not for self referencing! It is an array function. It is applied on whole array at once.

For self referencing equations you may use loop.

Code:
_SECTION_BEGIN( "Modded Negative Volume Index" );
// fixed self referencing  by trash
function xNVI(array)
{
    xROC = ROC( array, 1 );
    Vcond = ROC( V, 1 ) < 0;

    _NVI = 0; // initialize _NVI

    for ( i = 1; i < BarCount; i++ )
    {
        prevNVI = _NVI[ i - 1 ]; // previous value of _NVI

        if ( Vcond[i] )
            _NVI[ i ] = prevNVI + xROC[ i ];
        else
            _NVI[ i ] = prevNVI;
    }

    return _NVI + 100;
}

Plot( xNVI(Avg), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style" ) );
_SECTION_END();

That's what I try to found somewhere, the "loop" method to rewrite the function itself! Just something I noted:

The algorithm itself plots "same pattern" as the built in NVI() formula at Ami, but given some time; the Ami indicator index growth much more than the one your wrote with the same array CLOSE.

Eventhough this little issue, the function plots what it supposed to plot given this mathematical criteria!

Thanks a lot Trash! :clap:
 
_SECTION_BEGIN("Bollinger Bands");

_SECTION_BEGIN("DispMA");
P = ParamField("Field");
Type = ParamList("Type", "Simple,Exponential,Double Exponential,Tripple Exponential,Wilders,Weighted");
Periods = Param("Periods", 8, 2, 300 );
Displacement = Param("Displacement", 3, -50, 50 );
m = 0;

if( Type == "Simple" ) m = MA( P, Periods );
if( Type == "Exponential" ) m = EMA( P, Periods );
if( Type == "Double Exponential" ) m = DEMA( P, Periods );
if( Type == "Tripple Exponential" ) m = TEMA( P, Periods );
if( Type == "Wilders" ) m = Wilders( P, Periods );
if( Type == "Weighted" ) m = WMA( P, Periods );

Plot( m, _DEFAULT_NAME(), ParamColor("Color", ColorCycle), ParamStyle("Style"), 0, 0, Displacement );
_SECTION_END();



P=m ;
//P = ParamField("Price field",-1);
Periods = Param("Periods", 8, 2, 300, 1 );
Width = Param("Width", 1, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style );
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style );
_SECTION_END();

MA8 = MA(C,8);
//Plot(MA8,"MA8",colorRed,styleThick);
_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

dear all
can any one add buy signal above top bollinger band
and sell below at bottom bollinger band
with arrow pls
 
_SECTION_BEGIN("Bollinger Bands");

_SECTION_BEGIN("DispMA");
P = ParamField("Field");
Type = ParamList("Type", "Simple,Exponential,Double Exponential,Tripple Exponential,Wilders,Weighted");
Periods = Param("Periods", 8, 2, 300 );
Displacement = Param("Displacement", 3, -50, 50 );
m = 0;

if( Type == "Simple" ) m = MA( P, Periods );
if( Type == "Exponential" ) m = EMA( P, Periods );
if( Type == "Double Exponential" ) m = DEMA( P, Periods );
if( Type == "Tripple Exponential" ) m = TEMA( P, Periods );
if( Type == "Wilders" ) m = Wilders( P, Periods );
if( Type == "Weighted" ) m = WMA( P, Periods );

Plot( m, _DEFAULT_NAME(), ParamColor("Color", ColorCycle), ParamStyle("Style"), 0, 0, Displacement );
_SECTION_END();



P=m ;
//P = ParamField("Price field",-1);
Periods = Param("Periods", 8, 2, 300, 1 );
Width = Param("Width", 1, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style );
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style );
_SECTION_END();

MA8 = MA(C,8);
//Plot(MA8,"MA8",colorRed,styleThick);
_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

dear all
can any one add buy signal above top bollinger band
and sell below at bottom bollinger band
with arrow pls
Try something like this:

Buy = C < ( BBandBot( P, Periods, Width );
Sell = C > ( BBandBot( P, Periods, Width );

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, L, H ), -25 );
 
Hi Senior Mmber and Happy Sir

Can you please let me know how to plot the below trailing stop loss in Amibroker

Trailng stop : Highest low(taking highest low of every bar) - atr(20)
 

Similar threads

Zerodha – Open Paperless Account

Open online account with Zerodha. Free delivery trading and Max Rs 20 for Intraday, F&O, Currency and Commodity Trading. Intraday High leverage with MIS, CO and BO.

Name:Phone:
Email:City:
State:
Are you a day trader?