Change Param values based on timeframe

hi,

I want to use different default values for Bollinger Bands based on TimeFrame.
Ex: in Daily TF, use 20,125,1.5
in Weekly TF, use 24,20,2

Below is the code i was trying to use, but setting Timeframe to Daily, Weekly, always picks the default assignments (24,20,2) in inWeekly branch, in code below. How can i change the param values based on timeframe?

_SECTION_BEGIN( "BB Squeeze" );
//BBMedian = MA(C,20);

if( inDaily )
{
    defaultCount = 20;
    defaultPeriod = 125;
    defaultWidth = 1.5;

}

if( inWeekly )
{
    defaultCount = 24;
    defaultPeriod = 20;
    defaultWidth = 2;
}

SDCount = Param( "SDCount", defaultCount, 1, 300, 1 );
Periods = Param( "Periods", defaultPeriod, 1, 300, 1 );
Width = Param( "Width", defaultWidth, 0, 10, 0.05 );

P = StDev( C, SDCount );
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 );
Plot( P, "StdDev", colorBlack );



_SECTION_END();

Read this:
https://www.amibroker.com/guide/afl/interval.html

Also, when in doubt, log variables... such as inDaily and inWeekly and see what they contain.

It doesn't always pick the weekly branch, it's executing both blocks of code within the if statements because both conditions resolve to true...

Tried using Interval. Its not working as expected.
The code branch(daily/weekly) is picked based on timeframe selected in Amibroker, when i add the chart to pane. i.e. if timeframe in Amibroker is Daily when i add this chart to Pane, daily logic runs , and same for weekly.

timeframe = Interval( 2 );

if( StrMatch( timeframe , "Daily" ) )
{
    defaultCount = 20;
    defaultPeriod = 125;
    defaultWidth = 1.5;

}

if( StrMatch( timeframe , "Weekly" ) )
{
    defaultCount = 24;
    defaultPeriod = 20;
    defaultWidth = 2;
}

You have to compare Interval() function with reserved variable.

Also Params are cached. So if you add variable value into Param* it won't change when you change interval. But it would change when you change interval and then click Reset all (for example).

instead you should add default value to constant param value.

matstr	=

"{" +
"{20,24}," +  // defaultCount
"{125,20}," + // defaultPeriod
"{1.5,2.0}" +  // defaultWidth
"}";

col = ((Interval() == inDaily) +
       (Interval() == inWeekly) * 2);
col = Max(0, col-1);
mat	= MxFromString(matstr);

SDCount = mat[0][col] + Param( "defaultCount + SDCount", 0, 0, 300, 1 );
Periods = mat[1][col] + Param( "defaultPeriod + Periods", 0, 0, 300, 1 );
Width = mat[2][col] + Param( "defaultWidth + Width", 0, 0, 10, 0.05 );

P = C;
Color = ParamColor( "Color", colorCycle );
Style = ParamStyle( "Style" );
Plot( C, "Price", colorDefault, styleBar );
Plot( BBandTop( P, Periods, Width ), StrFormat("BBTop(%g,%g)", Periods, Width), Color, Style );
Plot( BBandBot( P, Periods, Width ), StrFormat("BBBot(%g,%g)", Periods, Width), Color, Style );
Plot( StDev(C, SDCount), StrFormat("StdDev(%g)", SDCount), colorBlack, styleOwnScale );

Besides this one

would have to be e.g.

timeframe = Interval( 2 );

defaultCount = 1;
defaultPeriod = 1;
defaultWidth = 1;

if( StrMatch( timeframe , "inDaily" ) )
{
    defaultCount = 20;
    defaultPeriod = 125;
    defaultWidth = 1.5;

}

if( StrMatch( timeframe , "inWeekly" ) )
{
    defaultCount = 24;
    defaultPeriod = 20;
    defaultWidth = 2;
}

Or....

1 Like
if(Interval() ==(inDaily))
{
Periods =15;
width=2;
color = colorblack;
}
else if(Interval() ==(inWeekly))
{
periods = 20;
width = 2;
color = colorred;
}

P = ParamField("Price field",-1);
/*Periods = Param("Periods", 15, 2, 300, 1 );
Width = Param("Width", 2, 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 ); 

Thank you @fxshrat . 1st Solution works.

Also, want to clarify if the string to be compared against would be "Daily" or "inDaily"
The function reference for "Interval" notes:

•format = 2 - returns STRING with name of interval such as "Weekly/Monthly/Daily/Hourly/15-minute/5-tick"

Please log it and see for yourself what it is.
You don't have to ask what any variable or function return is anymore if you're able see it.

https://www.amibroker.com/guide/afl/_trace.html

2 Likes

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