[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 0/312/712/37/712312/ATR",712, 0, 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= 0, FALSE, 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", "Simple", if(ENTERbullfilter = "C", "Complex","None")); exitstrategytext := if(EXITstrategy=0,"HOLD", 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 - Entry : ("+enterstrategytext+") - Enter Filter : ("+enterbullfiltertext+") - Exit : ("+exitstrategytext+")"; 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; numyears := round(totaldaysactive / 365,1); printposition := CLOSE*0.44; { **** 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 exitfilter ) AND ma(C,12,E) > ma(C,7,E) ; exitcondition := if (exitstrategy = 0, penultimatebar, 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; 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 := 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 ); {[namew=DDtoday;color = green;]DDtoday;} [name=MAXDDpc;target=newpane;color=red]; maxddpercent; [name=DispDDpc;color = blue;]; DispDDpc;