Simple Coding Help - No Promise.

Help needed for the following market profile afl code

Hello team I'm using the market profile afl code that has been given below. But i need some upgrade on the provided afl code. Can someone help me with this. I need open price with the letter (O) and close price with (*) to be included to this . And can you also add the volume profile respective to the TPO's generated, like the one in the link below

https://drive.google.com/file/d/0B-OGRZWRf1P9SjJJU0xLNnEyWUE/view

"I NEED NEED THE AFL TO BE LOOKED SOMETHING LIKE THE IMAGE PROVIDED"

​Below i have attached the afl code i use. Can you please edit and update it .?

_SECTION_BEGIN("MarketProfile");
SetChartOptions(0,chartShowArrows|chartShowDates);
//------------------------------------------------------------------------------
//
// Formula Name: Market Profile
//

//Market Profile

PlotOHLC(Ref(O,-1),Ref(H,-1),Ref(L,-1),Ref(C,-1),"Price",colorBlack,styleNoLine);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g", Ref(O,-1), Ref(H,-1), Ref(L,-1), Ref(C,-1) ));

EnMP2= ParamList("MarketProfile","Letters|Solid|Lines");
styleLines = ParamStyle("Style", styleLine, maskAll);

Type=ParamList("Type","Price Profile|Volume Profile");
Period= ParamList("Base","Hourly|Daily|Weekly|Monthly",1);

Den = Param("Density", 3, 0.25, 100, 0.25); // Resolution in terms of $
percent=Param("Value Area", 70, 1, 100, 1);
ViewTPOCount= ParamToggle("Show TPO Count", "No|Yes",1);
ViewPOC = ParamToggle("Show POC", "No|Yes",1);
ViewVALVAH = ParamToggle("Show VAL VAH Line", "No|Yes",1);
Viewfill = ParamToggle("Show VA Fill", "No|Yes",1);
Colorpoc=ParamColor("Color POC", colorYellow);
Colorfill=ParamColor("Color Fill", ColorRGB(20,40,60));

EnIB = ParamToggle("Show Initial Balance", "Yes|No");
IBBars = Param("Initial Balance Bars", 2, 0, 10, 1);

if(Period=="Daily"){
BarsInDay = BarsSince(Day() != Ref(Day(), -1));
Bot = TimeFrameGetPrice("L", inDaily, 0);
Top = TimeFrameGetPrice("H", inDaily, 0);
Vol = TimeFrameGetPrice("V", inDaily, 0);
}

if(Period=="Hourly"){
BarsInDay = BarsSince(Minute() != Ref(Minute(), -1));
Bot = TimeFrameGetPrice("L", in5Minute, 0);
Top = TimeFrameGetPrice("H", in5Minute, 0);
Vol = TimeFrameGetPrice("V", in5Minute, 0);
}

if(Period=="Weekly"){
BarsInDay = BarsSince(DayOfWeek() < Ref( DayOfWeek(), -1 ));
Bot = TimeFrameGetPrice("L", inWeekly, 0);
Top = TimeFrameGetPrice("H", inWeekly, 0);
Vol = TimeFrameGetPrice("V", inWeekly, 0);
}

if(Period=="Monthly" ){
BarsInDay = BarsSince(Month() != Ref(Month(), -1));
Bot = TimeFrameGetPrice("L", inMonthly, 0);
Top = TimeFrameGetPrice("H", inMonthly, 0);
Vol = TimeFrameGetPrice("V", inMonthly, 0);
}

CurTop = HHV(H,BarsInDay+1);
Curbot = LLV(L,BarsInDay+1);
Range = Highest(Top-Bot);
TodayRange = Top - Bot;

AveRange = Sum(Top-Bot,30)/30;
LAveRange = AveRange[BarCount-1];

// Initialization
baseX = 0;
baseY = floor(Bot[0]/Den)*Den;
relTodayRange = 0;
firstVisBar = Status("firstvisiblebar");
lastVisBar = Status("lastvisiblebar");

D=.0005;
total=0;
totaldn=0;
totalup=0;
shiftup=0;
shiftdn=0;
startr=0;

for (j=0; j <= 100; j++) {
x[j] = 0;
}

i0 = 0;
i1 = 0;
for (i=0; i<BarCount; i++) {
if (BarsInDay == 0 AND i < firstVisBar) {
i0 = i;
}
if (BarsInDay == 0 AND i >= lastVisBar) {
i1 = i;
}
}

