[description = This SIMPLE LONG BUY/SELL strategy uses only Happy Face (HF), MA5 and SROC Trend. This indicator displays the following information: Entry and exits - UP & DOWN arrows. Exit arrow is accompanied by a tick if this trade is profitable, or a cross if a loss, plus the percentage gain or loss for that trade Summary line gives : Position on Chart - Change to move up & down to avoid overlapping printing Brief description of strategy & period selected (default 110 bars) P - Profit - componded over the selected period - user selectable WR - Win Ratio - percentage of wins from total trades DD - Maximum Draw Down % / Current Draw Down % TIM - Time In Market % PPA - Profit Per Annum % (Profit / num years) Traders Rate of Change - TROCr (RAW) TROCs (SMOOTH) ADVICE - BUY, SELL, HOLD, STAY OUT determined by strategy EXPECTATION per Trade; author=Bleakley, Des;target=price;][color=black]; printposn := input("Print Position ", 1, 1, 20); roc_bars := input("ROC BARS ", 52, 5, 100); emasmooth := input("Smoothing period", 10, 1, 1000); hist_bars := input("History bars (0=ALL) ", 110, 0, 1000); entry_SAP := inputtext("SHOW ALL ENTRY POINTS? Y/N ","N" ); exit_SAP := inputtext("SHOW ALL EXIT POINTS? Y/N ","N" ); apply_system_stop := inputtext("APPLY SYSTEM STOP? Y/N ", "N"); show_all_entries := entry_SAP = "Y" OR entry_SAP = "y"; show_all_exits := exit_SAP = "Y" OR exit_SAP = "y"; system_on := apply_system_stop="Y" OR apply_system_stop="y"; MA5 := 5; MA_method := SIMPLE; close_HF := 6; TP_ATR_num := 3; max_bar_num := lastvalue(barnumber); history_bars := if(hist_bars>max_bar_num, max_bar_num, hist_bars); startbar := if(history_bars=0,65 ,max_bar_num - history_bars); earlybar := barnumber < 65 OR barnumber < startbar; lastbar := barnumber=max_bar_num; last_ten_bars := barnumber>=max_bar_num-10 ; printpositionH:= HHV(HIGH,50)*(printposn/10+1); printpositionL := LLV(LOW,50)*(printposn/10)*0.8; firstbalance := 100; { NOTIONAL INITIAL INVESTMENT } sys_id := "SYS902SE - MA5x2 & SROCx2/BOT EXIT"; { **** DETERMINE PERIOD OF CHART **** } thisbardate := now(); prevbardate := hist( now(),1); daysdiff := datediff(prevbardate, thisbardate); period := if ( daysdiff <7, "DAILY", if ( daysdiff <30, "WEEKLY", if ( daysdiff < 93, "MONTHLY", if ( daysdiff<365, "QUARTERLY","YEARLY")))); period_factor :=if (period="DAILY", 260, if( period="WEEKLY", 52, if( period="MONTHLY", 12, if( period = "QUARTERLY", 4, 1)))); { **** CALCULATE VARIOUS INDEX TREND VALUES **** } XJO312UP := ma(LoadSymbol("XJO",CLOSE),3,E)>ma(LoadSymbol("XJO",CLOSE),12,E); NAS312UP := ma(LoadSymbol("US.^COMP",CLOSE),3,E)>ma(LoadSymbol("US.^COMP",CLOSE),12,E); SP500312UP := ma(LoadSymbol("US.^SPX",CLOSE),3,E)>ma(LoadSymbol("US.^SPX",CLOSE),12,E); USA_stock := left(security.symbol,3) = "US."; USE_THIS_INDEX := if(USA_stock,NAS312UP,XJO312UP); { **** Calculate raw & smoothed annual rise **** } boughtprice := hist(CLOSE, roc_bars); risetodate := CLOSE - boughtprice; risepc := (risetodate/boughtprice)*100; smoothpcS := ma( risepc, emasmooth, S); smoothpcE := ma( risepc, emasmooth, E); smoothpc_up_one := hist(smoothpcS,1) < smoothpcS; smoothpc_down_one := hist(smoothpcE,1) >= smoothpcE; smoothpc_up_two := hist(smoothpc_up_one,1) AND smoothpc_up_one; smoothpc_down_two := hist(smoothpc_down_one,1) AND smoothpc_down_one; { **** CALCULATE SMACD VALUE **** } fast := 12; slow := 26; smooth := 9; mafast := ma(C,fast,E); maslow := ma(C,slow,E); osc := ((wait(mafast - maslow, 26))/C)*100; signal := ma(osc,smooth,E); SMACD_up_nearby := barssince(cross(osc,signal)) <6 AND osc >0; SMACD_DOWN := osc < signal AND signal <5; { **** HAS BELL RUNG? **** } roc_st := roc(CLOSE,14,PERCENT); bell := cross(roc_st,0); bell_nearby := barssince(bell) <6 AND roc_st >0; { **** MA5 & HAPPY FACE NEARBY CALCULATIONS **** } MA5_down_one := hist(ma(CLOSE,MA5,MA_method),1) >= ma(CLOSE,MA5,MA_method); MA5_down_two := hist(MA5_down_one,1) AND MA5_down_one; MA5_up_one := hist(ma(CLOSE,MA5,MA_method),1) < ma(CLOSE,MA5,MA_method); MA5_up_two :=hist(MA5_up_one,1) AND MA5_up_one; ATR_profit_line := (ma(CLOSE,13,SIMPLE) + TP_ATR_num*ATR(21)); happy_face := C >= ATR_profit_line; happy_face_nearby := barssince(happy_face)< close_HF; last_HF_value := valuewhen(1,happy_face,CLOSE); HF_drop := (CLOSE/last_HF_value) <0.96; first_HF := happy_face AND NOT hist(happy_face_nearby,1); {[name=HFD;linestyle=text;]if(happy_face_nearby,close/last_HF_value,undefined);Close*0.7;} { **** DEFINE ENTER AND EXIT CONDITIONS **** } en_1 := if(system_on, USE_THIS_INDEX,TRUE); en_2 := MA5_up_two AND smoothpc_up_two; en_3 := MA5_up_two AND smoothpc_up_one AND bell_nearby; ex_0 := MA5_down_two; ex_1 := MA5_down_one AND smoothpc_down_one ; ex_2 := happy_face_nearby AND MA5_down_one AND CLOSE= HHV(Close,close_HF); ex_3 := happy_face_nearby AND smoothpc_down_one AND CLOSE= HHV(Close,close_HF); ex_4 := happy_face_nearby and HF_drop; exitcondition := NOT WHITE AND (ex_1 OR ex_2 OR ex_3); entercondition := NOT exitcondition AND WHITE AND en_1 AND (en_2 OR en_3) AND NOT first_HF ; {[name=XHF;linestyle=marker;marker=type1;]HFXATR;} {[name=su2;linestyle=marker;marker=type2;]smoothpc_up_two;} { **** ARE WE IN OR OUT OF THE MARKET? **** } INMKT := if( earlybar, FALSE, if( prev AND exitcondition, FALSE, if( NOT prev AND entercondition, TRUE, prev))); { **** CHANGE FROM INMKT TO NOT INMKT DEFINES BUY/SELL SIGNALS - LONG SYSTEM **** } buysignal := NOT hist(INMKT,1) AND INMKT; sellsignal := hist(INMKT,1) AND NOT INMKT; { **** COUNT BARS IN OR OUT OF MARKET **** } TOTALINMKT := if (earlybar, 0, if (INMKT, prev+1, prev)); TOTALOUTMKT := if (earlybar, 0, if ( NOT INMKT, prev+1, prev)); TOTALNUMBARS := TOTALINMKT + TOTALOUTMKT; INMKTRATIO := TOTALINMKT / TOTALNUMBARS; total_years := TOTALNUMBARS / period_factor; { **** CALCULATE PROFIT **** } tomorrowopen := if(lastbar, CLOSE, future (OPEN, 1)); lastbuyprice := if(buysignal, tomorrowopen, prev); profit := tomorrowopen - lastbuyprice; profitpc := (profit/lastbuyprice)*100; win := profit >0; loss := profit <= 0; uplift_since_buy := if (INMKT OR sellsignal, (tomorrowopen/lastbuyprice),0); balance := if(earlybar, firstbalance, if( buysignal, prev, if( sellsignal, valuewhen(1, buysignal, prev)*uplift_since_buy, if( INMKT, valuewhen(1,buysignal, prev)*uplift_since_buy, prev)))); final_profit := balance-firstbalance; PPA := final_profit/total_years; Rlast_trade := round(profitpc,1); { **** CALCULATE NUMBER OF WINS AND LOSSES **** } numwins := if(earlybar,0,if(((lastbar AND INMKT) OR sellsignal) AND win, prev+1, prev)); numlosses := if(earlybar, 0,if(((lastbar AND INMKT) OR sellsignal) AND loss AND NOT buysignal, prev+1, prev)); total_trades := numwins+numlosses; winratio := if ( total_trades = 0, 0 , (numwins/total_trades)*100 ); total_gains := if(barnumber=1,0, if( ((lastbar AND hist(INMKT,1)) OR (sellsignal AND hist(INMKT,1))) AND win, prev+profitpc,prev)); total_losses := if(barnumber=1,0, if( ((lastbar AND hist(INMKT,1)) OR (sellsignal AND hist(INMKT,1))) AND loss, prev+profitpc,prev)); RAG := if(numwins=0,0,+round(total_gains/numwins,2)); RAL := if(numlosses=0,0,+round(total_losses/numlosses,2)); { **** CALCULATE EXPECTATION **** } lp :=(total_gains/if(numwins=0,1,numwins))*winratio; rp := (total_losses/if(numlosses=0,1,numlosses))*(100-winratio); exp_PPT := lp + rp; REPPT := round(exp_PPT/100,2); { **** CALCULATE DRAW DOWN **** } highthistrade := if(INMKT OR sellsignal, highestsince(1,buysignal, CLOSE), CLOSE); thistradeDDpc := if(sellsignal,min(((highthistrade-future(open,1))/highthistrade)*100,((highthistrade-CLOSE)/highthistrade)*100), ((highthistrade-CLOSE)/highthistrade)*100); maxDDpcthistrade := highestsince(1,buysignal,thistradeDDpc); maxDDpc_all_time := highest ( maxDDpcthistrade ); { **** DECIDE ON RECOMMENDATION ***** } bars_buy_ago := if (entercondition AND NOT hist(INMKT,1), 0, barssince(buysignal)); ss_buy := if(bars_buy_ago = 0 AND INMKT," BUY NOW &", if(bars_buy_ago<6 AND INMKT, " BUY "+bars_buy_ago+" &","")); ss_sell := if( exitcondition AND hist(INMKT,1), " SELL &",""); ss_wait := if( NOT INMKT OR sellsignal, " STAY OUT", ""); ss_hold := if ( INMKT," HOLD",""); rec_string := ss_buy+ss_sell+ss_wait+ss_hold; { **** CALCULATE TROC VALUES **** } TROCraw := PPA/INMKTRATIO; TROCma := ma(TROCraw,emasmooth,E); T_MA_s := " | MA5 "+ if(MA5_down_one,"-5%","OK"); T_SROC_s := " | SROC "+ if(smoothpc_down_one,"-5%","OK"); T_SMACD_s :=" | SMACD "+if(SMACD_DOWN,"-5%","OK"); T_INMKT_s := " | "+if(INMKT,"INMKT OK","NOT INMKT - ROTATE OUT - ZERO"); T_adjust_factor := if(NOT INMKT,0,1+if(MA5_down_one,-0.05,0)+if(smoothpc_down_one,-0.05,0)+if(SMACD_DOWN,-0.05,0)); TROCr := round(TROCraw); TROCa := round( TROCraw *(T_adjust_factor)); TROCs := round(TROCma); TROCh := round(TROCraw *(T_adjust_factor)*1.05); { **** Round Results for later Display **** } RTIM := round(INMKTRATIO*100); RPPA := round(PPA); RWINR := round(winratio); RTY := round(total_years,2); hb := if(history_bars=0,lastvalue(barnumber),history_bars); strategy := "("+printposn+") | "+sys_id+" | "+hb+" bars("+RTY+"y) from "+hist(now, hb-1 ); { **** DISPLAY OUTPUTS **** } { ** Profit or Loss ** } [Name=PorL;linestyle=text;fontstyle=bold;textalign=below,centre;fontsize=8;visible=false;]; {cox := round(((HHV(CLOSE,6)-tomorrowopen)/HHV(close,6))*100 );} if( (lastbar AND hist(INMKT,1)) OR (sellsignal AND hist(INMKT,1)), round(profitpc,2)+"/"+round(final_profit)+"%"{+" / "+cox},undefined); printpositionH*0.9; { ** Last Trade Profit or Loss ** } [Name=LT;linestyle=text;fontsize=8;fontstyle=bold;textalign=above,left;visible=false;] if( lastbar AND hist(INMKT,1), " LT:"+Rlast_trade+"%",undefined);close*0.8; { ** Long Bar Summary ** } [Name=LBS;linestyle=text;FontStyle=Bold;fontsize=8;textalign=above,right;visible=true;] if( lastbar, strategy+" | P:"+round(final_profit)+"% | WR:"+RWINR+"% of "+total_trades+" | DD:"+round(maxDDpc_all_time)+"%M"+if(INMKT,"/"+round(thistradeDDpc)+"%C","")+" | TIM:"+RTIM+"% | PPA:"+RPPA+"% | TROC "+TROCr{+"/Ta"+TROCa+"/Ts"+TROCs+"/Th"+TROCh+if(TROCma