//@version=5
strategy("Q-Trend Strategy FTMO", overlay=true, default_qty_type=[Link],
default_qty_value=1, initial_capital=1000, pyramiding=1, commission_value=0.05)
// Inputs
src = input(close, "Source", group="Main settings")
p = [Link](200, "Trend period", group="Main settings", tooltip="Changes STRONG
signals' sensitivity.", minval=1)
atr_p = [Link](14, "ATR Period", group="Main settings", minval=1)
mult = [Link](1.0, "ATR Multiplier", step=0.1, group="Main settings",
tooltip="Changes sensitivity: higher period = higher sensitivity.")
mode = [Link]("Type A", "Signal mode", options=["Type A", "Type B"],
group="Mode")
use_ema_smoother = [Link]("No", "Smooth source with EMA?", options=["Yes",
"No"], group="Source")
src_ema_period = [Link](3, "EMA Smoother period", group="Source")
color_bars = input(true, "Color bars?", group="Addons")
show_tl = input(true, "Show trend line?", group="Addons")
// New inputs for enabling long and short positions
enableLong = input(true, "Enable Long Trades", group="Trade Options")
enableShort = input(true, "Enable Short Trades", group="Trade Options")
// Calculations
src := use_ema_smoother == "Yes" ? [Link](src, src_ema_period) : src
h = [Link](src, p)
l = [Link](src, p)
d = h - l
m = (h + l) / 2
m := bar_index > p ? m[1] : m
// Calculate the ATR
currentAtr = [Link](atr_p)
epsilon = mult * currentAtr[1]
change_up = (mode == "Type B" ? [Link](src, m + epsilon) : [Link](src, m +
epsilon)) or src > m + epsilon
change_down = (mode == "Type B" ? [Link](src, m - epsilon) : [Link](src, m
- epsilon)) or src < m - epsilon
m := (change_up or change_down) and m != m[1] ? m : change_up ? m + epsilon :
change_down ? m - epsilon : nz(m[1], m)
// Plotting the trend line
plot(show_tl ? m : na, "Trend Line", color=[Link]([Link], 0), linewidth=2)
// Optional: Color bars based on the trend direction
barcolor(color_bars ? (change_up ? [Link] : change_down ? [Link] : na) :
na)
// Strategy conditions and entries for long positions
longCondition = change_up and enableLong and strategy.position_size <= 0 // Prevent
immediate sell after buy
if (longCondition)
[Link]("Long", [Link])
trailStopLong = currentAtr * mult
[Link]("Exit Long", "Long", trail_points=trailStopLong,
trail_offset=trailStopLong * 0.5)
// Strategy conditions and entries for short positions
shortCondition = change_down and enableShort and strategy.position_size >= 0 //
Prevent immediate buy after sell
if (shortCondition)
[Link]("Short", [Link])
trailStopShort = currentAtr * mult
[Link]("Exit Short", "Short", trail_points=trailStopShort,
trail_offset=trailStopShort * 0.5)
use_period = [Link](false, "Specific period?", group="period")
startDate = [Link](timestamp("01 Jan 2020"), "Start Date", group="period")
endDate = [Link](timestamp("01 Jan 2025"), "End Date", group="period")
// Indicators
inDateRange = use_period ? ((time >= startDate) and (time < endDate)) : true
// Backtest Metrics
strategy_pnl = [Link] + [Link]
bnh_strategy_pnl_pcnt = (strategy_pnl / strategy.initial_capital) * 100
float bnh_start_bar = na
bnh_start_bar := na(bnh_start_bar[1]) or inDateRange != true ? close :
bnh_start_bar[1]
float bnl_buy_hold_equity = na
bnl_buy_hold_equity := inDateRange == true ? ((close - bnh_start_bar) /
bnh_start_bar) * 100 : bnl_buy_hold_equity[1]
bnh_vs_diff = bnh_strategy_pnl_pcnt - bnl_buy_hold_equity
bnh_diff_color = bnh_vs_diff > 0 ? [Link]([Link], inDateRange ? 60 : 100) :
[Link]([Link], inDateRange ? 60 : 100)
var Table = [Link](position.top_right, columns=2, rows=4, border_width=1,
bgcolor=[Link], border_color=[Link])
[Link](table_id=Table, column=0, row=0, text_color=(bnh_strategy_pnl_pcnt >
bnl_buy_hold_equity) ? [Link] : [Link],
text_size=[Link],
text="Buy & hold profit")
[Link](table_id=Table, column=1, row=0, text_color=(bnh_strategy_pnl_pcnt >
bnl_buy_hold_equity) ? [Link] : [Link],
text_size=[Link],
text=[Link](bnl_buy_hold_equity, '#.##') + ' %')
[Link](table_id=Table, column=0, row=1,
text_color=(bnh_strategy_pnl_pcnt < bnl_buy_hold_equity) ? [Link] :
[Link],
text_size=[Link],
text="Strategy profit")
[Link](table_id=Table, column=1,
row=1,
text_color=(bnh_strategy_pnl_pcnt < bnl_buy_hold_equity) ? [Link] :
[Link],
text_size=[Link],
text=[Link](bnh_strategy_pnl_pcnt, '#.##') + ' %')
[Link](table_id=Table,
column=0,
row=2,
text_color=[Link],
text_size=[Link],
text="Start Date")
[Link](table_id=Table,
column=1,
row=2,
text_color=[Link],
text_size=[Link],
text=[Link]("{0,date,dd-MM-YYYY}",
[Link].entry_time(1)))