Australian (ASX) Stock Market Forum

Please point out mistake

Joined
28 September 2012
Posts
9
Reactions
0
Following code is meant to exclude/control time of BT.. I expected it to run BT on trades triggered during selected time.But somehow it includes all trades(not as per set time).If you need I can post full code too.Pl. point out mistake.T

//--------------------- 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;
 

Attachments

  • BT with time not working.PNG
    BT with time not working.PNG
    86.5 KB · Views: 5
Re: Pl. point out mistake.

Sorry.Did not post full code.Rest of the code

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);
 
These two

Sell=SellOK OR EndTime ;

Cover=CoverOK OR EndTime ;

are wrong in case of Endtime


You should add timenum()

Sell=SellOK OR Timenum() >= EndTime ;

Cover=CoverOK ORTimeNum() >= EndTime ;

Also you should probably use delay for Buy and Short as you want to enter on next bar's Open.

Code:
tn = TimeNum(); 

EA = tn >= 093000 AND tn <= 151500; 

Buycond = buyOK AND EA;
Buy = Ref(Buycond, -1); 
BuyPrice = Open;

Sell = SellOK OR tn >= EndTime; 
SellPrice = Close;

Shortcond = ShortOK AND EA;
Short = Ref(Shortcond, -1);
ShortPrice = Open;

Cover = CoverOK OR tn >= EndTime; 
CoverPrice = Close;

TitleTradeTime= "\nBacktest Trade With Time = ON";

Or maybe this way

Code:
tn = TimeNum(); 

EA = tn >= 093000 AND tn <= 151500;

Buy = Ref(BuyOK, -1) AND EA; 
BuyPrice = Open;

Sell = SellOK OR tn >= EndTime; 
SellPrice = Close;

Short = Ref(ShortOK, -1) AND EA;
ShortPrice = Open;

Cover = CoverOK OR tn >= EndTime; 
CoverPrice = Close;

TitleTradeTime= "\nBacktest Trade With Time = ON";


Don't understand your other problem. Maybe you should post the full code.
 
Dear JOSHMI

Thanks for prompt help.I am posting full code.Pl. correct mistakes.




_SECTION_BEGIN("BackTest_ready For");

// in this Version the exploration is Corect
SetBarsRequired(sbrAll,sbrAll ); /* All bars are here and not only that we see in the chart */
SetTradeDelays( 0, 0, 0, 0 ); /* delay entry/exit by zero bar */

////////////////////// HERE WE WRITE YOUR System /////////////////////
_SECTION_BEGIN("BB");

RS2=ParamToggle("BB ", "Hide|Show",1);
Period = Param("Period", 9, 2, 30, 1 );
Width = Param("Width", 1.5, 0, 10, 0.05 );
x2=ParamColor("BB color",colorRed|styleThick);

_SECTION_END();

_SECTION_BEGIN("BB1");

RS3=ParamToggle("BB1", "Hide|Show",1);
Period1 = Param("Period1",20, 2, 30, 1 );
Width1 = Param("Width1", 2, 0, 10, 0.05 );
x3=ParamColor("BB1 color",colorBlue | styleThick );

_SECTION_END();

mid= WMA(C,Period);
sd = StDev( C, Period );
Top = mid + Width * sd;
Bot = mid - Width * sd;


mid1= WMA(C,Period1);
sd1 = StDev( C, Period1 );
Top1 = mid1 + Width1 * sd1;
Bot1 = mid1 - Width1 * sd1;


Plot(C,"",colorGrey50,ParamStyle("Style Of Bars",styleBar,maskAll));
TFmn = in1Minute * 75;

