//@version=5 strategy("MA30 Long Strategy ", overlay=true) // Set the trading date range startDate = timestamp(2020, 01, 01, 00, 00) endDate = timestamp(2024, 12, 31, 23, 59) // Calculate the 30-period moving average ma30 = ta.sma(close, 30) ma2= ta.sma(close, 10) // Calculate the standard deviation of the last 30 periods stdDev = ta.stdev(close, 30) // Maximum position size as 10% of the available capital positionSize = strategy.equity * 1 / close // Define the exit level (1.5 standard deviations below the MA30) stopLossPercentage = 0.08 stopPriceShort = close * (1 + stopLossPercentage) exitLevel = ma2 - (1.5* stdDev) // Define the strategy logic for trading if (time >= startDate and time <= endDate) // Only trade within the specified date range // Open a long position when the stock price crosses above the MA30 if (ta.crossover(close, ma30)) // Open a long position using 10% of the fund strategy.entry("Long", strategy.long, qty = positionSize) // Close the position when the stock price drops below 1.5 standard deviations from the MA30 if (close < ma30 ) // Close the long position strategy.close("Long") longlen = input.int(30, "Long MA Length", minval=1) long = ta.sma(close, longlen) plot(long, color = color.rgb(249, 18, 18), title="Long MA")
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter