[description=DJB - Variable ENTRY & EXIT - Select In & OUT strategy to examine. ENTRY 1 : New High, 2 : RISE, 3 : New High or RISE. Entry Bull Filter 0 : None, 1 : Simple, 2 : Complex. Exit 312 : 3 on 12, 712 : 7on 12, 37 : 3 on 7, any other value n : n x ATR; category = CASTLE; author = Bleakley, Des ; target=PRICE; ] ENTERstrategy := input ("Entry Strategy 1 - NH, 2 - Rise, 3 - BOTH",1, 1, 3 ); ENTERbullfilter := inputtext("Bull Filter - None, Simple, Complex","N"); EXITstrategy := input("Exit Strategy 312/712/37/712312/n x ATR",712, 0.1, 712312); enterstrategytext := if (ENTERstrategy = 1, "New High", if( enterstrategy = 2, "Rising MA Trend", if(enterstrategy = 3, "BOTH New High & Rising MA ","ERR"))); exitstrategyisATR := if (EXITstrategy=312, FALSE, if(EXITstrategy=712,FALSE, if(EXITstrategy=37, FALSE, if(EXITstrategy=712312, FALSE, TRUE)))); ATRmultiple := if (exitstrategyisATR, EXITstrategy, 0); enterbullfiltertext := if(ENTERbullfilter = "S", "S", if(ENTERbullfilter = "C", "C","None")); exitstrategytext := if(EXITstrategy = 312, "3 on 12", if(EXITstrategy = 712, "7 on 12", if(EXITstrategy = 37, "3 on 7", if(EXITstrategy = 712312, "7 on 12 OR 3 on 12", exitstrategy+" x ATR")))); strategy := "VARIABLE - "+enterstrategytext+" - Enter Filter ("+enterbullfiltertext+") - Exit "+exitstrategytext; NBARSBACK := 52; { Set period for new high look back } totalbars := lastvalue( barnumber); lastbar := barnumber = totalbars; firstdate := firstvalue(now()); lastdate := lastvalue( now()); totaldaysactive := datediff(firstdate, lastdate) - NBARSBACK; numyears := totaldaysactive / 365; printposition := HIGH*1.3; { **** Rising Moving Averages **** } rising3on7 := alltrue( ma(C,3,E) >= ma(C,7,E), 5); rising7on12 := alltrue( ma(C,7,E) >= ma(C,12,E), 5); allrising := rising3on7 AND rising7on12 ; { **** 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); complexenterfilter := ef1 AND ef2; { XAO > 6 EMA & > 24 EMA - uptrend market? } simpleenterfilter := xaoc > ma(LoadSymbol("XAO", Close ),12,E); { XAO > 12 EMA - uptrend market? } exitfilter := xaoc < ma(LoadSymbol("XAO", Close ),6,E); { ***** Decide on entry conditions ***** } newhigh := if (barnumber<=NBARSBACK, FALSE, CLOSE >= highest(CLOSE,NBARSBACK) ); enterconditionA := if( ENTERstrategy = 1, newhigh, if (ENTERstrategy = 2, allrising, if (ENTERstrategy = 3, (newhigh AND allrising), FALSE))); { Entry condition A - Is this bar an ENTER? } enterconditionB := if ( ENTERbullfilter = "S", simpleenterfilter, if( ENTERbullfilter = "C", complexenterfilter, TRUE )); { Entry condition B - Apply Bull Enter Filter? } 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 **** } ts := C - ATRmultiple*ATR(21); [name=ATR Trailing Stop;color=orange]; if (exitstrategyisATR,HHV(ts,21), FALSE); exitonATR := CLOSE < HHV(ts,21); exit3on12 := ma(C,3,E) < ma(C,12,E); exit7on12 := ma(C,7,E) < ma(C,12,E); exit3on7 := ma(C,3,E) < ma(C,7,E); exiton3 := exitfilter AND ma(C,12,E) > ma(C,3,E); exiton7 := (NOT exiton3 ) AND ma(C,12,E) > ma(C,7,E) ; exitcondition := if (exitstrategy = 712312, exiton3 OR exiton7, if (exitstrategy = 312, exit3on12, if (exitstrategy = 712, exit7on12, if (exitstrategy = 37, exit3on7, exitonATR)))); { **** 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; { **** Look at status of INMKT to see if we have triggered a buy or sell condition **** } buysignal := INMKT AND NOT hist(INMKT,1); sellsignal := NOT INMKT AND hist(INMKT,1); { **** Remember Date of latest buysignal **** } lastbuydate := if ( buysignal, now(), prev ); { **** Remember Enter Price when we get buy signal for later profit calculation **** } ENTERPRICE := if ( buysignal, future(OPEN,1), prev); 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 ); { **** All calculations done - now just plot data onto chart **** } { **** PRINT ENTER & EXIT ARROWS **** } [Name = "ENTER";color=black;Linestyle = marker; marker = type13; ]; buysignal; [Name = "EXIT";color=black;Linestyle = marker; marker=type14;]; sellsignal; { **** Mark exit bar **** } [color=black]; [name="x3";linestyle=marker;marker = type3]; sellsignal AND ( (exitstrategy=312) OR(exitstrategy=712312) ); [name="x7";linestyle=marker;marker = type7]; sellsignal AND ( (exitstrategy = 712) OR (exitstrategy=712312) );; {[name="x7";linestyle=marker;marker = type7]; sellsignal AND (exiton7 OR exit7on12);} [name="atr";linestyle=marker;marker = type20]; sellsignal AND exitstrategyisATR; { *********** 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; { Price for BUY } [name="sell";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( sellsignal,"Sell: "+round(future(OPEN,1),2), undefined); printposition*0.55; { Price for SELL } { ************ 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 ); 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";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;;]; if ( sellsignal and loss,"Loss : "+round(profit,2)+"("+Rprofitpercentage+"%)", undefined); printposition*0.53; { ****** PRINT Summary Stats ********* } latestprofit := if ( (buysignal AND lastbar), 0, if( INMKT, (CLOSE/hist(ENTERPRICE,1)-1) * 100, 0)); totalnettprofit := sum(tradeprofitorlosspercent); returnperyear := totalnettprofit / numyears; averagewin := if ( numwins>0, totalwinpercent/numwins, 0); averageloss := if ( numlosses>0, totallosspercent/numlosses, 0); profitratio := if ( averageloss=0, 1, averagewin / -averageloss ); perannum := returnperyear / INMKTRATIO; INorOUT := if(INMKT,"IN","OUT"); performsincestart := per(CLOSE)+100; currtradedays := datediff( lastbuydate, NOW() ); lasttradepc := if ( currtradedays = 0, 0, (latestprofit / currtradedays) * 365 ); { **** Round for printing **** } Rlatestprofit := round( latestprofit, 2); Raveragewin := round( averagewin, 2); Raverageloss := round( averageloss, 2); Rprofitratio := round( profitratio, 2); Rtotaldaysactive := round( totaldaysactive ); Rnumyears := round(numyears,1); RINMKTRATIO := round(INMKTRATIO*100); Rmaxddpercent := round(MAXDDpercent, 2); Rwinratio := round ( winratio ); Rtotalnettprofit := round ( totalnettprofit, 2); Rperannum := round( perannum, 1); Rlasttradepc := round(lasttradepc); Rppt :=round(totalnettprofit/totalnumtrades,2); [name="SUMMARY";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=9;]; if ( lastbar,strategy+" - Curr "+INorOUT+" @ "+Rlatestprofit+"% ("+Rlasttradepc+"% PA) - MAX DD = "+Rmaxddpercent+"%", undefined); printposition*0.92; if ( lastbar,"Profit : "+Rtotalnettprofit+"% W/L : "+numwins+"/"+numlosses+" WR : "+Rwinratio+"% IN : "+RINMKTRATIO+"% of "+Rnumyears+" years - ("+Rtotaldaysactive+"D) - ROC : "+round(ROC(C,126,%))+"%", undefined); printposition*0.88; if ( lastbar ,"Avg W ("+Raveragewin+"%) = "+Rprofitratio+" times Avg L ("+Raverageloss+"%) PA : "+Rperannum+"% PPT: "+Rppt+"%", undefined); printposition*0.84;