[description=DJB - NEW HIGH ENTRY & HOLD; author = Bleakley, Des ; target=PRICE; ] strategy := "NEW HIGH - BUY AND HOLD "; NBARSBACK := 52; { Set period for new high look back } totalbars := lastvalue( barnumber); lastbar := barnumber = totalbars; penultimatebar := barnumber = (totalbars-1); firstdate := firstvalue(now()); lastdate := lastvalue( now()); totaldaysactive := datediff(firstdate, lastdate) - NBARSBACK; Rtotaldaysactive := round( totaldaysactive ); numyears := totaldaysactive / 365; Rnumyears := round (numyears, 1); printposition := HIGH*1.3; [color=black]; { **** Set up Entry and Exit filters **** } xaoc := LoadSymbol("XAO", Close); ef1 := xaoc >= ma(LoadSymbol("XAO", Close ),6,E); ef2 := xaoc >= ma(LoadSymbol("XAO",Close),24,E); enterfilter := ef1 AND ef2; exitfilter := xaoc < ma(LoadSymbol("XAO", Close ),6,E); { ***** Decide on entry conditions ***** } newhigh := if (barnumber<=NBARSBACK, FALSE, CLOSE >= highest(CLOSE,NBARSBACK) ); enterconditionA := enterfilter; { Entry condition A - Is overall market in up trend ? } enterconditionB := newhigh; { Entry condition B - Is this bar a NBARS HIGH? } enterconditionC := ma(C,11,E) > ma(C,12,E); { Entry condition C - Is this stock in up trend ? } enterconditionD := ma(C,7,E) > ma(C,12,E); { Entry condition D - Is this stock in up trend ? } entercondition := enterconditionA AND enterconditionB AND enterconditionC AND enterconditionD; { Select entry conditions } { **** Decide on exit conditions **** } exitcondition := lastbar; { ***** According to our entry and exit conditions, are we in or out of the market? ***** } INMKT := if ( entercondition, true, if ( exitcondition, false, prev )); { **** Count number of bars in and out of market **** } INMKTcount := if (INMKT,+1, 0); OUTMKTcount := if (NOT INMKT, +1, 0); totalINMKT := sum(INMKTcount); totalOUTMKT := sum(OUTMKTcount); INMKTratio := totalINMKT / totalbars; INMKTpercent := round(INMKTratio*100); { **** Look at status of INMKT to see if we have triggered a buy or sell condition *** } buysignal := INMKT AND NOT hist(INMKT,1); sellsignal := penultimatebar; {NOT INMKT AND hist(INMKT,1);} { **** Remember Enter Price when we get buy signal for later profit calculation **** } ENTERPRICE := if ( buysignal, future(OPEN,1), prev ); { **** Calculate Drawdown **** } THIShightodate := if (buysignal, CLOSE, if ( sellsignal, 0, if ( (CLOSE > prev AND INMKT), CLOSE, prev ))); DDtoday := if ( sellsignal, hist(THIShightodate,1)-CLOSE, THIShightodate -CLOSE); DDtodaypercent := if(sellsignal, ( DDtoday / hist(THIShightodate,1)) *100, (DDtoday / THIShightodate)*100); DispDDpc := if(INMKT,DDtodaypercent , if(sellsignal,DDtodaypercent,0)); MAXDDpercent := highest( DDtodaypercent ); Rmaxddpercent := round(MAXDDpercent, 2); { **** All calculations done - now just plot data onto chart **** } { **** PRINT ENTER & EXIT ARROWS **** } [Name = "ENTER";color=black;Linestyle = marker; marker = type13; ]; buysignal; { **** Mark exit bar **** } [color=black]; [name="X";linestyle=marker; marker=type14]; lastbar; { *********** PRINT Buy & Sell prices = next OPEN ************* } { [name="buy";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( buysignal,"Buy: "+round(future(OPEN,1),2), undefined); printposition*0.55; [name="sell";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( sellsignal,"Sell: "+round(future(OPEN,1),2), undefined); printposition*0.55; } { ************ PRINT Stats per Individual Trade *************** } lastbuyprice := hist( ENTERPRICE, 1); tomorrowopen := future (OPEN, 1); profit := tomorrowopen - lastbuyprice; profitpercentage := (profit / lastbuyprice) * 100; win := profitpercentage > 0; loss := profitpercentage <= 0; tradeprofitorlosspercent := if (sellsignal, profitpercentage, 0 ); thisiswin := if (sellsignal AND win, +1,0); thisisloss := if (sellsignal AND loss, +1,0); numwins := sum(thisiswin); numlosses := sum(thisisloss); totalnumtrades := numwins + numlosses; winratio := if ( totalnumtrades = 0, 0 , (numwins/totalnumtrades)*100 ); Rwinratio := round ( winratio ); thiswinvalue := if (thisiswin, profitpercentage,0); thislossvalue := if (thisisloss, profitpercentage,0); totalwinpercent := sum(thiswinvalue); totallosspercent := sum(thislossvalue); Rprofitpercentage := round( profitpercentage, 2); { [name="PSell";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( sellsignal and win,"Profit : $"+round(profit,2)+"("+Rprofitpercentage+"%)", undefined); printposition*0.53; [name="LSell";]; if ( sellsignal and loss,"Loss : $"+round(profit,2)+"("+Rprofitpercentage+"%)", undefined); printposition*0.53; } { ****** PRINT Summary Stats ********* } latestprofit := (CLOSE/hist(ENTERPRICE,1)-1) * 100; Rlatestprofit := round( latestprofit, 2); totalnettprofit := sum(tradeprofitorlosspercent); Rtotalnettprofit := round ( totalnettprofit, 2); returnperyear := totalnettprofit / numyears; Rreturnperyear := round( returnperyear, 2); averagewin := if ( numwins>0, round(totalwinpercent/numwins,2), 0); averageloss := if ( numlosses>0, round(totallosspercent/numlosses,2), 0); profitratio := if ( averageloss=0, 1, round ( averagewin / -averageloss, 2)); perannum := returnperyear / INMKTRATIO; Rperannum := round( perannum, 1); factor := if (totalnettprofit<=0 OR (-averageloss >= averagewin), 0,winratio * profitratio); volfactor := if (totalnumtrades>15, 1.5, if( totalnumtrades<6, 0.75,1)); Rfactor := round(factor,0);Rvolfactor := round( volfactor*factor,0); INorOUT := if(INMKT,"IN","OUT"); firstclose := firstvalue(CLOSE); lastclose := lastvalue (CLOSE); profitsincestart := (lastclose / firstclose) * 100; Rprofitsincestart := round( profitsincestart ); [name="SUMMARY";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=9;]; if ( penultimatebar,strategy+" - MAX DD = "+Rmaxddpercent+"%", undefined); printposition*0.92; if ( penultimatebar,"Profit : "+Rtotalnettprofit+"% W/L : "+numwins+"/"+numlosses+" WR :"+Rwinratio+"% IN : "+INMKTPERCENT+"% of "+Rnumyears+" years - ("+Rtotaldaysactive+"D)", undefined); printposition*0.89; if ( penultimatebar ,"Avg W ("+averagewin+"%) = "+profitratio+" times Avg L ("+averageloss+"%) PA : "+Rperannum+"% ", undefined); printposition*0.86;