Author Message:
Q-Trend is an multipurpose indicatorm that can be used for swing- and trend-trading equally on any timeframe (non-volatile markets are better for this thing).
Settings:
- Trend period - used to calculate trend line in the special moments(will explain below);
- ATR Multiplier - changes sensitivity. The higher the multiplier = the more sensitive it is.
- Also option to smooth source data (helps get cleaner signals, as always).
How to use?
Signals are given on the chart. Also ou can use trend line as S/R line.
The idea behind:
Terms:
SRС = Source
TL = trend line ;
MP = ATR multiplier;
ATR = ATR
TL = (highest of source P-bars back + lowest of source P-bars back) / 2
Epsilon = MP * ATR
I was thinking for a week about combining volatility and relation between highest and lowest price point. That why I called indicator Q-Trend = Quantitative Trend, as I was trying to think about price in a mathematical way.
Okay, time to go philosophical:
1) TL is shows good price trend, but as it is slow enough and not enough informative, we need add additional conditions to produce signals.
2) Okay, so what can we add as conditions? We need to take volatility into account, as it is crucial in the moments of market uncertainty. So let's use ATR ( Average True Range ) somehow. My idea is that if SRC breaks TL + ATR, then it means that there will be upmove and we update our TL. Analogically for SRC breaking TL - ATR (breaks are crosses of TL +- ATR lines).
Conclusion:
- if SRC breaks TL + ATR, it is a BUY signal and update of trend line ;
- if SRC breaks TL - ATR, it is a SELL signal and update of trend line ;
CODE:
CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// © tarasenko_
#indicator("Q-Trend", overlay = 1)
# Converted and by Sam4Cok@Samer800 - 12/2022
#// Inputs
input showTrendLine = yes; # "Show trend line?"
input color_bars = yes; # "Color bars?"
input source = close; # "Source"
input BuySelBubble = no;
input StrongBuySelBubble = yes;
input TrendPeriod = 50; # "Trend period to changes STRONG signals' sensitivity"
input atrLength = 14;
input mult = 1.0; # "ATR Multiplier"
input signalMode = {default "Type A", "Type B"};
input useWicks = no;
input confimredCandle = no;
input smoothing = no; # "Smooth source with EMA?"
input smoothingType = AverageType.EXPONENTIAL;
input smoothPeriod = 3; # "EMA Smoother period"
def na = Double.NaN;
def h = high; def l = low; def c = close; def o = open;
def mode = if signalMode == signalMode."Type B" then 1 else 0;
def confCl = if !confimredCandle then source else source[1];
def src = if smoothing then MovingAverage(smoothingType, confCl, smoothPeriod) else confCl;
def confHi = if !confimredCandle then h else h[1];
def confLo = if !confimredCandle then l else l[1];
def srcOp = if !confimredCandle then o else o[1];
def srcHi = if useWicks then confHi else src;
def srcLo = if useWicks then confLo else src;
#// Calculations
def hh = Highest(srcHi, TrendPeriod); # // Highest of src p-bars back;
def ll = Lowest(srcLo, TrendPeriod); # // Lowest of src p-bars back.
def d = hh - ll;
def trend;
def m1 = (hh + ll) / 2;
def m = if isNaN(trend[1]) or trend[1]==0 then m1 else trend[1];
def nATR = ATR(LENGTH = atrLength)[1];
def epsilon = mult * nATR;
def bandUp = m + epsilon;
def bandDn = m - epsilon;
def crossUp = if src > bandUp then crossUp[1] + 1 else 0;
def crossDn = if src < bandDn then crossDn[1] + 1 else 0;
def cross = (src>bandUp and src[1]<=bandUp[1]) or (src<bandDn and src[1]>=bandDn[1]);
def change_up = (if(mode,cross,0)) or crossUp > 0;
def change_down = (if(mode,cross,0)) or crossDn > 0;
def sb = srcOp < ll + d / 8 and srcOp >= ll;
def ss = srcOp > hh - d / 8 and srcOp <= hh;
def strong_buy = sb or sb[1] or sb[2] or sb[3] or sb[4];
def strong_sell = ss or ss[1] or ss[2] or ss[3] or ss[4];
trend = if (change_up or change_down) and m != trend[1] then m else
if change_up then bandUp else
if change_down then bandDn else trend[1];
def ls = if change_up then 1 else if change_down then -1 else ls[1];
def color = if ls > 0 then 1 else 0;
#// Plottings
plot trendLine = if showTrendLine then trend else na; # "trend line"
trendLine.AssignValueColor(if color then createColor(33,150,243) else CreateColor(255,152,0));
trendLine.SetLineWeight(2);
#-- Bubbles
AddChartBubble(BuySelBubble and change_up and ls[1] != 1 and !strong_buy, l, "Buy", Color.DARK_GREEN, no);
AddChartBubble(BuySelBubble and change_down and ls[1] != -1 and !strong_sell, h, "Sell", Color.DARK_RED, yes);
AddChartBubble(StrongBuySelBubble and change_up and ls[1] !=1 and strong_buy, l, "Strong", Color.GREEN, no);
AddChartBubble(StrongBuySelBubble and change_down and ls[1] != -1 and strong_sell, h, "Strong", color.RED, yes);
#-- Bar Color.
AssignPriceColor(if !color_bars then color.CURRENT else
if color then if source>trend then Color.GREEN else Color.DARK_GREEN else
if source<trend then Color.RED else Color.DARK_RED);
#-- END Code
Last edited by a moderator: