Thursday, September 9, 2010

MarketProfile Code With Target Levels

[SameTickOpt = True]
[LegacyColorValue = true];

{
Indicator Name: !!AmasterMarketP6
This code originally was coded by ghkramer and then added to by owen, and lookn4ddeal has added to it as well. I made a few tweaks to it as well, and cleaned up the code so it's easier to read.

Code originally posted in the TradeStation forums.
Links:
https://www.tradestation.com/Discussions/Topic.aspx?Topic_ID=93008
https://www.tradestation.com/Discussions/Topic.aspx?Topic_ID=50398
https://www.tradestation.com/Discussions/Topic.aspx?Topic_ID=51450
}


inputs:
PlacFirststartTLonchart ( 1 ) {,RetracePct(5)},
DeltaPrice ( .25 ),
//highVolColor ( green ),
lowVolColor ( red ), {lightgray}
color ( green ), {was cyan}
{mostrecent ( true ),}
SinDistVol ( false ),
Daysver ( true ),
daybac ( 3 ),
seetext ( true ),
alerts ( false ),
scalelocationpercent ( .5 ) ;

variables:
barsend ( 0 ),
Startid ( 1 ),
EndId ( 0 ),
seeDotlines ( true ),
mostrecent ( true ) {,realtimeend(true)},
endatdate ( true ),
deletpast ( false ),
FivLines ( false ),
seeplots ( false ),
HiV1LowV2Both3non0 ( 3 ) ;

variables:
PercTpointF ( false ) {, HistoWindowType(0)},
delettls ( true ),
deletpeaks ( true ),
seedots ( false ),
textLoc ( 100 ) ;


///////////////////////////

variables:
{maxhistoprev ( 0 ),}
prevyprice ( 0 ),
PrevEndDate ( 0 ),
PrevEndTime ( 0 ),
prevypricex ( 0 ),
PrevEndDatex ( 0 ),
PrevEndTimex ( 0 ) ;
{ prevypricedif ( 0 ),
PrevEndDatedif ( 0 ),
PrevEndTimedif ( 0 ) ;
}

{
Input:
TextVertPct ( 81 ) ;
}


variables:
jBar ( 0 ),
jCol ( 0 ),
jPrice ( 0 ),
jLow ( 0 ),
jHigh ( 0 ),
NbrPriceRows ( 0 ),
MinPrice(1000000),
MaxPrice ( 0 ),
HistoPrice ( 0 ),
dPrice ( 0 ),
dP2 ( 0 ),
lx ( 0 ),
dVol ( 0 ),
TotVol ( 0 ),
dVolx ( 0 ),
TotVolx ( 0 ),
dp ( 0 ),
DisplayIntensity ( 0 ),
MaxHisto ( 0 ),
MaxHistox ( 0 ),
EndDate ( 0 ),
EndTime ( 0 ),
EndVal ( 0 ),
{
EndDatex ( 0 ),
EndTimex ( 0 ),
EndValx ( 0 ),
}
EndDatedif ( 0 ),
EndTimedif ( 0 ),
EndValdif ( 0 ),
jDate ( 0 ),
TLRef ( 0 ),
TLRefx ( 0 ),
TLRefdif ( 0 ),
TLRefdif2 ( 0 ),
j ( 0 ),
{
Temp ( 0 ),
}
yPrice ( 0 ),
yPricex ( 0 ),
MaxPriceRows ( 1000 ), {v2.4 Must be = dim(Histo[]) - 1}
FirstTimeThru(true),
NbrTLs ( 0 ),
iTL ( 0 ),
NbrTLsx ( 0 ),
iTLx ( 0 ),
iTLdif ( 0 ),
NbrTLsdif ( 0 ),
xProfileLength ( 0 ) ;

variables:
barnum ( 0 ),
startbar ( 0 ),
xhistobars ( 0 ),
d15 ( 0 ),
dum ( 0 ) ;


array:
Dates[1500] ( 0 ),
Times[1500] ( 0 ),
Histo[1500] ( 0 ),
iTLRef[1050,5] ( 0 ),
LisTLs[1050] ( 0 ),
txt[1050] ( 0 ),
TLval[1050] ( 0 ) ;

