Moving this logic to TradingView requires a mindset shift. While MQL and C# process every real-time tick in an infinite loop, Pine Script evaluates historically on candle closes (OHLC data).
TradingView's strategy engine handles a lot of the heavy lifting for us. We don't need to write custom trailing stop loops or risk management math from scratch—Pine Script's strategy.exit and built-in equity variables manage this natively.
Here is the complete forex-scalping.com strategy translated into Pine Script v5.
Code: Select all
//@version=5
strategy("WTI Scalper - forex-scalping.com", overlay=true, calc_on_every_tick=true, initial_capital=10000, default_qty_type=strategy.cash)
// =========================================================================
// INPUTS
// =========================================================================
grp_risk = "Risk Management"
risk_pct = input.float(1.0, title="Risk Per Trade (%)", group=grp_risk, step=0.1)
sl_ticks = input.int(200, title="Stop Loss (Ticks)", group=grp_risk)
tp_ticks = input.int(400, title="Take Profit (Ticks)", group=grp_risk)
grp_ind = "Indicators"
fast_len = input.int(20, title="Fast EMA", group=grp_ind)
slow_len = input.int(50, title="Slow EMA", group=grp_ind)
rsi_len = input.int(14, title="RSI Period", group=grp_ind)
grp_sess = "Session & Filters"
sess_str = input.session("1400-1700", title="Trading Session", group=grp_sess)
max_dd = input.float(5.0, title="Max Daily Drawdown (%)", group=grp_sess, step=0.5)
grp_trail = "Trailing Stop"
trail_pts = input.int(150, title="Trailing Activation (Ticks)", group=grp_trail)
trail_stp = input.int(50, title="Trailing Step (Ticks)", group=grp_trail)
// =========================================================================
// INDICATORS & CONDITIONS
// =========================================================================
fast_ema = ta.ema(close, fast_len)
slow_ema = ta.ema(close, slow_len)
rsi = ta.rsi(close, rsi_len)
// Time Filter
in_session = not na(time(timeframe.period, sess_str))
// =========================================================================
// DAILY DRAWDOWN TRACKER
// =========================================================================
var float day_start_equity = na
var bool is_locked_today = false
// Reset at the start of a new daily session
if ta.change(time("D"))
day_start_equity := strategy.equity
is_locked_today := false
// Calculate live drawdown
current_dd = 0.0
if not na(day_start_equity)
current_dd := ((day_start_equity - strategy.equity) / day_start_equity) * 100
// Trigger liquidation and lockout
if current_dd >= max_dd and not is_locked_today
strategy.close_all(comment="Max DD Hit")
is_locked_today := true
// =========================================================================
// POSITION SIZING
// =========================================================================
// Calculate how many contracts to buy based on risk % and tick value
risk_amount = strategy.equity * (risk_pct / 100)
tick_value = syminfo.mintick * syminfo.pointvalue
loss_per_contract = sl_ticks * tick_value
qty = loss_per_contract > 0 ? (risk_amount / loss_per_contract) : 0
// =========================================================================
// EXECUTION LOGIC
// =========================================================================
// Entry Conditions
buy_cond = ta.crossover(fast_ema, slow_ema) and rsi > 50 and in_session and not is_locked_today
sell_cond = ta.crossunder(fast_ema, slow_ema) and rsi < 50 and in_session and not is_locked_today
if buy_cond and strategy.position_size == 0
strategy.entry("Long", strategy.long, qty=qty)
strategy.exit("Exit Long", "Long", loss=sl_ticks, profit=tp_ticks, trail_points=trail_pts, trail_offset=trail_stp)
if sell_cond and strategy.position_size == 0
strategy.entry("Short", strategy.short, qty=qty)
strategy.exit("Exit Short", "Short", loss=sl_ticks, profit=tp_ticks, trail_points=trail_pts, trail_offset=trail_stp)
// =========================================================================
// ON-CHART DASHBOARD
// =========================================================================
var table dash = table.new(position.top_left, 2, 4, bgcolor=color.new(color.black, 70), border_color=color.gray, border_width=1)
if barstate.islast
// Color logic for DD
color dd_color = color.lime
if current_dd > (max_dd * 0.75)
dd_color := color.orange
if current_dd >= max_dd
dd_color := color.red
// Headers
table.cell(dash, 0, 0, "forex-scalping.com", text_color=color.yellow, text_halign=text.align_left)
table.cell(dash, 1, 0, "Scalper", text_color=color.yellow, text_halign=text.align_right)
// Data
table.cell(dash, 0, 1, "Live Equity:", text_color=color.white, text_halign=text.align_left)
table.cell(dash, 1, 1, str.tostring(strategy.equity, "#.##"), text_color=color.white, text_halign=text.align_right)
table.cell(dash, 0, 2, "Daily DD:", text_color=color.white, text_halign=text.align_left)
table.cell(dash, 1, 2, str.tostring(current_dd, "#.##") + "%", text_color=dd_color, text_halign=text.align_right)
table.cell(dash, 0, 3, "Status:", text_color=color.white, text_halign=text.align_left)
table.cell(dash, 1, 3, is_locked_today ? "LOCKED" : "ACTIVE", text_color=is_locked_today ? color.red : color.lime, text_halign=text.align_right)
Key Architectural Shifts in Pine Script
Native Trailing Stop Math: Notice how short the strategy.exit() line is. Pine Script natively handles the step-based trailing stop by utilizing trail_points (activation distance) and trail_offset (the step size).
calc_on_every_tick: Because this is a scalping strategy with tight trailing stops, calc_on_every_tick=true is enabled in the header. If you deploy this live, the strategy engine will evaluate the trailing stop intra-bar, exactly like MT5 or cTrader.
The Missing Spread Filter: You'll notice the maximum spread filter is gone. TradingView does not store historical spread data (syminfo.spread does not exist historically). While we could track the real-time spread using close - ask, it would ruin historical backtesting results by generating errors. For TradingView, it's safer to rely on your broker execution settings or use webhooks to route signals to a local MT5/cTrader terminal where the spread filter handles execution.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.