TimeFrameSet(TFmn);
ShowBBmn9F=ParamToggle("BB9 MTFmn F", "Himne|Show",1);
ShowBBmn9L=ParamToggle("BB9 MTFmn L", "Himne|Show",1);
PMTFmn9 = Param("Periomn9 MTFmn", 9, 2, 30, 1 );
WMTFmn9 = Param("Wimnth9 MTFmn", 1.5, 0, 10, 0.05 );
ShowBBmn20F=ParamToggle("BB20 MTFmn F", "Himne|Show",1);
ShowBBmn20L=ParamToggle("BB20 MTFmn L", "Himne|Show",1);
PMTFmn20 = Param("Periomn20 MTFmn", 20, 2, 30, 1 );
WMTFmn20 = Param("Wimnth20 MTFmn", 2, 0, 10, 0.05 );
Mmn9= WMA(C,PMTFmn9);
smd9 = StDev( C, PMTFmn9 );
Topmn9 = Mmn9 + WMTFmn9 * smd9;
Botmn9 = Mmn9 - WMTFmn9 * smd9;
Mmn20 = WMA(C,PMTFmn20);
smd20 = StDev( C, PMTFmn20);
Topmn20 = Mmn20 + WMTFmn20 * smd20;
Botmn20 = Mmn20 - WMTFmn20 * smd20;
TimeFrameRestore();
MD9Fmn = TimeFrameExpand(Mmn9,TFmn, expandFirst );
TP9Fmn = TimeFrameExpand(TOPmn9,TFmn, expandFirst );
BT9Fmn = TimeFrameExpand(BOTmn9,TFmn, expandFirst );
MD20Fmn = TimeFrameExpand(Mmn20,TFmn, expandFirst );
TP20Fmn = TimeFrameExpand(TOPmn20,TFmn, expandFirst );
BT20Fmn = TimeFrameExpand(BOTmn20,TFmn, expandFirst );
MD9Lmn = TimeFrameExpand(Mmn9,TFmn, expandLast );
TP9Lmn = TimeFrameExpand(TOPmn9,TFmn, expandLast );
BT9Lmn = TimeFrameExpand(BOTmn9,TFmn, expandLast );
MD20Lmn = TimeFrameExpand(Mmn20,TFmn, expandLast );
TP20Lmn = TimeFrameExpand(TOPmn20,TFmn, expandLast );
BT20Lmn = TimeFrameExpand(BOTmn20,TFmn, expandLast );
if(ShowBBmn9F)
Plot(MD9Fmn, "MID BAND9 Daily First", colorDarkOliveGreen, styleLine |styleDots| styleNoRescale );
if(ShowBBmn9F)
Plot(TP9Fmn, "TOP BAND9 Daily First", colorRed, styleLine| styleNoRescale);
if(ShowBBmn9F)
Plot(BT9Fmn, "BOT BAND9 Daily First", colorGreen, styleLine |styleNoRescale );
if(ShowBBmn9L)
Plot(MD9Lmn, "MID BAND9 Daily Last", colorBrightGreen, styleLine |styleDots| styleThick|styleNoRescale );
if(ShowBBmn9L)
//Plot(TP9Lmn, "TOP BAND9 Daily Last", colorBrightGreen, styleLine |styleDots| styleThick |styleNoRescale );
if(ShowBBmn9L)
//Plot(BT9Lmn, "BOT BAND9 Daily Last", colorBrightGreen, styleLine |styleDots| styleThick|styleNoRescale );
if(ShowBBmn20F)
Plot(MD20Fmn, "MID BAND20 Daily First", colorBlack, styleLine |styleDots| styleNoRescale );
if(ShowBBmn20F)
Plot(TP20Fmn, "TOP BAND20 Daily First", colorDarkRed, styleLine | styleNoRescale );//styleDashed|styleDots|
if(ShowBBmn20F)
Plot(BT20Fmn, "BOT BAND20 Daily First", colorDarkGreen, styleLine |styleNoRescale );//styleDashed|styleDots|
if(ShowBBmn20L)
Plot(MD20Lmn, "MID BAND20 Daily Last", colorGreen, styleLine |styleDots| styleThick|styleNoRescale );
if(ShowBBmn20L)
Plot(TP20Lmn, "TOP BAND20 Daily Last", colorDarkRed, styleLine |styleDots| styleThick|styleNoRescale );
if(ShowBBmn20L)
Plot(BT20Lmn, "BOT BAND20 Daily Last", colorDarkGreen, styleLine |styleDots| styleThick|styleNoRescale );
_SECTION_END();


////////////////////// HERE WE WRITE YOUR System SIGNALS /////////////////////
// below i wrote 4 condition for buy,sell to fiil them up if you wish ...
//--------------------- condition Long
CondL1=(L<BT9Fmn) AND (C>BT9Fmn);
CondL2=0;
CondL3=0;
CondL4=0;
//--------------------- condition SELL
CondSL1=H<Ref(H,-1); // Cross( 85 ,Con1 );
CondSL2=0;
CondSL3=0;
CondSL4=0;
//--------------------- condition short
CondS1=(H>TP9Fmn) AND (C<TP9Fmn);
CondS2=0; //Cross( Con1 ,10 );
CondS3=0;
CondS4=0;
//--------------------- condition Cover
CondC1=L>Ref(L,-1);; //Cross( 25,Con1 );
CondC2=0; //Cross( Con1 ,10 );
CondC3=0;
CondC4=0;

//--------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);
SellOK =Ref(CondSL1,-1);
ShortOK = Ref(CondS1,-1);
CoverOK =Ref(CondC1,-1);



// -------- DO NOT TOUCH BELOW HERE IF YOU NOT KN/OW/ WHAT ARE YOU DOING THIS WAS WRITTEN BY CODER,SINCE I DO NOT KNOW CODING,I HAVE NOT MADE ANY CHANGES ----------------------
//--------------------- 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)*100; "BuyProfit= "+WriteVal(BuyProfit,1.4); // return a single trade BUY-Sell
ShortProfit =((lastpriceShort-lastpriceCover)/lastpriceCover)*100;

//// 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),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,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
Percentange = IIf(Sell,((lastpriceSELL-lastpriceBuy)/lastpriceBuy)*100,IIf(Cover,((lastpriceShort-lastpriceCover)/lastpriceCover)*100,Null));

//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
//
// 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";

These two

Sell=SellOK OR EndTime ;

Cover=CoverOK OR EndTime ;

are wrong in case of Endtime


You should add timenum()

Sell=SellOK OR Timenum() >= EndTime ;

Cover=CoverOK ORTimeNum() >= EndTime ;

Also you should probably use delay for Buy and Short as you want to enter on next bar's Open.

Code:
tn = TimeNum(); 

EA = tn >= 093000 AND tn <= 151500; 

Buycond = buyOK AND EA;
Buy = Ref(Buycond, -1); 
BuyPrice = Open;

Sell = SellOK OR tn >= EndTime; 
SellPrice = Close;

Shortcond = ShortOK AND EA;
Short = Ref(Shortcond, -1);
ShortPrice = Open;

Cover = CoverOK OR tn >= EndTime; 
CoverPrice = Close;

TitleTradeTime= "\nBacktest Trade With Time = ON";

Or maybe this way

Code:
tn = TimeNum(); 

EA = tn >= 093000 AND tn <= 151500;

Buy = Ref(BuyOK, -1) AND EA; 
BuyPrice = Open;

Sell = SellOK OR tn >= EndTime; 
SellPrice = Close;

Short = Ref(ShortOK, -1) AND EA;
ShortPrice = Open;

Cover = CoverOK OR tn >= EndTime; 
CoverPrice = Close;

TitleTradeTime= "\nBacktest Trade With Time = ON";


Don't understand your other problem. Maybe you should post the full code.
 
Top