[description=DJB - ENTRY & EXIT ARROWS with profit calculations using EMA UP Strategy & 7 on 3 or 7on12 exit; author = Bleakley, Des ; target=PRICE; ] ; strategy := "RISING TREND - Entry Filter (C) - Exit 3on12 or 7on12"; [color=blue;]; 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 := CLOSE*0.65; { **** Rising Moving Averages **** } rising3on7 := if (barnumber <8, FALSE, alltrue( ma(C,3,E) >= ma(C,7,E), 5)); rising7on12 := if (barnumber<13, FALSE, 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); enterfilter := ef1 AND ef2; exitfilter := xaoc < ma(LoadSymbol("XAO", Close ),6,E); { ***** Decide on entry conditions ***** } enterconditionA := enterfilter; { Entry condition A - Is overall market in up trend ? } enterconditionB := allrising; { Entry condition B - EMA's rising } entercondition := enterconditionA AND enterconditionB; { Select entry conditions } { **** Decide on exit conditions **** } exiton3 := exitfilter AND ma(C,12,E) > ma(C,3,E); exiton7 := (NOT exiton3 ) AND ma(C,12,E) > ma(C,7,E) ; exitcondition := exiton3 OR exiton7; { ***** According to our entry and exit conditions, are we in or out of the market? ***** } INMKT := if ( barnumber <25, FALSE, 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=blue;marker = type13]; buysignal; [Name = "EXIT"; Linestyle = marker;color=blue; marker=type14]; sellsignal; { **** Mark exit bar **** } [name="x3";linestyle=marker;marker = type3]; sellsignal AND exiton3; [name="x7";linestyle=marker;marker = type7]; sellsignal AND exiton7; { *********** 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.55; [name=" ";linestyle=text;textalign=below,centre;fontstyle=bold;fontsize=6;]; if ( sellsignal,"Sell: "+round(future(OPEN,1),2), undefined); printposition*0.55; { **** Remember Date of latest buysignal **** } lastbuydate := if ( buysignal, now(), prev ); { **** Remember Enter Price (tomorrow's OPEN) on buy signal for later profit calculation **** } ENTERPRICE := if ( buysignal, future(OPEN,1), 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; { **** 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 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;