//@version=5
strategy("Q-Trend Strategy FTMO", overlay=true, default_qty_type=strategy.fixed,
default_qty_value=1, initial_capital=1000, pyramiding=1, commission_value=0.05)
// 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 sensitivity.")
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.int(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" ? ta.ema(src, src_ema_period) : src
h = ta.highest(src, p)
l = ta.lowest(src, p)
d = h - l
m = (h + l) / 2
m := bar_index > p ? m[1] : m
// Calculate the ATR
currentAtr = ta.atr(atr_p)
epsilon = mult * currentAtr[1]
change_up = (mode == "Type B" ? ta.cross(src, m + epsilon) : ta.crossover(src, m +
epsilon)) or src > m + epsilon
change_down = (mode == "Type B" ? ta.cross(src, m - epsilon) : ta.crossunder(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=color.new(color.blue, 0), linewidth=2)
// Optional: Color bars based on the trend direction
barcolor(color_bars ? (change_up ? color.green : change_down ? color.red : 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)
strategy.entry("Long", strategy.long)
trailStopLong = currentAtr * mult
strategy.exit("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)
strategy.entry("Short", strategy.short)
trailStopShort = currentAtr * mult
strategy.exit("Exit Short", "Short", trail_points=trailStopShort,
trail_offset=trailStopShort * 0.5)
use_period = input.bool(false, "Specific period?", group="period")
startDate = input.time(timestamp("01 Jan 2020"), "Start Date", group="period")
endDate = input.time(timestamp("01 Jan 2025"), "End Date", group="period")
// Indicators
inDateRange = use_period ? ((time >= startDate) and (time < endDate)) : true
// Backtest Metrics
strategy_pnl = strategy.netprofit + strategy.openprofit
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 ? color.new(color.green, inDateRange ? 60 : 100) :
color.new(color.red, inDateRange ? 60 : 100)
var Table = table.new(position.top_right, columns=2, rows=4, border_width=1,
bgcolor=color.black, border_color=color.gray)
table.cell(table_id=Table, column=0, row=0, text_color=(bnh_strategy_pnl_pcnt >
bnl_buy_hold_equity) ? color.gray : color.green,
text_size=size.normal,
text="Buy & hold profit")
table.cell(table_id=Table, column=1, row=0, text_color=(bnh_strategy_pnl_pcnt >
bnl_buy_hold_equity) ? color.gray : color.green,
text_size=size.normal,
text=str.tostring(bnl_buy_hold_equity, '#.##') + ' %')
table.cell(table_id=Table, column=0, row=1,
text_color=(bnh_strategy_pnl_pcnt < bnl_buy_hold_equity) ? color.gray :
color.green,
text_size=size.normal,
text="Strategy profit")
table.cell(table_id=Table, column=1,
row=1,
text_color=(bnh_strategy_pnl_pcnt < bnl_buy_hold_equity) ? color.gray :
color.green,
text_size=size.normal,
text=str.tostring(bnh_strategy_pnl_pcnt, '#.##') + ' %')
table.cell(table_id=Table,
column=0,
row=2,
text_color=color.yellow,
text_size=size.normal,
text="Start Date")
table.cell(table_id=Table,
column=1,
row=2,
text_color=color.yellow,
text_size=size.normal,
text=str.format("{0,date,dd-MM-YYYY}",
strategy.closedtrades.entry_time(1)))