variables:
dumsr ( 0 );

variables:
Swingdir ( 0 ),
SwingBar ( 0 ),
SwingPrice ( 0 ),
SwingDate ( 0 ),
SwingTime ( 0 ),
id ( 0 ),
x ( 0 ),
xbar ( 0 ) ;

variables:
Swingdirj ( 0 ),
SwingBarj ( 0 ),
SwingPricej ( 0 ),
SwingDatej ( 0 ),
SwingTimej ( 0 ),
idj ( 0 ) ;

variables:
DD ( 0 ),
TT ( 1 ),
PP ( 2 ),
barNo ( 3 ),
Dir ( 4 ),
VolUDT ( 0 ),
xend ( 0 ),
xxend ( 0 ),
xvalue1 ( 0 ),
colored ( 0 ) ;
{
array:
SW[42,9] ( 0 );
}
array:
Hj[1501,9] ( 0 ) ;

variables:
LisID ( 0 ) ;
{color(0)};

variables:
stopbar ( 11111111111110 ),
Hilo ( 0 ),
xavg ( 0 ) ;
{dir ( 0 )}

variables:
reset ( false ),
newnum ( 0 ),
xx1 ( 0 ),
md ( 0 ),
mt ( 0 ),
HLs ( c ),
EL ( 0 ),
lbar ( 0 ),
lbar2 ( 0 ),
enbar ( 0 ),
Lb ( false ),
Lb2 ( false ),
nDaysBack ( 0 ),
MinutesPerSession ( 0 ),
BarsPerSession ( 0 ) ;

var:
ex ( 0 ) ;

var:
xa ( 0 ) ;

{
array:
tlval[40] ( 0 ) ;
}


barnum=barnumber;

if
date<>date[1]
and
lbar2=0
then
begin
lbar2=lbar;
lbar=barnum[1];
end;

if
date<>date[1]
then
enbar=barnum[1];

if
Daysver
and
lboc
then
begin
if
daybac=1
then
xHistoBars = {barnum-}((barnum-enbar));

if
daybac>-1
then
xHistoBars = {barnum-}(((lbar-lbar2)*daybac)+barnum-enbar);

if
lb2 = false
then
begin
plot12[xHistoBars+2](c+5);
plot13[xHistoBars+2](c-5);

dum = text_new(date[xHistoBars+2], time[xHistoBars+2], close-6,"start location of referenced volume");
lb2=true;
end;
end;

if
(Daysver=false
and
TL_Exist(PlacFirststartTLonchart) )
then
begin
if
(Daysver=false
and
TL_GetBeginDate(PlacFirststartTLonchart)=date
and
TL_GetBeginTime(PlacFirststartTLonchart)=time)
then
stopbar= {FindBar(BD, BT)}barnum;
end;

if
barnum = stopbar
then
begin
plot10[2](c+5, "plot10", darkred);
plot11[2](c-5, "plot11", darkgreen);
end;

if
Daysver = false
and
barnum > stopbar
then
xHistoBars = barnum - stopbar;
xxend = 0;
//color = colordn ;

{------------------- (Main: Compute Histogram and plot)-------------------}
{------------------- (Calculate Histogram parameters)-------------------}

if
(xHistoBars > 0)
and
(CurrentBar >= xHistoBars)
{and
lboc}
then
begin
MinPrice = Lowest(L, xHistoBars);
MaxPrice = Highest(H, xHistoBars);
end;

{------------------- (xxx)-------------------}

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;

{------------------- (Initialize Histogram Cells)-------------------}

for
jPrice = {0}xxend
to
NbrPriceRows
begin
Histo[jPrice] = 0;
end;

{------------------- (Compute Histogram)-------------------}
{------------------- (dPrice must be > 0, otherwise exit without Plotting v1.1 fix)-------------------}
if
dPrice > 0
then
{end1} begin
TotVol = 0;
dVol = 0;
TotVolx = 0;
dVolx = 0;

