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

MPLite V5.80++ for Amibroker (AFL)

Rating:
4 / 5 (Votes 6)
Tags:
amibroker

Someone solved the over 500 plot()/plotohlc() errors using the Graphic API.

Further simplify & improved for 5.80++.

So far so good.

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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
_SECTION_BEGIN( "MPLite" );
// - there was a plot limit call introduced after 5.41.0 beta
//   i.e. "•Warning 502: Calling Plot()/PlotOHLC over 500 times is displayed in indicator in runtime to prevent abuse"
// - uses GFX for major calls of plot only if AB version newer than 5.40 - this only delays reaching the plot limit
// http://www.***********.com/amibroker/2961-market-profile-amibroker-new-13.html
 
Version (5.80);
PS_SOLID=0;
PS_DASH=1;
PS_DOT=2;
PS_DASHDOT=3;
PS_DASHDOTDOT=4;
PS_NULL=5;
PS_INSIDEFRAME=6;
 
function Lastthursday()
{
 Daysinmonth=
    IIf(Month()==2, IIf(Year()%4==0 AND Year()%100!=0,29,28),
    IIf(Month()==4 OR Month()==6 OR Month()==9 OR Month()==11,30,31));
  
 return IIf(Daysinmonth-Day()<7 AND DayOfWeek()==4,1,
        IIf(Daysinmonth-Day()<8 AND DayOfWeek()==3 AND Ref(DayOfWeek(),1)!=4 AND Day()!=Daysinmonth,1,0));
  
}
 
 
procedure GfxPlotLine( ix0, iy0, ix1, iy1, iColor, iWidth, iStyle)
{
    GfxSetBkMode(1);
    GfxSelectPen( iColor, iWidth, iStyle );
    GfxMoveTo( ix0, iy0 );
    GfxLineTo( ix1, iy1 );
}
 
 
PlotOHLC( O, H, L, C, "Price", IIf( C > O, colorGreen, colorRed ), styleCandle );
 
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );
 
totalVisible = Lastvisiblebar - FirstVisibleBar;
 
 
//Den = Param("Density", 1, 0.1, 10, 0.1);
percent = Param( "Value Area", 70, 1, 100, 1 );
Type = ParamList( "Type", "Price Profile|Volume Profile" );
Period = ParamList( "Base", "Hourly|Daily|Weekly|Monthly|Lastthursday|Yearly", 1 );
x_scale = Param( "Horizontal_scale", 2, 0, 10, 0.1 );
EnMP2 = ParamStyle( "Style", styleLine | styleNoLabel, maskAll );
 
EnIB = ParamToggle( "Show Initial Balance""No|Yes", 0 );
IBBars = Param( "Initial Balance Bars", 2, 1, 10, 1 );
ViewYvalues = ParamToggle( "Show Yesterdays Values", "No|Yes", 1 );
ViewVlines = ParamToggle( "Show Vertical Base Lines", "No|Yes", 1 );
Viewvalues = ParamToggle( "Show Values", "No|Yes", 0 );
ViewVpoc = ParamToggle( "Show Virgin POC", "No|Yes", 1 );
ViewTPO = ParamToggle( "Show TPO Count", "No|Yes", 0 );
 
Color_Above_VA = ParamColor( "Color_Above_VA"colorGrey40 );
Color_VA = ParamColor( "Color_VA", colorBlueGrey );
Color_Below_VA = ParamColor( "Color_Below_VA", colorGrey40 );
Color_POC_line = ParamColor( "Color_POC_Line", colorYellow );
 
color_YVAH = ParamColor( "YVAH", colorWhite );
color_YVAL = ParamColor( "YVAL", colorWhite );
color_YPOC = ParamColor( "YPOC", colorYellow );
 
IBColor = ParamColor( "IB Color", colorWhite );
IBstyle = ParamStyle( "IB style", styleLine, maskAll );
Color_Virgin_POC = ParamColor( "Virgin Poc Color", colorYellow );
Color_Base_Line = ParamColor( "Base Line Color", colorDarkGrey );
 