i1 = BarCount-1;
for (i=i0; i<=i1; i++) {
if (BarsInDay == 0) {
baseX = i;
baseY = floor(Bot/Den)*Den;
maxY = floor(Top/Den)*Den;
relTodayRange = (maxY-baseY)/Den;

for (j=0; j <= relTodayRange; j++) {
x[j] = 0;
}
}

range_x=lastVisBar-firstVisBar;
spread = Param("X Space", 112, 1, 200, 1);
tpl = Param("Time Per Letter (mins)", 30, 1, 360, 1);
Intervalmin=Interval()/60;
flt =Param("First Letter (Bars)", 1, 1, 60, 1);
teb=ParamToggle("To Each Bar","No|Yes");
Color=Param("Color Threshold",20,1,50,1);
stopg=0;
stopr=0;
new=0;

Voloumeunit=Vol/LastValue(BarsInDay);

if (EnMP2 == "Letters") {
for (j=0; j<= relTodayRange; j++) {
if (L <= baseY+j*Den AND H >= baseY+j*Den) {
PlotTextSetFont("", "Arial", 6, BarCount-1, Close[ BarCount - 3 ], colorGreen, colorDefault, -20 );
PlotTextSetFont("C", "Arial", 40, 100, 100, colorGreen, colorDefault, -20 );
PlotText(StrExtract(" A , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , R , S , T , U , V , W , X , Y , Z, a , b , c , d , e , f , g , h , i , j , k , L , m , n ,o , p , q , r , s , t , u , v , w , x , y , z ",
IIf(BarsInDay<flt,0,floor(BarsInDay/(tpl/Intervalmin))-0)), baseX+IIf(teb==1,BarsInDay,x[j]*(range_x/spread)), baseY+j*Den,
colorWhite,ColorHSB(10+((floor(BarsInDay/(tpl/Intervalmin)))*Color),160,140));
x[j]++;
PlotTextSetFont("", "Arial", 10, BarCount-1, Close[ BarCount - 3 ], colorGreen, colorDefault, -20 );
}
}
}

else if (EnMP2 == "Lines" OR EnMP2 == "Solid") {
for (j=0; j<= relTodayRange; j++) {
if (L <= baseY+j*Den AND H >= baseY+j*Den) {
if(Type=="Price Profile"){x[j]=x[j]+1;}
else if(Type=="Volume Profile"){x[j]=x[j]+round(V/Voloumeunit);}

}
}
}

// Draw Initial Balance after 11am bar is complete
if (BarsInDay == IBBars AND EnIB == 0) {
Line1 = LineArray(i-2, curtop[i-1],i+7, curtop[i-1],0,True);
Plot(Line1,"",colorLightGrey,styleLine+styleDashed |styleNoRescale);
Line1 = LineArray(i-2, curbot[i-1],i+7, curbot[i-1],0,True);
Plot(Line1,"",colorLightGrey,styleLine+styleDashed |styleNoRescale);
}

// Examine x[j]
if ((i < BarCount - 1 AND BarsInDay[i+1] == 0) OR i == BarCount-1) {
maxXj = 0;
maxj = 0;
for (j=0; j<= relTodayRange; j++) {
if (maxXj < x[j]) {maxXj = x[j]; maxj = j; StaticVarSet("Maxj",j); new=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(Maxj-a>0 AND Maxj+a<relTodayRange)
{
if(MaxXj+total[Maxj+a]-total[Maxj]+total[Maxj-1]-total[Maxj-(a+1)]>=Value_area) {shiftup=a; shiftdn=a; break;}
}
else if(Maxj-a<1 )
{
if(MaxXj+total[Maxj+a]-total[Maxj]+total[Maxj-1]>=Value_area){shiftup=a; shiftdn=maxj-1; break;}
}
else if(Maxj+a>relTodayRange )
{
if(MaxXj+total[relTodayRange]-total[Maxj]+total[Maxj-1]-total[Maxj-(a+1)] >=Value_area){shiftup=relTodayRange-maxj; shiftdn=a; break;}
}
}

Vah = LineArray(baseX, baseY+(maxj+shiftup)*Den, i, baseY+(maxj+shiftup)*Den,0,True);
Val = LineArray(baseX, baseY+(maxj-shiftdn)*Den, i, baseY+(maxj-shiftdn)*Den,0,True);
poc = LineArray(baseX, baseY+maxj*Den, i, baseY+maxj*Den,0,True);
if(ViewVALVAH==1){Plot(Vah,"",ParamColor("Color_VA ", colorLightBlue),styleLine|styleNoRescale);
Plot(Val,"",ParamColor("Color_VA", colorLightBlue),styleLine|styleNoRescale);}
if(ViewPOC==1){Plot(poc,"",Colorpoc,styleLine|styl eNoRescale);}
PlotText(""+(baseY+(maxj+shiftup)*Den),i-5,baseY+(maxj+shiftup)*Den,colorWhite);
PlotText(""+(baseY+(maxj-shiftdn)*Den),i-5,baseY+(maxj-shiftdn)*Den,colorWhite);
if(ViewTPOCount==1){PlotText(""+total[maxj],basex,bot-(Top-bot)*0.05,ParamColor("Color_VAL", colorLavender));
PlotText(""+(total[relTodayRange]-total[maxj]),basex,Top+(Top*0.0005),ParamColor("Color_VAH", colorLavender));}

if(ViewPOC==1){PlotText(""+(baseY+maxj*Den),i-5,baseY+maxj*Den,Colorpoc);}
}

if (i < BarCount - 1 AND BarsInDay[i+1] == 0 OR i == BarCount-1) {

for (p = 1; p < relTodayRange+1; p++){
line = LineArray(baseX, baseY+p*Den, baseX+x[p], baseY+p*Den);
line2 = LineArray(baseX, baseY+(p-1)*Den, baseX+x[p-1], baseY+(p-1)*Den);

if (EnMP2 == "Solid")
{
PlotOHLC( Line, Line, Line2, Line2, "",IIf(p>(maxj+shiftup),ParamColor("Color_VAH" , colorLavender),IIf(p<=(maxj+shiftup)AND p>(maxj-shiftdn),ParamColor("Color_VA", colorLightBlue),ParamColor("Color_VAL", colorLavender))) ,styleCloud|styleNoRescale|styleNoLabel);
}
if (EnMP2 == "Lines")
{
Plot(line,"",IIf(p>(maxj+shiftup),ParamColor("Colo r_VAH", colorLavender),IIf(p<=(maxj+shiftup)AND p>(maxj-shiftdn),ParamColor("Color_VA", colorLightBlue),ParamColor("Color_VAL", colorLavender))) , styleLines|styleNoLabel);
}

}
if(Viewfill==1){PlotOHLC(Vah,Vah,Val,Val,"",Colorf ill,styleCloud|styleNoRescale|styleNoLabel);}

}
}

_SECTION_END();

_SECTION_BEGIN("Gradient Backfill");
SetChartBkGradientFill( ParamColor("BgTop", ColorRGB( 0,0,0 )),

ParamColor("BgBottom", ColorRGB( 0,0,0 )),ParamColor("titleblock",ColorRGB( 192,192,192 )));
_SECTION_END();
 

Nehal_s143

Well-Known Member
Hi

I am using 10min afl code to plot candle color on 5 min chart, but candle is showing color from wrong place, color should change from place of vertical lines placed manually.

please check and correct where did I went wrong to expand the afl


Code:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();

_SECTION_BEGIN("Trend");
a=20; 
p=5; 
Om=DEMA(O,p);
hm=DEMA(H,p);
lm=DEMA(L,p);
Cm=DEMA(C,p);
HACLOSE=(Om+Hm+Lm+Cm)/4;

LTColor=IIf(Haclose>MA(HaClose,a),colorGreen, colorRed);
//Plot( C, "Close", LTColor, styleCandle); 
//Plot(2, "",LTColor, styleOwnScale| styleArea|styleNoLabel,-0.5,100);

_SECTION_END();

_SECTION_BEGIN("10min Trend");
tf=10;
tfrm=in1Minute*tf;
TimeFrameSet(tfrm);

a2=20; 
p2=5; 
Om2=DEMA(O,p2);
hm2=DEMA(H,p2);
lm2=DEMA(L,p2);
Cm2=DEMA(C,p2);
HACLOSE2=(Om2+Hm2+Lm2+Cm2)/4;

TimeFrameRestore();

HACLOSE2f = TimeFrameExpand(HACLOSE2, tfrm);
      a2f = TimeFrameExpand(a2, tfrm);

LTColor2=IIf(Haclose2f>MA(HaClose2f,a2f),colorGreen, colorRed);
Plot( C, "Close", LTColor2, styleCandle); 

//Plot(2, "",LTColor2, styleOwnScale| styleArea|styleNoLabel,-0.5,100);

_SECTION_END();
 

Attachments

Nehal

Try this

Code:
_SECTION_BEGIN("Price");

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

_SECTION_BEGIN("Trend");
a=20; 
p=5; 
Om=DEMA(O,p);
hm=DEMA(H,p);
lm=DEMA(L,p);
Cm=DEMA(C,p);
HACLOSE=(Om+Hm+Lm+Cm)/4;

LTColor=IIf(Haclose>MA(HaClose,a),colorGreen, colorRed);
//Plot( C, "Close", LTColor, styleCandle); 
//Plot(2, "",LTColor, styleOwnScale| styleArea|styleNoLabel,-0.5,100);

_SECTION_END();


_SECTION_BEGIN("10min Trend");
tf=10;
a2=20; 
p2=5; 
tfrm=in1Minute*tf;
TimeFrameSet(tfrm);

Om2=DEMA(O,p2);
hm2=DEMA(H,p2);
lm2=DEMA(L,p2);
Cm2=DEMA(C,p2);
HACLOSE2=(Om2+Hm2+Lm2+Cm2)/4;
HAMA = MA(HaClose2,a2);
ColorCode = HACLOSE2 > HAMA;
TimeFrameRestore();

ColorCodeF = TimeFrameExpand(ColorCode, tfrm, expandLast);
LTColor2=IIf(ColorCodeF,colorGreen, colorRed);
Plot( C, "Close", LTColor2, styleCandle); 

_SECTION_END();
 

bapu4

Well-Known Member
Hi Seniors,
I need buy/sell information by scanning/exploring this afl, pl add required the afl lines for this rsi macd afl.

Code:
[CODE]_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));