for
jBar = {0}xxend
to
(xHistoBars-1) {changed for endid}
begin
jLow = floor((L[jBar] - MinPrice)/dPrice); {31Jan05, removed +1 , v2.2 fix}
jHigh = floor((H[jBar] - MinPrice)/dPrice); {31Jan05, removed +1 , v2.2 fix}
if
((jHigh - jLow) > 0)
then {31Jan05, case where all trades at 1 price, v2.3}
begin
if
(SinDistVol)
then
dp = 180/(jHigh - jLow); {30Jan05,chged * to /, moved out of loop, v2.1}

if
(SinDistVol )= false
then
dVol = ticks[jBar]/(jHigh - jLow);

{------------------- (scale by area of sin pedestal = 1/2.785)-------------------}

for
jPrice = jLow
to
jHigh
begin
if
(SinDistVol)
then
dVol = 0.35991*{volume}Ticks[jBar]*(sine((jPrice-jLow)*dp ) + 0.25);

if
(SinDistVol)
then
TotVol = TotVol + dVol;
Histo[jPrice] = Histo[jPrice] + dVol;

if
(SinDistVol) = false
then
TotVol = TotVol + dVol;
end;
end;
end;
{------------------- (Plot Histogram)-------------------}

{Volume must be > 0, otherwise, exit without plotting v1.1 fix}

if
(TotVol > 0)
then
{end2} begin
{------------------- (Normalize Histogram)-------------------}
MaxHisto = -100000;
MaxHistox = -100000;

for
jPrice = 0
to
NbrPriceRows
begin
Histo[jPrice] = Histo[jPrice]/TotVol;

if
(Histo[jPrice] > MaxHisto)
then
MaxHisto = Histo[jPrice];
end;

{------------------- (Plot Profile using trendlines v2.0 fix-)-------------------}

{---------Scale Histogram-----------}
for
jPrice = 0
to
NbrPriceRows
begin
Histo[jPrice] = (round((xHistoBars-xxend)*Histo[jPrice]/MaxHisto, 0)*.875);
end;


{------------------- (xxx)-------------------}
for
j = 0
to
NbrPriceRows
begin
Dates[j] = Date[round((xHistoBars - Histo[j])*scalelocationpercent,0)];
Times[j] = Time[round((xHistoBars - Histo[j])*scalelocationpercent,0)];
end;

if
lb = false
then
begin
plot10[round((xHistoBars - Histo[NbrPriceRows])*scalelocationpercent,0)](c+5, "plot10", red);
plot11[round((xHistoBars - Histo[NbrPriceRows])*scalelocationpercent,0)](c-5, "plot11", green);

if
scalelocationpercent <> 1
then
dum = text_new(date[round((xHistoBars - Histo[NbrPriceRows])*scalelocationpercent,0)],
time[round((xHistoBars - Histo[NbrPriceRows])*scalelocationpercent,0)],c-6,"scaled location smaller to fit chart");
lb = true;
end;

{------------------- (Ticks)-------------------}

{Delete prior set of TLs}
if
FivLines
then
d15=5
else
d15=1;

if
((FirstTimeThru = false)
and
(NbrTLs > 0))
then
begin
{if deletpeaks then }
for
iTL = 1
to
LisID
begin
if
deletpeaks
and
LisTLs[iTL]>0
and
xvalue1<>2
or
(deletpast
and
LisTLs[iTL]>0)
then
begin
if
seetext
then
text_delete(txt[iTL]);
TL_Delete(LisTLs[iTL]);
end;
LisID=0;
end;

for
iTL = 1
to
NbrTLs
begin
if
delettls
and
iTLRef[iTL,D15]>0
and
xvalue1<>2
or
(deletpast
and
iTLRef[iTL,1]>0)
then
TL_Delete(iTLRef[iTL,D15]);
{ iTLRef[iTL,5]= iTLRef[iTL,4];
iTLRef[iTL,4]= iTLRef[iTL,3];
iTLRef[iTL,3]= iTLRef[iTL,2];
iTLRef[iTL,2]= iTLRef[iTL,1];
if iTLRef[iTL,2]>0 and FivLines then
TL_SetColor(iTLRef[iTL,2], color+7); }
NbrTLs=0;
end;
end;