switch (Period)
  {
 
    case "Yearly":   
        BarsInDay = BarsSince(Year() != Ref(Year(), -1));
        tf = inMonthly;
        break;
 
    case "Monthly":   
        BarsInDay = BarsSince(Month() != Ref(Month(), -1));
        tf = inMonthly;
        break;
     
     
    case "Weekly":
        BarsInDay = BarsSince(DayOfWeek() < Ref( DayOfWeek(), -1 ));
        tf = inWeekly;
        break;
     
    case "Hourly":
        BarsInDay = BarsSince(Hour() != Ref(Hour(), -1));
        tf = inHourly;
        break;
     
    case "Daily":
        BarsInDay = BarsSince(Day() != Ref(Day(), -1));
        tf = inDaily;
        break;
     
  }
   
 
  if(Period=="Lastthursday"  )
  {
    BarsInDay = BarsSince(Lastthursday()==0 AND Ref(Lastthursday(), -1)==1);
    bot1=ValueWhen(BarsInDay==0,L);
    Bot2 = ValueWhen(Ref(BarsInDay)==1 OR BarIndex()>BarCount-2 ,LLV(L,BarsInDay),0);
    bot=Min(bot1,bot2);
    top1=ValueWhen(BarsInDay==0,H);
    Top2 =ValueWhen(Ref(BarsInDay,1)==1 OR BarIndex()>BarCount-2,HHV(H,BarsInDay),0);
    top=Max(top1,top2);
    Vol = ValueWhen(Ref(BarsInDay,1)==1 OR BarIndex()>BarCount-2 ,Sum(V,BarsInDay),0);
  }
  else
  {
    Bot = TimeFrameGetPrice("L", tf, 0);
    Top = TimeFrameGetPrice("H", tf, 0);
    Vol = TimeFrameGetPrice("V", tf, 0); 
  }
 
CurTop = HHV( H, BarsInDay + 1 );
Curbot = LLV( L, BarsInDay + 1 );
 
Range =  Top - Bot;
coverage = LastValue( Ref( ATR( 10 ), -1 ) );
 
den = ( coverage / 10 );
//den=1;
 
multiplier = IIf( round( totalVisible ) / 300 < 1, 1, round( totalVisible ) / 300 );
 
if ( ViewVlines)
    Plot( BarsInDay == 0, "", Color_Base_Line, styleHistogram | styleOwnScale | styleLine | styleNoLabel );
 
 
relTodayRange = 0;
 
x = 0;
basey = 0;
basex = 0;
newday = 0;
total = 0;
shiftup = 0;
shiftdn = 0;
//Line = Null;
Voloumeunit = 0;
 
 
 GfxSetOverlayMode( 1 );
 GfxSetCoordsMode( 1 ); // bar/price mode (instead of pixel)
 
