Simple Coding Help - No Promise.

casoni

Well-Known Member
Yes I got it Cond 3 and cond 6 is for future reference.

Now I made some changes in above mentioned formula. The new afl is as;

_SECTION_BEGIN("MACD Exploration");
r1 = Param( "Fast avg", 66, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );
Z=Param("zig",1,0,10,0.1);


Cond1 = Cross(MACD(r1,r2),Signal(r1,r2,r3));

Cond3 = Zig(C,z)>Ref(Zig(C,z),-4);
Buy = Cover = Cond1 AND Cond3;

Cond4 = Cross(Signal(r1,r2,r3),MACD(r1,r2));

Cond6 = Zig(C,z)<Ref(Zig(C,z),-4);
Sell = Short = Cond4 AND Cond6;

Now the signal is generating on the chart is not future based and the backtesting results not according the chart.

Now how to get accurate results in backtesting ?
No..
Zig function itself looks into future
Go to Analysis > Code check and Profile , you will get the answer ..
 
//originally Posted by tools&tradingideas [ Edward.Pottasch ]
function vstop_func(tr)
{
trailArray[ 0 ] = C[ 0 ]; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = trailArray[ i - 1 ];

if (C[ i ] > prev AND C[ i - 1 ] > prev)
{
trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
}
else if (C[ i ] < prev AND C[ i - 1 ] < prev)
{
trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
}
else if (C[ i ] > prev)
{
trailArray[ i ] = C[ i ] - tr[ i ];
}
else
{
trailArray[ i ] = C[ i ] + tr[ i ];
}
}
return trailArray;
}

per = Param("per",20, 5, 150, 1);
mult = Param("mult",2, 1, 4, 0.05);
tr = mult * ATR(per);
trailArray = vstop_func(tr);
//trailArray = Ref(trailArray,-1);

SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(IIf(trailArray > C,trailArray,Null),"\ntrailShort",ParamColor("Colo rTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("Color TrailLong",ColorRGB(0,255,0)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );

// added
xx=EMA(C,34);
Plot(xx,"Ema34:",3,1);

//----------- 1
Buy1 = C>xx AND Cross(C,trailarray);
Sell1 = C<xx AND Cross(trailarray,C);
PlotShapes(Buy1*shapesmallUpTriangle+Sell1*shapesmallDownTriangle,IIf(Buy1,colorBlue,colorYellow),0,IIf(Buy1,L,H)) ;

//------------ 2
Buy2 = C>xx AND C>trailarray;
Sell2 = C<xx AND C<trailarray;
buy2=ExRem(buy2,sell2);
sell2=ExRem(sell2,buy2);
PlotShapes(Buy2*shapeUparrow+Sell2*shapeDownarrow,IIf(Buy2,colorgreen,colorred),0,IIf(Buy2,L-30,H+30)) ;

//------------ 3
Buy3 = Cross(C,trailarray) AND Cross(C,xx);
Sell3 = Cross(trailarray,C) AND Cross(xx,C);
PlotShapes(Buy3*shapeuparrow+Sell3*shapeDownarrow,IIf(Buy3,colorpalegreen,colororange),0,IIf(Buy3,L-40,H+40)) ;

Thank you
For backtesting this strategy on BankNifty, i want to add the following conditions. Request your help on this.

1. When buy or sell has been triggered, initial SL should be set at 50 points.
2. Then SL should be moved by every 20 points if there is rise of 100 points for both long and short in banknifty
3. Exit the trade if the full candle (i.e open and closed) is closed below EMA34 or SL is hit.
4. Currently signals are getting generated based on open / close price. Signals should be generated based on high / low and NOT open / close.

===========================================

Following is the condition i am using for generating Buy/Sell signals. Also, is there a way of sending orders to Zerodha Trader when the signals are generated in Amibroker?

Buy2 = O>xx AND C>xx AND C>trailarray;
Sell2 = O<xx AND C<xx AND C<trailarray;
buy2=ExRem(buy2,sell2);
sell2=ExRem(sell2,buy2);

Buy=Cover=Buy2;
Sell=Short=Sell2;

PlotShapes(Buy2*shapeUpTriangle+Sell2*shapeDownTriangle, IIf(Buy2,colorBlue,colorYellow),0,IIf(Buy2,L-30,H+30)) ;
================================================

Thanks in advance..
 
Last edited:
No..
Zig function itself looks into future
Go to Analysis > Code check and Profile , you will get the answer ..
Ok, I have removed the Zig function from the AFL now the modified afl is as follows;

_SECTION_BEGIN("MACD Exploration");
r1 = Param( "Fast avg", 66, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

Cond1 = Cross(MACD(r1,r2),Signal(r1,r2,r3));
Buy = Cover = Cond1;

Cond2 = Cross(Signal(r1,r2,r3),MACD(r1,r2));
Sell = Short = Cond2;

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



Trigger = WriteIf(Buy, "Buy", "") + WriteIf(Sell, "Sell", "");

_N(Title = StrFormat("{{NAME}} {{DATE}} {{INTERVAL}}: O=%1.2f, H=%1.2f, L=%1.2f, C=%1.2f, V=%1.0f\n{{VALUES}}", O, H, L, C, V));

BG = IIf(Buy, colorPaleGreen, IIf(Sell, colorRose, colorDefault));
FG = IIf(Buy, colorDarkGreen, IIf(Sell, colorDarkRed, colorDefault));

if(Status("action") == actionIndicator);
{
Plot(C, "", colorGrey50, styleBar);
PlotShapes(IIf(Buy, shapeCircle, shapeNone),colorGreen, 0,L, Offset=-30);
PlotShapes(IIf(Sell, shapeCircle, shapeNone),colorRed, 0,H, Offset=30);
PlotShapes(IIf(Buy, shapeHollowUpArrow, Null), colorDarkGreen, 0,L,Offset=-20);
PlotShapes(IIf(Sell, shapeHollowDownArrow, Null), colorDarkRed, 0,L,Offset=-20);
}

But the Problem is same, signal is not coming in the backtesting as generated on the chart.

Now how to modify the AFl for matching the results ?

Please help.
 

cellclinic

Well-Known Member
Hello Friends ...

Kindly help me out once more by modifying attached code ...

1) thick line black
2) horizontal line @ 0 level
3) buy sell signal when line goes above or below 0 line .