trendup = IIf(MACD(12,26) > 0 AND MACD(12,26) > Signal(12,26,9), colorBlue, colorWhite);
trendcolor = IIf(MACD(12,26) < 0 AND MACD(12,26) < Signal(12,26,9), colorRed, trendup);
Plot( C, "Close", trendcolor, styleBar | styleThick );

//RSIup = RSI(7) > 70;
//RSIdown = RSI(7) < 30;

sp = Param( "RSI Period", 7, 1, 100 );
r = RSI( sp );
RSIup = r > 70; 
RSIdown = r < 30;

shape = RSIup * shapeNone + RSIdown * shapeNone;
PlotShapes(shape, IIf( RSIup, colorBrightGreen, colorRed ), 0, IIf( RSIup, Low, High ) );

if( ParamToggle("Tooltip shows", "All Values | Only Prices" ) )
{
ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 1 )));
}
_SECTION_END();

SetChartBkColor(ParamColor("Panel color ",colorBlack));

PlotOHLC( Open, High, Low, Close, "", colorLime, styleBar| styleThick );



_SECTION_BEGIN("trailstops");
EntrySignal = C > ( LLV( L, 20 ) + 2 * ATR( 10 ) ); 
ExitSignal = C < ( HHV( H, 20 ) - 2 * ATR( 10 ) ); 
Color = IIf( EntrySignal, colorBlue, IIf( ExitSignal, colorOrange, colorGrey50 )); 
TrailStop = HHV( C - 2 * ATR(10), 15 ); 
ProfitTaker = EMA( H, 13 ) + 2 * ATR(10); 