for (  i = IIf( FirstVisibleBar - 100 > 1, FirstVisibleBar - 100, FirstVisibleBar ); i < Lastvisiblebar AND i < BarCount - 1 ; i++  )
{
    if ( BarsInDay[i] == 0 )
    {
        t = BarsInDay[i-1];
 
 
        if ( EnIB )
        {
            IBH = HHV( H, IBBars );
            IBL = LLV( L, IBBars );
            /*
            Line1 = LineArray( basex, IBH[basex+IBBars-1], i, IBH[basex+IBBars-1] );
            Plot( Line1, "", IBColor, IBstyle );
            Line2 = LineArray( basex, IBL[basex+IBBars-1], i, IBL[basex+IBBars-1] );
            Plot( Line2, "", IBcolor, IBstyle );
            */
            GfxPlotLine(basex, hY=IBH[basex+IBBars-1], i, hY,
                        IBColor, 1, PS_SOLID);   
            GfxPlotLine(basex, hY=IBL[basex+IBBars-1], i, hY,
                        IBColor, 1, PS_SOLID);               
             
        }
 
        //////////////////////////////////
        poc = 0;
        pocj = 0;
        midrange = int( relTodayRange / 2 ) + 1;
 
        for ( j = 1; j <= relTodayRange + 1 ; j++ )
        {
            if ( poc < x[j] )
            {
                poc = x[j];
                pocj = j;
            }
            else
                if ( poc == x[j] )
                {
                    if ( abs( midrange - j ) < abs( midrange - pocj ) )
                    {
                        poc = x[j];
                        pocj = j;
                    }
                }
 
        }
 
        for ( n = 1; n <= relTodayRange; n++ )
            total[n] = x[n] + total[n-1];
 
        Value_area = ( total[relTodayRange] * percent ) / 100;
 
        for ( a = 1; a <= relTodayRange; a++ )
        {
            if ( pocj - a > 0 AND pocj + a < relTodayRange )
            {
                if ( poc + total[pocj+a] - total[pocj] + ( total[pocj] - poc ) - total[pocj-( a+1 )] >= Value_area )
                {
                    shiftup = shiftdn = a;
                    break;
                }
            }
            else
                if ( pocj - a < 1 )
                {
                    if ( poc + total[pocj+a] - total[pocj] + ( total[pocj] - poc ) >= Value_area )
                    {
                        shiftup = a;
                        shiftdn = pocj;
                        break;
                    }
                }
                else
                    if ( pocj + a > relTodayRange )
                    {
                        if ( poc + total[relTodayRange] - total[pocj] + ( total[pocj] - poc ) - total[pocj-( a+1 )] >= Value_area )
                        {
                            shiftup = floor( relTodayRange ) - pocj;
                            shiftdn = a + 1;
                            break;
                        }
                    }
        }
 
 
        //Vah = LineArray( baseX[i], baseY + ( pocj + shiftup ) * den, i, baseY + ( pocj + shiftup ) * den );
        //Val = LineArray( baseX[i], baseY + ( pocj - shiftdn ) * den, i, baseY + ( pocj - shiftdn ) * den );
        //pocline = LineArray( basex, basey + pocj * den, basex[i] + poc, basey + pocj * den );
         
         
         
        if ( ViewVpoc)
        {
            Virginpoc = basey + pocj * den;
            newi = Null;
 
            for ( j = i; j <= ( BarCount - 1 ) ; j++ )
                if ( L[j] < Virginpoc AND H[j] > Virginpoc )
                {
                    newi = j;
                    break;
                }
                else
                    newi = BarCount;
 
            Plot( LineArray( basex[i], hY=basey + pocj*den, newi, hY), "", Color_Virgin_POC, styleNoRescale );
             
             
        }
 
         
 
        if ( ViewYvalues)
        {
           //Vahn = LineArray( i, baseY + ( pocj + shiftup ) * den, i + t, baseY + ( pocj + shiftup ) * den );
           //pocn = LineArray( i, baseY + ( pocj ) * den, i + t, baseY + ( pocj ) * den );
           //Valn = LineArray( i, baseY + ( pocj - shiftdn ) * den, i + t, baseY + ( pocj - shiftdn ) * den );
 
            GfxPlotLine( i, baseY + ( pocj + shiftup ) * den, i + t, baseY + ( pocj + shiftup ) * den,
                 color_YVAH, 0, PS_DOT);
                  
            GfxPlotLine( i, baseY + ( pocj ) * den, i + t, baseY + ( pocj ) * den,
                 color_YPOC, 0, PS_DOT);    
                  
            GfxPlotLine(i, baseY + ( pocj - shiftdn ) * den, i + t, baseY + ( pocj - shiftdn ) * den,
                 color_YVAL, 0, PS_DOT);
                      
            //Plot( Vahn, "", color_YVAH, styleDashed | styleNoRescale );
            //Plot( Valn, "", color_YVAL, styleDashed | styleNoRescale );
            //Plot( pocn, "", color_YPOC, styleDashed | styleNoRescale );
             
             
        }
 
 
        if ( ViewTPO )
        {
            nnn = HHV( H, BarsInDay );
            PlotText( "" + ( total[relTodayRange] - total[pocj] ), basex[i], nnn[i], colorLightGrey );
            PlotText( "" + ( total[pocj-1] ), basex[i], basey - den, colorLightGrey );
        }
 
        if ( Viewvalues)
        {
            PlotText( "" + ( ( basey + pocj*den ) ), baseX[i], basey + pocj*den, colorWhite, colorDarkGrey );
            PlotText( "" + ( ( baseY + ( pocj + shiftup )*den ) ), baseX[i], baseY + ( pocj + shiftup )*den, colorWhite, colorDarkGrey );
            PlotText( "" + ( ( baseY + ( pocj - shiftdn )*den ) ), baseX[i], baseY + ( pocj - shiftdn )*den, colorWhite, colorDarkGrey );
        }
 
        //-- Plot Market Profile
        for ( p = 0; p <= relTodayRange + 1; p = p + multiplier )
        {
            GfxPlotLine( baseX, hY=baseY + p * Den, baseX + x[p], hY,
                            IIf( p >  ( pocj + shiftup ), Color_Above_VA,
                            IIf( p <= ( pocj + shiftup ) AND p >= ( pocj - shiftdn ), Color_VA,
                              Color_Below_VA )), 1, PS_SOLID);
                               
        }
         
        //-- Plot PoC
        GfxPlotLine( baseX, hY = basey + pocj * den, basex[i] + poc, hY,
                     Color_POC_Line, 1, PS_SOLID);                 
         
                           
         
                      
///////////////////
        basex = 0;
        x = 0;
        Basex = i;
        baseY = Bot[i];
        relTodayRange = Range[i] / Den;
        Voloumeunit = Vol[i] / LastValue( BarsInDay );
 
    }
 
    for ( j = 0; j <= relTodayRange ; j++ )
    {
        if ( L[i] <= Bot[i] + j*Den AND H[i] >= Bot[i] + j*Den  )
        {
 
            if ( Type == "Price Profile" )
            {
                x[j] += x_scale;
            }
            else
                if ( Type == "Volume Profile" )
                {
                    x[j] += round( V[i] / Voloumeunit ) + 1;
                }
 
        }
    }
}
 
 
if ( EnIB )
{
    IBH = HHV( H, IBBars );
    IBL = LLV( L, IBBars );
    Line1 = LineArray( basex, IBH[basex+IBBars-1], i, IBH[basex+IBBars-1] );
    Plot( Line1, "", IBColor, IBstyle );
    Line2 = LineArray( basex, IBL[basex+IBBars-1], i, IBL[basex+IBBars-1] );
    Plot( Line2, "", IBColor, IBstyle );
}
 
