Simple Coding Help - No Promise.

labdhi

Active Member
wow !! , a very good thread , a great learning base for afl coding ,
thanks happy ji for starting this awesome thread ........also for pratap ji for continuning it ,

sir i finalisied my system , now i want to backtest it on ami ,
trade with 2 lots always , initial equity 200,000 rs ,script only banknifty-1 , commissions shd be deducted,
what backtest code is to be added in my afl ,
also in setting window in amibroker in analysis window ,
there r options
future mode = ?
round lot size = ?
commissions=?
account margins = ?

what values r to be done in this window ?
also is there any other option , which we need to consider while backtesting ?
sorry if questions r too many or funny , but i am totally novice in terms of backtesting ,
plz sir write a code or sir simply state a process to backtest my trading idea ..........
senior's , any link for understanding backtest fundas in amibroker ......
this is basic thing that's why senior's r not answering , but if possible plz share some links where i get more information , as i am totally novice in ami ....want to test my ideas with slight variation , as by testing bar to bar all ideas is not possible in 10-15 m tf ...........
 

dell

Well-Known Member
MACDcolor=IIf(a1b1>a1b1c1,colorGreen,IIf(a1b1c1>a1b1,colorOrange,colorDarkGrey));
Plot(10, "", macdcolor, styleNoLabel | styleOwnScale | styleArea, -1,20);

senior's,...............this is ribbon code , if we want to write " macd " on ribbon itself , than what is to be added in code , want to write on it as i have 5 ribbons , so not to get confused , i want to write name on ribbon itself .........is it possible in upper mentioned afl ?
 
Please ignore this message i have found the solution :) I have used TimeNum function

Hi

I am using Open Range Breakout.afl for MCX which marks high low of 10.00 am to 10.15 am and plots signal on either side breakout

I want this signal to be plotted only for next 30 min i.e till 10.45 am, if price crosses opening range after 10.45 am anytime during the day signal should not come, but afl should show range yellow lines till close of market, as opening range lines acts good support and resistance

Please help
 

ethan hunt

Well-Known Member
Hello Ethan

Maybe not many are interested in spending time on it as it is a very basic system and if one searches can be freely found on internet.

I am posting a basic implementation of it . . . have not coded for alerts and popups . . .

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

_SECTION_BEGIN("MA Xross Sys");
//Optimization parameters for MA
P1 = Optimize("Period 1",Param("Period 1", 20, 2, 300, 1), 10, 300, 10);
P2 = Optimize("Period 2",Param("Period 2", 60, 2, 300, 1), 20, 300, 10);
A=MA(Close,P1);		B=MA(Close,P2);

//Plot MAs
Plot(A,"\nMA1",ParamColor("Color1", colorGreen),ParamStyle("Style1") ); 
Plot(B,"MA2", ParamColor("Color2", colorYellow), ParamStyle("Style2") ); 

//MA-Cross System for Backtest 
Buy = Cross(A,B); 		Sell = Cross(B,A);
Buy = ExRem(Buy,Sell);	Sell = ExRem(Sell,Buy);
Short = Sell; 			Cover = Buy;
UP = Flip(Buy,Sell);		DN = Flip(Sell,Buy);
//Arrows
PlotShapes(Buy+2*Sell, IIf(Buy,colorGreen,colorRed), 0,IIf(Buy,L,H));

//Trade Action on next bar open after MA-Cross
SetTradeDelays(1, 1, 1, 1); 
BuyPrice   = Open;	SellPrice  = Open;
CoverPrice = Open;	ShortPrice = Open;

//Position sizing
SetPositionSize(1,4);

//Stops
ApplyStop(0,2,25); // Stop Loss
ApplyStop(1,2,50); //Profit Stop

//Plots for TGT & Stops
Long  = ValueWhen(Ref(Buy,-1),O);
Shrt = ValueWhen(Ref(Sell,-1),O);

Plot(IIf(UP,Long,Shrt),"ENTRY_PRICE",colorLightGrey,styleStaircase|styleNoRescale|styleDashed);

Plot(IIf(UP,Long+50,Null),"\nLong_TGT",colorBlueGrey,styleStaircase|styleNoRescale);
Plot(IIf(UP,Long-25,Null),"Long_SL",   colorLime,    styleStaircase|styleNoRescale);

Plot(IIf(DN,Shrt-50,Null),"\nShort_TGT",colorRed, styleStaircase|styleNoRescale);
Plot(IIf(DN,Shrt+25,Null),"Short_SL",   colorBlue,styleStaircase|styleNoRescale|styleDashed);

/*
Plot(SelectedValue(IIf(UP,Long-25,Null)),"Long_SL",colorRed,styleStaircase|styleNoRescale,0,0,10);
Plot(SelectedValue(IIf(UP,Long+50,Null)),"Long_TGT",colorBlue,styleStaircase|styleNoRescale,0,0,10);
Plot(SelectedValue(IIf(DN,Shrt+25,Null)),"Short_SL",colorBlue,styleStaircase,0,0,10);
Plot(SelectedValue(IIf(DN,Shrt-50,Null)),"Short_TGT",colorRed,styleStaircase,0,0,10);
*/

//exploration
Filter = Buy OR Sell;
_SECTION_END();
I have implemented the basic functionality and commented the code, as the spirit of this thread is learning to do your own code . . .
now one can easily modify this template and use it for other entry / exit strategies.


:) Happy
thanks ...
 
Code:
//Initialization
MO[0] = Open[0];
MC[0] = Close[0];
for(i = 1; i < BarCount; i++ ) 
{
	MO[i] = (MO[i-1]+MC[i-1])/2;
	MC[i] = (MO[i-1]+High[i]+Low[i]+Close[i])/4;
}
This is what I have coded, will the results be the same or should I change it to what you have coded?

