required backtesting simulator AFL......

amitrandive

Well-Known Member
#3
Skeleton of AFL given in the same blog.

//SetOption(“FuturesMode”, True );
//Range1 = Param( “Moving Average”, 6, 2, 100, 1 );
//Range1 = Optimize(“Range1?,43,2,100,1);//38 in 15' & 7 in 1'

//slowma = TSF( C, Range1 );
//Plot( slowma , “TSF (” + Range1 + “)”, colorRed, 1 );

////////////////////// HERE WE WRITE YOUR System SIGNALS /////////////////////
// below i wrote 4 condition for buy,sell to fiil them up if you wish …
//——————— condition Long
//CondL1 = Cross( C, slowma );
//CondL2 = 0;
//CondL3 = 0;
//CondL4 = 0;
//——————— condition SELL
//CondSL1 = Cross( slowma , C );//H = 153000;
CondSL2 = 0;
CondSL3 = 0;
CondSL4 = 0;
//——————— condition short
//CondS1 = Cross( slowma , C );
CondS2 = 0;
CondS3 = 0;
CondS4 = 0;
//——————— condition Cover
//CondC1 = Cross( C, slowma );// L > Ref( L, -1 ) OR TimeNum() >= 153000;
CondC2 = 0;
CondC3 = 0;
CondC4 = 0;

//——–AND THE FINAL —BUY / SHORT WE HAVE TO WRITE BELOW HERE—–
// for EXAMPLE BUYOK= xyzCondL1= abc OR CondL5=pqr AND CondL9=zxc AND NOT CondL13=qwe;
// Buy = Ref(Buy,-1); BuyPrice = Open; //DECLARE THE”Open” OF THE NEXT BAR
//buyOK = ( Ref( CondL1, -1 ) ) ;//OR (Ref(CondL3,-1)) (Ref(CondL1,-1)) OR (Ref(CondL2,-1)) OR (Ref(CondL3,-1)) OR
//SellOK = Ref( CondSL1, -1 );
//ShortOK = ( Ref( CondS1, -1 ) ) ;//OR (Ref(CondS3,-1)) (Ref(CondS1,-1)) OR (Ref(CondS2,-1)) OR (Ref(CondS3,-1)) OR
//CoverOK = Ref( CondC1, -1 );

// ——– DO NOT TOUCH BELOW HERE IF YOU NOT KN/OW/ WHAT ARE YOU DOING ———————-
//——————— trading hours 91500 to 153000
//TradeTime = ParamToggle( ” backTest with Time?”, “No|Yes”, 0 ) ;
//StartTime = ParamTime( “Start Time”, “8:00? );
//EndTime = ParamTime( “End Time”, “21:40? );
//EA = TimeNum() >= starttime AND TimeNum() = 080000 AND TimeNum() <= 214000; // <<<<<< 0, colorBrightGreen, colorRed );
//ColorShortProfit = IIf( ShortProfit > 0, colorBrightGreen, colorRed );

//SetOption( “NoDefaultColumns”, True );
//AddTextColumn( Name(), “Symbol”, 77, 1, colorDefault, 70 );
AddColumn( DateTime(), “Date”, formatDateTime, 1, colorDefault, 160 );
// these 5 linea are for Long trades
AddColumn( IIf( Buy, Asc( “B”, 0 ), Asc( “S” ) ), “PosL”, formatChar );
AddColumn( Buy, “Buy”, 1 );
AddColumn( IIf( Buy, lastpriceBuy, Null ) , “Buy”, 1.2, colorDefault, colorDefault, 80 );
AddColumn( IIf( Sell, lastpriceSELL, Null ), “Sell”, 1.2, colorDefault, colorDefault, 80 );
AddColumn( a1 = IIf( Sell, ( lastpriceSELL – lastpriceBuy ), Null ), “Long-Pip”, 1.4, colorDefault, colorBuyProfit , 60 );
AddColumn( a2 = IIf( Sell, ( ( lastpriceSELL – lastpriceBuy ) / lastpriceBuy * 100 ), Null ), “Long %”, 1.2, colorDefault, colorBuyProfit , 60 );

// these 5 linea are for Short trades
AddColumn( IIf( Short, Asc( “S”, 0 ), Asc( “C” ) ), “PosS”, formatChar );
AddColumn( Sell, “Short”, 1 );
AddColumn( IIf( Short, lastpriceShort, Null ) , “Short”, 1.2, colorDefault, colorDefault, 80 );
AddColumn( IIf( Cover, lastpriceCover, Null ), “Cover”, 1.2, colorDefault, colorDefault, 80 );
AddColumn( b1 = IIf( Cover, ( lastpriceshort – lastpriceCover ), Null ), “ShortPip”, 1.4, colorDefault, ColorShortProfit , 60 );
AddColumn( b2 = IIf( Cover, ( ( lastpriceshort – lastpriceCover ) / lastpriceshort * 100 ), Null ), “Short %”, 1.2, colorDefault, ColorShortProfit , 60 );

