Wont' let me post in one go. Paste both posts into new indicator verify
This code is used in TS. Not sure if Multicharts can run it.
{Indicator: gkMtkProfileTL
gkMtkProfileTL Histogram Plot
Poor Man's Market Profile and Value Area Indicator
This indicator plots a rough estimate of a volume histogram for a user specified
number of bars using Trendlines. The indicator approximates the volume
distribution within a bar by use of a sin or uniform distribution. This approximated
distribution is then accumulated over the user specified number of bars to form the histogram.
For intraday charts the user may specify a fixed length moving window for the histogram (e.g.,
the last hour's number of bars) or one that starts a 1 to 5 days prior and includes all the data
up through the current bar. A feature not yet implemented is same as the latter except that today's
data is not used.
The histogram uses color to identify two Value Zones based upon user specified area percentages (e.g.,
70% (Zone1) and 90% (Zone2)).
Inputs:
ProfileLength : number of bars to use for the profile (<= 2000)
HistoWindowType : 0: use a fixed length (ProfileLength) window that moves with each new bar, otherwise (v2.6)
1: histogram from 1st bar NbrDaysBack through current bar (uses todays data)
2: histogram from 1st bar NbrDaysBack through close of prior day (doesn't use todays data) ***NOT YET IMPLEMENTED***
NbrDaysBack : Number of days back to start the histogram (range: 1 to 5 days) (v2.6)
Zone1 and Zone2 : Value zone areas in percent (Must be between 0 and 100); Zone1 Prob < Zone2 Prob
Zone1Color : Color of 1st (Inter) Zone: 111111111111111
Zone2Color : Color of 2nd (Middle)Center Zone: 22222---------------22222
Zone3Color : Color of 3rd (Outer) Zone: 3333-------------------------33333
MedianColor : Color of Median Volume Histogram bar (set it the same as the Zone2Color if you don't want it)
HistoWidth : User specified histogram bar width, 0 = thinnest, 6 thickest
HistoStyle : User specified bar style, 1: solid, 2: dashed, 3: dotted, etc., max value: 5
DeltaPrice : Histogram bar resolution in (e.g., 0.05 means plot a histogram bar every 5 cents of price)
HistoBars : number of bars (time axis) used to plot volume histogram
SinDistVol : true = use a sin distribution for volume in a bar; false = use a uniform distribution
This indicator has only been tested on TS2000i.
v1, ghkramer, 12Sep04
v1.1 ghkramer, 14Oct04
added logic to prevent divide by zero when there's no range or volume
v2.0 ghkramer, 24 Oct04 Beta Test
Converted from Probability Map plot to Trendline plotting and added Value Area Colors
v2.1 ghkramer 30Jan05, Corrected bug in sin distribution calc: chged '*' to '/' in
calculation of dp; additionally, moved calc of dp outside of loop.
v2.2 ghkramer 31Jan05, changed histogram array dimension from '1:N' to '0:N'
v2.3 ghkramer 31Jan05, Test for case where all trades are at 1 price for bar (i.e., H = L)
v2.4 ghkramer 12Feb05, Replaced PriceRows input variable with dPrice; now compute NbrPriceRows
and increased Histo array size
v2.5 ghkramer 21Mar06, removed debug statements and cleaned-up code
v2.6 ghkramer 23Mar06, allowed user to fix start of histogram at NbrDaysBack
}
inputs: ProfileLength(51),
HistoWindowType(1), {v2.6}
NbrDaysBack(3), {v2.6}
Zone1(70), Zone2(90),
Zone1Color(Blue), Zone2Color(darkgray), Zone3Color(darkgreen), MedianColor(Yellow),
HistoWidth(0), {v2.5 User specified bar width}
HistoStyle(1), {v2.5 User specified bar style}
DeltaPrice(0.05), {v2.4 Replaced PriceRows input variable with DeltaPrice}
HistoBars(30), SinDistVol(true);
var: jBar(0), jCol(0), jPrice(0), jLow(0), jHigh(0), NbrPriceRows(0),
MinPrice(0), MaxPrice(0), HistoPrice(0), dPrice(0), dP2(0),
dVol(0), TotVol(0), dp(0), DisplayIntensity(0), MaxHisto(0),
vZone1(0), vZone2(0),
Prob1Upper(0), Prob2Upper(0), Prob2Lower(0), Prob1Lower(0), TailProb(0), CDF(0), CDF1(0),
Price1Upper(0), Price2Upper(0), Price2Lower(0), Price1Lower(0), MedianHisto(0),
iHistoWidth(0), iHistoStyle(0),
NotYetFound1Up(false), NotYetFound2Up(false), NotYetFound2Lo(false), NotYetFound1Lo(false),
BeginDate(0), BeginTime(0), BeginVal(0), EndDate(0), EndTime(0), EndVal(0),
jDate(0), TLRef(0), j(0), Temp(0), yPrice(0),
MaxPriceRows(1000), {v2.4 Must be = dim(Histo[]) - 1}
FirstTimeThru(true), NbrTLs(0), iTL(0),
nDaysBack(0), xProfileLength(0), {v2.6}
BarsPerSession(0), BarsToday(0), MinutesPerSession(0), MinutesToday(0); {v2.6}
array: Dates[2000](0), Times[1000](0);
array: Histo[2001](0); {Dim must be >= MaxPriceRows+1} {v2.4, increased array size}
array: iTLRef[2000](0);
{========Initialization=========}
if (CurrentBar = 1) then
begin
{Constrain inputs}
if (Zone1 < 1) then
vZone1 = 1
else if (Zone1 > 99) then
vZone1 = 99
else
vZOne1 = Zone1;
if (Zone2 < 1) then
vZone1 = 1
else if (Zone2 > 99) then
vZone2 = 99
else
vZOne2 = Zone2;
if (vZone1 > vZone2) then
begin
Temp = vZone1;
vZone1 = vZone2;
vZone2 = Temp;
end;
iHistoWidth = HistoWidth;
if (HistoWidth < 0) then
iHistoWidth = 0
else if (HistoWidth > 6) then
iHistoWidth = 6;
iHistoStyle = HistoStyle;
if (HistoStyle < 1) then
iHistoStyle = 1
else if (HistoStyle > 5) then
iHistoStyle = 5;
if (HistoWindowType = 0) then {v2.6}
begin
xProfileLength = ProfileLength;
end
else
begin
{if (DataCompression < 2) then {TS2000i}}
if (BarType < 2) then {TS8}
begin
if SessionEndDay(1,CurrentSession(1)) > SessionStartDay(1,CurrentSession(1)) then
MinutesPerSession = TimeToMinutes(SessionEndTime(1,CurrentSession(1))) + ( 24 * 60 - ( TimeToMinutes( SessionStartTime(1,CurrentSession(1)) ) ) )
else MinutesPerSession = TimeToMinutes(SessionEndTime(1,CurrentSession(1))) - TimeToMinutes(SessionStartTime(1,CurrentSession(1) ));
BarsPerSession = round(MinutesPerSession/BarInterval, 0);
xProfileLength = BarsPerSession*nDaysBack;
end;
if (NbrDaysBack < 1) then
nDaysBack = 1
else if (NbrDaysBack > 5) then
nDaysBack = 5
else
nDaysBack = NbrDaysBack;
end;
{calc histogram probability levels for Value areas:
Zone1: Prob1Upper and Prob1Lower
Zone2: Prob2Upper and Prob2Lower }
TailProb = (100-vZone1)/200;
Prob1Upper = 1 - TailProb; {TailProb;}
Prob1Lower = TailProb; {1 - TailProb;}
TailProb = (100-vZone2)/200;
Prob2Upper = 1 - TailProb; {TailProb;}
Prob2Lower = TailProb; {1 - TailProb;}
end;
{==========Main: Compute Histogram and plot=========}
if (HistoBars > 0) then
begin
{--------Calculate Histogram parameters---------}
if (CurrentBar >= xProfileLength) then
begin
{if (DataCompression < 2) then} {TS2000i}
if (BarType < 2) then {TS8}
begin
if (HistoWindowType = 1) then {histogram from 1st bar NbrDaysBack through current bar}
begin
if SessionEndDay(1,CurrentSession(1)) > SessionStartDay(1,CurrentSession(1)) then
begin
if TimeToMinutes(Time) > TimeToMinutes(SessionStartTime(1,CurrentSession(1) )) then
MinutesToday = TimeToMinutes(Time) - TimeToMinutes(SessionStartTime(1,CurrentSession(1) ))
else MinutesToday = ( (24 * 60 ) - TimeToMinutes(SessionStartTime(1,CurrentSession(1) )) ) + TimeToMinutes(Time) ;
end
else
MinutesToday = TimeToMinutes(Time) - TimeToMinutes(SessionStartTime(1,CurrentSession(1) )) ;
BarsToDay = round(MinutesToday/BarInterval, 0);
xProfileLength = BarsToDay + BarsPerSession*nDaysBack;
end;
end;
{Find Price limits}
MinPrice = Lowest(L, xProfileLength);
MaxPrice = Highest(H, xProfileLength);
if (DeltaPrice <= 0) then {v2.4 Change}
begin
NbrPriceRows = 100;
dPrice = (MaxPrice - MinPrice)/NbrPriceRows;
end
else {user specified DeltaPrice}
begin
NbrPriceRows = floor((MaxPrice - MinPrice)/DeltaPrice) + 1;
if (NbrPriceRows < 1) then {s/b >= 1, but test anyway to prevent divide by 0}
NbrPriceRows = 1;
if (NbrPriceRows <= MaxPriceRows) then
dPrice = DeltaPrice
else {constrain NbrPriceRows to array limits}
begin
NbrPriceRows = MaxPriceRows;
dPrice = (MaxPrice - MinPrice)/NbrPriceRows;
end;
end;
dP2 = dPrice/2;