Page 1 of 1
Adaptive Period for indicator
Posted: 23 Oct 2014 22:41
by pablo
Hi,
I'm new to Nano Trader, and still having trouble with some coding.
Now, I'm trying to code RSI, that I can input adaptive period. But instead of having RSI change period every other bar my RSI recalculates and adapts values all the way to the first bar.
Code:
Express AdaptiveRSI
Vars
series MyRSI,period;
Calculation
CalculateAtEveryTick(false);
if CurrentBarIndex() > 25 then
begin
if Floor(CurrentBarIndex()/2) = Ceiling(CurrentBarIndex()/2)
then
period = 10;
else
period = 20;
end
If IsFinalBar() then
begin
RSI(close, myRSI, period);
end
interpretation
begin
end
//plot (period, black, 2);
plot (MyRSI, red, 2);
Removing IsFinalBar() does not help at all.
Anybody ever encountered such a problem? Will appreciate all help.
Thanks,
Pawel
Re: Adaptive Period for indicator
Posted: 24 Oct 2014 10:29
by stargate
Hi,
if Floor(CurrentBarIndex()/2) = Ceiling(CurrentBarIndex()/2)
This condition is not possible, a number can not be both equal to its rounded upper and lower its rounded
This is not a coding problem but rather a problem of algorithm
cordialement
Bernard
Re: Adaptive Period for indicator
Posted: 24 Oct 2014 11:52
by pablo
Hi,
There is no logical problem.
if Floor(CurrentBarIndex()/2) = Ceiling(CurrentBarIndex()/2) returns true for even numbers and return false for odd numbers.
Variable "period" zigzags between 10 and 20 if you plot it on chart. I only chose 10/20 variation to show that I'm having trouble with variable period for indicators.
Pawel
Re: Adaptive Period for indicator
Posted: 24 Oct 2014 18:16
by stargate
Re: Adaptive Period for indicator
Posted: 24 Oct 2014 20:20
by pablo
Thank you for your time, but the indicator shown on chart is RSI(10). If you wait till next bar the period will change to 20, and whole indicator will be recalculated. At least this is what happens to me. Attached two files that show the problem. Check original RSI settings.
Thanks
Pawel
Re: Adaptive Period for indicator
Posted: 26 Oct 2014 19:24
by stargate
Hi,
Adaptive period : Digital Signal Processor (DSP) algorithm :
ScreenShot270.png
Re: Adaptive Period for indicator
Posted: 27 Oct 2014 15:05
by pablo
Hi,
From your chart I understand that it can be done. I was hoping, that built-in functions can work with variable periods. I assume, they can't.
I am not a programmer so The Digital Signal Processor (DSP) algorithm does not ring a bell for me

