Simple Coding Help - No Promise.

Hi all,

I trying to develop MTF supertrend.


Following is my afl. I got the 15 minute supertrend into 5 minute chart.

However, I have problem with Buy and Sell arrows.


Kindly look the code and help me.






SetBarsRequired(100000,0);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ParamColor("bkcolor",colorTan));
GfxSetBkMode(0);
GfxSetOverlayMode(1);

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

//SetTradeDelays(1,1,1,1);
SetPositionSize(1, spsShares);

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

TimeFrameSet( 15*in1Minute ); // switch now to 15Min

Factor=Param("Factor",4,1,10,1);
Pd=Param("ATR Periods",12,1,100,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount-1; i++) {
TrendUp = Null;
TrendDown = Null;

trend=1;


if (Close>Up[i-1]) {
trend=1;
if (trend[i-1] == -1) changeOfTrend = 1;

}
else if (Close<Dn[i-1]) {
trend=-1;
if (trend[i-1] == 1) changeOfTrend = 1;
}
else if (trend[i-1]==1) {
trend=1;
changeOfTrend = 0;
}
else if (trend[i-1]==-1) {
trend=-1;
changeOfTrend = 0;
}

if (trend<0 && trend[i-1]>0) {
flag=1;
}
else {
flag=0;
}

if (trend>0 && trend[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}

if (trend>0 && Dn<Dn[i-1]){
Dn=Dn[i-1];
}

if (trend<0 && Up>Up[i-1])
{ Up=Up[i-1];
}

if (flag==1)
{ Up=(H+L)/2+(Factor*iATR);;
}
if (flagh==1)
{ Dn=(H+L)/2-(Factor*iATR);;
}
if (trend==1) {
TrendUp=Dn;
if (changeOfTrend == 1) {
TrendUp[i-1] = TrendDown[i-1];
changeOfTrend = 0;
}
}
else if (trend==-1) {
TrendDown=Up;
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1];
changeOfTrend = 0;
}
}
}



kol=IIf(TrendUp,colorBlue,IIf(TrendDown,colorRed, colorBlack));

Buy = C>TrendUp;
Sell = C<TrendDown;
Short = C<TrendDown;
Cover = C>TrendUp;



TimeFrameRestore(); // restore time frame to original



TrendUp15=TimeFrameExpand(TrendUp, 15*in1Minute);
TrendDown15=TimeFrameExpand(TrendDown, 15*in1Minute);

Plot(TrendUp15,"TrendUp15",colorBrown,styleThick);
Plot(TrendDown15,"TrendDown15",colorGreen,styleThick);

kol15=TimeFrameExpand(Kol, 15*in1Minute);
Plot( C, "Close", kol15, styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

BuyE = TimeFrameExpand(Buy, 15*in1Minute);
SellE = TimeFrameExpand(Sell, 15*in1Minute);
ShortE = TimeFrameExpand(Short, 15*in1Minute);
CoverE = TimeFrameExpand(Cover, 15*in1Minute);



BuyE=ExRem(BuyE,SellE);
SellE=ExRem(SellE,BuyE);
ShortE=ExRem(ShortE,CoverE);
CoverE=ExRem(CoverE,ShortE);


PlotShapes(IIf(BuyE, shapeUpArrow, shapeNone),colorBlue, 0,L, Offset=-40);
PlotShapes(IIf(SellE, shapeDownArrow, shapeNone),colorRed, 0,H, Offset=-40);
//PlotShapes(IIf(ShortE, shapeStar, shapeNone),colorGold, 0, H, Offset=40);
//PlotShapes(IIf(CoverE, shapeStar, shapeNone),colorBrown, 0,H, Offset=15);






Thanks
Amruta
 
HI AMPA

I have one doubt about mtf code.
you can expand a moving average to higher tf.
I feel doubtful about expanding a buy signal from one tf to a higher tf.
you may check this part carefully by coding buy sell signals after you get expanded moving averages or trend etc first.

happy trading!
 

Nehal_s143

Well-Known Member
I use to always think is it possible to encash V shape recovery/fall like we had seen recently in Nifty on 10.01.14

Yes, I got the answer We can, see below link for details

http : // blog.nobletrading . com / 2010/05/bump-and-run-reversal-chart-pattern . html



with my limited afl knowledge trying to code afl for the same, will try to post the afl if able to code :)
 

Nehal_s143

Well-Known Member
I use to always think is it possible to encash V shape recovery/fall like we had seen recently in Nifty on 10.01.14

Yes, I got the answer We can, see below link for details

http : // blog.nobletrading . com / 2010/05/bump-and-run-reversal-chart-pattern . html



with my limited afl knowledge trying to code afl for the same, will try to post the afl if able to code :)
I am trying with below afl, still need to lot of modifications