MO[0] = O[0]+C[0]/2;
MC[0] = MO[0]+H[0]+L[0]+C[0]/4;

for (i=1; i<BarCount; i++)
{
MC = (MO[i-1]+H+L+Close)/4;
MO = (MC[i-1] + MO[i-1])/2;
}
 

pratapvb

Well-Known Member
This is what I have coded, will the results be the same or should I change it to what you have coded?

MO[0] = O[0]+C[0]/2;
MC[0] = MO[0]+H[0]+L[0]+C[0]/4;

for (i=1; i<BarCount; i++)
{
MC = (MO[i-1]+H+L+Close)/4;
MO = (MC[i-1] + MO[i-1])/2;
}


this initialization maybe better....atleast for MC....for MO[0] it could be just O[0] or as show by you, as for MO it is dependined on previous bar values


but you need ( ) around the statements....

like
(......) / 2
(......) / 4

else only the last term will be divided by 2 or 4
 
I have buy, sell, short & cover values. I want to display it in the title along with green or red color, which would change based on the values being hit or not. Please help with coding this. I don't know if the code below is correct or not but it would give an idea of what am trying to achieve.

Title = Name()+" "+Date()+" MHA -- MO:"+NumToStr(MO,1.2,True)+" / MH:"+NumToStr(MH,1.2,True)+" / ML:"+NumToStr(ML,1.2,True)+" / MC:"+NumToStr(MC,1.2, True)+"
Buy:"+iif(H>Long,encodecolor(colorgreen),encodecolor(colorred))+numtostr(Long,1.2,True)+" Sell:"+ iif(L<Longstop,encodecolor(colorred),encodecolor(colorgreen))+numtostr(Longstop,1.2,True)+
" Short:"+IIf(L<Sllshrt,EncodeColor(colorRed),EncodeColor(colorGreen))+NumToStr(Sllshrt,1.2,True)+" Cover:"+IIf(H>Shortstop,EncodeColor(colorGreen),EncodeColor(colorRed))+NumToStr(Shortstop,1.2,True)+" Lg Tgt:"+NumToStr(Longtarget,1.2,True)+" Sh Tgt:"+NumToStr(Shorttarget,1.2,True);
 

amitrandive

Well-Known Member
I need an exploration to buy a stock/index when price crosses 50% of yesterday's range upward and sell when price crosses 50% of yesterday's range downward.
 

Nehal_s143

Well-Known Member
hi

I am using a afl which has limitation of max 4 days, I want to use it for 8 days, if I use 8 days period i am getting error says too Many arguments or value is coming wrong

Please help me to modify afl for 8 days instead of 4 days

Modified AFL
==========

_SECTION_BEGIN("8 Day Pivot Range");
Plot( C, "Close", ParamColor("Color", colorLavender ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
TDC=TimeFrameGetPrice( "C", inDaily, -1);
dh1=TimeFrameGetPrice( "H", inDaily, -1);
dh2=TimeFrameGetPrice( "H", inDaily, -2);
dh3=TimeFrameGetPrice( "H", inDaily, -3);
dh4=TimeFrameGetPrice( "H", inDaily, -4);
dh5=TimeFrameGetPrice( "H", inDaily, -5);
dh6=TimeFrameGetPrice( "H", inDaily, -6);
dh7=TimeFrameGetPrice( "H", inDaily, -7);
dh8=TimeFrameGetPrice( "H", inDaily, -8);
dl1=TimeFrameGetPrice( "l", inDaily, -1);
dl2=TimeFrameGetPrice( "l", inDaily, -2);
dl3=TimeFrameGetPrice( "l", inDaily, -3);
dl4=TimeFrameGetPrice( "l", inDaily, -4);
dl5=TimeFrameGetPrice( "l", inDaily, -5);
dl6=TimeFrameGetPrice( "l", inDaily, -6);
dl7=TimeFrameGetPrice( "l", inDaily, -7);
dl8=TimeFrameGetPrice( "l", inDaily, -8);

Maxh1=Max(dh1,dh2);
Maxh2=Max(dh3,dh4);
Maxh3=Max(dh5,dh6);
Maxh4=Max(dh7,dh8);

Maxh=Max(Maxh1,Maxh2);

//error
//if used
//Maxh=Max(Maxh1,Maxh2,Maxh3,Maxh4);// error says too MAny arguments

//OR
//error
//if used
//Maxh=Max(Maxh1,Maxh2) AND Max(Maxh3,Maxh4); // error is value is wrong

Minl1=Min(dl1,dl2);
Minl2=Min(dl3,dl4);
Minl3=Min(dl5,dl6);
Minl4=Min(dl7,dl8);

Minl=Min(Minl1,Minl2);


TDPP = (Maxh + Minl + TDC)/3;
PP2 = (Maxh + Minl)/2;
Dif = abs(TDPP-PP2);
PPU = TDPP + Dif;
PPL = TDPP - Dif;

Plot (TDPP,"TDPR",colorYellow,1);
Plot(PPU, "",colorYellow,styleDashed);
Plot(PPL, "",colorYellow,styleDashed);


Title = EncodeColor(colorWhite)+ "" + "" + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorBlue) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+" 3dh= "+WriteVal(Maxh)+" 3dL= "+WriteVal(Minl);

mycfrh=IIf(C>PPU,colorBlue,
IIf(C<PPL,colorRed,//31));
IIf(C<PPU AND C>PPL ,colorYellow,31)));

Plot(6, "", myCfrh, styleOwnScale| styleArea|styleNoLabel,-0.5,100);

_SECTION_END();
 

Similar threads