// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © Hiubris_Indicators //@version=5 strategy(title = "Pivot Points Stop Strategy", overlay = true, default_qty_value = 100, max_lines_count=500, max_labels_count=500, initial_capital=100000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true) shiftMonday= input(true, title='Jump over Sunday') max_lines = input(2, title='Number of Days to show Pivots') is_newbar(res) => t = time(res) not na(t) and (na(t[1]) or t > t[1]) // PIVOT POINTS STANDARD show_pivots1 = true col1 = input.color(color.orange, title="S/R Colors") col0 = input.color(color.purple, title="Pivot Color") tz = syminfo.timezone get_pivots() => lb = dayofweek==dayofweek.monday and shiftMonday? 2 : 1 xHigh = request.security(syminfo.tickerid, 'D', high [lb], barmerge.gaps_off, barmerge.lookahead_on) xLow = request.security(syminfo.tickerid, 'D', low [lb], barmerge.gaps_off, barmerge.lookahead_on) xClose = request.security(syminfo.tickerid, 'D', close[lb], barmerge.gaps_off, barmerge.lookahead_on) xpivot = (xHigh + xLow + xClose) / 3 xrange = xHigh - xLow xR1 = xpivot + xpivot - xLow xS1 = xpivot - (xHigh - xpivot) xR2 = xpivot + xrange xS2 = xpivot - xrange xR3 = xpivot + 2 * xrange xS3 = xpivot - 2 * xrange xR4 = xpivot + 3 * xrange xS4 = xpivot - 3 * xrange [xpivot, xR1, xS1, xR2, xS2, xR3, xS3, xR4, xS4] [pi, R1, S1, R2, S2, R3, S3, R4, S4] = get_pivots() shiftCond = shiftMonday? dayofweek!=dayofweek.monday : true draw_label(x, txt, col) => newpiv = is_newbar('D') and shiftCond newpiv_n = ta.valuewhen(newpiv, bar_index, 0) var lblArr = array.new_label() if newpiv array.push(lblArr, label.new(newpiv_n , x, style=label.style_none, textcolor=col, text=txt)) if array.size(lblArr)>max_lines label.delete(array.shift(lblArr)) var lnArr1 = array.new_line() var lnArr2 = array.new_line() if newpiv array.push(lnArr1, line.new(bar_index[1], x[1], newpiv_n[1], x[1], color=col)) array.push(lnArr2, line.new(bar_index, x, bar_index+1[1], x, color=col)) if array.size(lnArr2) > 0 last_line = array.get(lnArr2, 0) line.set_xy1(last_line, newpiv_n , x) line.set_xy2(last_line, bar_index, x) if array.size(lnArr1)>max_lines-1 line.delete(array.shift(lnArr1)) if array.size(lnArr2)>1 line.delete(array.shift(lnArr2)) if show_pivots1 draw_label(pi, 'P' , col0) draw_label(S1, 'S1', col1) draw_label(R1, 'R1', col1) draw_label(S2, 'S2', col1) draw_label(R2, 'R2', col1) draw_label(S3, 'S3', col1) draw_label(R3, 'R3', col1) draw_label(S4, 'S4', col1) draw_label(R4, 'R4', col1) // Trading Session tf = input.timeframe('60', title='Timeframe') tc_tf = request.security(syminfo.tickerid, tf, time_close) TIME = time(tf) h = input.int(09, title='  Check 1st Candle Time', inline='ttt') m = input.int(00, title=':', inline='ttt') in_session(sess) => t = time(timeframe.period, sess, tz) r = na(t) ? false : true r session = input.session("0900-1600", title="Trading Session")+":1234567" inSess = in_session(session) var pos = 0.0 trade = closeS4 isTime_ = inSess and time_close==tc_tf and trade and nz(pos[1])==0 var sent = 0 sent:= inSess and not inSess[1]? (isTime_? 1 : 0) : isTime_ and nz(sent[1])==0? 1 : sent[1] isTime = isTime_ and (nz(sent[1])==0 or (inSess and not inSess[1])) below = close>R3? R3 : close>R3? R3 : close>R2? R2 : close>R1? R1 : close>pi? pi : close>S1? S1 : close>S2? S2 : close>S3? S3 : S4 above = close buy_price10 = 0.0 sell_price10= 0.0 buy_price20 = 0.0 sell_price20= 0.0 if close>R3 and closebuy_price1 , 0) and buy_price1 !=0 short_limit1 = ta.valuewhen(isTime, closebuy_price2 , 0) and buy_price2 !=0 short_limit2 = ta.valuewhen(isTime, closebuy_price1 short1= sell_price1==0? false : short_limit1? high>sell_price1 : highbuy_price2 short2= sell_price2==0? false : short_limit2? high>sell_price2 : high=0 or na(pos[1])) long_exit = dayofweek==dayofweek.friday and ExitSess and pos[1]>0 short_exit= dayofweek==dayofweek.friday and ExitSess and pos[1]<0 if (long_exit and not shortCond) or (short_exit and not longCond) pos:=0 sent := 0 // EXIT FUNCTIONS // sl = input.float(40.0, title="Stop Loss     ", minval=0, step=0.1, inline='sl ') sl_sw = input.string('Pips', title='', inline='sl', options=['Pips', '%']) use_tp1 = input(true, title="", inline="tp1") use_tp2 = input(true, title="", inline="tp2") use_tp3 = input(true, title="", inline="tp3") use_tp4 = input(true, title="", inline="tp4") use_tp5 = input(true, title="", inline="tp5") use_tp6 = input(true, title="", inline="tp6") use_tp7 = input(true, title="", inline="tp7") use_tp8 = input(true, title="", inline="tp8") tp_sw = input.string('Pips', title='Take Profits', options=['Pips', '%']) tp1 = input.float(20.0, title="Take Profit 1", minval=0, step=0.1, inline='tp1') tp2 = input.float(40.0, title="Take Profit 2", minval=0, step=0.1, inline='tp2') tp3 = input.float(60.0, title="Take Profit 3", minval=0, step=0.1, inline='tp3') tp4 = input.float(80.0, title="Take Profit 4", minval=0, step=0.1, inline='tp4') tp5 = input.float(100.0, title="Take Profit 5", minval=0, step=0.1, inline='tp5') tp6 = input.float(120.0, title="Take Profit 6", minval=0, step=0.1, inline='tp6') tp7 = input.float(140.0, title="Take Profit 7", minval=0, step=0.1, inline='tp7') tp8 = input.float(160.0, title="Take Profit 8", minval=0, step=0.1, inline='tp8') tsl = input.float(60.0, title="Trailing SL Value ", minval=0, step=0.1, inline='tsl') trig = input.float(40.0, title="Trailing SL Trigger", minval=0, step=0.1, inline='trig') tsl_sw = input.string('Pips', title='', inline='tsl', options=['Pips', '%']) pre_perc_tp1 = input(12.5, title="Qty%", inline="tp1") pre_perc_tp2 = input(12.5, title="Qty%", inline="tp2") pre_perc_tp3 = input(12.5, title="Qty%", inline="tp3") pre_perc_tp4 = input(12.5, title="Qty%", inline="tp4") pre_perc_tp5 = input(12.5, title="Qty%", inline="tp5") pre_perc_tp6 = input(12.5, title="Qty%", inline="tp6") pre_perc_tp7 = input(12.5, title="Qty%", inline="tp7") pre_perc_tp8 = input(12.5, title="Qty%", inline="tp8") be = input(20.0, title='Break Even', inline='be') be_sw = input.string('Pips', title='', inline='be', options=['Pips', '%']) perc_tp1 = pre_perc_tp1==0? 100 : pre_perc_tp1 perc_tp2 = pre_perc_tp2==0? 100 : pre_perc_tp2 perc_tp3 = pre_perc_tp3==0? 100 : pre_perc_tp3 perc_tp4 = pre_perc_tp4==0? 100 : pre_perc_tp4 perc_tp5 = pre_perc_tp5==0? 100 : pre_perc_tp5 perc_tp6 = pre_perc_tp5==0? 100 : pre_perc_tp6 perc_tp7 = pre_perc_tp5==0? 100 : pre_perc_tp7 perc_tp8 = pre_perc_tp5==0? 100 : pre_perc_tp8 sum_tp8 = perc_tp1 + perc_tp2 + perc_tp3 + perc_tp4 + perc_tp5 + perc_tp6 + perc_tp7 + perc_tp8 sum_tp7 = perc_tp1 + perc_tp2 + perc_tp3 + perc_tp4 + perc_tp5 + perc_tp6 + perc_tp7 sum_tp6 = perc_tp1 + perc_tp2 + perc_tp3 + perc_tp4 + perc_tp5 + perc_tp6 sum_tp5 = perc_tp1 + perc_tp2 + perc_tp3 + perc_tp4 + perc_tp5 sum_tp4 = perc_tp1 + perc_tp2 + perc_tp3 + perc_tp4 sum_tp3 = perc_tp1 + perc_tp2 + perc_tp3 sum_tp2 = perc_tp1 + perc_tp2 sum_tp1 = perc_tp1 get_sltp(buy_price, sell_price) => long_entry = ta.valuewhen(isTime, buy_price , 0) short_entry = ta.valuewhen(isTime, sell_price, 0) // Trailing SL trig_long1 = ta.valuewhen(isTime, buy_price + trig, 0) trig_short1 = ta.valuewhen(isTime, sell_price - trig, 0) trig_long2 = ta.valuewhen(isTime, buy_price * ( 1+ trig/100), 0) trig_short2 = ta.valuewhen(isTime, sell_price * ( 1- trig/100), 0) trig_long = tsl_sw=='Pips'? trig_long1 : trig_long2 trig_short = tsl_sw=='Pips'? trig_short1 : trig_short2 inprofit1_long = 0 inprofit1_short = 0 inprofit1_long := isTime? 0 : high>trig_long and (pos>0 and not longCond )? 1 : inprofit1_long[1] inprofit1_short := isTime? 0 : low trail_long[1]? high : pos<1 ? 0 : trail_long[1] trail_short := shortCond? low : low-1 ? 99999 : trail_short[1] trail_long_final = inprofit1_long ==1? (be_sw=='Pips'? trail_long - tsl : trail_long * (1 - tsl/100)) : 0 trail_short_final = inprofit1_short==1? (be_sw=='Pips'? trail_short + tsl : trail_short * (1 + tsl/100)) : 99999 // Simple Stop Loss and Take Profit sl_long1 = long_entry - sl sl_short1 = short_entry + sl sl_long2 = long_entry * (1 - sl/100) sl_short2 = short_entry * (1 + sl/100) sl_long0 = sl_sw=='Pips' ? sl_long1 : sl_long2 sl_short0 = sl_sw=='Pips' ? sl_short1 : sl_short2 tp_long1 = tp_sw=='Pips' ? long_entry + tp1 : long_entry * (1 + tp1/100) tp_long2 = tp_sw=='Pips' ? long_entry + tp2 : long_entry * (1 + tp2/100) tp_long3 = tp_sw=='Pips' ? long_entry + tp3 : long_entry * (1 + tp3/100) tp_long4 = tp_sw=='Pips' ? long_entry + tp4 : long_entry * (1 + tp4/100) tp_long5 = tp_sw=='Pips' ? long_entry + tp5 : long_entry * (1 + tp5/100) tp_long6 = tp_sw=='Pips' ? long_entry + tp6 : long_entry * (1 + tp6/100) tp_long7 = tp_sw=='Pips' ? long_entry + tp7 : long_entry * (1 + tp7/100) tp_long8 = tp_sw=='Pips' ? long_entry + tp8 : long_entry * (1 + tp8/100) tp_short1 = tp_sw=='Pips' ? short_entry - tp1 : short_entry * (1 - tp1/100) tp_short2 = tp_sw=='Pips' ? short_entry - tp2 : short_entry * (1 - tp2/100) tp_short3 = tp_sw=='Pips' ? short_entry - tp3 : short_entry * (1 - tp3/100) tp_short4 = tp_sw=='Pips' ? short_entry - tp4 : short_entry * (1 - tp4/100) tp_short5 = tp_sw=='Pips' ? short_entry - tp5 : short_entry * (1 - tp5/100) tp_short6 = tp_sw=='Pips' ? short_entry - tp6 : short_entry * (1 - tp6/100) tp_short7 = tp_sw=='Pips' ? short_entry - tp7 : short_entry * (1 - tp7/100) tp_short8 = tp_sw=='Pips' ? short_entry - tp8 : short_entry * (1 - tp8/100) sl_long = math.max(sl_long0 , trail_long_final) sl_short = math.min(sl_short0, trail_short_final) last_tp_long = use_tp8 and sum_tp8>=100? tp_long8 : use_tp7 and sum_tp7>=100? tp_long7 : use_tp6 and sum_tp6>=100? tp_long6 : use_tp5 and sum_tp5>=100? tp_long5 : use_tp4 and sum_tp4>=100? tp_long4 : use_tp3 and sum_tp3>=100? tp_long3 : use_tp2 and sum_tp2>=100? tp_long2 : use_tp1 and sum_tp1>=100? tp_long1 : 99999 last_tp_short = use_tp8 and sum_tp8>=100? tp_short8 : use_tp7 and sum_tp7>=100? tp_short7 : use_tp6 and sum_tp6>=100? tp_short6 : use_tp5 and sum_tp5>=100? tp_short5 : use_tp4 and sum_tp4>=100? tp_short4 : use_tp3 and sum_tp3>=100? tp_short3 : use_tp2 and sum_tp2>=100? tp_short2 : use_tp1 and sum_tp1>=100? tp_short1 : 0 // Trailing Stop Loss be_long1 = ta.valuewhen(isTime, buy_price + be, 0) be_short1 = ta.valuewhen(isTime, sell_price - be, 0) be_long2 = ta.valuewhen(isTime, buy_price * (1 + be/100), 0) be_short2 = ta.valuewhen(isTime, sell_price * (1 - be/100), 0) be_long = be_sw=='Pips' ? be_long1 : be_long2 be_short = be_sw=='Pips' ? be_short1 : be_short2 inprofit_long = 0 inprofit_short = 0 inprofit_long := isTime? 0 : high>be_long and (pos>0 and not longCond )? 1 : inprofit_long[1] inprofit_short := isTime? 0 : low sl_short_1[1] and pos[1]==-1) or (high>sl_short_1 and shortCond and short1) or (high>sl_short_2[1] and pos[1]==-2) or (high>sl_short_2 and shortCond and short2) final_long_tp = (high>last_tp_long_1 [1] and pos[1]== 1) or (high>last_tp_long_1 and longCond and long1 ) or (high>last_tp_long_2 [1] and pos[1]== 2) or (high>last_tp_long_2 and longCond and long2 ) final_short_tp = (low i_startTime) and (time < i_endTime) equity = strategy.initial_capital + strategy.netprofit if equity>0 and timeCond if isTime // 1 if long_limit1 and buy_price1!=0 strategy.entry("long1" , strategy.long , oca_name='entry', oca_type=strategy.oca.cancel, limit=buy_price1 ) if not long_limit1 and buy_price1!=0 strategy.entry("long1" , strategy.long , oca_name='entry', oca_type=strategy.oca.cancel, stop=buy_price1 ) if short_limit1 and sell_price1!=0 strategy.entry("short1", strategy.short, oca_name='entry', oca_type=strategy.oca.cancel, limit=sell_price1) if not short_limit1 and sell_price1!=0 strategy.entry("short1", strategy.short, oca_name='entry', oca_type=strategy.oca.cancel, stop=sell_price1) if pre_perc_tp1>0 and use_tp1 strategy.exit("SL/TP1_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long1_1 , comment_profit ='TP1', comment_loss='SL', qty_percent=perc_tp1) strategy.exit("SL/TP1_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short1_1, comment_profit ='TP1', comment_loss='SL', qty_percent=perc_tp1) if pre_perc_tp2>0 and use_tp2 strategy.exit("SL/TP2_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long2_1, comment_profit ='TP2', comment_loss='SL', qty_percent=perc_tp2) strategy.exit("SL/TP2_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short2_1, comment_profit ='TP2', comment_loss='SL', qty_percent=perc_tp2) if pre_perc_tp3>0 and use_tp3 strategy.exit("SL/TP3_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long3_1, comment_profit ='TP3', comment_loss='SL', qty_percent=perc_tp3) strategy.exit("SL/TP3_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short3_1, comment_profit ='TP3', comment_loss='SL', qty_percent=perc_tp3) if pre_perc_tp4>0 and use_tp4 strategy.exit("SL/TP4_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long4_1, comment_profit ='TP4', comment_loss='SL', qty_percent=perc_tp4) strategy.exit("SL/TP4_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short4_1, comment_profit ='TP4', comment_loss='SL', qty_percent=perc_tp4) if pre_perc_tp5>0 and use_tp5 strategy.exit("SL/TP5_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long5_1, comment_profit ='TP5', comment_loss='SL', qty_percent=perc_tp5) strategy.exit("SL/TP5_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short5_1, comment_profit ='TP5', comment_loss='SL', qty_percent=perc_tp5) if pre_perc_tp6>0 and use_tp6 strategy.exit("SL/TP6_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long6_1, comment_profit ='TP6', comment_loss='SL', qty_percent=perc_tp6) strategy.exit("SL/TP6_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short6_1, comment_profit ='TP6', comment_loss='SL', qty_percent=perc_tp6) if pre_perc_tp7>0 and use_tp7 strategy.exit("SL/TP7_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long7_1, comment_profit ='TP7', comment_loss='SL', qty_percent=perc_tp7) strategy.exit("SL/TP7_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short7_1, comment_profit ='TP7', comment_loss='SL', qty_percent=perc_tp7) if pre_perc_tp8>0 and use_tp8 strategy.exit("SL/TP8_1", from_entry = "long1" , stop=sl_long_1 , limit=tp_long8_1, comment_profit ='TP8', comment_loss='SL', qty_percent=perc_tp8) strategy.exit("SL/TP8_1", from_entry = "short1" , stop=sl_short_1 , limit=tp_short8_1, comment_profit ='TP8', comment_loss='SL', qty_percent=perc_tp8) strategy.exit("SL_1", from_entry = "long1" , stop=sl_long_1) strategy.exit("SL_1", from_entry = "short1" , stop=sl_short_1) if isTime // 2 if long_limit2 and buy_price2!=0 strategy.entry("long2" , strategy.long , oca_name='entry', oca_type=strategy.oca.cancel, limit=buy_price2 ) if not long_limit2 and buy_price2!=0 strategy.entry("long2" , strategy.long , oca_name='entry', oca_type=strategy.oca.cancel, stop=buy_price2 ) if short_limit2 and sell_price2!=0 strategy.entry("short2", strategy.short, oca_name='entry', oca_type=strategy.oca.cancel, limit=sell_price2) if not short_limit2 and sell_price2!=0 strategy.entry("short2", strategy.short, oca_name='entry', oca_type=strategy.oca.cancel, stop=sell_price2) if pre_perc_tp1>0 and use_tp1 strategy.exit("SL/TP12", from_entry = "long2" , stop=sl_long_2 , limit=tp_long2_2 , comment_profit ='TP1', comment_loss='SL', qty_percent=perc_tp1) strategy.exit("SL/TP12", from_entry = "short2" , stop=sl_short_2 , limit=tp_short2_2, comment_profit ='TP1', comment_loss='SL', qty_percent=perc_tp1) if pre_perc_tp2>0 and use_tp2 strategy.exit("SL/TP22", from_entry = "long2" , stop=sl_long_2 , limit=tp_long2_2, comment_profit ='TP2', comment_loss='SL', qty_percent=perc_tp2) strategy.exit("SL/TP22", from_entry = "short2" , stop=sl_short_2 , limit=tp_short2_2, comment_profit ='TP2', comment_loss='SL', qty_percent=perc_tp2) if pre_perc_tp3>0 and use_tp3 strategy.exit("SL/TP32", from_entry = "long2" , stop=sl_long_2 , limit=tp_long3_2, comment_profit ='TP3', comment_loss='SL', qty_percent=perc_tp3) strategy.exit("SL/TP32", from_entry = "short2" , stop=sl_short_2 , limit=tp_short3_2, comment_profit ='TP3', comment_loss='SL', qty_percent=perc_tp3) if pre_perc_tp4>0 and use_tp4 strategy.exit("SL/TP42", from_entry = "long2" , stop=sl_long_2 , limit=tp_long4_2, comment_profit ='TP4', comment_loss='SL', qty_percent=perc_tp4) strategy.exit("SL/TP42", from_entry = "short2" , stop=sl_short_2 , limit=tp_short4_2, comment_profit ='TP4', comment_loss='SL', qty_percent=perc_tp4) if pre_perc_tp5>0 and use_tp5 strategy.exit("SL/TP52", from_entry = "long2" , stop=sl_long_2 , limit=tp_long5_2, comment_profit ='TP5', comment_loss='SL', qty_percent=perc_tp5) strategy.exit("SL/TP52", from_entry = "short2" , stop=sl_short_2 , limit=tp_short5_2, comment_profit ='TP5', comment_loss='SL', qty_percent=perc_tp5) if pre_perc_tp6>0 and use_tp6 strategy.exit("SL/TP62", from_entry = "long2" , stop=sl_long_2 , limit=tp_long6_2, comment_profit ='TP6', comment_loss='SL', qty_percent=perc_tp6) strategy.exit("SL/TP62", from_entry = "short2" , stop=sl_short_2 , limit=tp_short6_2, comment_profit ='TP6', comment_loss='SL', qty_percent=perc_tp6) if pre_perc_tp7>0 and use_tp7 strategy.exit("SL/TP72", from_entry = "long2" , stop=sl_long_2 , limit=tp_long7_2, comment_profit ='TP7', comment_loss='SL', qty_percent=perc_tp7) strategy.exit("SL/TP72", from_entry = "short2" , stop=sl_short_2 , limit=tp_short7_2, comment_profit ='TP7', comment_loss='SL', qty_percent=perc_tp7) if pre_perc_tp8>0 and use_tp8 strategy.exit("SL/TP82", from_entry = "long2" , stop=sl_long_2 , limit=tp_long8_2, comment_profit ='TP8', comment_loss='SL', qty_percent=perc_tp8) strategy.exit("SL/TP82", from_entry = "short2" , stop=sl_short_2 , limit=tp_short8_2, comment_profit ='TP8', comment_loss='SL', qty_percent=perc_tp8) strategy.exit("SL2", from_entry = "long2" , stop=sl_long_2) strategy.exit("SL2", from_entry = "short2" , stop=sl_short_2) if not inSess and not isTime strategy.cancel('long1' ) strategy.cancel('short1') strategy.cancel('long2' ) strategy.cancel('short2') if long_exit and timeCond strategy.close("long1" , comment="Exit") if short_exit and timeCond strategy.close("short1", comment="Exit") if long_exit and timeCond strategy.close("long2" , comment="Exit") if short_exit and timeCond strategy.close("short2", comment="Exit")