CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// Based on "Supertrend" by KivancOzbilgic and "QQE signals" by colinmck
#// Modified by YourUsername
#// A big thank you to KivancOzbilgic for creating and sharing the Supertrend script!
#// A big thank you to colinmck for creating and sharing the QQE signals script!
#strategy('Q-Trend', overlay=true, process_orders_on_close=true, explicit_plot_zorder=true)
# Convereted by Sam4COK@Samer800 - 05/2023
input useChartTimeframe = yes;
input Aggregation = {Default "10 Min", "15 Min", "30 Min", "60 Min"};
input ColorBars = yes; # 'Match Candle Color to Trend'
input showTradebox = yes; # 'Show Tradebox'
input Show_Dashboard = yes; # 'Show Dashboard'
input Risk = 0.5; # 'Risk (bps)'
input RiskToRewardRatio = 1.0; # 'Risk/Reward Ratio'
input atr_period = 10; # 'ATR Period'
input SupertrendSource = hl2; # 'Source'
input atrMultiplier = 3.0; # 'ATR Multiplier'
input changeAtrCalculationMethod = yes; # 'ATR Calculation Method'
input rsiLength = 14; # 'RSI Length'
input rsiSmoothing = 6; # 'RSI Smoothing'
input qqeFactor = 4.238; # 'Quantitative Factor'
input minNoOfBarsForDashboardCalc = 100; # 'Hide the dashboard if there are less than this manu bars on screen'
def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def agg = if Aggregation==Aggregation."10 Min" then AggregationPeriod.TEN_MIN else
if Aggregation==Aggregation."15 Min" then AggregationPeriod.FIFTEEN_MIN else
if Aggregation==Aggregation."30 Min" then AggregationPeriod.THIRTY_MIN else
if Aggregation==Aggregation."60 Min" then AggregationPeriod.HOUR else current;
def tf = if useChartTimeframe then current else agg;
def high_ = high(Period=tf);
def low_ = low(Period=tf);
def close_ = close(PEriod=tf);
def bar_index = AbsValue(CompoundValue(1, BarNumber(),0));
def x_show_dash = if Show_Dashboard then if bar_index > minNoOfBarsForDashboardCalc then yes else na else na;
#// SUPERTREND
def tr = TrueRange(high_, close_, low_);
def atr2 = Average(tr, atr_period);
def nATR = if changeAtrCalculationMethod then ATR(LENGTH=atr_period) else atr2;
def up;
def up_ = SupertrendSource - atrMultiplier * nATR;
def up1 = if (isNaN(up[1]) or up[1]==0) then up_ else up[1];
up = if close_[1] > up1 then max(up_, up1) else up_;
def dn;
def dn_ = SupertrendSource + atrMultiplier * nATR;
def dn1 = if (isNaN(dn[1]) or dn[1]==0) then dn_ else dn[1];
dn = if close_[1] < dn1 then min(dn_, dn1) else dn_;
def super_trend;
def supertrend = if (isNaN(super_trend[1]) or super_trend[1]==0) then 1 else super_trend[1];
super_trend = if supertrend == -1 and close_ > dn1 then 1 else
if supertrend == 1 and close_ < up1 then -1 else supertrend;
plot upPlot = if super_trend == 1 then up else na; # 'Bullish'
upPlot.SetDefaultColor(Color.GREEN);
def buySignal = super_trend == 1 and super_trend[1] == -1;
plot buyPoint = if buySignal then up else na; # 'Bull Start'
buyPoint.SetPaintingStrategy(PaintingStrategy.SQUARES);
buyPoint.SetDefaultColor(Color.GREEN);
plot dnPlot = if super_trend == 1 then na else dn; # 'Bearish'
dnPlot.SetDefaultColor(Color.RED);
def sellSignal = super_trend == -1 and super_trend[1] == 1;
plot SellPoint = if sellSignal then dn else na; # 'Bear Start'
SellPoint.SetPaintingStrategy(PaintingStrategy.SQUARES);
SellPoint.SetDefaultColor(Color.RED);
def mPlot = ohlc4;
AddCloud(mPlot, upPlot, Color.DARK_GREEN); # 'Bull Trend Area'
AddCloud(dnPlot, mPlot, Color.DARK_RED); # 'Bear Trend Area'
def changeCond = super_trend != super_trend[1];
#// Q
def src = close_;
def wild_period = rsiLength * 2 - 1;
def rsi = rsi(Price=src,Length= rsiLength);
def rsi_ma = ExpAverage(rsi, rsiSmoothing);
def atr_rsi = AbsValue(rsi_ma[1] - rsi_ma);
def atr_rsi_ma = ExpAverage(atr_rsi, wild_period);
def dar = ExpAverage(atr_rsi_ma, wild_period) * qqeFactor;
def longband;# = 0.0
def shortband;# = 0.0
def trend;# = 0
def trend_ = if (isNaN(trend[1]) or trend[1]==0) then 1 else trend[1];
def atr_rsi_delta_fast = dar;
def rsi_idx = rsi_ma;
def newshortband = rsi_idx + atr_rsi_delta_fast;
def newlongband = rsi_idx - atr_rsi_delta_fast;
longband = if rsi_idx[1] > longband[1] and rsi_idx > longband[1] then max(longband[1], newlongband) else newlongband;
shortband = if rsi_idx[1] < shortband[1] and rsi_idx < shortband[1] then min(shortband[1], newshortband) else newshortband;
def cross_1 = crosses(longband[1],rsi_idx);
def cross_2 = crosses(rsi_idx,shortband[1]);
trend = if cross_2 then 1 else
if cross_1 then -1 else trend_;
def atr_rsi_fast = if trend == 1 then longband else shortband;
#// Find all the QQE Crosses
def q_xlong;
def q_xshort;
def q_xlong_ = if isNaN(q_xlong[1]) then 0 else q_xlong[1];
def q_xshort_ = if isNaN(q_xshort[1]) then 0 else q_xshort[1];
q_xlong = if atr_rsi_fast < rsi_idx then q_xlong_ + 1 else 0;
q_xshort = if atr_rsi_fast > rsi_idx then q_xshort_ + 1 else 0;
#//Conditions
def q_long = q_xlong == 1;
def q_short =q_xshort == 1;
#// Plotting
def is_buy = q_long and super_trend == -1;
def is_sell = q_short and super_trend == 1;
def is_buy_strong = q_long and super_trend == 1;
def is_sell_strong = q_short and super_trend == -1;
def in_trend;
if is_buy {
in_trend = 1;
} else
if is_sell {
in_trend = -1;
} else
if is_buy_strong {
in_trend = 2;
} else
if is_sell_strong {
in_trend = -2;
} else {
in_trend = in_trend[1];
}
AssignPriceColor(if !ColorBars or isNaN(close_[-1]) then Color.CURRENT else
if in_trend==2 then Color.GREEN else
if in_trend==1 then Color.DARK_GREEN else
if in_trend==-2 then Color.RED else
if in_trend==-1 then Color.DARK_RED else Color.GRAY);
AddChartBubble(is_buy and !is_buy[1], low, "buy", Color.DARK_GREEN, no); # 'Quantitative Buy'
AddChartBubble(is_sell and !is_sell[1], high, "Sell", Color.DARK_RED, yes); # 'Quantitative Sell'
AddChartBubble(is_buy_strong and !is_buy_strong[1], low, "Strong Buy", Color.GREEN, no); # 'Quantitative Strong Buy'
AddChartBubble(is_sell_strong and !is_sell_strong[1], high, "Strong Sell", Color.RED, yes); # 'Quantitative Strong Sell'
#// Trade
def is_trade;
def entry_price;
def entry_price_bi;
def stop_loss;
def take_profit;
#is_trade := switch
if is_buy or is_buy_strong {
is_trade = yes;
} else
if is_sell or is_sell_strong {
is_trade = no;
} else {
is_trade = is_trade[1];
}
def trade = is_trade;
if is_buy or is_buy_strong {
entry_price_bi = bar_index;
} else
if is_sell or is_sell_strong {
entry_price_bi = bar_index;
} else {
entry_price_bi = entry_price_bi[1];
}
def highBar = bar_index>=highestAll(entry_price_bi);
entry_price = GetValue(close_, bar_index - entry_price_bi);
stop_loss = if trade then entry_price * (1 - Risk/100) else
entry_price * (1 + Risk/100);
take_profit = if trade then entry_price + (AbsValue(entry_price - stop_loss) * RiskToRewardRatio) else
entry_price - (AbsValue(entry_price - stop_loss) * RiskToRewardRatio);
def entryprice = entry_price;
def stoploss = stop_loss;
def takeprofit = take_profit;
AddLabel(x_show_dash, "Entry price: " + Round(entryprice, 2), Color.WHITE);
AddLabel(x_show_dash, "Stop loss: " + Round(StopLoss, 2), Color.PINK);
AddLabel(x_show_dash, "Take profit: " + Round(takeprofit, 2), Color.LIGHT_GREEN);
AddLabel(x_show_dash, if in_trend==2 then "Trade: Strong Buy" else
if in_trend==1 then "Trade: Buy" else
if in_trend==-2 then "Trade: Strong Sell" else
if in_trend==-1 then "Trade: Sell" else "Trade: No Trend",
if in_trend==2 then Color.GREEN else
if in_trend==1 then Color.DARK_GREEN else
if in_trend==-2 then Color.RED else
if in_trend==-1 then Color.DARK_RED else Color.GRAY);
#--- Trade
def entry = if entryprice==entryprice[1] then entryprice else na;
def stop = if stoploss==stoploss[1] then stoploss else na;#stop[1];
def profit = if takeprofit==takeprofit[1] then takeprofit else na;
plot entryLine = if showTradebox and highBar then entry else na;
entryLine.SetStyle(Curve.SHORT_DASH);
entryLine.SetDefaultColor(Color.GRAY);
plot StopLine = if showTradebox and highBar then stop else na;
StopLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
StopLine.AssignValueColor(if in_trend>0 then Color.MAGENTA else Color.CYAN);
plot ProfitLine = if showTradebox and highBar then profit else na;
ProfitLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfitLine.AssignValueColor(if in_trend>0 then Color.CYAN else Color.MAGENTA);
AddCloud(if trade then entryLine else StopLine, if trade then StopLine else entryLine, Color.DARK_RED);
AddCloud(if trade then ProfitLine else entryLine,if trade then entryLine else ProfitLine, Color.DARK_GREEN);
#--- END of CODE