# Change indicator parameters if other indicator chages

Since the topic of this post is wrt interval, I may have creatyed some confusion - just to clarify - I am trying to write a code which "automatically changes the values of stochastic ( "periods, Ksmooth, Dsmooth") based on the ATR volatility" .

I only come to the forum for help after trying with what little understanding I have, so I consider myself as a newbie ! Here is another cry for help--

I have been using an ATR based volatility (ribbon plotted below stochastic for visual look only) and then manually (param) change the values of "periods, Ksmooth, Dsmooth" for the stochastic indicator as the volatility changes.

I have tried to write a code to automatically change the values, with change in volatility, but still being relatively new have had no success.
First I will post the code "

``````t
// using ATR as a measure of volatility - hourly chart has 7 bars in a day,  14 bars = 2days and 35 bars = 1 week
X = (ATR(14)-LLV(ATR(14),35))*100/(HHV(ATR(14),35)-LLV(ATR(14),35)); //% position of current ATR(14) over last 35 bars
zonecolor = IIf(x > 70,colorRed, IIf( x > 40 AND x < 70, coloryellow, colorLime)); // below 40, color lime  = low volatility
Plot(3,"  Zone",Zonecolor,styleOwnScale|styleArea|styleNoLabel|styleNoTitle,0, 100 );

///// Is there a way to automatically change the values of "periods, Ksmooth, Dsmooth" as the ATR volatility changes /////