/* plot price chart and stops */ 
Plot( TrailStop, "Trailing stop", colorGold, styleThick | styleLine ); 
Plot( C, "Price", color, styleBar ); 

/* plot color ribbon */ 
Plot( 2, "", Color, styleArea | styleOwnScale | styleNoLabel, -0.1, 50 );


_SECTION_BEGIN("GFX EMA");
procedure Plotlinewidth(pvalue,ptitle,pcolor,pstyle,pmin,pmax,pxshift,plinewidth,pshowdate)
{
local pvalue,ptitle,pcolor,pstyle,pmin,pmax,pxshift,plinewidth,ppenstyle,pshowdate;
local Miny,Maxy; local Lvb,fvb; local pxwidth,pxheight; local TotalBars,axisarea; local i,x,y;
if(plinewidth>0 && Status("action") == 1 && (pstyle & styleLine==styleLine)){
GfxSetOverlayMode(0);
Miny=Status("axisminy"); Maxy=Status("axismaxy");
lvb=Status("lastvisiblebar"); fvb=Status("firstvisiblebar");
pxwidth=Status("pxwidth"); pxheight=Status("pxheight");
TotalBars=Lvb-fvb; xaxisarea=56; 
if(pshowdate)yaxisarea=10;
else
yaxisarea=0; i=0;
x=5+i*(pxwidth-xaxisarea-10)/( TotalBars+1);
y=5+yaxisarea+(pvalue[i+fvb]-Miny)*(pxheight-yaxisarea-10)/(Maxy-Miny);
GfxMoveTo(x,pxheight - y);
for(i=1; i<TotalBars AND i<(BarCount-fvb); i++) 
{
GfxSelectPen(pcolor[i + fvb],plinewidth,0);
x=5+i*(pxwidth-xaxisarea-10)/(TotalBars+1);
y=5+yaxisarea+(pvalue[i+fvb]-Miny)*(pxheight-yaxisarea-10)/(Maxy-Miny);
GfxLineTo(x,pxheight - y);
}}}
RequestTimedRefresh(2); 
_SECTION_END();



_SECTION_BEGIN("Small Triggers");
p1 = Param("TL 1 Periods", 20, 5, 50, 1);
p2 = Param("TL 2 Periods", 5, 3, 25, 1);
TL1 = LinearReg(C, p1);
TL2 = EMA(TL1, p2);
Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorBrightGreen), ParamColor("TL Dn Colour", colorCustom12));
Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Large Triggers");
p3 = Param("TL 3 Periods", 80, 5, 100, 1);
p4 = Param("TL 4 Periods", 20, 3, 100, 1);
TL3 = LinearReg(C, p3);
TL4 = EMA(TL3, p4);
Col1 = IIf(TL3 > TL4, ParamColor("TLL Up Colour", colorBlue), ParamColor("TLL Dn Colour", colorRed));
Plot(TL3, "TriggerLine 3", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL4, "TriggerLine 4", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Fibo Retrace and Extensions");
fibs = ParamToggle("Plot Fibs","Off|On",1);
pctH = Param ("Pivot Hi %", 0.325,0.001,2.0,0.002);
HiLB = Param ("Hi LookBack",1,1,BarCount-1,1);
pctL = Param ("Pivot Lo %", 0.325,0.001,2.0,0.002);
LoLB = Param ("Lo LookBack",1,1,BarCount-1,1);
Back = Param ("Extend Left = 2",1,1,500,1);
Fwd = Param("Plot Forward", 0, 0, 500, 1);
text = ParamToggle("Plot Text","Off|On",1);
hts = Param ("Text Shift", -33.5,-50,50,0.10);
style =ParamStyle("Line Style",styleLine,styleNoLabel);
x = BarIndex();
pRp = PeakBars( H, pctH, 1) == 0;
yRp0 = SelectedValue(ValueWhen( pRp, H, HiLB));
xRp0 = SelectedValue(ValueWhen( pRp, x, HiLB));
pSp = TroughBars( L, pctL, 1) == 0;
ySp0 = SelectedValue(ValueWhen( pSp, L, LoLB));
xSp0 = SelectedValue(ValueWhen( pSp, x, LoLB));
Delta = yRp0 - ySp0;

function fib(ret)
{
retval = (Delta * ret);
Fibval = IIf(ret < 1.0 
AND xSp0 < xRp0, yRp0 - retval, IIf(ret < 1.0 
AND xSp0 > xRp0, ySp0 + retval,IIf(ret > 1.0 
AND xSp0 < xRp0, yRp0 - retval, IIf(ret > 1.0 
AND xSp0 > xRp0, ySp0 + retval, Null)))); 
return FibVal;
}

x0 = Min(xSp0,xRp0)-Back;
x1 = (BarCount -1);

r236 = fib(0.236); r236I = LastValue (r236,1);
r382 = fib(0.382); r382I = LastValue (r382,1);
r050 = fib(0.50); r050I = LastValue (r050,1);
r618 = fib(0.618); r618I = LastValue (r618,1);
r786 = fib(0.786); r786I = LastValue (r786,1);
e127 = fib(1.27); e127I = LastValue (e127,1);
e162 = fib(1.62); e162I = LastValue (e162,1);
e200 = fib(2.00); e200I = LastValue (e200,1);
e262 = fib(2.62); e262I = LastValue (e262,1);
e424 = fib(4.24); e424I = LastValue (e424,1);

p00 = IIf(xSp0 > xRp0,ySp0,yRp0); p00I = LastValue (p00,1);
p100 = IIf(xSp0 < xRp0,ySp0,yRp0); p100I = LastValue (p100,1);
color00 =IIf(xSp0 > xRp0,colorLime,colorRed);
color100 =IIf(xSp0 < xRp0,colorLime,colorRed);

numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);