I can go down to RSI formula and moving averages formulas and get what I want with loops - I guess this is the way to get it done in Express. I will definitely have trouble with Standard Deviation and ATR for variable periods.
I'm working with John Ehlers indicators (
http://www.davenewberg.com/Trading/EhlersCodes.html - code need to slightly adapted for Express), and want to use his Cycle Perid for indicators, etc. It works better then fixed period. Specially for smoothing filters.
Pawel
Re: Adaptive Period for indicator
Posted: 27 Oct 2014 19:02
by stargate
// Auteur : Bernard Chamblain
// Allias : Stargate
// Date : 28/10/2012
// Version : 1.01
// D'après J.Ehlers "Rocket Science for Traders".page 2
=============================================================
Express AdaptivePeriod
Vars
numeric
CycPart(0.5),
count,
jI,
Avg,
jQ,
MD;
//Length;
Series
Smooth,
Detrender,
I1,
Q1,
I2,
Q2,
Re,
Im,
Period,
SmoothPeriod,
MedianPrice,
CCI,
Length,
Price;
Calculation
CalculateAtEveryTick(false);
Price = (H+L)/2;
if currentBarIndex() > 5 then
begin
Smooth = (4*Price + 3*Price[1] + 2*Price[2] + Price[3]) / 10;
Detrender = (0.0962*Smooth + 0.5769*Smooth[2] - 0.5769*Smooth[4] - 0.0962*Smooth[6]) + (0.075*Period[1] + 0.54);
Q1 = (0.0962*Detrender + 0.5769*Detrender[2] - 0.5769*Detrender[4] - 0.0962*Detrender[6] ) * (0.075*Period[1] + 0.54);
I1 = Detrender[3];
jI = (0.0962*I1 + 0.5769*I1[2] - 0.5769*I1[4] - 0.0962*I1[6]) * (0.075*Period[1] + 0.54);
jQ = (0.0962*Q1 + 0.5769*Q1[2] - 0.5769*Q1[4] - 0.0962*Q1[6]) * (0.075*Period[1] + 0.54);
I2 = I1 - jQ;
Q2 = Q1 + jI;
I2 = 0.2*I2 + 0.8*I2[1];
Q2 = 0.2*Q2 + 0.8*Q2[1];
Re = I2*I2[1] + Q2*Q2[1];
Im = I2*Q2[1] - Q2*I2[1];
Re = 0.2*Re + 0.8*Re[1];
Im = 0.2*Im + 0.8*Im[1];
if Im <> 0 and Re <> 0 then
Period = 360/ArcTangent(Im/Re);
if Period >1.5*Period[1] then
Period = 1.5*Period[1];
if Period < 0.67*Period[1] then
Period = 0.67*Period[1];
if Period < 6 then
Period = 6;
if Period > 200 then
Period =200;
Period = 0.2*Period + 0.8*Period[1];
SmoothPeriod = 0.33*Period + 0.67*SmoothPeriod[1];
Length = Round((CycPart*Period),1);
end
Interpretation
begin
end
plot (Period,"Blue",2);
Plot (SmoothPeriod,"Blue",1);
//plot (Length,"Blue",1);
==================================================================
Bons trades
Bernard
Re: Adaptive Period for indicator
Posted: 29 Oct 2014 21:08
by pablo
Hi,
Thx for the code. Can I see how you compute your RSI? Are you using built-in formula?
Here's code for cycle period I use. To me it seems easier to interpret than the one you are showing. It comes from site I mentioned above.
Express Hilbert_Period
Vars
///// Cycle Period Indicator by John Ehlers - Measures the Cycle Period - coded by dn
series InPhase,Quadrature,Phase,DeltaPhase,InstPeriod,Period,Price,Value3,Value4,Imult,Qmult;
numeric count;
calculation
CalculateAtEveryTick(false);
If CurrentBarIndex() > 60 then begin
//Detrend Price
Price = (high + Low)/2;
Value3 = Price - Price[7];
Imult = 0.635;
Qmult = 0.338;
// Compute InPhase and Quadrature components
Inphase = 1.25*(Value3[4] - 0.635*Value3[2]) + 0.635*InPhase[3];
Quadrature = Value3[2] - 0.338*Value3 + 0.338*Quadrature[2];
// Use ArcTangent to compute the current phase
If AbsValue(InPhase +InPhase[1]) > 0 then Phase = ArcTangent(AbsValue((Quadrature+Quadrature[1]) / (InPhase+InPhase[1])));
// Resolve the ArcTangent ambiguity
If InPhase < 0 and Quadrature > 0 then Phase = 180 - Phase;
If InPhase < 0 and Quadrature < 0 then Phase = 180 + Phase;
If InPhase > 0 and Quadrature < 0 then Phase = 360 - Phase;
// Compute a differential phase, resolve phase wraparound, and limit delta phase errors
DeltaPhase = Phase[1] - Phase;
If Phase[1] < 90 and Phase > 270 then DeltaPhase = 360 + Phase[1] - Phase;
If DeltaPhase < 1 then DeltaPhase = 1;
If DeltaPhase > 60 then Deltaphase = 60;
// Sum DeltaPhases to reach 360 degrees. The sum is the instantaneous period.
InstPeriod = 0;
Value4 = 0;
count = 0;
while
Sum(DeltaPhase,count) < 360 //and InstPeriod <> 0
begin
count = count + 1;
end
InstPeriod = count;
// Resolve Instantaneous Period errors and smooth
If InstPeriod = 0 then InstPeriod = InstPeriod[1];
Period = round(0.25*InstPeriod + 0.75*Period[1],0);
end
interpretation
begin
end
Plot (period,red,1);
Pawel