a3 = IIf( Sell, a1, IIf( Cover, b1 , Null ) ); // tell me the points of every trade
a4 = IIf( Sell, a2, IIf( Cover, b2 , Null ) ); // tell me the % of every trade
Mycolor = IIf( a3 > 0 , colorGreen, colorRed ); //is it correct??????no ap3ut it after a3 line this line
AddColumn( a3, “Gain/Loss”, 1.4, Mycolor, colorBuyProfit , 80 );
AddColumn( a4, “Gain/Loss % “, 1.2, Mycolor, colorBuyProfit , 100 );
// contains wining trades of every 2 lines

// added from post START
Diffsell = ( ValueWhen( Sell, SellPrice ) – ValueWhen( Buy, BuyPrice ) ) / TickSize;

Diffcov = ( ValueWhen( Cover, CoverPrice ) – ValueWhen( Short, ShortPrice ) ) / TickSize;

abschg = IIf( Sell, Diffsell, IIf( Cover, Diffcov, 0 ) );

cumticks = Cum( abschg );

AddColumn( cumticks, “CumTick”, 1, colorDefault, colorDefault, 65 );

AddColumn( cumticks * PointValue, “Profit”, 1, colorDefault, colorDefault, 65 );

// Filter = Sell || Cover;

profit = IIf( Sell, ValueWhen( Sell, eq ) – ValueWhen( Buy, eq ), IIf( Cover, ValueWhen( Cover, eq ) – ValueWhen( Short, eq ), 0 ) );

cumprof = Cum( profit );

AddColumn( cumprof , “Cumul Profit”, 1, colorDefault, colorDefault, 65 );
// added from post END

//AddColumn(a3,”Gain/Loss”,1.4,colorDefault,colorBuyProfit ,80); // contains wining trades of every 2 lines
// all above are corect for gia BUY SELL sort cover

AddSummaryRows( 1, 1.4, 4, 7, 9, 12, 13, 15 );
} // end actionExplore