_SECTION_BEGIN("SHARE CLINIC TREND LINE");

SetBarsRequired(sbrAll,sbrAll);
xx = Cum(1);
nbar = Param("nbar",5,2,50,1);

// define fractals
PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);
PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L);

// lower trendline, segment 1: between fractal points
startval_L = ValueWhen(PLow,L,1);
endval_L = ValueWhen(PLow,L,0);
startbar_L = ValueWhen(PLow,xx,1);
endbar_L = ValueWhen(PLow,xx,0);
aa_L = (endval_L-startval_L)/(endbar_L-startbar_L);
bb_L = startval_L;
trendline_L = aa_L * (xx - startbar_L) + bb_L;
// slope calculations for display purpose only
dtrendline_L = trendline_L - Ref(trendline_L,-1);

// lower extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_L_extend = ValueWhen(PLow,L,2);
endval_L_extend = ValueWhen(PLow,L,1);
startbar_L_extend = ValueWhen(PLow,xx,2);
endbar_L_extend = ValueWhen(PLow,xx,1);
aa_L_extend = (endval_L_extend-startval_L_extend)/(endbar_L_extend-startbar_L_extend);
bb_L_extend = startval_L;
trendline_L_extend = aa_L_extend * (xx - startbar_L) + endval_L_extend;
// slope calculations for display purpose only
dtrendline_L_extend = trendline_L_extend - Ref(trendline_L_extend,-1);
dtrendline_L_extend = IIf(PLow,Ref(dtrendline_L,-1),dtrendline_L_extend);

// lower extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_L_extend2 = ValueWhen(PLow,L,3);
endval_L_extend2 = ValueWhen(PLow,L,2);
startbar_L_extend2 = ValueWhen(PLow,xx,3);
endbar_L_extend2 = ValueWhen(PLow,xx,2);
aa_L_extend2 = (endval_L_extend2-startval_L_extend2)/(endbar_L_extend2-startbar_L_extend2);
bb_L_extend2 = endval_L_extend2;
trendline_L_extend2 = aa_L_extend2 * (xx - endbar_L_extend2) + endval_L_extend2;
// slope calculations for display purpose only
dtrendline_L_extend2 = trendline_L_extend2 - Ref(trendline_L_extend2,-1);
dtrendline_L_extend2 = IIf(PLow,Ref(dtrendline_L_extend,-1),dtrendline_L_extend2);

// upper trendline, segment 1: between fractal points
startval_H = ValueWhen(PHigh,H,1);
endval_H = ValueWhen(PHigh,H,0);
startbar_H = ValueWhen(PHigh,xx,1);
endbar_H = ValueWhen(PHigh,xx,0);
aa_H = (endval_H-startval_H)/(endbar_H-startbar_H);
bb_H = startval_H;
trendline_H = aa_H * (xx - startbar_H) + bb_H;
// slope calculations for display purpose only
dtrendline_H = trendline_H - Ref(trendline_H,-1);

// upper extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_H_extend = ValueWhen(PHigh,H,2);
endval_H_extend = ValueWhen(PHigh,H,1);
startbar_H_extend = ValueWhen(PHigh,xx,2);
endbar_H_extend = ValueWhen(PHigh,xx,1);
aa_H_extend = (endval_H_extend-startval_H_extend)/(endbar_H_extend-startbar_H_extend);
bb_H_extend = startval_H;
trendline_H_extend = aa_H_extend * (xx - startbar_H) + endval_H_extend;
// slope calculations for display purpose only
dtrendline_H_extend = trendline_H_extend - Ref(trendline_H_extend,-1);
dtrendline_H_extend = IIf(PHigh,Ref(dtrendline_H,-1),dtrendline_H_extend);

// upper extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_H_extend2 = ValueWhen(PHigh,H,3);
endval_H_extend2 = ValueWhen(PHigh,H,2);
startbar_H_extend2 = ValueWhen(PHigh,xx,3);
endbar_H_extend2 = ValueWhen(PHigh,xx,2);
aa_H_extend2 = (endval_H_extend2-startval_H_extend2)/(endbar_H_extend2-startbar_H_extend2);
bb_H_extend2 = endval_H_extend2;
trendline_H_extend2 = aa_H_extend2 * (xx - endbar_H_extend2) + endval_H_extend2;
// slope calculations for display purpose only
dtrendline_H_extend2 = trendline_H_extend2 - Ref(trendline_H_extend2,-1);
dtrendline_H_extend2 = IIf(PHigh,Ref(dtrendline_H_extend,-1),dtrendline_H_extend2);