if(fibs==1)
{
Plot(LineArray(xRp0-Fwd,yRp0,x1,yRp0,Back),"PR",32,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(xSp0-Fwd,ySp0,x1,ySp0,Back),"PS",27,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r236,x1,r236,Back),"",45,style|styleNoRescale, Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r382,x1,r382,Back),"",44,style|styleNoRescale, Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r050,x1,r050,Back),"",41,style|styleNoRescale, Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r618,x1,r618,Back),"",43,style|styleNoRescale, Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r786,x1,r786,Back),"",42,style|styleNoRescale, Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e127,x1,e127,Back),"e127",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e162,x1,e162,Back),"e162",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e200,x1,e200,Back),"p200",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e262,x1,e262,Back),"p262",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e424,x1,e424,Back),"p424",25,style|styleNoRescale,Null, Null,Fwd);
}

if(text==1)
{ 
PlotText(" 0% = " + WriteVal(p00,fraction), LastValue(BarIndex())-(numbars/hts), p00I + 0.05, color00);
PlotText("23% = " + WriteVal(r236,fraction), LastValue(BarIndex())-(numbars/hts), r236I + 0.05, 45);
PlotText("38% = " + WriteVal(r382,fraction), LastValue(BarIndex())-(numbars/hts), r382I + 0.05, 44);
PlotText("50% = " + WriteVal(r050,fraction), LastValue(BarIndex())-(numbars/hts), r050I + 0.05, 41);
PlotText("62% = " + WriteVal(r618,fraction), LastValue(BarIndex())-(numbars/hts), r618I + 0.05, 43);
PlotText("78% = " + WriteVal(r786,fraction), LastValue(BarIndex())-(numbars/hts), r786I + 0.05, 42);
PlotText("100% = " + WriteVal(p100,fraction), LastValue(BarIndex())-(numbars/hts),p100I + 0.05, color100);
PlotText("127% = " + WriteVal(e127,fraction), LastValue(BarIndex())-(numbars/hts),e127I + 0.05, 47);
PlotText("162% = " + WriteVal(e162,fraction), LastValue(BarIndex())-(numbars/hts),e162I + 0.05, 47);
PlotText("200% = " + WriteVal(e200,fraction), LastValue(BarIndex())-(numbars/hts),e200I + 0.05, 47);
PlotText("262% = " + WriteVal(e262,fraction), LastValue(BarIndex())-(numbars/hts),e262I + 0.05, 47);
PlotText("424% = " + WriteVal(e424,fraction), LastValue(BarIndex())-(numbars/hts),e424I + 0.05, 25);
}
_SECTION_END();

/* **********************************

Code to automatically identify pivots

********************************** */

// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",100,50,5000,10);
nBars = Param("Number of bars", 12, 5, 40);

// -- Title.

Title = Name() + " (" + StrLeft(FullName(), 15) + ") O: " + Open + ", 

H: " + High + ", L: " + Low + ", C: " + Close;

// -- Plot basic candle chart

PlotOHLC(Open, High, Low, Close, 

"BIdx = " + BarIndex() + 

"\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L

+ "\n"+"C ",

colorBlack, styleBar); 

GraphXSpace=7;

// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;

// -- looking back from the current bar, how many bars 

// back were the hhv and llv values of the previous 

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of 

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend

curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] < 

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for 

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

if (aLLVBars[curBar] < aHHVBars[curBar]) {

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

} 

// -- loop through bars

} 

// -- Basic attempt to add a pivot this logic may have missed

// -- OK, now I want to look at last two pivots. If the most 

// recent low pivot is after the last high, I could

// still have a high pivot that I didn't catch

// -- Start at last bar

curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar]; 

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the 

// pivot information arrays