//——————— appearance of various signals of long / short in the chart
if ( Status( “Action” ) == actionIndicator )
{
SetChartBkColor( ParamColor( “backround “, colorBlack ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
strWeekday = StrMid( “SunMonTueWedThuFriSat”, SelectedValue( DayOfWeek() ) * 3, 3 );
Title1 = Name() + “,” + Date() + “,” + ” TimeFrame: ” + Interval( 2 ) + “\n Open ” + O + “, High ” + H + “, Low ” + L + “, Close ” + C + “, ” + “(” + WriteVal( ROC( C, 1 ), 1.2 ) + “%)” ;

Display_Bars = ParamToggle( “Display Bars”, “No|Yes”, 1 );

if ( Display_Bars )
Plot( C, “” , colorWhite, styleBar );

if ( TradeTime )
Plot( IIf( EA, 6, -1e10 ), “”, colorBlueGrey , styleOwnScale | styleArea | styleNoLabel, -1, 90 );

////////////////////// Arrows
belakiaBS = ParamToggle( “Arrows Buy-Sell”, “HIDE|SHOW”, 1 );

if ( belakiaBS )
{
PlotShapes( IIf ( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, -30 );
PlotShapes( IIf ( Buy, shapeSmallCircle, shapeNone ), colorGreen, 0, O, 0 );
PlotShapes( IIf ( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -40 );
PlotShapes( IIf ( Sell, shapeSmallCircle, shapeNone ), colorRed, 0, O, 0 );
}

belakiaSC = ParamToggle( “Arrows Short-Cover”, “HIDE|SHOW”, 1 );

if ( belakiaSC )
{
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorRed, 0, H, -30 );
PlotShapes( IIf ( Short, shapeSmallCircle, shapeNone ), colorRed, 0, O, 0 );
PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorOrange, 0, L, -40 );
PlotShapes( IIf ( Cover, shapeSmallCircle, shapeNone ), colorOrange, 0, O, 0 );
}

////////////////////// Sum Total buy and short positions//YOU MAY OMIT THIS SECTION,BECAUSE THIS SEEMS TO BE APPLICABLE TO NIFTY INDEX.

// Value of one lot of Nifty is approx. Rs.250,000 for which Rs.25000 margin is required.
// commission for 1 lot=Rs250 Stop=12
TotalPosBuy = Cum( Buy );

x1 = Cum( BuyProfit );

x2 = Sum( ShortProfit, 98 );

NumContracts = Param( “NumContracts “, 1, 1, 100, 1 );

Commission = TickSize * Param( “Commission”, 250, 0, 450, 1 ); // 250 per lot

TotalCommisionTrade = TotalPosBuy * NumContracts * Commission ;

TitleSynolo = ” Entry Long Trades = ” + TotalPosBuy + “\n Num Contracts ” + NumContracts + “\nTotal Commision Of Long Trades RS = ” + TotalCommisionTrade +
“\n”;

EventNum = TotalPosBuy;

FirstVisibleBar = Status( “FirstVisibleBar” );

Lastvisiblebar = Status( “LastVisibleBar” );

for ( b = Firstvisiblebar; b < Lastvisiblebar AND b 0, colorGreen, colorRed );
ColorShortProfit = IIf( ShortProfit > 0, colorGreen, colorRed );
////////////////////// PlotText Total Pos Buy appear
TextEventNum = ParamToggle( “Event TotalPosBuy”, “HIDE|SHOW”, 0 );

if ( TextEventNum )
{
if ( Buy )
PlotText( “\n\n\n\nEvent\n” + NumToStr( EventNum, 1.0, False ), b, L, colorWhite );

if ( Sell )
PlotText( “Event\n” + NumToStr( EventNum, 1.0, False ), b, H, colorWhite );
}

////////////////////// PlotText we appear gainers / lossers
TextPipsBS = ParamToggle( “Plot Buy Pips”, “HIDE|SHOW”, 0 );

if ( TextPipsBS )
{
if ( Buy )
PlotText( “Buy\n\n” + lastpriceBuy , b, L – dist, colorBrightGreen ); //ColorShortProfit

if ( Sell )
PlotText( “Sell\n” + lastpriceSELL + “\n” + WriteVal( BuyProfit , 1.4 ) , b, H + dist, colorBlack, colorBuyProfit );
}

TextPipsSC = ParamToggle( “Plot Short Pips”, “HIDE|SHOW”, 0 );

if ( TextPipsSC )
{
if ( Short )
PlotText( “\n\n\nShort\n” + lastpriceShort, b, H + dist, colorOrange );

if ( Cover )
PlotText( “\n\n\nCover\n” + lastpriceCover + “\n” + WriteVal( ShortProfit , 1.4 ), b, L – dist, colorBlack, ColorShortProfit );
}

///////// END ///////////// PlotText we appear gainers / lossers

////////////////////// PlotText we appear the Time of the Trade
myDateTime = DateTime();

TextDateOnChart = ParamToggle( “Plot Text Date Time”, “SHOW TextDate|HIDE TextDate”, 0 );

if ( TextDateOnChart )
{
if ( Buy )
PlotText( “\n\n\n” + NumToStr( myDateTime, formatDateTime ) + “”, b, H[ b ] – dist, colorLime );

if ( Sell )
PlotText( “\n\n\n” + NumToStr( myDateTime, formatDateTime ) + “” , b, L[ b ] + dist, colorLime );
}

//////// END ////////////// PlotText we appear the Time of the Trade

} // end FirstVisibleBar

Eq = Equity( 1 );

if ( ParamToggle( “Equity”, “HIDE|SHOW”, 0 ) )
Plot( Eq, “”, colorYellow, 1 | styleOwnScale );

Title = ” # ” + strWeekday + ” # ” + Title1 + TitleTradeTime + “\n\n” + TitleSynolo ;

} // end actionIndicator

_SECTION_END();////////////////////// Sum Total buy AND Short positions

// Value of one lot of Nifty is approx.Rs.250,000 for which Rs.25000 margin is required.

// commission for 1 lot=Rs250; Stop=12

TotalPosBuy = Cum( Buy );

x1 = Cum( BuyProfit );

x2 = Sum( ShortProfit, 98 );

NumContracts = Param( “NumContracts “, 1, 1, 100, 1 );

Commission = TickSize * Param( “Commission”, 250, 1, 450, 1 ); // 250 per lot

SumCommisionBuyTrades = TotalPosBuy * NumContracts * Commission ;

TotalPosShort = Cum( Short );

SumCommisionShortTrades = TotalPosShort * NumContracts * Commission ;

TotalCommisionTrade = 0;

TitleSynolo = ” Entry Long Trades = ” + TotalPosBuy + ” Entry
Short Trades = ” + TotalPosShort + “\n Num Contracts ” + NumContracts
+ “\nTotal Commision Of Long
Trades RS = ” + SumCommisionBuyTrades +

“\nTotal Commision Of Short
Trades RS = ” + SumCommisionShortTrades
+

“\n”;

/*
///////////////////////////////pop up stuff ///////////////////////////////
// Call the AFL from Include folder FIRst
#include_once
// samble PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
// samble2 PopupWindowEx( “ID:1?, “testing”, “nomber of alert “, 5, -1, -1 );
LastClose= LastValue(C); // if you like to add this popup will show you

if (LastValue(BuyOK )) PopupWindowEx( ” BUY ID:1?, “Get Ready to BUY \n”+Name() + ” “+ Interval(2)+” : “+ ” Last =”+LastClose , “Buy Alert -”, 20, 100, 1 ) ; //appear for 20 seconts
PlaySound(“c:\\windows\\media\\ding.wav”);

if (LastValue(ShortOK )) PopupWindowEx( ” SHORT ID:2?, “Get Ready to SHORT \n”+Name() + ” “+ Interval(2) + ” : “+ ” Last =”+LastClose , “Short Alert “, 20, 1, 150 ) ;

PlaySound(“c:\\windows\\media\\ding.wav”);
 

Similar threads