[description = This indicator recommends to stay out of the market if the INDEX is in a down trend based on MA312. It also indicates whether a BOT (Best Of Three) exit should be taken, and whether a SYS92L entry is available;author=Bleakley, Des;target=price;][color=blue]; show_SAP := inputtext("SHOW ALL DATA POINTS? Y/N ","Y" ); hist_bars := input("History bars (0=ALL) ", 110, 0, 1000); show_all_points := show_SAP = "Y" OR show_SAP = "y"; exit_SAP := inputtext("SHOW ALL EXIT POINTS? Y/N ","Y" ); entry_SAP := inputtext("SHOW ALL ENTRY POINTS? Y/N ","Y" ); show_INDEX_info := inputtext("SHOW INDEX INFO? Y/N ", "Y"); show_system := inputtext("SHOW SYSTEM ? Y/N ", "Y"); show_bell := inputtext("SHOW BELL ? Y/N ","Y" ); show_tst := inputtext("SHOW TEST ? Y/N ","Y" ); show_hf := inputtext("SHOW HAPPY FACE ? Y/N ","Y"); show_all_entries := show_all_points OR ( entry_SAP = "Y" OR entry_SAP = "y"); show_all_exits := show_all_points OR (exit_SAP = "Y" OR exit_SAP = "y"); show_INDICES := (show_INDEX_info ="Y" OR show_INDEX_info ="y"); show_bells := show_all_points OR (show_bell = "Y" OR show_bell = "y"); show_test := (show_tst= "Y" OR show_tst = "y"); show_syst := (show_system = "Y" OR show_system = "y"); apply_system_stop := inputtext("APPLY SYSTEM STOP? Y/N ", "N"); system_on := apply_system_stop="Y" OR apply_system_stop="y"; show_happy_face := (show_hf= "Y" OR show_hf = "y"); roc_bars := input("ROC BARS : ", 52, 1, 100); max_bar_num := lastvalue(barnumber); lastbar := barnumber = max_bar_num; 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; hb := if(history_bars=0,lastvalue(barnumber),history_bars); { **** 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 := XJO312UP;{if(USA_stock,NAS312UP,XJO312UP);} secindx := if ( isdefined(security.sectorindex), " ("+security.sectorindex+")", " "); { **** I OWN THIS STOCK **** } stock_boughtprice := if ( security.symbol = "A2M", 11.324, if ( security.symbol = "NEA", 2.0079, if ( security.symbol = "WTC", 19.42087, if ( security.symbol = "PME", 13.55, if ( security.symbol = "NHC", 3.81445, if ( security.symbol = "XRO", 42.965, if ( security.symbol = "SAR", 3.385, if ( security.symbol = "APX", 17.319, if ( security.symbol = "CPU", 20.605, if ( security.symbol = "NWH", 2.0723, if ( security.symbol = "BVS", 4.602395, if ( security.symbol = "xxx", 2.518, if ( security.symbol = "PLS", 1.134, if ( security.symbol = "xxx", 8.21, if ( security.symbol = "xxx", 0.279, if ( security.symbol = "xxx", 5.678, if ( security.symbol = "xxx", 1.614, if ( security.symbol = "xxx", 22.557, if ( security.symbol = "xxx", 5.714, if ( security.symbol = "xxx", 16.949, if ( security.symbol = "xxx", 6.047, if ( security.symbol = "xxx", 18.327, if ( security.symbol = "xxx", 34.569, { UK STOCKS } if ( security.symbol = "xxx", 1850.3, if ( security.symbol = "xxx", 445.09, if ( security.symbol = "xxx", 1377.4, { USA STOCKS } if ( security.symbol = "US.AAPL", 205.848, if ( security.symbol = "US.NFLX", 392.479, if ( security.symbol = "US.NTAP", 78.395, if ( security.symbol = "US.AMZN", 1814, if ( security.symbol = "US.MA", 207.287, if ( security.symbol = "US.MSFT", 109.306, if ( security.symbol = "US.ADBE", 273.11, if ( security.symbol = "US.xxx", 189.6667, if ( security.symbol = "xxx", 185.946, if ( security.symbol = "xxx", 85.687, 0)))))))))))))))))))))))))))))))))))); stockboughtdate := if( security.symbol = "A2M", date(2019,1,16), if( security.symbol = "NEA", date(2019,1,17), if( security.symbol = "WTC", date(2019,1,17), if( security.symbol = "PME", date(2019,1,18), if( security.symbol = "NHC", date(2019,1,22), if( security.symbol = "XRO", date(2019,2,4), if( security.symbol = "SAR", date(2019,2,4), if( security.symbol = "CPU", date(2018,11,9), if( security.symbol = "APX", date(2019,2,6), if( security.symbol = "NWH", date(2019,2,11), if( security.symbol = "BVS", date(2019,2,11), if( security.symbol = "PLS", date(2017,11,10), if( security.symbol = "xxx", date(2018,6,25), if( security.symbol = "xxx", date(2018,1,15), if( security.symbol = "xxx", date(2018,3,20), if( security.symbol = "xxx", date(2018,3,16), if( security.symbol = "xxx", date(2018,6,28), if( security.symbol = "xxx", date(2018,9,27), if( security.symbol = "xxx", date(2018,6,27), if( security.symbol = "xxx", date(2018,10,1), if( security.symbol = "TLG", date(2014,8,29), if( security.symbol = "xxx", date(2018,6,19), if( security.symbol = "xxx", date(2018,3,19), { UK STOCKS } if( security.symbol = "xxx", date(2017,10,5), if( security.symbol = "xxx", date(2017,10,9), if( security.symbol = "xxx", date(2018,1,4), { USA STOCKS } if( security.symbol = "US.AAPL", date(2018,8,3), if( security.symbol = "US.NFLX", date(2018,6,19), if( security.symbol = "US.NTAP", date(2018,6,21), if( security.symbol = "US.AMZN", date(2018,7,12), if( security.symbol = "US.MA", date(2018,7,20), if( security.symbol = "US.MSFT", date(2018,7,27), if( security.symbol = "US.CME", date(2018,11,9), if( security.symbol = "US.ADBE", date(2018,9,17), if( security.symbol = "xxx", date(2017,12,12), if( security.symbol = "xxx", date(2018,3,5), if( security.symbol = "xxx", date(2018,1,8), now()))))))))))))))))))))))))))))))))))))); iownthis := stock_boughtprice > 0; stock_profit_pc := round((close/stock_boughtprice-1)*100,2); displine := (lastvalue(barnumber) - barnumber) <10; boughttoday := onorskipped(stockboughtdate); [name=pc1;linestyle=pricecolor;color=magenta;width=4];iownthis AND boughttoday; [name=p2c;linestyle=pricecolor;color=red];NOT WHITE; [name=pc3;linestyle=pricecolor;color=green];WHITE; [name=dot;linestyle=marker;marker=type28;color=magenta]; iownthis AND boughttoday; { **** CALCULATE RAW AND SMOOTHED RATE OF CHANGE **** } emasmooth := 10; MA5 := 5; MA_method := SIMPLE; TP_ATR_num := 3; close_hf := 6; 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; { **** 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); first_HF := happy_face AND NOT hist(happy_face_nearby,1); big_jump := close/open >1.15; big_jump_nearby := barssince(big_jump)<6; { **** 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 ; no_action := NOT (exitcondition OR entercondition); testexit := exitcondition; testenter := NOT testexit AND white AND en_1 AND en_2 AND bell_nearby AND NOT big_jump_nearby AND NOT happy_face; INMKTtst := if( earlybar, FALSE, if( prev AND testexit, FALSE, if( NOT prev AND testenter, TRUE, prev))); buytst := NOT hist(INMKTtst,1) AND INMKTtst; selltst := hist(INMKTtst,1) AND NOT INMKTtst; {[name=en2;linestyle=marker;marker=type1];en_2; [name=en3;linestyle=marker;marker=type2];en_3;} {[name=enc;linestyle=marker;marker=type3];entercondition;} { **** ARE WE IN OR OUT OF THE MARKET? **** } INMKT := if( earlybar, FALSE, if( prev AND exitcondition, FALSE, if( NOT prev AND entercondition, TRUE, prev))); INMKT_s := if (INMKT," - INMKT", " - NOT INMKT"); { **** 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; {[name=bs;linestyle=marker;marker=type1];buysignal; [name=ss;linestyle=marker;marker=type1];sellsignal;} bars_since_last_exit := barssince(exitcondition); bars_since_exit := barssince(NOT exitcondition); { **** CALCULATE NUMBER OF WINS AND LOSSES **** } tomorrowopen := if(lastbar, CLOSE, future (OPEN, 1)); profit := if(earlybar,FALSE,tomorrowopen>=valuewhen(1,buysignal,tomorrowopen)); win := profit; loss := NOT profit; 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 , prev+1, prev)); total_trades := numwins+numlosses; winratio := if ( total_trades = 0, 0 , (numwins/total_trades)*100 ); RWINR := round(winratio); {[name=wr;linestyle=text];numwins+"/"+numlosses;close*0.4;} { **** CALCULATE NUMBER OF WINS AND LOSSES - TEST CASE **** } profittst := if(earlybar,FALSE,tomorrowopen>=valuewhen(1,buytst,tomorrowopen)); wintst := profittst; losstst := NOT profittst; numtstwins := if(earlybar,0,if(((lastbar AND INMKTtst) OR selltst) AND wintst, prev+1, prev)); numtstlosses := if(earlybar, 0,if(((lastbar AND INMKTtst) OR selltst) AND losstst, prev+1, prev)); total_tst_trades := numtstwins+numtstlosses; wintstratio := if ( total_tst_trades = 0, 0 , (numtstwins/total_tst_trades)*100 ); RWINTSTR := round(wintstratio); { **** CALCULATE LAP / GAP RATIO **** } tomorrowlow := if(lastbar, LOW, future (LOW, 1)); lappy := if(earlybar,0,if(entercondition AND tomorrowlow <= CLOSE, prev+1, prev)); gappy := if(earlybar,0,if(entercondition AND tomorrowlow > CLOSE, prev+1, prev)); total_lapgap := lappy+gappy; Rlgr := round(if ( total_lapgap = 0, 0 , (lappy/total_lapgap)*100 )); {[name=gp;linestyle=marker;marker=type8];entercondition AND tomorrowlow > CLOSE; [name=lp;linestyle=marker;marker=type9];entercondition AND tomorrowlow <= CLOSE; [name=lg;linestyle=text];lappy+"/"+gappy+"/"+Rlgr;CLOSE*0.6;} { **** CALCULATE BS SCORE **** } barsback := 50; lowline := ma(C,13,SIMPLE) - 1*ATR(21); up712_count := sum(ma(C,7,E)>ma(C,12,E),barsback); above_lowline_count := sum(C>lowline,barsback); BS_score := round(((up712_count+above_lowline_count)/(2*barsback))*100,2); { **** DISPLAY ENTRY / EXIT ARROWS IF APPROPRIATE **** } [Name = SAPinlb; Linestyle = marker; color=black;marker = type13;size=large;]; lastbar AND entercondition; [Name = SAPin; Linestyle = marker; color=black;marker = type13;size=tiny;]; NOT lastbar AND show_all_entries AND entercondition; [Name = SAPoutlb;linestyle=marker;marker=type14;color=red;size=large]; lastbar AND exitcondition; [Name = SAPout;linestyle=marker;marker=type14;color=red;size=tiny]; NOT lastbar AND show_all_exits AND exitcondition; [Name = NO-ACTION;linestyle=marker;marker=type12;color=orange;size=large]; lastbar AND no_action; [Name = NO-ACTIONt; Linestyle = text; color=blue;font=14;fontstyle=bold;]; if (lastbar, if( iownthis," IN PORTFOLIO ("+if(stock_profit_pc>0,"+","")+stock_profit_pc+"%)"," NOT IN PORTFOLIO"),undefined);close; if (lastbar AND no_action," HOLD "+security.symbol+secindx+INMKT_s,undefined);close*0.9; [color=green]; if (lastbar AND entercondition, " BUY "+security.symbol+secindx+INMKT_s,undefined);close*0.9; [color=red]; if (lastbar AND exitcondition, " SELL "+security.symbol+secindx+" NOW",undefined);close*0.9; [color=blue]; if (lastbar,+" WR:"+RWINR+"% of "+total_trades+" | TWR:"+RWINTSTR+"% of "+total_tst_trades,undefined);close*0.8; if (lastbar,+" BS:"+BS_score+" | LGR:"+Rlgr,undefined);close*0.7; if (lastbar,+" "+hb+" bars from "+hist(now, hb-1 ),undefined);close*0.6; [color=green]; if (lastbar {AND NOT USA_stock} AND hist(XJO312UP,1), " XJO - SYSTEM OPEN", "");CLOSE * 0.5; [color=red]; if (lastbar {AND NOT USA_stock} AND NOT hist(XJO312UP,1), " XJO - SYSTEM CLOSED", "");CLOSE * 0.5; {[color=green]; if (lastbar AND USA_stock AND NAS312UP, " NASDAQ - SYSTEM OPEN", "");CLOSE * 0.5; [color=red]; if (lastbar AND USA_stock AND NOT NAS312UP, " NASDAQ - SYSTEM CLOSED", "");CLOSE * 0.5; [color=green]; if (lastbar AND USA_stock AND SP500312UP, " SP500 - SYSTEM OPEN", "");CLOSE * 0.45; [color=red]; if (lastbar AND USA_stock AND NOT SP500312UP, " SP500 - SYSTEM CLOSED", "");CLOSE * 0.45;} { **** DISPLAY INDEX TREND CONDITIONS **** } [Name= INDEX UP;linestyle=text;color=blue;fontsize=14;font=bold;textalign=centre,right;] {if(show_INDICES AND lastbar AND USA_stock, if(NAS312UP, "OK TO ENTER - NASDAQ IN UP TREND FOR LAST "+barssince(NOT NAS312UP)+" BARS","NASDAQ IN DOWN TREND FOR LAST "+barssince(NAS312UP)+" BARS - DO NOT ENTER NEW NASDAQ POSITIONS"),undefined);CLOSE*0.6; if(show_INDICES AND lastbar AND USA_stock, if(SP500312UP, "OK TO ENTER - SP4500 IN UP TREND FOR LAST "+barssince(NOT SP500312UP)+" BARS","SP500 IN DOWN TREND FOR LAST "+barssince(SP500312UP)+" BARS - DO NOT ENTER NEW SP500 POSITIONS"),undefined);CLOSE*0.5;} if(show_INDICES AND lastbar {AND NOT USA_stock}, if(XJO312UP ,"OK TO ENTER - XJO IN UP TREND FOR LAST "+barssince(NOT XJO312UP)+" BARS","XJO IN DOWN TREND FOR LAST"+barssince(XJO312UP)+" BARS - DO NOT ENTER NEW XJO POSITIONS"),undefined);CLOSE*0.4; if(lastbar AND exitcondition AND show_INDICES,"LAST "+bars_since_exit+" BARS OF "+security.symbol+" SAY EXIT - SELL IMMEDIATELY IF HELD",undefined);CLOSE*0.3; if(lastbar AND entercondition AND show_INDICES,security.symbol+" - OK TO BUY - OR HOLD IF HELD",undefined);CLOSE*0.3; if(lastbar AND no_action AND show_INDICES,security.symbol+" - DO NOTHING - OK TO HOLD IF HELD",undefined);CLOSE*0.3; { **** DISPLAY ROC UP TICK BELL **** } [name=ROCu;linestyle=marker;marker=type24;size=tiny;position=below;]show_bells AND bell_nearby; { **** DISPLAY ENTRY AND EXIT ARROWS **** } [Name = EXIT;linestyle=marker;marker=type14;;visible=true;size=tiny;];show_syst AND exitcondition AND hist(INMKT,1); [Name = ENTER; Linestyle = marker;marker = type13;size=tiny;];show_syst AND buysignal OR( lastbar AND entercondition AND (NOT hist(INMKT,1))); [Name = P; Linestyle = marker;marker = type15;size=tiny;]; show_syst AND (sellsignal OR (lastbar AND INMKT)) AND profit; [Name = L; Linestyle = marker;marker = type20;size=tiny;]; show_syst AND (sellsignal OR (lastbar AND INMKT)) AND loss; [Name = HF; Linestyle = marker;marker = type21;size=tiny;]; show_happy_face AND happy_face; { **** DISPLAY TEST ARROWS **** } [Name = tstin; Linestyle = marker; color=green;marker = type13;size=tiny;visible=false]; show_test AND testenter; [Name = tstout;linestyle=marker;marker=type14;color=green;size=tiny;visible=false]; show_test AND testexit; [Name = EXITtst;linestyle=marker;marker=type14;color=green;visible=true;size=tiny;];show_test AND testexit AND hist(INMKTtst,1); [Name = ENTERtst; Linestyle = marker;marker = type13;color=green;size=tiny;];show_test AND (buytst OR( lastbar AND testenter AND (NOT hist(INMKTtst,1)))); [Name = Ptst; Linestyle = marker;marker = type15;color=green;size=tiny;]; show_test AND (selltst OR (lastbar AND INMKTtst)) AND wintst; [Name = Ltst; Linestyle = marker;marker = type20;color=green;size=tiny;]; show_test AND (selltst OR (lastbar AND INMKTtst)) AND losstst;