Drag and Drop HMA several times

Hello everyone,

When I drag and drop MA two Times on The Price chart it return error to me,
but when I drag and drop it one time it run well,
What's the potential error in my code ?
thanks in advance


_SECTION_BEGIN( "HMA Param" );
function PriceChoice( price_choice )
{
    price_value = 0;

    switch( price_choice )
    {
        case "Open":
            price_value = O;
            break;

        case "High":
            price_value = H;
            break;

        case "Low":
            price_value = L;
            break;

        case "Close":
            price_value = C;
            break;

        case "Average":
            price_value = ( H + L + C ) / 3;
            break;

        case "Volume":
            price_value = V;
            break;

        case "Vwap":
            price_value = OpenInt;
            break;
    }


    return price_value;
}


function UpDwnClr( MaPram)
{	
	trend = 0;


    for( i = 1 ; i < BarCount; i++ )
	{
		if( i < BarCount )
		{
			if( MaPram[i] > MaPram[i - 1] )
				trend[i] = 1;
			else
			{
				if( MaPram[i] < MaPram[i - 1] )
					trend[i] = -1;
				else
					trend[i] = trend[i - 1];
			}
		}
		else
			trend[i] = 0;
    }

   return trend;
}


HMA_Period = Param( "HMA period", 25, 10, 100 );
HMA_Price  = ParamList( "HMA Price", "Open|High|Low|Close|Average|Volume|Vwap" );
HMAUpColor  = ParamColor("HMA UpColor", colorGreen);
HMADwnColor = ParamColor("HMA DownColor", colorRed);
HMA_1st = HMA( PriceChoice(HMA_Price), HMA_Period );  
trend   = UpDwnClr( HMA_1st) ; 
upHMA   = trend > 0;
downHMA = trend < 0;

HMAColor  = IIf( upHMA, HMAUpColor, IIf( downHMA, HMADwnColor, HMAUpColor ) );
Plot( HMA_1st, _DEFAULT_NAME(),HMAColor, ParamStyle( "HMA Style" ) , Null, Null, 0, 0, 2);
_SECTION_END();


Filter = C;


@ahm.montaser this happens because when you drop your indicator twice the resulting formula will also have two copies of the functions you wrote; this is not allowed (function names in a formula should be unique).

One way to address this is to move the functions to a separate include file and then use the #include_once to "include" them only once in the resulting formula that is "built" with the drag&drop actions.

Moreover, before saving your custom indicator, do not include the _SECTION_BEGIN / END as explained in this guide article, in the section titled:
Special functions: SECTION_BEGIN, _SECTION_END, _SECTION_NAME, _DEFAULT_NAME, _PARAM_VALUES explained

They will automatically be added by Amibroker when you drag the indicator (based on the filename you assigned to your indicator, plus, if needed, a progressive number) so you can set independent parameters for each indicator instance.

Exists already.
No need for repetition.


It is not forbidden to add custom sections with custom section name.

I never said it is forbidden; you can surely do it for indicators that you plan to apply only once to a chart, but if you add a named _SECTION_BEGIN() (and the relative end) to your indicator before saving it, when you drop them multiple times on a chart, the resulting formula will have "duplicate" _SECTION_BEGIN() names and you'll NOT get the possibility, using the "Parameters..." window, to change the user-defined parameters individually for each instance of the indicator.

Check for example the various moving averages default formulas: none of them is included in a _SECTION_BEGIN / END.

As indicated in the guide:

When dropping indicator AmiBroker automatically checks for already existing section names and auto-numbers similarly named sections to avoid conflicts. Section name also appears in the Parameter dialog.

I do not know if there is another way to get the same result, but this is what is suggested in the official guide.

For future reference, here is a clarification about the use of _SECTION_BEGIN / _SECTION_END by @Tomasz.

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