for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

} 

} else {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar]; 

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the 

// pivot information arrays

for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}

// -- Dump inventory of high pivots for debugging

/*

for (k=0; k<nHPivs; k++) {

_TRACE("High pivot no. " + k

+ " at barindex: " + aHPivIdxs[k] + ", " 

+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k], 

DateTime(), 1), formatDateTime)

+ ", " + aHPivHighs[k]);

}

*/

// -- OK, let's plot the pivots using arrows

PlotShapes(

IIf(aHPivs==1, shapeHollowDownArrow, shapeNone), colorRed, 0,

High, Offset=-15);

PlotShapes(

IIf(aLPivs==1, shapeHollowUpArrow , shapeNone), colorBrightGreen, 0, 

Low, Offset=-15);
_SECTION_END();

_SECTION_BEGIN("Dots Color");
acc = Param("Acceleration", 0.01, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.1, 0, 1, 0.001 );
myColor = IIf (C > SAR( acc, accm ) , colorTurquoise , colorRed);
Plot( SAR( acc, accm ), _DEFAULT_NAME(), myColor , ParamStyle("Style", styleDots |styleThick | styleNoLine, maskDefault | styleDots | styleThick | styleNoLine ) );
_SECTION_END();

SetChartBkGradientFill( ParamColor("BgTop", ColorRGB( 172,172,172 )),

ParamColor("BgBottom", ColorRGB( 172,172,172 )),ParamColor("titleblock",ColorRGB( 172,172,172 )));




SetChartBkGradientFill( ParamColor( "TopColor", ColorRGB( 217, 197, 137 ) ), ParamColor( "BottomColor", ColorRGB( 254, 244, 224 ) ) );
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

_SECTION_BEGIN("SingleMA");

