//@version=5
strategy("Wickless Candle Fib Strategy v2", overlay=true, pyramiding=0, default_qty_type=strategy.cash, initial_capital=10000, currency=currency.USD)
// =========================================================================
// 1) Inputs & Configuration
// =========================================================================
riskAmount = input.float(100, title="Risk Amount ($)", minval=1, tooltip="The fixed dollar amount you want to risk per trade.")
fibSL = input.float(-0.27, title="Fibonacci Stop Loss Level", tooltip="Fib level for Stop Loss.")
fibTP = input.float(1.5, title="Fibonacci Take Profit Level", tooltip="Fib level for Take Profit.")
tradeDirection = input.string("Both", title="Trade Direction", options=["Long", "Short", "Both"], tooltip="Choose to trade Long only, Short only, or both directions.")
// EMA Settings
emaLength = input.int(21, title="EMA Length", minval=1, tooltip="Length of the EMA filter.")
useEMAFilter = input.bool(true, title="Use EMA Filter", tooltip="Enable/disable EMA filter for entries.")
// =========================================================================
// 2) Calculate EMA
// =========================================================================
ema21 = ta.ema(close, emaLength)
// =========================================================================
// 3) State Variables (For tracking pending orders)
// =========================================================================
var float pendingEntry = na
var float pendingSL = na
var float pendingTP = na
var bool tradeCancelled = false
// =========================================================================
// 4) Condition Definitions
// =========================================================================
// LONG SIGNAL: Bullish candle with no lower wick (open == low) AND price above EMA
isWicklessBullish = (close > open) and (open == low)
longEMACondition = not useEMAFilter or (close > ema21)
validLongSignal = isWicklessBullish and longEMACondition
// SHORT SIGNAL: Bearish candle with no upper wick (open == high) AND price below EMA
isWicklessBearish = (close < open) and (open == high)
shortEMACondition = not useEMAFilter or (close < ema21)
validShortSignal = isWicklessBearish and shortEMACondition
// =========================================================================
// 5) Cancellation Logic (Requirement: TP hit before Entry)
// =========================================================================
// Reset cancellation flag at the start of each bar calculation if we are flat
if strategy.position_size == 0
tradeCancelled := false
// Identify pending orders based on signals from the previous bar [1]
pendingLongOrder = (strategy.position_size == 0) and validLongSignal[1] and (tradeDirection == "Long" or tradeDirection == "Both")
pendingShortOrder = (strategy.position_size == 0) and validShortSignal[1] and (tradeDirection == "Short" or tradeDirection == "Both")
// --- Check for LONG cancellation ---
if (pendingLongOrder)
// If the current bar's high reached the pending TP level before entry
if (high >= pendingTP[1])
strategy.cancel("Long")
tradeCancelled := true
// --- Check for SHORT cancellation ---
if (pendingShortOrder)
// If the current bar's low reached the pending TP level before entry
if (low <= pendingTP[1])
strategy.cancel("Short")
tradeCancelled := true
// =========================================================================
// 6) Trade Calculation & Execution
// =========================================================================
// Only calculate and enter new trades if we are flat and no trade was cancelled on this bar
if (strategy.position_size == 0 and not tradeCancelled)
// Reset previous pending levels if a new signal hasn't formed yet
if not validLongSignal and not validShortSignal
pendingEntry := na
pendingSL := na
pendingTP := na
// --- LONG ENTRY LOGIC ---
if ((tradeDirection == "Long" or tradeDirection == "Both") and validLongSignal)
// Fib levels are drawn from Low (0) to High (1)
fibRange = high - low
// 1. Entry (Fib 0): Low of the candle
longEntryPrice = low
// 2. Stop Loss (Fib -0.27)
longStopLossPrice = low + fibRange * fibSL
// 3. Take Profit (Fib 1.5)
longTakeProfitPrice = low + fibRange * fibTP
// Store levels for plotting and cancellation checks on the next bar
pendingEntry := longEntryPrice
pendingSL := longStopLossPrice
pendingTP := longTakeProfitPrice
// 4. Calculate Position Size
riskPerUnit = longEntryPrice - longStopLossPrice
if (riskPerUnit > 0)
longPositionSize = riskAmount / riskPerUnit
// Place a LIMIT order at the calculated entry price
strategy.entry("Long", strategy.long, qty=longPositionSize, limit=longEntryPrice)
strategy.exit("Exit Long", from_entry="Long", stop=longStopLossPrice, limit=longTakeProfitPrice)
// --- SHORT ENTRY LOGIC ---
if ((tradeDirection == "Short" or tradeDirection == "Both") and validShortSignal)
// Fib levels are drawn from High (0) to Low (1)
fibRange = low - high // Note: low - high for short fib range
// 1. Entry (Fib 0): High of the candle
shortEntryPrice = high
// 2. Stop Loss (Fib -0.27)
shortStopLossPrice = high + fibRange * fibSL
// 3. Take Profit (Fib 1.5)
shortTakeProfitPrice = high + fibRange * fibTP
// Store levels for plotting and cancellation checks on the next bar
pendingEntry := shortEntryPrice
pendingSL := shortStopLossPrice
pendingTP := shortTakeProfitPrice
// 4. Calculate Position Size
riskPerUnit = shortStopLossPrice - shortEntryPrice
if (riskPerUnit > 0)
shortPositionSize = riskAmount / riskPerUnit
// Place a LIMIT order at the calculated entry price
strategy.entry("Short", strategy.short, qty=shortPositionSize, limit=shortEntryPrice)
strategy.exit("Exit Short", from_entry="Short", stop=shortStopLossPrice, limit=shortTakeProfitPrice)
// =========================================================================
// 7) Resetting State Variables
// =========================================================================
// If a trade is filled (position_size != 0) OR if a trade was cancelled, reset the pending visualization levels.
if (strategy.position_size != 0 or tradeCancelled)
pendingEntry := na
pendingSL := na
pendingTP := na
// =========================================================================
// 8) Plotting & Visuals
// =========================================================================
// Plot EMA
plot(ema21, title="EMA 21", color=color.new(color.orange, 0), linewidth=2)
// Highlight the signal candles (only when valid with EMA filter)
bgcolor(validLongSignal and strategy.position_size == 0 ? color.new(color.green, 80) :
validShortSignal and strategy.position_size == 0 ? color.new(color.red, 80) : na)
// Plot PENDING order levels
plot(pendingEntry, title="Pending Entry", color=color.new(color.blue, 0), style=plot.style_linebr, linewidth=2)
plot(pendingSL, title="Pending SL", color=color.new(color.red, 0), style=plot.style_linebr, linewidth=1)
plot(pendingTP, title="Pending TP", color=color.new(color.green, 0), style=plot.style_linebr, linewidth=1)
CONNVERT THIS SAME CODE FOR MT5 .... Copy each and every lof