tld = ParamToggle("All trendlines","show|hide",1);
if (tld)
{
// omit uptrending upper trendlines and downtrending lower trendlines
trendline_L = IIf(dtrendline_L > 0,trendline_L,Null);
trendline_L_extend = IIf(dtrendline_L_extend > 0,trendline_L_extend,Null);
trendline_L_extend2 = IIf(dtrendline_L_extend2 > 0,trendline_L_extend2,Null);
trendline_H = IIf(dtrendline_H < 0,trendline_H,Null);
trendline_H_extend = IIf(dtrendline_H_extend < 0,trendline_H_extend,Null);
trendline_H_extend2 = IIf(dtrendline_H_extend2 < 0,trendline_H_extend2,Null);
}

trendline_L_extend2 = IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null);
trendline_H_extend2 = IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null);

// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
chartflag = ParamToggle("Heikin Ashi","show|hide",1);
if (chartFlag)
{
Plot( C, "\nPrice",colorWhite, styleCandle );
}
else
{
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 ) );
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Modified " + Name(), colorWhite, styleCandle );
}

//PlotShapes(shapeSmallCircle*PLow,colorGreen,0,L,-10);
//PlotShapes(shapeSmallCircle*PHigh,colorRed,0,H,10) ;
// segment 1
Plot(trendline_L, "", colorBrightGreen,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend,Null), "", colorLightGrey,styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null), "",colorDarkGreen, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null), "",colorDarkGreen,styleNoLine | styleDots| styleThick);
// segment 1
Plot(trendline_H, "", colorRed,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend,Null), "", colorLightGrey, styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null), "",colorOrange, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null), "",colorOrange, styleNoLine | styleDots | styleThick);

tsupp=(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null));
tresi=(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null));

Buy = Cross(tresi, C);
Short = C<O;
Sell = Cross(tsupp, C);
Cover = C>O;
Buy=ExRem(Buy,Sell);
Short=ExRem(Short,Cover);
Sell=ExRem(Sell,Buy);
Cover=ExRem(Cover,Short);

PlotShapes(Buy*shapeUpArrow,colorBlue,0,L,-45);
PlotShapes(Sell*shapeHollowDownArrow,colorPink,0,L,-45);
PlotShapes(Short*shapeDownArrow,colorRed,0,H,-45);
PlotShapes(Cover*shapeHollowUpArrow,colorSkyblue,0,H,-45);


_SECTION_END();
 

amitrandive

Well-Known Member
I am trying with below afl, still need to lot of modifications


_SECTION_BEGIN("SHARE CLINIC TREND LINE");

SetBarsRequired(sbrAll,sbrAll);
xx = Cum(1);
nbar = Param("nbar",5,2,50,1);

// define fractals
PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);
PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L);

// lower trendline, segment 1: between fractal points
startval_L = ValueWhen(PLow,L,1);
endval_L = ValueWhen(PLow,L,0);
startbar_L = ValueWhen(PLow,xx,1);
endbar_L = ValueWhen(PLow,xx,0);
aa_L = (endval_L-startval_L)/(endbar_L-startbar_L);
bb_L = startval_L;
trendline_L = aa_L * (xx - startbar_L) + bb_L;
// slope calculations for display purpose only
dtrendline_L = trendline_L - Ref(trendline_L,-1);

// lower extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_L_extend = ValueWhen(PLow,L,2);
endval_L_extend = ValueWhen(PLow,L,1);
startbar_L_extend = ValueWhen(PLow,xx,2);
endbar_L_extend = ValueWhen(PLow,xx,1);
aa_L_extend = (endval_L_extend-startval_L_extend)/(endbar_L_extend-startbar_L_extend);
bb_L_extend = startval_L;
trendline_L_extend = aa_L_extend * (xx - startbar_L) + endval_L_extend;
// slope calculations for display purpose only
dtrendline_L_extend = trendline_L_extend - Ref(trendline_L_extend,-1);
dtrendline_L_extend = IIf(PLow,Ref(dtrendline_L,-1),dtrendline_L_extend);

// lower extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_L_extend2 = ValueWhen(PLow,L,3);
endval_L_extend2 = ValueWhen(PLow,L,2);
startbar_L_extend2 = ValueWhen(PLow,xx,3);
endbar_L_extend2 = ValueWhen(PLow,xx,2);
aa_L_extend2 = (endval_L_extend2-startval_L_extend2)/(endbar_L_extend2-startbar_L_extend2);
bb_L_extend2 = endval_L_extend2;
trendline_L_extend2 = aa_L_extend2 * (xx - endbar_L_extend2) + endval_L_extend2;
// slope calculations for display purpose only
dtrendline_L_extend2 = trendline_L_extend2 - Ref(trendline_L_extend2,-1);
dtrendline_L_extend2 = IIf(PLow,Ref(dtrendline_L_extend,-1),dtrendline_L_extend2);

