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 10 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
_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