//version=5
strategy("Swing Trading Strategy", overlay=true)
// Define parameters
fastLength = input(10, "Fast MA Length")
slowLength = input(20, "Slow MA Length")
stopLossPct = input(2, "Stop Loss Percentage")
takeProfitPct = input(3, "Take Profit Percentage")
// Calculate moving averages
fastMA = sma(close, fastLength)
slowMA = sma(close, slowLength)
// Entry conditions
enterLong = crossover(fastMA, slowMA)
enterShort = crossunder(fastMA, slowMA)
// Exit conditions
exitLong = crossunder(close, fastMA) or (strategy.position_size > 0 and close < (1 - stopLossPct/100) * strategy.position_avg_price) or (strategy.position_size > 0 and close > (1 + takeProfitPct/100) * strategy.position_avg_price)
exitShort = crossover(close, fastMA) or (strategy.position_size < 0 and close > (1 + stopLossPct/100) * strategy.position_avg_price) or (strategy.position_size < 0 and close < (1 - takeProfitPct/100) * strategy.position_avg_price)
// Execute trades
if enterLong
strategy.entry("Long", strategy.long)
if enterShort
strategy.entry("Short", strategy.short)
if exitLong
strategy.close("Long")
if exitShort
strategy.close("Short")
strategy("Swing Trading Strategy", overlay=true)
// Define parameters
fastLength = input(10, "Fast MA Length")
slowLength = input(20, "Slow MA Length")
stopLossPct = input(2, "Stop Loss Percentage")
takeProfitPct = input(3, "Take Profit Percentage")
// Calculate moving averages
fastMA = sma(close, fastLength)
slowMA = sma(close, slowLength)
// Entry conditions
enterLong = crossover(fastMA, slowMA)
enterShort = crossunder(fastMA, slowMA)
// Exit conditions
exitLong = crossunder(close, fastMA) or (strategy.position_size > 0 and close < (1 - stopLossPct/100) * strategy.position_avg_price) or (strategy.position_size > 0 and close > (1 + takeProfitPct/100) * strategy.position_avg_price)
exitShort = crossover(close, fastMA) or (strategy.position_size < 0 and close > (1 + stopLossPct/100) * strategy.position_avg_price) or (strategy.position_size < 0 and close < (1 - takeProfitPct/100) * strategy.position_avg_price)
// Execute trades
if enterLong
strategy.entry("Long", strategy.long)
if enterShort
strategy.entry("Short", strategy.short)
if exitLong
strategy.close("Long")
if exitShort
strategy.close("Short")
Clause de non-responsabilité
Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.
Clause de non-responsabilité
Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.