// 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 = "QTrend MA SAR Strategy", overlay = true, default_qty_value = 100, initial_capital=100000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true) max_bars = input(9, title='Max Candles since the Q-Trend condition') // Heikin Ashi Functions is_HA = chart.is_heikinashi ha_close= (open + close + high + low)/4 ha_open = 0.0 ha_open:= (nz(ha_open[1])+ha_close[1]) /2 ha_high = math.max(math.max(math.max(ha_close, ha_open), high), low) ha_low = math.min(math.min(math.min(ha_close, ha_open), high), low) ha_green = ha_close>ha_open ha_red = ha_close e1 = ta.ema(src, length) e2 = ta.ema(e1, length) dema = 2 * e1 - e2 dema ma(source, length, type) => type == "SMA" ? ta.sma(source, length) : type == "EMA" ? ta.ema(source, length) : type == "SMMA (RMA)" ? ta.rma(source, length) : type == "WMA" ? ta.wma(source, length) : type == "VWMA" ? ta.vwma(source, length) : type == "DEMA" ? dema(source, length) : na ma1_type = input.string("EMA" , "" , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=ribbon_grp) ma1_length = input.int (200 , "" , inline="MA #1", minval=1, group=ribbon_grp) ma1 = ma(final_close, ma1_length, ma1_type) plot(ma1, color = #f6c309, title="MA №1") security_f(x, tf)=> htf = request.security(syminfo.tickerid, tf, x[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_on)[barstate.isrealtime ? 0 : 1] ctf = x r = tf=="" or tf==timeframe.period ? ctf : htf r // SAR start = input(0.02, group='PSAR') increment = input(0.02, group='PSAR') maximum = input(0.2, "Max Value", group='PSAR') out = ta.sar(start, increment, maximum) sar = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, out, barmerge.gaps_off, barmerge.lookahead_on) plot(sar, "ParabolicSAR", style=plot.style_cross, color=#2962FF) // Inputs src = input(close, "Source", group = "Main settings") p = input.int(200, "Trend period", group = "Main settings", tooltip = "Changes STRONG signals' sensitivity.", minval = 1) atr_p = input.int(14, "ATR Period", group = "Main settings", minval = 1) mult = input.float(1.0, "ATR Multiplier", step = 0.1, group = "Main settings", tooltip = "Changes sensitivity: higher period = higher sensitivty.") mode = input.string("Type A", "Signal mode", options = ["Type A", "Type B"], group = "Mode") use_ema_smoother = input.string("No", "Smooth source with EMA?", options = ["Yes", "No"], group = "Source") src_ema_period = input(3, "EMA Smoother period", group = "Source") color_bars = input(true, "Color bars?", group = "Addons") show_tl = input(true, "Show trend line?", group = "Addons") signals_view = input.string("All", "Signals to show", options = ["All", "Buy/Sell", "Strong", "None"], group = "Signal's Addon") signals_shape = input.string("Labels", "Signal's shape", options = ["Labels", "Arrows"], group = "Signal's Addon") buy_col = input(color.green, "Buy colour", group = "Signal's Addon", inline = "BS") sell_col = input(color.red, "Sell colour", group = "Signal's Addon", inline = "BS") src := use_ema_smoother == "Yes" ? ta.ema(src, src_ema_period) : src // Source; qtrend() => h = ta.highest(src, p) // Highest of src p-bars back; l = ta.lowest(src, p) // Lowest of src p-bars back. d = h - l ls = "" // Tracker of last signal m = (h + l) / 2 // Initial trend line; m := bar_index > p ? m[1] : m atr = ta.atr(atr_p)[1] // ATR; epsilon = mult * atr // Epsilon is a mathematical variable used in many different theorems in order to simplify work with mathematical object. Here it used as sensitivity measure. change_up = (mode == "Type B" ? ta.cross(src, m + epsilon) : ta.crossover(src, m + epsilon)) or src > m + epsilon // If price breaks trend line + epsilon (so called higher band), then it is time to update the value of a trend line; change_down = (mode == "Type B" ? ta.cross(src, m - epsilon) : ta.crossunder(src, m - epsilon)) or src < m - epsilon // If price breaks trend line - epsilon (so called higher band), then it is time to update the value of a trend line. sb = open < l + d / 8 and open >= l ss = open > h - d / 8 and open <= h strong_buy = sb or sb[1] or sb[2] or sb[3] or sb[4] strong_sell = ss or ss[1] or ss[2] or ss[3] or ss[4] m := (change_up or change_down) and m != m[1] ? m : change_up ? m + epsilon : change_down ? m - epsilon : nz(m[1], m) // Updating the trend line. ls := change_up ? "B" : change_down ? "S" : ls[1] // Last signal. Helps avoid multiple labels in a row with the same signal; long0 = change_up and ls[1] != "B" short0= change_down and ls[1] != "S" [long0, short0] [long0, short0] = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, qtrend(), barmerge.gaps_off, barmerge.lookahead_on) // Chart Plot & Alerts plotshape(long0 , textcolor=color.lime, color=color.lime, style=shape.triangleup , title="Q Buy" , text="Q" , location=location.belowbar, offset=0, size=size.tiny) plotshape(short0, textcolor=color.red, color=color.red , style=shape.triangledown, title="Q Sell", text="Q", location=location.abovebar , offset=0, size=size.tiny) tz = input.string('UTC+2', title='TimeZone') session = input.session("0800-1800", title="Trading Session")+":1234567" maxTradesDay = input(1, title='Maximum Trades per day') // Trading Session in_session(sess) => t = time(timeframe.period, sess, tz) r = na(t) ? false : true r inSess = in_session(session) var tradesDay = 0 long_ = ta.barssince(long0 )<=max_bars and final_close>ma1 and close>sar and inSess and tradesDay[1]=min short_= ta.barssince(short0)<=max_bars and final_closesl_short[1] and pos[1]==-1 final_long_tp = high>tp_long[1] and pos[1]==1 final_short_tp = low i_startTime) and (time < i_endTime) equity = strategy.initial_capital + strategy.netprofit if equity>0 and timeCond if longCond strategy.entry("long" , strategy.long ) if shortCond strategy.entry("short", strategy.short) strategy.exit("SL/TP", from_entry = "long" , stop=sl_long , limit=tp_long , comment_profit ='TP', comment_loss='SL') strategy.exit("SL/TP", from_entry = "short", stop=sl_short, limit=tp_short, comment_profit ='TP', comment_loss='SL') show_sltp = input(true, title="Show SL/TP Lines on Chart") xtl=plot(show_sltp and pos== 1? tp_long : na, color=color.green, style=plot.style_linebr, title="TP Long ") xts=plot(show_sltp and pos==-1? tp_short : na, color=color.green, style=plot.style_linebr, title="TP Short") xsl=plot(show_sltp and pos== 1? sl_long : na, color=color.red , style=plot.style_linebr, title="SL Long ") xss=plot(show_sltp and pos==-1? sl_short : na, color=color.red , style=plot.style_linebr, title="SL Short") xel=plot(show_sltp and pos== 1?long_entry : na, color=color.blue , style=plot.style_linebr, title="E Long ") xes=plot(show_sltp and pos==-1?short_entry: na, color=color.blue , style=plot.style_linebr, title="E Short") fill(xel, xtl, color=color.new(color.green, 80)) fill(xel, xsl, color=color.new(color.red , 80)) fill(xes, xts, color=color.new(color.green, 80)) fill(xes, xss, color=color.new(color.red , 80)) longCond_txt = syminfo.ticker + 'Buy \nEntry Price: ' + str.tostring(long_entry , format.mintick) + '\n' + 'SL: ' + str.tostring(sl_long , format.mintick) + '\n' + 'TP: ' + str.tostring(tp_long , format.mintick) shortCond_txt = syminfo.ticker + 'Sell \nEntry Price: ' + str.tostring(short_entry, format.mintick) + '\n' + 'SL: ' + str.tostring(sl_short, format.mintick) + '\n' + 'TP: ' + str.tostring(tp_short, format.mintick) if longCond alert(longCond_txt, alert.freq_once_per_bar_close) if shortCond alert(shortCond_txt, alert.freq_once_per_bar_close)