Freebie week ahead

Feeling a little bored - here is the deal :slight_smile:

Starting from now until a week from now I will be trying to code ( AFL) anything you throw at me (indiscriminately) and obviously for free.
I will try my best, and if I can do it, you wll be provided with a solution with the sole condition that anything I find interesting will be posted publicly in this forum.

That's it.

Edit: I advise you to pm me with your request not to clutter this thead/forum

11 Likes

Hi, I think I have an interesting indicator, it's the QQE (Quantitative Qualitative Estimation).
A version of it in afl exists but it's not the one I know.
I have the code in easylanguage and I failed to translate it to afl.
It includes also 2 functions, one for the modified exponential moving average (MEMA)
and one for the QQELine.
It's a subchart indicator.

// QQE (Quantitative Qualitative Estimation)
Inputs: 
Price( Close ),
RSI_Period( 14 , 1 ),
RSI_Period2( 7 , 1 ),
ATR_Period( 14 , 1 ),
Delta_Period( 14, 1 ),
Val_Delta1( 2.618 ),
Val_Delta2( 4.236 ),
Smoothing( 2 , 1 ),
suplines(false),
OverSold( 30, 0, 100 ), 
OverBought( 70, 0, 100 );
Vars:
rsiVal, thRSI, tlRSI, trRSI, atrRSI, 
deltaATR_1, deltaATR_2,trRSIFast, trRSISlow, fillColor;

rsiVal = RSI( Close, RSI_Period );
thRSI = Iff( rsiVal[1] > rsiVal, rsiVal[1], rsiVal );
tlRSI = Iff( rsiVal[1] < rsiVal, rsiVal[1], rsiVal );
trRSI = thRSI - tlRSI;
atrRSI = Mema( trRSI, ATR_Period );
deltaATR_1 = Mema( atrRSI, Delta_Period ) * Val_Delta1;
trRSIFast = QQELine( rsiVal, deltaATR_1, Smoothing );
deltaATR_2 = Mema( atrRSI, Delta_Period )* Val_Delta2;
trRSISlow = QQELine( rsiVal, deltaATR_2, Smoothing );

If trRSIFast > trRSISlow Then
fillColor = TransparentColor( Cyan, 220 )
Else fillColor = TransparentColor( Magenta, 220 );

DrawLine( trRSIFast, "Fast", StyleSolid, 1, fillColor );
DrawLIne( trRSISlow, "Slow", StyleSolid, 1, fillColor );
DrawArea( trRSIFast, trRSISlow, "FAst", "Slow", fillColor, Transparent, Transparent );
DrawLine( rsiVal, "RSI", StyleSolid, 2, white );
if suplines then begin
DrawLine( overbought, "overbought", StyleDot, 1, magenta );
DrawLine( oversold, "oversold", StyleDot, 1, cyan );
end;

-----

// Modified Exponential Moving Average
Inputs: 
Price( NumericSeries ), 
Period( NumericSimple );
Var: 
smoothing( 0 );

If Period <> 0 Then
smoothing = 1 / Period 
Else smoothing = 1;
If CurrentBar = 1 Or IsInvalid( MEMA[1] ) Then
MEMA = Average( Price, Period )
Else MEMA = ( smoothing * Price ) + ( ( 1 - smoothing ) * MEMA[1] );
	
-------	

// QQE line 
Inputs:
indiValue( NUmericSeries ),
deltaRSIValue( NumericSimple ),
smoothPeriod( NumericSimple );
Vars:
prevVal, prevIndi, higherCond, lowerCond, qqeValue;

prevVal = qqeValue[1];
prevIndi = indiValue[1];
higherCond = ( prevIndi > prevVal ) And ( indiValue > prevVal );
lowerCond = ( prevIndi < prevVal ) And ( indiValue < prevVal );
qqeValue = Iff( indiValue = prevVal, prevVal, 
Iff( lowerCond, MinList( prevVal, indiValue + deltaRSIValue ), 
Iff( higherCond, MaxList( prevVal, indiValue - deltaRSIValue ), 
Iff( indiValue > prevVal, indiValue - deltaRSIValue, indiValue + deltaRSIValue ) ) ) );

QQELine = XAverage( qqeValue, smoothPeriod );

image

I don't think Aron is really bored and since it is public and FIFO here is translation to AFL.

That MEMA is just AMA in AFL.
XAverage line in EL is just EMA.