//////////////////////////////////
poc = 0;
pocj = 0;
midrange = int( relTodayRange / 2 ) + 1;
 
for ( j = 1; j <= relTodayRange + 1 ; j++ )
{
    if ( poc < x[j] )
    {
        poc = x[j];
        pocj = j;
    }
    else
        if ( poc == x[j] )
        {
            if ( abs( midrange - j ) < abs( midrange - pocj ) )
            {
                poc = x[j];
                pocj = j;
            }
        }
 
}
 
for ( n = 1; n <= relTodayRange; n++ )
    total[n] = x[n] + total[n-1];
 
Value_area = ( total[relTodayRange] * percent ) / 100;
 
 
for ( a = 1; a <= relTodayRange; a++ )
{
    if ( pocj - a > 0 AND pocj + a < relTodayRange )
    {
        if ( poc + total[pocj+a] - total[pocj] + ( total[pocj] - poc ) - total[pocj-( a+1 )] >= Value_area )
        {
            shiftup = shiftdn = a;
            break;
        }
    }
    else
        if ( pocj - a < 1 )
        {
            if ( poc + total[pocj+a] - total[pocj] + ( total[pocj] - poc ) >= Value_area )
            {
                shiftup = a;
                shiftdn = pocj;
                break;
            }
        }
        else
            if ( pocj + a > relTodayRange )
            {
                if ( poc + total[relTodayRange] - total[pocj] + ( total[pocj] - poc ) - total[pocj-( a+1 )] >= Value_area )
                {
                    shiftup = floor( relTodayRange ) - pocj;
                    shiftdn = a + 1;
                    break;
                }
            }
}
 
Vah = LineArray( baseX[i], baseY + ( pocj + shiftup ) * den, i, baseY + ( pocj + shiftup ) * den );
Val = LineArray( baseX[i], baseY + ( pocj - shiftdn ) * den, i, baseY + ( pocj - shiftdn ) * den );
pocline = LineArray( basex, basey + pocj * den, basex[i] + poc, basey + pocj * den );
 
if ( ViewTPO)
{
    PlotText( "" + ( total[relTodayRange] - total[pocj] ), basex[i], top[i], colorLightGrey );
    PlotText( "" + ( total[pocj-1] + x_scale ), basex[i], basey - den, colorLightGrey );
}
 
if ( Viewvalues )
{
    PlotText( "" + ( ( basey + pocj*den ) ), baseX[i], basey + pocj*den, colorWhite, colorDarkGrey );
    PlotText( "" + ( ( baseY + ( pocj + shiftup )*den ) ), baseX[i], baseY + ( pocj + shiftup )*den, colorWhite, colorDarkGrey );
    PlotText( "" + ( ( baseY + ( pocj - shiftdn )*den ) ), baseX[i], baseY + ( pocj - shiftdn )*den, colorWhite, colorDarkGrey );
}
 
 
 
for ( p = 0; p <= relTodayRange + 1; p = p + multiplier )
{
        GfxPlotLine( baseX,        hY=baseY + p * Den,
                     baseX + x[p], hY,
                     IIf( p > ( pocj + shiftup ), Color_Above_VA,
                     IIf( p <= ( pocj + shiftup ) AND p >= ( pocj - shiftdn ), Color_VA, Color_Below_VA )), 1, PS_SOLID);
}
 
 
 
Plot( pocline, "", Color_POC_Line, styleNoRescale | styleNoLabel );
 
_SECTION_END();

9 comments

1. SPASHA

WHY ITS NOT WORKING FOR 5.7 VERSION

2. hotaro3

Not working, GfxSetCord ??

3. susanti9988

this version need 5.8 version, could you help me for 5.5 version?
thanks in advance

4. chetan ninganiya

@admin sir can you please help me understand the usage of this indicator ?

5. administrator

Try googling ‘Market profile’ lots of good resources available.

6. chetan ninganiya

thanks @admin sir.

7. niladri

Thx for sharing the modified code!!

For newer Amibroker’s higher than 5.8, to make it work just comment out line#7

Tested OK in Amibroker 6.2

8. ratan

can u pls correct the error when the period is changed to last thursday.

9. Krishnendu

It is working for me, but how can I set the “Initial Balance” to 1 hour?

Leave Comment

Please login here to leave a comment.

Back