// upper trendline, segment 1: between fractal points
startval_H = ValueWhen(PHigh,H,1);
endval_H = ValueWhen(PHigh,H,0);
startbar_H = ValueWhen(PHigh,xx,1);
endbar_H = ValueWhen(PHigh,xx,0);
aa_H = (endval_H-startval_H)/(endbar_H-startbar_H);
bb_H = startval_H;
trendline_H = aa_H * (xx - startbar_H) + bb_H;
// slope calculations for display purpose only
dtrendline_H = trendline_H - Ref(trendline_H,-1);

// upper extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_H_extend = ValueWhen(PHigh,H,2);
endval_H_extend = ValueWhen(PHigh,H,1);
startbar_H_extend = ValueWhen(PHigh,xx,2);
endbar_H_extend = ValueWhen(PHigh,xx,1);
aa_H_extend = (endval_H_extend-startval_H_extend)/(endbar_H_extend-startbar_H_extend);
bb_H_extend = startval_H;
trendline_H_extend = aa_H_extend * (xx - startbar_H) + endval_H_extend;
// slope calculations for display purpose only
dtrendline_H_extend = trendline_H_extend - Ref(trendline_H_extend,-1);
dtrendline_H_extend = IIf(PHigh,Ref(dtrendline_H,-1),dtrendline_H_extend);

// upper extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_H_extend2 = ValueWhen(PHigh,H,3);
endval_H_extend2 = ValueWhen(PHigh,H,2);
startbar_H_extend2 = ValueWhen(PHigh,xx,3);
endbar_H_extend2 = ValueWhen(PHigh,xx,2);
aa_H_extend2 = (endval_H_extend2-startval_H_extend2)/(endbar_H_extend2-startbar_H_extend2);
bb_H_extend2 = endval_H_extend2;
trendline_H_extend2 = aa_H_extend2 * (xx - endbar_H_extend2) + endval_H_extend2;
// slope calculations for display purpose only
dtrendline_H_extend2 = trendline_H_extend2 - Ref(trendline_H_extend2,-1);
dtrendline_H_extend2 = IIf(PHigh,Ref(dtrendline_H_extend,-1),dtrendline_H_extend2);

tld = ParamToggle("All trendlines","show|hide",1);
if (tld)
{
// omit uptrending upper trendlines and downtrending lower trendlines
trendline_L = IIf(dtrendline_L > 0,trendline_L,Null);
trendline_L_extend = IIf(dtrendline_L_extend > 0,trendline_L_extend,Null);
trendline_L_extend2 = IIf(dtrendline_L_extend2 > 0,trendline_L_extend2,Null);
trendline_H = IIf(dtrendline_H < 0,trendline_H,Null);
trendline_H_extend = IIf(dtrendline_H_extend < 0,trendline_H_extend,Null);
trendline_H_extend2 = IIf(dtrendline_H_extend2 < 0,trendline_H_extend2,Null);
}

trendline_L_extend2 = IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null);
trendline_H_extend2 = IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null);

// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
chartflag = ParamToggle("Heikin Ashi","show|hide",1);
if (chartFlag)
{
Plot( C, "\nPrice",colorWhite, styleCandle );
}
else
{
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 ) );
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Modified " + Name(), colorWhite, styleCandle );
}

//PlotShapes(shapeSmallCircle*PLow,colorGreen,0,L,-10);
//PlotShapes(shapeSmallCircle*PHigh,colorRed,0,H,10) ;
// segment 1
Plot(trendline_L, "", colorBrightGreen,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend,Null), "", colorLightGrey,styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null), "",colorDarkGreen, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null), "",colorDarkGreen,styleNoLine | styleDots| styleThick);
// segment 1
Plot(trendline_H, "", colorRed,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend,Null), "", colorLightGrey, styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null), "",colorOrange, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null), "",colorOrange, styleNoLine | styleDots | styleThick);

tsupp=(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null));
tresi=(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null));

Buy = Cross(tresi, C);
Short = C<O;
Sell = Cross(tsupp, C);
Cover = C>O;
Buy=ExRem(Buy,Sell);
Short=ExRem(Short,Cover);
Sell=ExRem(Sell,Buy);
Cover=ExRem(Cover,Short);

PlotShapes(Buy*shapeUpArrow,colorBlue,0,L,-45);
PlotShapes(Sell*shapeHollowDownArrow,colorPink,0,L,-45);
PlotShapes(Short*shapeDownArrow,colorRed,0,H,-45);
PlotShapes(Cover*shapeHollowUpArrow,colorSkyblue,0,H,-45);


_SECTION_END();
Nehal_s143

Be careful , code looks into the future.Check with bar replay :thumb:
 

Similar threads