// QQE line
function QQELine( indiValue, deltaRSIValue, smoothPeriod ) {
    /// translation to AFL by fxshrat@gmail.com
    /// @link https://forum.amibroker.com/t/freebie-week-ahead/14343/3
    local prevVal, prevIndi, qqeValue, indi_sum;
    local indi_diff, higherCond, lowerCond, result;
    prevVal = prevIndi = 0;
    qqeValue = Null;
    indi_sum = indiValue + deltaRSIValue;
    indi_diff = indiValue - deltaRSIValue;
    for ( i = 1; i < BarCount; i++ ) {
        prevVal = qqeValue[i - 1];
        prevIndi = indiValue[i - 1];
        higherCond = prevIndi > prevVal AND indiValue[i] > prevVal;
        lowerCond = prevIndi < prevVal  AND indiValue[i] < prevVal;
        qqeValue[i] = Iif( indiValue[i] == prevVal, prevVal,
                           Iif( lowerCond, Min( prevVal, indi_sum[i] ),
                                Iif( higherCond, Max( prevVal, indi_diff[i] ),
                                     Iif( indiValue[i] > prevVal, indi_diff[i], indi_sum[i] ) ) ) );
    }
    result = EMA( qqeValue, smoothPeriod );// EL XAverage is EMA
    return result;
}

// Modified Exponential Moving Average
function MEMA( Price, Period ) {
    local smoothing, result;
    smoothing = 1 / Max( 1, Period );
    result = AMA( Price, smoothing );
    return result;
}

/// QQE (Quantitative Qualitative Estimation)
/// translation to AFL by fxshrat@gmail.com
/// @link https://forum.amibroker.com/t/freebie-week-ahead/14343/3
Price = Close;
RSI_Period = 14;
RSI_Period2 = 7;
ATR_Period = 14;
Delta_Period = 14;
Val_Delta1 = 2.618;
Val_Delta2 = 4.236;
Smoothing = 2;
suplines = True;
OverSold = MxFromString("[30;0;10]");
OverBought = MxFromString("[70;0;100]");

rsiVal = RSIa(C, RSI_Period);
rc = ROC(rsiVal, 1);
thRSI = Iif(rc < 0, Ref(rsiVal, -1), rsiVal);
tlRSI = Iif(rc > 0, Ref(rsiVal, -1), rsiVal);
trRSI = thRSI - tlRSI;
atrRSI = Mema(trRSI, ATR_Period );
deltaATR_1 = Mema(atrRSI, Delta_Period) * Val_Delta1;
trRSIFast = QQELine(rsiVal, deltaATR_1, Smoothing);
deltaATR_2 = Mema(atrRSI, Delta_Period)* Val_Delta2;
trRSISlow = QQELine(rsiVal, deltaATR_2, Smoothing);

fillColor = IIf(trRSIFast > trRSISlow, colorTurquoise, colorViolet);

Plot(rsiVal, "RSI", colorWhite, styleLine);
Plot(trRSIFast, "Fast", fillColor, styleline);
Plot(trRSISlow, "Slow", fillColor, styleline);
style_cloud = styleCloud | styleClipMinMax | styleNoLabel;
PlotOHLC(trRSIFast, trRSIFast, trRSISlow, trRSISlow, "Fast Slow", fillColor, style_cloud);
if ( suplines ) {
    for ( i = 0; i < 3; i++ ) {
        Plotgrid( overbought[i][0], colorViolet );
        Plotgrid( oversold[i][0], colorTurquoise );
    }
}

I haven't compared to TS output but here is example picture of AFL version.
6

3 Likes

wow, thank you very much, works perfectly ! :slight_smile:

Hi,
I am using a slightly edited version of a code , I think taken from the library.

Basically it is trying to plot swing lines using "2 bar fractals" to identify pivots.

First the code --

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates | chartHideQuoteMarker);
GraphLabelDecimals = 2;
GraphXSpace =10;
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}}   |  {{DATE}}  |  Open %g  | Hi %g  |  Lo %g  | Close %g  |   (%.1f%%) {{VALUES}}", O, H, L, C,     SelectedValue( ROC( C, 1 ) ) ));
TC = IIf(C>Ref(C,-1),colorGreen,IIf(C<Ref(C,-1),colorRed,colorGold));
Plot( C, "Close", TC, styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 


_SECTION_BEGIN( "Fractals" );
x=xx=BarIndex();		Lx=LastValue(x);
nbar=Param("N Pivot Bars",2,2,21,1);

SetChartOptions(0,chartShowArrows|chartShowDates | chartHideQuoteMarker);
GraphLabelDecimals = 2;
GraphXSpace =10;

//Mark Fractals
pk=H>Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar)<=H;
tr=L<Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar)>=L;


PlotShapes(shapeSmallCircle*tr,IIf(Lx-ValueWhen(tr,x)>nbar,colorBlue,colorWhite),0,L,-10);
PlotShapes(shapeSmallCircle*pk,IIf(Lx-ValueWhen(pk,x)>nbar,colorRed,colorWhite),0,H,10);

