[description=DJB - ENTRY & EXIT ARROWS - Exit when ema goes flat, just before it turns down.; author = Bleakley, Des ; target=PRICE; ] NBARSBACK := input("New High Bars ", 52, 10, 100); { Set period for new high look back } maexit := input("EMA exit value ", 52, 1, 1000); ENTERbullfilter := inputtext("Bull Filter - None, Simple, Complex","N"); enterbullfiltertext := if(ENTERbullfilter = "S", "S", if(ENTERbullfilter = "C", "C","None")); strategy := "NEW HIGH ("+NBARSBACK+") - Entry Filter ("+enterbullfiltertext+") - FALLING EMA ("+maexit+")" ; totalbars := lastvalue( barnumber); lastbar := barnumber = totalbars; firstdate := firstvalue(now()); lastdate := lastvalue( now()); totaldaysactive := datediff(firstdate, lastdate) - NBARSBACK; numyears := totaldaysactive / 365; [name=ma;linestyle=solid;color=black;width=2]; ma(C,maexit,E); printposition := HIGH*1.3; { **** 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); { **** ENTRY CONDITIONS **** } newhigh := if (barnumber<=NBARSBACK, FALSE, CLOSE >= highest(CLOSE,NBARSBACK) ); waitperiod := if( NBARSBACK > maexit, NBARSBACK, maexit ); waitperiodover := barnumber > waitperiod; enterconditionA := LoadSymbol("XAO", Close) >= ma(LoadSymbol("XAO", Close ),12,E); { Entry condition A - Is market in up trend ? } enterconditionB := newhigh; { Entry condition B - Is this bar a NBARS HIGH? } enterconditionC := ma(C,7,E) > ma(C,12,E) AND ma(C,11,E) > ma(C,12,E); { Entry condition C - is 7 & 11 > 12 EMA? } enterconditionD := if (ENTERbullfilter = "S", simpleenterfilter, if( ENTERbullfilter = "C", complexenterfilter, TRUE )); entercondition := enterconditionA AND enterconditionB AND enterconditionC AND enterconditionD AND waitperiodover; { Select entry conditions } { **** EXIT CONDITIONS **** } exitema := ma(CLOSE,maexit,E) <= hist(ma(CLOSE,maexit,E),1); exitcondition := waitperiodover AND (exitema); { ***** According to our entry and exit conditions, are we in or out of the market? ***** } INMKT := if ( entercondition, TRUE, if ( exitcondition, FALSE, prev )); { **** 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); { **** PRINT ENTER & EXIT ARROWS **** } [Name = "ENTER"; Linestyle = marker; color=black;marker = type13]; buysignal; [Name = "EXIT"; Linestyle = marker;color=black; marker=type14]; sellsignal; { **** Mark exit bar to indicate a 7 on 12 exit **** } [name="x";linestyle=marker;color=black;marker = type20]; sellsignal and exitema; { *********** PRINT Buy & Sell prices = next OPEN ************* } [name=" ";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( buysignal,"Buy: "+round(future(OPEN,1),2), undefined); printposition*0.5; [name=" ";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( sellsignal,"Sell: "+round(future(OPEN,1),2), undefined); printposition*0.5; { **** Remember Date of latest buysignal **** } lastbuydate := if ( buysignal, now(), prev ); { **** Remember Enter Price (tomorrow's OPEN) when we get buy signal for later profit calculation **** } ENTERPRICE := if ( buysignal, future(OPEN,1), prev); { **** Count number of bars in and out of market **** } TOTALINMKT := if (INMKT,prev+1, prev); TOTALOUTMKT := if (NOT INMKT, prev+1, prev); INMKTRATIO := TOTALINMKT / totalbars; { **** Note highest Close in this trade **** } THIShightodate := if (buysignal, CLOSE, if ( sellsignal, 0, if ( (CLOSE > prev AND INMKT) , CLOSE, prev))); { **** Calculate Draw Down for his trade **** } 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 ); { ************ PRINT Notional Profit Per Trade *************** } lastbuyprice := hist( ENTERPRICE, 1); tomorrowopen := future (OPEN, 1); profit := tomorrowopen - lastbuyprice; profitpercentage := (profit / lastbuyprice) * 100; thisiswin := sellsignal AND profitpercentage > 0; thisisloss := sellsignal AND profitpercentage <= 0; numwins := if(thisiswin, prev+1, prev); numlosses := if(thisisloss, prev+1, prev); totalnumtrades := numwins + numlosses; winratio := if ( totalnumtrades = 0, 0 , (numwins/totalnumtrades)*100 ); totalwinpercent := if(thisiswin, prev+profitpercentage, prev); totallosspercent := if(thisisloss, prev+profitpercentage, prev); Rprofitpercentage := round( profitpercentage, 2); currtradedays := datediff( lastbuydate, NOW() ); profitpcpa := if( currtradedays=0,0, (profitpercentage / currtradedays) * 365); Rprofitpcpa := round( profitpcpa, 2 ); totalnettprofit := totalwinpercent + totallosspercent; [name="PSell";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( sellsignal and thisiswin,"Profit : "+round(profit,2)+"("+Rprofitpercentage+"%) in "+currtradedays+" days ("+Rprofitpcpa+"%PA)", undefined); printposition*0.53; [name="LSell";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( sellsignal and thisisloss,"Loss : "+round(profit,2)+"("+Rprofitpercentage+"%) in "+currtradedays+" days ("+Rprofitpcpa+"%PA)", undefined); printposition*0.53; {if(sellsignal,"PTD"+round(totalnettprofit,2)+"%",undefined);printposition*0.515;} { ****** PRINT Summary Stats ********* } lastprofit := if ( (buysignal AND lastbar), 0, if( INMKT, (CLOSE/hist(ENTERPRICE,1)-1) * 100, 0)); {totalnettprofit := totalwinpercent + totallosspercent;} 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; lasttradepc := if ( currtradedays = 0, 0, (lastprofit / currtradedays) * 365 ); { **** Round for printing **** } Rlastprofit := round( lastprofit, 2); Raveragewin := round( averagewin, 2); Raverageloss := round( averageloss, 2); Rprofitratio := round( profitratio, 2); Rtotaldaysactive := round( totaldaysactive ); Rnumyears := round(numyears,1); Rreturnperyear := round( returnperyear, 2 ); 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+" @ "+Rlastprofit+"% ("+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+"%) PA1: "+Rreturnperyear+"% PA2: "+Rperannum+"% PPT: "+Rppt+"%", undefined); printposition*0.84;