0

The code is below.

//@version=4
strategy("52 Weeks High with Trailing Stop", overlay=true)

// Define input variables
trailStopPercent = input(1.0, "Trailing Stop Percentage")

// Calculate the 52-week high
high52Weeks = highest(high, 252)

// Define entry conditions
enterLong = close \> high52Weeks

// Define exit conditions
exitLong = close \< low\[1\] \* (1 - trailStopPercent / 100)

// Calculate position size based on account capital
accountCapital = strategy.equity
positionSize = accountCapital \* 0.01 // 1% of account capital

// Execute long trade
if enterLong and strategy.position_size == 0
strategy.entry("Long", strategy.long, qty = positionSize)

// Exit long trade
if exitLong
strategy.close("Long")

// Plotting 52-week high
plot(high52Weeks, "52 Weeks High", color=color.blue, linewidth=2)

Run the code, expecting results in the strategy, displayed in the performance window in trading view but unfortunately did not return any results. It only displayed the overly on the chart but not the actual strategy results and performance.

1 Answers1

0

enterLong = close > high52Weeks can never be true. highest() will return the highest price within the given period. Price cannot close higher than the "highest".

Also, the way you are getting the 52-week high does not make any sense to me. Why are you using 252 as your length?

Lastly, your position size calculation is also wrong. If you want to use a price value in qty, you should add default_qty_type=strategy.cash to your strategy() call.

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Thanks, This was my original code, below strategy("52 Weeks High with Trailing Stop", overlay=true) trailStopPercent = input(1.0, "Trailing Stop Percentage") // Calculate the 52-week high high52Weeks = highest(high, 52) // Define entry conditions enterLong = close > high52Weeks // Define exit conditions exitLong = close < low[1] * (1 - trailStopPercent / 100) if enterLong strategy.entry("Long", strategy.long) if exitLong strategy.close("Long") // Plotting 52-week high plot(high52Weeks, "52 Weeks High", color=color.blue, linewidth=2) – imurphyp Jul 02 '23 at 10:30