// DRAWING SWING LINES
plotEWswings = ParamToggle( "EW Swings", "Off|On", 1 );

// Identify Peak and trough
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pk = IIf( pk, IIf( ValueWhen( pk, x, 2 ) < ValueWhen( tr, x,1), pk, IIf( ValueWhen( pk, H, 2 ) > H, False, pk ) ), pk );
pk = IIf( pk AND ValueWhen( pk, x, 0 ) > x, IIf( ValueWhen( pk, x, 0 ) < ValueWhen( tr, x, 0 ), IIf( ValueWhen( pk, H, 0 ) >= H, False, pk ), pk ), pk );

tr = IIf( tr, IIf( ValueWhen( tr, x, 2 ) < ValueWhen( pk, x ), tr, IIf( ValueWhen( tr, L, 2 ) < L, False, tr ) ), tr );
tr = IIf( tr AND ValueWhen( tr, x, 0 ) > x , IIf( ValueWhen( tr, x, 0 ) < ValueWhen( pk, x, 0 ), IIf( ValueWhen( tr, L, 0 ) <= L, False, tr ), tr ), tr );

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//bar index and value
px0=ValueWhen(pk,x,0);		ph0=ValueWhen(pk,H,0); 
 tx0=ValueWhen(tr,x,0);			tl0=ValueWhen(tr,L,0);
px1=ValueWhen(pk,x,1); 		ph1=ValueWhen(pk,H,1);
tx1=ValueWhen(tr,x,1);			 tl1=ValueWhen(tr,L,1);

//Plotting swing lines 
if( plotEWswings )
{
aa1=IIf(px0>tx1 ,(ph0-tl1)/(px0-tx1),0);
aa1=IIf(pk,Ref(aa1,-1),aa1) ;
ls1=aa1*(xx-tx1)+tl1;

bb1=IIf(px0>tx1 AND px1<tx1,1,0);
bb1=(bb1+Ref(bb1,-1)) AND ph0>tl0;	
bb1=IIf(bb1,1,0);
ls1=IIf(bb1,ls1,Null)  ;
Plot(ls1,"",colorBlue,styleLine);

aa1=IIf(tx0>px1,(tl0-ph1)/(tx0-px1),0);
aa1=IIf(tr,Ref(aa1,-1),aa1);
Ls1=aa1*(xx-px1)+ph1;

bb1=IIf(tx0>px1 AND tx1<px1,1,0);
bb1=bb1+Ref(bb1,-1) AND tl0 < ph1;
bb1=IIf(bb1,1,0) AND H > Ref( tl0,-1);
ls2=IIf(bb1,ls1,Null);

Plot(ls2,"",colorOrange,styleLine);

}
_SECTION_END();


Now a screen capture to show the problem - this occurs when the same bar forms a fractal low and a fractal high

image

Below is another screen capture where in I have drawn "trend lines in white colour" to show how the trend lines should plot --

image

I have been trying to get this for the past few months without any luck, a solution will be much appreciated.

Thanks and regards

@JEETU,

I don't think you can plot that way (vertical connection if peak and through at same bar).

But in order to at least connecting to next peak and through change lines

//bar index and value
px0=ValueWhen(pk,x,0);		ph0=ValueWhen(pk,H,0); 
tx0=ValueWhen(tr,x,0);		tl0=ValueWhen(tr,L,0);
px1=ValueWhen(pk,x,1); 		ph1=ValueWhen(pk,H,1);
tx1=ValueWhen(tr,x,1);		tl1=ValueWhen(tr,L,1);

to

//bar index and value
for ( i = 0; i < 2; i++ ) {
	VarSet( "ph"+ i, ValueWhen( pk, H, i ));
	VarSet( "px"+ i, ValueWhen( pk, x, i ));	
	VarSet( "tl"+ i, ValueWhen( tr, L, i ));
	VarSet( "tx"+ i, ValueWhen( tr, x, i ));
}
	
pk = ValueWhen(!(pk AND px0<tx0 AND ph1<ph0), pk);
tr = ValueWhen(!(tr AND tx0<px0 AND tl1>tl0), tr);

for ( i = 0; i < 2; i++ ) {
	VarSet( "ph"+ i, ValueWhen( pk, H, i ));
	VarSet( "px"+ i, ValueWhen( pk, x, i ));	
	VarSet( "tl"+ i, ValueWhen( tr, L, i ));
	VarSet( "tx"+ i, ValueWhen( tr, x, i ));
}

Before
6

After
7

2 Likes

@fxshrat --- as usual your solution solved my issue - I guess the only way to look at it -- is that when such a bar occurs I should drill down to a lower time frame and start counting waves there for a better understanding.

For me this is the solution.

Warm regards and thanks a ton.

1 Like