_SECTION_BEGIN("Stochastic %D");
periods = Param( "Periods", 15, 1, 200, 1 ); // colorred = 5, coloryellow = 14, colorlime = 21
Ksmooth = Param( "%K avg", 3, 1, 200, 1 ); // colorred = 3, coloryellow = 5, colorlime = 8
Dsmooth = Param( "%D avg", 3, 1, 200, 1 ); //colorred = 3, coloryellow = 5, colorlime = 8
Plot( StochD( periods , Ksmooth, DSmooth ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("Stochastic %K");
periods = Param( "Periods", 15, 1, 200, 1 ); //  colorred = 3, coloryellow = 5, colorlime = 8
Ksmooth = Param( "%K avg", 3, 1, 200, 1 ); //  colorred = 3, coloryellow = 5, colorlime = 8
Plot( StochK( periods , Ksmooth), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();
``````

The values of the stoch. depend on the volatility as measeured by the ATR and required values are
mentioned after "//".

The basic concept is from the book "High Probability Trading Strategies " by Robert C. Miner

@Tomasz has offered 2 suggestions for changing the values wrt the interval. I think some modification of the switch statement should work -- still trying with no success.

Any help will be much appreciated. Thanks.

@JEETU to create an indicator that automatically adjusts it's "look back" periods, you can code your own custom Stochastic and build in the volatility measure you want as a method of determining the Stochastic periods. The formula depends on if you want a longer or shorter look back period for higher (or lower) volatility. You can just rearrange the order of the adjustment depending on your goal but in this example the lookback period shortens with increase in volatility. And of course you should set your own preferred Minimum and Maximum periods.

``````VolPeriods = 35; // your volatility measure, look back periods
StochMinPds = Param( "Min", 5, 1, 20, 1 );
StochMaxPds = Param( "Max", 28, StochMinPds, 50, 1 );

VolatilityMeasure = ATR(14);

HiVolatility = HHV( VolatilityMeasure, VolPeriods );
LoVolatility = LLV( VolatilityMeasure, VolPeriods );

AdjustmentFactor = ( VolatilityMeasure - LoVolatility ) / ( HiVolatility - LoVolatility  + 0.00001);

StochPeriods = round( StochMinPds + ( StochMaxPds - StochMinPds ) * ( 1 - AdjustmentFactor ) );

HH = HHV( H, StochPeriods );
LL = LLV( L, StochPeriods );
AdaptiveStoch = ( C - LL ) / ( HH - LL ) * 100; // raw stochastic

///  Explore  ///
Filter=1;

///  Charting  ///
PlotGrid(80, colorYellow, 1);
PlotGrid(20, colorYellow, 1);

``````

The above is similar to a "raw" Stochastic K. If you want to smooth it then take a moving average of it.

It is worth reviewing the Stochastic. Basics: Fast Stochastics

5 Likes

@portfoliobuilder

Thanks for your guidance, I wanted to hard code the periods, so have made small changes to your code, a screen shot of the plot is posted below followed by the code.

``````VolPeriods = 35; // your volatility measure, look back periods
//StochMinPds =Param( "Min", 5, 1, 20, 1 );
//StochMaxPds = Param( "Max", 21, StochMinPds, 50, 1 );

VolatilityMeasure = ATR(14);

HiVolatility = HHV( VolatilityMeasure, VolPeriods );
LoVolatility = LLV( VolatilityMeasure, VolPeriods );

AdjustmentFactor = ( VolatilityMeasure - LoVolatility ) / ( HiVolatility - LoVolatility  + 0.00001);

//  round( StochMinPds + ( StochMaxPds - StochMinPds ) * ( 1 - AdjustmentFactor ) );

HH = HHV( H, StochPeriods );
LL = LLV( L, StochPeriods );
AdaptiveStoch = ( C - LL ) / ( HH - LL ) * 100; // raw stochastic

///  Explore  ///
Filter=1;

///  Charting  ///
Plot(3,"",zonecolor,styleOwnScale|styleArea|styleNoLabel|styleNoTitle,0, 100 );
Plot ( Std,"| Stoch" + "(" +StochPeriods+")"+" | %D " +"(" + StDperiod+")",colorViolet);
Plot ( Stk,"| %K " +"(" + Stkperiod+")",colorOrange);

PlotGrid(80, colorYellow, 1);		//PlotGrid(40, colorYellow, 1);
PlotGrid(20, colorYellow, 1);

RSIV = RSI(RSIperiod);
Plot(RSIV,"",colorBlack,styleLine, styleThick);*/

``````

Need some more guidance, I tried to do the same for RSI, refer last 3 lines of the code marked by" /* ",
but get an error message - Error 5 = " The funcction expected a NUMBER but found an array".

Same way also for RSI but the error message - grateful if you can explain why this error message and if possible give a solution.

Thanks

Do you really need spoon feeding for every little detail even if it is easily to be found out by oneself with little effort and as simple as making one step after another with your two legs?

How about using search? Ever heard of "search engines"? No?
Which will lead you to Error 5

Now what is the difficulty in understanding that you have used incorrect argument type?

Do you understand difference between number and array? No?
Using search will lead you to explanation again:
"What is array" is explained here.
Number is not a list of values but it is single value. Number may be an element of array, for example.
OK?

Last but not least here you may find list of functions accepting variable period (array or number). As you can see RSI is not part of that list.

1 Like

@JEETU as fxshrat has pointed out the error you are trying to understand, in AmiBroker the built in RSI indicator only accepts a number or scalar value for the “periods” and not an array. See User Guide,

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

So like in the previous Stochastic example you can recreate your own custom RSI that accepts a variable number of lookback periods. Or review the code examples by @tomasz

2 Likes

@portfoliobuilder
Thanks for providing the links and on first glance at the detailed explanation by Tomasz I should be able to ,
firstly learn much more and get the solution for the RSI.

In case I have any problems , will get back, however as of now I am pretty sure this acpect of my learning curve is complete.

Once again thanks a lot.

@portfoliobuilder

Posting a screen capture of the variable period RSI for a daily chart . The formula is also posted below.
Once again thanks a lot !

``````
_SECTION_BEGIN("VARIABLE PERIOD RSI");
// VARIABLE PERIOD RSI BASED ON ATR VOLATILITY - DAILY CHART

SetChartOptions(0,chartHideQuoteMarker);
GraphLabelDecimals = 2;
GraphXSpace = 10;
function VarRSI( array, periods )
{
Chg = array - Ref( array, -1 );
pc = Max( Chg, 0 );
nc = Max( -Chg, 0 );

pa = AMA( pc, 1/periods );
na = AMA( nc, 1/periods );

return 100 * pa / ( pa + na );
}
X = (ATR(14)-LLV(ATR(14),42))*100/(HHV(ATR(14),42)-LLV(ATR(14),42));
zonecolor = IIf(x > 70,colorRed, IIf( x < 40,  colorLime,coloryellow ));
Plot(3,"  Zone",Zonecolor,styleOwnScale|styleArea|styleNoLabel|styleNoTitle,0, 100 );
period = IIf(x > 70,8, IIf(x < 40,21,13));

Plot(VarRsi(C,period), "RSI "+"(" + period+")", colorBlue, styleLine | styleThick);
PlotGrid(50,colorBlack); PlotGrid(70,colorRed); PlotGrid(30,colorLime);PlotGrid(60,colorLightYellow); PlotGrid(40,colorLightYellow);
Plot (EMA(VarRsi(C,period),13),"EMA"+"(" + 13 +")", colorRed, styleDashed | styleThick);
_SECTION_END();
``````
4 Likes