HTML:
_SECTION_BEGIN("HPI");
function HPI( MultFactor, PercentMove )
{
 M = ( H + L ) /2;
 My = Ref( M, -1 );

 I = abs( OpenInt - Ref( OpenInt, -1 ) );
 G = Max( OpenInt, Ref( OpenInt, -1 ) );
 
 K = PercentMove * Volume * ( M - My ) * ( 1  + sign( M - My ) * 2 * I / G );

 //return AMA2( K, MultFactor / 100000, ( 1 - MultFactor ) / 100000 );
 
 return AMA( K, 1/MultFactor )/100000;  	


}

OI = C;

Plot( HPI( 20, 100 ), _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );
_SECTION_END();
 
Friends - In this Opening Range breakout afl not always signals are generated pls help to correct the mistake in the AFL.
Thanks in advance.

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 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

_SECTION_END();


breakoutime = 093000;

afterbreakout0 = Cross(TimeNum(),093000);
afterbreakout1 = TimeNum()>=093000;

NewDay = Day()!= Ref(Day(), -1);

highestoftheday = HighestSince(Newday,H,1);
Lowestoftheday = LowestSince(Newday,L,1);

ORBHigh = ValueWhen(afterbreakout0,highestoftheday,1);
ORBLow = ValueWhen(afterbreakout0,lowestoftheday,1);

Plot(C,"",colorGreen,styleCandle);
Range = ORBHigh - ORBLow;

TimeFrameSet(in1Minute);

Buy = Cross(C,orbhigh) AND afterbreakout1;
Sell = Cross(orblow,C) AND afterbreakout1;

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

Short=Cross(C,ORblow) AND afterbreakout1;
Cover=Cross(ORbhigh,C) AND afterbreakout1;

PlotShapes( shapeUpArrow * Buy, colorGreen,0,L,-12);
PlotShapes( shapeDownArrow * Sell, colorRed,0,H,-12);

//Plot(afterbreakout0,"",colorBlue,styleHistogram|styleOwnScale);

Plot(ORBHigh,"",colorGreen,styleDots);
Plot(ORBLow,"",colorRed,styleDots);
 

casoni

Well-Known Member
Hello Friends ...

Kindly help me out once more by modifying attached code ...

1) thick line black
2) horizontal line @ 0 level
3) buy sell signal when line goes above or below 0 line .

HTML:
_SECTION_BEGIN("HPI");
function HPI( MultFactor, PercentMove )
{
 M = ( H + L ) /2;
 My = Ref( M, -1 );

 I = abs( OpenInt - Ref( OpenInt, -1 ) );
 G = Max( OpenInt, Ref( OpenInt, -1 ) );
 
 K = PercentMove * Volume * ( M - My ) * ( 1  + sign( M - My ) * 2 * I / G );

 //return AMA2( K, MultFactor / 100000, ( 1 - MultFactor ) / 100000 );
 
 return AMA( K, 1/MultFactor )/100000;  	


}

OI = C;

Plot( HPI( 20, 100 ), _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );
_SECTION_END();
plot(0,"",31,32);
buy=cross(HPI( 20, 100 ),0);
sell=cross(0,HPI( 20, 100 ));
plotshapes(buy*1,5,0,0,0);
plotshapes(sell*2,4,0,0,0);
Plot( HPI( 20, 100 ), _DEFAULT_NAME(), COLORBLACK,STYLETHICK );
 

Riskyman

Well-Known Member
Can anyone help here please?

First calculate/plot 20 period EMA. Then plot an upper and lower band which is offset 2% from the Ema.

Upper band = Ema+(20th candle close*2%)
Lower band = Ema -(20th candle close*2%)


These are not bollinger bands in anyway. These are simple STATIC bands that track the EMA i.e they always maintain the same distance from the ema line. They dont converge/diverge like BB

Thanks.
 

casoni

Well-Known Member
Can anyone help here please?

First calculate/plot 20 period EMA. Then plot an upper and lower band which is offset 2% from the Ema.

Upper band = Ema+(20th candle close*2%)
Lower band = Ema -(20th candle close*2%)


These are not bollinger bands in anyway. These are simple STATIC bands that track the EMA i.e they always maintain the same distance from the ema line. They dont converge/diverge like BB

Thanks.
Hello ;
its built in , check " Percent Bands "
 

Riskyman

Well-Known Member
Can anyone help here please?

First calculate/plot 20 period EMA. Then plot an upper and lower band which is offset 2% from the Ema.

Upper band = Ema+(20th candle close*2%)
Lower band = Ema -(20th candle close*2%)


These are not bollinger bands in anyway. These are simple STATIC bands that track the EMA i.e they always maintain the same distance from the ema line. They dont converge/diverge like BB

Thanks.

I found an Afl for this. SO please dont take trouble to write a code. Thanks a lot
 

Similar threads