Australian (ASX) Stock Market Forum

Fix the bug...

Joined
28 September 2012
Posts
9
Reactions
0
Hi

Following code is custom backtester.

It returns wrong results for short trades.Pl. tweak the code//--------AND THE FINAL ---BUY / SHORT WE HAVE TO WRITE BELOW HERE-----
// for EXAMPLE BUYOK= CondL1=SEXE OR CondL5=NB8 AND CondL9=NUp1 AND NOT CondL13=bex;
// 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", "9:30" );
EndTime = ParamTime( "End Time", "15:15" );
EA = TimeNum() >= starttime AND TimeNum() <= endtime;
EA = TimeNum() >= 093000 AND TimeNum() <= 151500; // <<<<<<< ?????? time is fixed

if (TradeTime) {
//--------------------- Buy, Sell, Short, Cover, Here is with time
Buy=buyOK AND EA ; BuyPrice = Open;
Sell=SellOK OR EndTime ; SellPrice = Close;
Short= ShortOK AND EA; ShortPrice = Open;
Cover=CoverOK OR EndTime ; CoverPrice = Close;
TitleTradeTime= "\nBacktest Trade With Time = ON";
}
else {
//--------------------- Buy, Sell, Short, Cover, Here is without time
Buy=buyOK ; BuyPrice = Open;
Sell=SellOK ; SellPrice = Open;
Short= ShortOK ; ShortPrice = Open;
Cover=CoverOK ; CoverPrice = Open;
TitleTradeTime= "\nBacktest Trade Time is = OFF ";
}

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


//--------------------- EXPLORATION
lastpriceBuy=ValueWhen(Buy,Open,1);
lastpriceSELL=ValueWhen(Sell,Open,1);
lastpriceShort=ValueWhen(Short,Open,1);
lastpriceCover=ValueWhen(Cover,Open,1);
BuyProfit= ((lastpriceSELL- lastpriceBuy)/lastpriceSELL); "BuyProfit= "+WriteVal(BuyProfit,1.4); // return a single trade BUY-Sell
ShortProfit =((lastpriceShort-lastpriceCover)/lastpriceCover); "ShortProfit= "+WriteVal(ShortProfit,1.4);

//// Gainers/Lossers
monodesLong= Cum(BuyProfit) ;
monadesShort=Cum(ShortProfit );
total= monodesLong;

if(Status("Action")==actionScan OR Status("Action")==actionExplore)
{
Width60=60;
Filter = Buy OR Sell OR Short OR Cover;

colorBuyProfit = IIf(BuyProfit>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, 120);
// 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.4,colorDefault,colorDefault,60);
AddColumn(IIf(Sell,lastpriceSELL,Null),"SELL",1.4,colorDefault,colorDefault,60);
AddColumn(a1=IIf(Sell,(lastpriceSELL- Ref(lastpriceBuy,-1)/lastpriceBuy),Null),"Long-Pip",1.4,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.4,colorDefault,colorDefault,70);
AddColumn(IIf(Cover,lastpriceCover,Null),"Cover",1.4,colorDefault,colorDefault,70);
AddColumn(b1=IIf(Cover,(Ref(lastpriceshort,-1)-lastpriceCover/lastpriceShort),Null),"ShortPip",1.4,colorDefault,ColorShortProfit ,60);



a3=IIf(Sell,a1,IIf( Cover,b1 ,Null)); // tell me the points 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); // contains wining trades of every 2 lines

//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
// Dear Panos, Regards Bharat
// 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
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 < BarCount; b++)
{
dist = TickSize*9;
colorBuyProfit = IIf(BuyProfit>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


// Dear Panos, Regards Bharat


// 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 <PopupWindowID.afl>
// 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");
 
Top