Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Gann Swing for Amibroker (AFL)
javad
about 9 years ago
Amibroker (AFL)

Rating:
3 / 5 (Votes 8)
Tags:
amibroker, gann

Gann Swing chart with a minor modification to make the formula use the Plot function so that it can be overlayed better.

Indicator / Formula

Copy & Paste Friendly
_SECTION_BEGIN("Gann Swing Charts with text");

Version(5.60);

GraphXSpace = 10;

// Swing Charts 
// Main Formula designed by Tomasz Janeczko. Equal width swing and Text formulaby Marcin Gorzynski - Amibroker Support
// Alterations by OzTraderGuy

_SECTION_BEGIN("Price - Bars or Candles");

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

PriceStyle = ParamList("Price Style", "Candlesticks|Coloured Bars");

PriceStyle1 =  PriceStyle == "Candlesticks";
PriceStyle2 =  PriceStyle == "Coloured Bars";


_SECTION_END();


twobarmode = ParamToggle("Two bar mode","No|Yes",0);
PlotType = ParamList("Plot type", "MODE1 - Equal swing|MODE2 - Low level
GFX|MODE3 - Regular");
PlotType1 =  PlotType == "MODE1 - Equal swing";
PlotType2 =  PlotType == "MODE2 - Low level GFX";
PlotType3 =  PlotType == "MODE3 - Regular";



swingColor = ParamColor("SwingColor", colorLightBlue );
UpTextColor = ParamColor("Swing High Text Color", colorGreen );
DnTextColor = ParamColor("Swing Low Text Color", colorRed );

SwingWidth = Param("Swing Thickness (px)", 2, 1, 20, 1);
EMAWidth= Param("EMA Thickness (px)", 2, 1, 20, 1);
DisplaySwing = ParamToggle("Display Swing ","No|Yes",1);
displayText = ParamToggle("Display text","No|Yes",0);
displayEMA = ParamToggle("Display EMA","No|Yes",0);


ChartScale = ParamToggle("Chart scale", "Linear|Log",0);
if( ChartScale == 0 )
	SetChartOptions(1,chartShowArrows|chartShowDates);
else
	SetChartOptions(1,chartShowArrows|chartShowDates|chartLogarithmic);

if (PriceStyle1 AND PlotType2 OR PlotType3  )  Plot( C, "Close",  colorBlack ,
styleNoTitle | styleCandle| ParamStyle("Style")  ); 
if( PriceStyle2 AND PlotType2 OR PlotType3  )	Plot( C, "Close", IIf(
C>O,colorDarkGreen , colorRed), styleNoTitle | styleBar| ParamStyle("Style") );



_SECTION_END();


_SECTION_BEGIN("Gann Swing");


/* Gann Swing chart v4 */
/* Last modified 24 Feb 2001 */
/* AFL implementation by */
/* (C)2001 Tomasz Janeczko  */
/* one and two - bar reversal version */

/* Change this to 1 for two-bar mode */


outsidebar = Outside();
insidebar = H <= Ref( H, -1 ) AND L >= Ref( L, -1 ); upbar = H > Ref( H, -1 )
AND L >= Ref( L, -1 );
downbar = L < Ref( L, -1 ) AND H <= Ref( H, -1 );

upbar2 = upbar OR ( outsidebar AND Ref( downbar, 1 ) ) ;
downbar2 = downbar OR ( outsidebar AND Ref( upbar, 1 ) );

Poi =  outsidebar AND Ref( insidebar, 1 );
Poo =  outsidebar AND Ref( outsidebar, 1 );
Pooi = Poo AND Ref( insidebar, 2 );
Poio = Poi AND Ref( outsidebar, 2 );
Poii =  Poi AND Ref( insidebar, 2 );
Pooo = Poo AND Ref( outsidebar, 2 );
Poooi = Pooo AND Ref( insidebar, 3 );
Pooio = Pooi AND Ref( outsidebar, 3 );
Poioo = Poio AND Ref( outsidebar, 3 );

upbar3 = upbar2 OR ( Poi AND Ref( downbar, 2 ) );
downbar3 = downbar2 OR ( Poi AND Ref( upbar, 2 ) );
 
upbar4 = upbar3 OR ( Poo  AND Ref( upbar, 2 ) );
downbar4 = downbar3 OR ( Poo  AND Ref( downbar, 2 ) );

upbar5 = upbar4 OR  ( Pooi AND Ref( upbar, 3 ) );
downbar5 = downbar4 OR  ( Pooi  AND Ref( downbar, 3 ) );

upbar6 = upbar5 OR  ( Poio AND Ref( upbar, 3 ) );
downbar6 = downbar5 OR  ( Poio  AND Ref( downbar, 3 ) );

upbar7 = upbar6 OR  ( Poii AND Ref( downbar, 3 ) );
downbar7 = downbar6 OR  ( Poii  AND Ref( upbar, 3 ) );

upbar8 = upbar7 OR  ( Pooo AND Ref( downbar, 3 ) );
downbar8 = downbar7 OR  ( Pooo AND Ref( upbar, 3 ) );

upbar9 = upbar8 OR  ( Poooi AND Ref( downbar, 4 ) );
downbar9 = downbar8 OR  (Poooi  AND Ref( upbar, 4 ) );

upbar10 = upbar9 OR  ( Pooio AND Ref( downbar, 4 ) );
downbar10 = downbar9 OR  (  Pooio AND Ref( upbar, 4 ) );

upbar11 = upbar10 OR  (  Poioo AND Ref( downbar, 4 ) );
downbar11 = downbar10 OR  ( Poioo  AND Ref( upbar, 4 ) );

fupbar = upbar11;
fdownbar = downbar11;
 
// Two-bar mode handling
fupbar = IIf( twobarmode, fupbar AND Ref( fupbar, 1 ), fupbar );
fdownbar = IIf( twobarmode, fdownbar AND Ref( fdownbar, 1 ), fdownbar );

/* Barcount since most recent up and down swings */
Us = BarsSince( fupbar  ); 
Ds = BarsSince( fdownbar ) ;

Sd1 =IIf( Us==0, 1,  IIf( Ds==0, -1, 0) );

Sd2 = IIf( Sd1 == 1,  IIf( Ref( BarsSince(Sd1==1), -1) > Ref( BarsSince( Sd1 ==
-1), -1), 1, 0), 
          IIf( Sd1 == -1,  IIf( Ref( BarsSince(Sd1==1),-1) < Ref( BarsSince( Sd1
== -1),-1), -1, 0),  0 ) ); 

g0 =  ValueWhen( Sd2, Sd2 ); /* sample and hold non-zero values */

/* shift back one bar, add one dummy swing at the end and shift forward */
/* to ensure proper handling of the last swing */

bi = BarIndex();
lastbar = bi == LastValue( bi );
swinglevel = IIf( g0 == -1, HighestSince( Sd2 == 1, H ), LowestSince( Sd2 == -1,
L ) );
swinglevel = IIf( lastbar, IIf( g0 == -1, LowestSince( Sd2 == -1, L ),
HighestSince( Sd2 == 1, H )), Ref( swinglevel, 1 ) );

/* Gann Swing chart drawn here */
myGraph1 =  Ref( ValueWhen( Ref( Sd2 != 0, 1 ) OR lastbar, swinglevel ,  0 ), -1
);


graphUPStartX = myGraph1 > Ref( MyGraph1, -1);
GraphDNStartX = myGraph1 < Ref( MyGraph1, -1);
myGraph2up = ValueWhen( GraphDNStartX, Ref(HighestSince(graphUPStartX ,
High),-1), 0);
myGraph2dn = ValueWhen( graphUPStartX OR Lastbar, Ref(LowestSince(GraphDNStartX
, Low),-1), 0);
LastLeg = bi >= LastValue( ValueWhen(graphUPStartX OR GraphDNStartX, bi ) );
myGraph2up = IIf( LastLeg, mygraph1, mygraph2up);
myGraph2dn = IIf( LastLeg, mygraph1, mygraph2dn);
myGraph1 = IIf( g0 == -1, myGraph2dn , myGraph2up  );



_SECTION_END();


_SECTION_BEGIN( "TEXT TO GANN SWING CHART" );


BarsOffset = Param("Shift Text Sideways", -1, -10, 0, 1);
dist = Param("Shift Text Up or Down", 1.2, 0.1, 10, 0.1)*ATR(200);

bi = BarIndex();
dt = DateTime();


textUPStartX = myGraph1 > Ref( MyGraph1, -1);
textDNStartX = myGraph1 < Ref( MyGraph1, -1);

UPsw = BarsSince( textUPStartX ) < BarsSince( textDNStartX ) ;


textUpStartBI = ValueWhen( textUPStartX , bi );
textUpEndBI = ValueWhen( textDnStartX , bi,0 );
textDnStartBI = ValueWhen( textDnStartX , bi );
textDnEndBI = ValueWhen( textUPStartX , bi,0 );

upBarsCount = ValueWhen( textDnStartX OR Lastbar, ValueWhen( H == myGraph1, 
BarsSince(L == myGraph1)  ),0 );
DnBarsCount = ValueWhen( textUPStartX OR Lastbar, ValueWhen( L == myGraph1, 
BarsSince(H == myGraph1)  ),0 );

DateUp =  ValueWhen( textDnStartX OR Lastbar, ValueWhen( H == myGraph1,  dt  ),0
);
DateDn = ValueWhen( textUPStartX OR Lastbar, ValueWhen( L == myGraph1,  dt ),0
);

TextUpPosition = bi == floor( (textUpEndBI + textUpStartBI)/2) AND Upsw;
TextDnPosition = bi == floor( (textDnEndBI + textDnStartBI)/2) AND NOT Upsw;
TextUpPosition = Ref(TextUpPosition , -BarsOffset);
TextDnPosition = Ref(TextDnPosition  , -BarsOffset);

CurrentSwing = ValueWhen( textUPStartX OR textDNStartX, myGraph1);
PreviousSwing = ValueWhen( textUPStartX OR textDNStartX, Ref( myGraph1,-1) );

range = abs(CurrentSwing - PreviousSwing);

CurrentSwingShifted = Ref( CurrentSwing , -BarsOffset);


LevelUP = Ref( myGraph1, -BarsOffset) + dist;
LevelDN = Ref( myGraph1, -BarsOffset) - (dist*0.5);  // Adjusted Level down
range = Ref( range , -BarsOffset);

upBarsCount = Ref( upBarsCount , -BarsOffset);
DnBarsCount = Ref( DnBarsCount , -BarsOffset);
DateUp =  Ref( DateUp , -BarsOffset);
DateDn = Ref( DateDn , -BarsOffset);

if ( ( PlotType2 OR Plottype3 ) AND displaytext )
{


    for ( i = 0; i < BarCount; i++ )
    {
        if ( TextUpPosition[i] )
        {
            textUp =  NumToStr( CurrentSwingShifted [ i ], 1.2, 0 )
                      + "\n"
                      + DateTimeToStr( DateUp [ i ] )
                      + "\n"
                      + "R:" + NumToStr( range[ i ], 1.2, 0 )
                      + "\n"
                      + "Bars:" + NumToStr( upBarsCount [ i ], 1 )
                      + "\n"  ;

            PlotText( textUp , i, LevelUP[ i ], UpTextColor );
        }



        if ( TextDnPosition [i] )
        {
            textDn =  NumToStr( CurrentSwingShifted [ i ], 1.2, 0 )
                      + "\n"
                      + DateTimeToStr( DateDn [ i ] )
                      + "\n"
                      + "R: " + NumToStr( range[ i ], 1.2, 0 )
                      + "\n"
                      + "Bars: " + NumToStr( DnBarsCount [ i ], 1 )
                      + "\n";



            PlotText( textDn , i, LevelDN[ i ], DnTextColor );
        }
    }
}



if ( DisplaySwing AND PlotType2 )
{
    Plot( myGraph1, "Swing Chart", swingColor, styleStaircase, Null, Null, 0, 0,
SwingWidth );
}

if ( DisplaySwing AND PlotType3 )
{
   Plot( myGraph1, "Swing Chart",  swingColor, styleStaircase | styleSwingDots,
Null, Null, 0, Zorder = 1 );

}

if ( displayEMA AND PlotType2  )
{
    Plot( EMA( Close, 200 ), "",colorTurquoise, styleLine|styleNoRescale, Null,
Null, 0, 0, EMAWidth );
    Plot( EMA( Close, 50 ), "",colorLime, styleLine|styleNoRescale, Null, Null,
0, 0, EMAWidth );
    Plot( EMA( Close, 20 ), "",colorBlue, styleLine|styleNoRescale, Null, Null,
0, 0, EMAWidth );
}

if ( displayEMA AND PlotType3  )
{
    Plot( EMA( Close, 200 ), "",colorTurquoise, styleLine|styleNoRescale );
    Plot( EMA( Close, 50 ), "",colorLime, styleLine|styleNoRescale);
    Plot( EMA( Close, 20 ), "",colorBlue, styleLine|styleNoRescale );
}



//equal width swings

bars = Param( "MODE 1 - Equal bars swing width", 5, 3, 10, 1 );
Count = 2;
myVal = LastValue( myGraph1 );
myNewChart = Null;
myNewChart[ BarCount-1 ]  = myVal;
positionUp = IIf( LastValue( BarsSince( textUpStartX ) < BarsSince( textDnStartX
) ), 1, -1 );//-1

if ( PlotType1 )
{
    j = BarCount - 1;
    for ( i = BarCount - 1; i > 0; i-- )
    {
        if ( Count == ceil( bars / 2 ) - BarsOffset )
        {


            bar = Max( 0, Min( j + BarsOffset, BarCount - 1 ) );
            bbarsCount = IIf( 	positionUp == 1, upBarsCount [ bar ], dnBarsCount
[ bar ] );
            bdate = IIf( 	positionUp == 1, DateUp[ bar ], DateDn[ bar ] );


            dist2 = IIf( positionup == 1, dist, dist * 0.4 );   // Made up dist2 to raise the lower swing text





            textUp =  NumToStr( CurrentSwingShifted [ bar ], 1.2, 0 )
                      + "\n"
                      + DateTimeToStr( bdate [ bar ] )
                      + "\n"
                      + "R:" + NumToStr( range[ bar ], 1.2, 0 )
                      + "\n"
                      + "Bars:" + NumToStr( bbarsCount , 1 )
                      + "\n" ;



            PlotText( textUp , i, myVal + positionUp * dist2[ j ], IIf(positionUp == 1, UpTextColor, DnTextColor) );
        }


        if ( Count < bars )
        {
            myNewChart[ i ]  = myVal;
            Count++;
        }
        else
        {
            while ( mygraph1[ j ] == myVal AND j > 0 )
            {
                j--;

            }

            myNewChart[ i ]  = myVal;

            myVal = myGraph1[ j ];

            Count = 1;
            positionUp *= -1;
        }
    }

    Plot( myNewChart, "Swing Chart", ParamColor( "Equal Swing Color", colorBlack
), styleSwingDots | styleStaircase, Null, Null, 0, 0, EMAWidth );
}
_SECTION_END();

3 comments

1. anandnst

How to use these numbers?

2. monumobile2007

bakwasss

3. lukaszplewa

good job man!

Leave Comment

Please login here to leave a comment.

Back