SetChartOptions(0, chartShowDates | chartWrapTitle);
Plot(C,"",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
kk = Optimize( "mult", Param( "mult", 2, 1, 6, 0.25 ), 1, 2, 0.25 );
Per = Optimize( "period", Param( "period", 10, 5, 50, 1 ), 5, 50, 1 );
sdfact = Param( "Standard Deviation Factor", 2, 0.5, 5, 0.1 );
offset = Param( "Offset", 2, 1, 50, 1 );
tc = ParamList( "Channel Display", List = "No Channel|Channel|ChannelRT|Both
Channels", 1 );
ms = ParamToggle( "Trend", "Regular|Smoothed", 1 );

x = Cum( 1 );
HaClose = ( O + H + L + C ) / 4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );

if ( ms == 0 )
{
nm = ( H - L );
j = ( O + H + L + C ) / 4;
}
else
{
nm = ( HaHigh - HaLow );
j = ( HaOpen + HaHigh + HaLow + HaClose ) / 4;
}

rfsctor = WMA( nm, Per );

revers = kk * rfsctor;
Trend = 1;
NW[0] = 0;

for ( i = 1;i < BarCount;i++ )
{
if ( Trend[i-1] == 1 )
{
if ( j[i] < NW[i-1] )
{
Trend[i] = -1;
NW[i] = j[i] + Revers[i];
}
else
{
Trend[i] = 1;

if ( ( j[i] - Revers[i] ) > NW[i-1] )
{
NW[i] = j[i] - Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}

if ( Trend[i-1] == -1 )
{
if ( j[i] > NW[i-1] )
{
Trend[i] = 1;
NW[i] = j[i] - Revers[i];
}
else
{
Trend[i] = -1;

if ( ( j[i] + Revers[i] ) < NW[i-1] )
{
NW[i] = j[i] + Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}
}

cp = ( H + L ) / 2;

Plot( IIf( NW < j, NW, Null ), "\ntrailLong", ParamColor( "ColorTrailLong",
colorBlue ), styleStaircase | styleDots );
Plot( IIf( NW > j, NW, Null ), "\ntrailShort", ParamColor( "ColorTrailShort",
colorRed ), styleStaircase | styleDots );

Buy = NW < j;
Short = NW > j;

Cover=Buy = ExRem(Buy, Short);
Sell=Short = ExRem(Short, Buy);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorBlue, 0,NW);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,NW);



_SECTION_END();

_SECTION_BEGIN("Small Triggers");
p1 = Param("TL 1 Periods", 55, 5, 50, 1);
p2 = Param("TL 2 Periods", 2, 3, 25, 1);
TL1 = LinearReg(C, p1);
TL2 = EMA(TL1, p2);
Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorBrightGreen), ParamColor("TL Dn Colour", colorCustom12));
Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("SmoothHeikin MA Swing");
SetBarsRequired(200,0);
GraphXSpace = 5;
SetChartOptions(0,chartShowArrows|chartShowDates);
k = Param("K", 1.5, 1, 5, 0.1);
Per = Param("ATR", 3, 1, 30, 0.50);
k1 = Optimize("K", 1, 0.1, 5, 0.1);
Per1 = Optimize("ATR", 3, 1, 30, 0.50);
HACLOSE=(O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
j=Haclose;
//================================================== ================================================== ===================
//=========================Indicator================ ================================================== ============================
f=ATR(14);

rfsctor = WMA(H-L, Per);

revers = k * rfsctor;

Trend = 1; 
NW[0] = 0; 


for(i = 1; i < BarCount; i++)
{
if(Trend[i-1] == 1) 
{
if(j[i] < NW[i-1]) 
{
Trend[i] = -1; 
NW[i] = j[i] + Revers[i]; 
}
else 
{
Trend[i] = 1;
if((j[i] - Revers[i]) > NW[i-1])
{
NW[i] = j[i] - Revers[i]; 
}
else
{
NW[i] = NW[i-1];
}
} 
}
if(Trend[i-1] == -1) 
{
if(j[i] > NW[i-1]) 
{
Trend[i] = 1; 
NW[i] = j[i] - Revers[i]; 
}
else 
{
Trend[i] = -1;
if((j[i] + Revers[i]) < NW[i-1]) 
{
NW[i] = j[i] + Revers[i]; 
}
else
{
NW[i] = NW[i-1];
}
}
}
}

//===============system================
Plot(NW, "", IIf(Trend == 1, 3, 25), 1);

Buy=Cover=Cross(j,nw);
Sell=Short=Cross(nw,j);
SWING =WriteIf(j > nw, "UPTREND","DOWNTREND");
GfxSetTextColor( colorLightBlue );
GfxSelectFont("Arial", 8 );
shape = Buy * shapeCircle + Sell * shapeCircle;
_SECTION_END();

_SECTION_BEGIN( "KBrain" );
//========================Initiation================ ========
bts = ParamToggle( "BrainTrend1 signal", "No|Yes" , 1 );
btst = ParamToggle( "BrainTrend1 stop", "No|Yes" , 1 );
btsl = ParamToggle( "BrainTrend1 stop line", "No|Yes" , 1 );
period = Param( "Period", 14, 6, 20, 1 );
x1 = 53;
x2 = 47;
d = 2.3;
f = 7;
s = 1.5;
range = ATR( f );
Range1 = ATR( f ) / d;
Range2 = ( ATR( f ) * s ) / 4;
range3 = ATR( 10 );
R = ( ( HHV( H, period ) - C ) / ( HHV ( H, period ) - LLV ( L, period ) ) ) * -100;
EMA1 = EMA( R, Period );
EMA2 = EMA( EMA1, 5 );
Difference = EMA1 - EMA2;
ZeroLagEMA = EMA1 + Difference;
value2 = abs( ZeroLagEMA );


function PercentR( periods )
{
return -100 * ( HHV( H, periods ) - C ) / ( HHV( H, periods ) - LLV( L, periods ) );
}


for ( i = 1; i < Period - 1; i++ )
{


tm = 1;
bstop = 1;
sstop = 1;
p = 1;
}

for ( i = period + 10; i < BarCount; i++ )
{
C[0] = 0;
Value2[0] = 0;
p[0] = 0;
Plot1[0] = 0;
Plot2[0] = 0;
Val1 = 0;
Val2 = 0;
temp[0] = 0;
Value3[0] = 0;
tm[0] = 0;
p1[i] = 0;
bt1[0] = 0;
bt2[0] = 0;
r[0] = 0;
bt1a[0] = 0;
bt2a[0] = 0;
istop[0] = Val1[0];
stop[i] = Val1[0];
bstop[0] = 0;
sstop[i] = 0;
//==================================Indicators====== ======================
{
if ( value2[i] < x2 AND abs( Close[i] - Close[i-2] ) > range1[i] )
p[i] = 1 ;
else
{
if ( value2[i] > x1 AND abs( Close[i] - Close[i-2] ) > Range1[i] )
p[i] = 2 ;

else

p[i] = 0;
}
}

if ( ( value2[i] < x2 AND p[i] == 1 ) OR ( value2[i] < x2 AND p[i] == 0 ) )

{
if ( abs( Close[i] - Close[i-2] ) > Range1[i] )
{
Plot1[i] = H[i];
Plot2[i] = L[i];
}
else
{
Plot1[i] = Plot1[i-1];
Plot2[i] = Plot2[i-1];
}
}
else
{
if ( ( value2[i] > x1 AND p[i] == 2 ) OR ( value2[i] > x1 AND p[i] == 0 ) )

{
Plot1[i] = L[i];
Plot2[i] = H[i];
}
else
{
Plot1[i] = Plot1[i-1];
Plot2[i] = Plot2[i-1];
}
}

//==================KBrain Signal ====================================
{
if ( value2[i] < x2 AND ( abs( Close[i] - Close[i-2] ) > Range1[i] ) )
{
if ( p[i] == 1 OR p[i] == 0 )
Value3[i] = L[i] - range3[i];

val1[i] = Value3[i];

p[i] = 1;

temp[i] = 1;
}
else
{
temp[i] = temp[i-1];
}

{
if ( value2[i] > x1 AND ( abs( Close[i] - Close[i-2] ) > Range1[i] ) )
{
if ( p[i] == 2 OR p[i] == 0 )
Value3[i] = H[i] + range3[i];

val2[i] = Value3[i];

p[i] = 2;

temp[i] = 2;;
}

}
}

{

if ( temp[i] == 1 AND Plot1[i] > 0 AND tm[i] != 1 )
tm[i] = 1;

if ( temp[i] == 2 AND Plot2[i] > 0 AND tm[i] != 2 )
tm[i] = 2;

}

//===============================stop=============== =========================
{
if ( value2[i] < x2 AND ( abs( Close[i] - Close[i-2] ) > Range1[i] )AND p[i] != 2 )

{
value3[i] = L[i] - range3[i];
va1[i] = Value3[i];
p1[i] = 2;
r[i] = Va1[i];
bstop[i] = Va1[i];
bt2[i] = bt2[i-1];
}

if ( value2[i] > x1 AND ( abs( Close[i] - Close[i-2] ) > Range1[i] )AND p[i] != 1 )

{
Value3[i] = H[i] + range3[i];
va2[i] = Value3[i];
p1[i] = 1;

r[i] = Va2[i];
sstop[i] = Value3[i];
bt1[i] = bt1[i-1];
}

}



if ( val1[i] == 0 AND val2[i] == 0 AND p[i] == 0 )
{
bstop[i] = bstop[i-1];
sstop[i] = sstop[i-1];
}


if ( bstop[i] < bstop[i-1] AND tm[i] == 1 AND tm[i-1] == 1 )
bstop[i] = bstop[i-1];

if ( sstop[i] > sstop[i-1] AND tm[i] == 2 AND tm[i-1] == 2 )
sstop[i] = sstop[i-1];

}

//=============================SYSTEM=============== =======================
Mycolor = IIf( p == 1, colorLime, IIf( p == 2, colorRed, colorBlue ) );

PlotShapes( shapeCircle* ( bts AND tm == 1 AND Ref( tm, -1 ) == 2 ), colorTurquoise, 0, bstop, 0 );

PlotShapes( shapeCircle* ( bts AND tm == 2 AND Ref( tm, -1 ) == 1 ), colorCustom12, 0, sstop, 0 );

PlotShapes( IIf( btst AND p == 1, shapeSmallCircle, Null ), colorTurquoise, 0, bstop, 0 );

Plot( IIf( btsl AND tm == 1, bstop, Null ), "", colorPaleBlue, 1 );

PlotShapes( IIf( btst AND p == 2, shapeSmallCircle, Null ), colorCustom12, 0, sstop, 0 );

Plot( IIf( btsl AND tm == 2, sstop, Null ), "", colorLightYellow, 1 );

Buy = Cover = ( bts AND tm == 1 AND Ref( tm, -1 ) == 2 );

Sell = Short = ( bts AND tm == 2 AND Ref( tm, -1 ) == 1 );

SellPrice = ValueWhen( Sell, C, 1 );

BuyPrice = ValueWhen( Buy, C, 1 );

Long = Flip( Buy, Sell );

Shrt = Flip( Sell, Buy );

_SECTION_END();
[/CODE]
Thanks and regards
 
Last edited:

Nehal_s143

Well-Known Member
Mastermind007 sir even this code is working perfectly, thank you for help:thanx:

Code:
_SECTION_BEGIN("10min Trend");
tf=10;
a2=20; 
p2=5; 
tfrm=in1Minute*tf;
TimeFrameSet(tfrm);

Om2=DEMA(O,p2);
hm2=DEMA(H,p2);
lm2=DEMA(L,p2);
Cm2=DEMA(C,p2);
HACLOSE2=(Om2+Hm2+Lm2+Cm2)/4;
HAMA2 = MA(HaClose2,a2);

TimeFrameRestore();

HACLOSE2f = TimeFrameExpand(HACLOSE2, tfrm);
   HAMA2f = TimeFrameExpand(HAMA2, tfrm);

LTColor2=IIf(Haclose2f>HAMA2f,colorGreen, colorRed);
Plot( C, "Close", LTColor2, styleCandle); 

//Plot(2, "",LTColor2, styleOwnScale| styleArea|styleNoLabel,-0.5,100);

_SECTION_END();
 

bapu4

Well-Known Member
Hi vijkrish,
I missed your ps on PM,ya I used //in sound it keeps saying buy,buy,buy it doesn't stop tell me howto stop it ,secondly I have given 2 afls in above posts pl have a look if you can help.
 
hi Seniors,
is there a way to include the time at which a signal is generated into message box? Plz hep.
which message box? Where is your code in which you want to add this? Kindly attach it (whole or relevant part of it) here. You can either use Now(0) which will return you time-stamp of the moment or if you want to deal with past candles, you can use DateTime() or DateNum() or TimeNum()
 
Hi ...I want a AFL code for my moving average cross over system with Back-test facility(Intra-Day)
Here is the Logic...

BUY Condition :

Condition1 = Close > EMA10 (Candle 1)
Condition2 = next candle's close also > EMA10 (candle 2)

But I want Buy arrow in next candle(or u can say the 3rd candle's Open price)
But with one more condition here..Third candle also close in Green
If 3rd candle is Red than Avoid Buy and inplace of BUY signal I want Sell signal

Sell Condition :

Condition1 = Close < EMA10(Candle 1)
Condition2 = next candle's close also < EMA10 (candle 2)

But I want Sell arrow in next candle(Candle 3) But
With one more condition here ...Third Candle also close in RED
If 3rd candle is Green than avoid Buy and inplace of SELL I want BUY signal

is it possible ??
 

Similar threads