Golden crossover
2025-01-23
Category: None

//@version=5
strategy("Golden Crossover Strategy", overlay=true)

// Inputs for moving averages and targets
shortMA_length = input(50, title="Short MA Length")
longMA_length = input(200, title="Long MA Length")
takeProfitPercent = input(2.0, title="Take Profit (%)") / 100
stopLossPercent = input(1.0, title="Stop Loss (%)") / 100

// Calculate moving averages
shortMA = ta.sma(close, shortMA_length)
longMA = ta.sma(close, longMA_length)

// Plot the moving averages
plot(shortMA, color=color.blue, title="Short MA")
plot(longMA, color=color.red, title="Long MA")

// Conditions for entry and exit
longCondition = ta.crossover(shortMA, longMA)
if (longCondition)
    strategy.entry("Long", strategy.long)
    
// Setting up targets and stop loss
takeProfitPrice = strategy.position_avg_price * (1 + takeProfitPercent)
stopLossPrice = strategy.position_avg_price * (1 - stopLossPercent)

if (strategy.position_size > 0) // Check if in a long position
    strategy.exit("Take Profit", "Long", limit=takeProfitPrice, stop=stopLossPrice)