{--------Plot Market Profile-----------}

for
j = 0
to {round(}
NbrPriceRows{*.5,0)} {fix v2.2}
{end3} begin
yPrice = MinPrice + j*dPrice;
{BeginVal = yPrice;}
EndDate = Dates[j];
EndTime = Times[j];

if
j >0
then
begin
prevyprice = MinPrice + (j-1)*dPrice;;
PrevEndDate = Dates[j-1];
PrevEndTime = Times[j-1];
end
else
begin
prevyprice = yPrice;
PrevEndDate = Dates[j];
PrevEndTime = Times[j];
end;

TLRef = TL_New(prevendDate, prevendTime, prevyPrice, EndDate, EndTime, yPrice) ;
{Save TLRef}
NbrTLs = NbrTLs + 1;
iTLRef[NbrTLs,1] = TLRef;
TL_SetSize(TLRef, 0) ;
TL_SetStyle(TLRef, 1) ;

if
FivLines = false
then
colored = color+7
else
colored = color;

TL_SetColor(TLRef, colored);

{------------------- (xxx)-------------------}

if
HiV1LowV2Both3non0 > 0
and
j > 3
and
j < NbrPriceRows
then
begin {histo is}
condition1 =
(
Histo[j] > Histo[j-2]
and
Histo[j] > Histo[j+2]
and
(
Histo[j] >= Histo[j-1]
and
Histo[j]>{=} Histo[j+1] {peakes}
)
);

condition2 =
(
Histo[j] < Histo[j-2]
and
Histo[j] < Histo[j+2]
)
and
(
Histo[j] <= Histo[j-1]
and
Histo[j] <{=} Histo[j+1] {lows}
) ;

condition3 =
(
Histo[j] > Histo[j-3]
and
Histo[j] > Histo[j+3] {peakes}
);

condition4 =
(
Histo[j] < Histo[j-3]
and
Histo[j] );
end;

{ if 1=1 and (condition1 {or condition2} )and (condition3 {or condition4 }) then begin}

if
(
HiV1LowV2Both3non0 = 1
and
condition1
and
condition3
)
or
(
HiV1LowV2Both3non0 = 2
and
condition2
and
condition4
)
or
(
HiV1LowV2Both3non0 = 3
and
condition1
and
condition3
)
or
(
HiV1LowV2Both3non0 = 3
and
condition2
and
condition4
)
then
begin
{------------------- (xxx)-------------------}
LisID = LisID + 1;
if
seeplots = false
{and
id>1}
then
begin
LisTLs[LisID] = TL_New({Begin}endDate, {Begin}endTime, {Prev}yPrice, {SW[id,DD]}date, {SW[id,TT]}time,{Prev}yPrice);
TL_SetStyle(LisTLs[LisID], 1) ;
TL_SetSize(LisTLs[LisID], 1) ;
TL_SetColor(LisTLs[LisID], {yellow}color );

if
condition2
and
condition4
then
begin
TL_SetColor(LisTLs[LisID], lowVolColor ); {low volume level:lightgray}
TL_SetSize(LisTLs[LisID], 0) ;
end;
//text
if
seetext
then
txt[LisID] = text_new(date,time+textLoc*BarInterval,yPrice,NumT oStr(yPrice,2));

if
seetext
then
text_SetStyle(txt[LisID],1,2);
end;//seeplots
end;
{end3} end;

{------------------- (xxx)-------------------}

FirstTimeThru = false;
{end2} end; {LastBarOnChart}
{end1} end; {TotVol > 0}

{------------------- (xxx)-------------------}

if
(
LisID > 39
)
then
begin
for
x = 1
to
40
begin
LisTLs[x-1] = LisTLs[x];
LisID=39;
end;
end;

if
currentbar = 1
then
clearprintlog;

xvalue1 = 0;

{------------------- alert once per bar code -------------------}

if
alerts
then
begin
for
xa = 1
to
40
begin
if
LisTLs[xa] <> 0
then
TL_SetAlert( LisTLs[xa], 2 );
end;
end;//alerts

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.