Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("SMA 10/50 5L 5P 5D Test", overlay=true)
- // Define the SMAs
- sma10 = ta.sma(close, 10)
- sma50 = ta.sma(close, 50)
- // Entry condition: 10-day SMA crosses above 50-day SMA
- longCondition = ta.crossover(sma10, sma50)
- // Define stop loss and take profit percentages
- stopLossPercent = 5
- takeProfitPercent = 5
- // Entry logic
- if (longCondition)
- strategy.entry("Long", strategy.long)
- // Retrieve the bar index of the first open trade
- entryBarIndex = strategy.opentrades.entry_bar_index(strategy.opentrades - 1)
- // Calculate bars since entry
- barsSinceEntry = na(entryBarIndex) ? na : bar_index - entryBarIndex
- // Stop loss and take profit levels
- stopLossLevel = strategy.position_avg_price * (1 - stopLossPercent / 100)
- takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercent / 100)
- // Exit conditions
- exitCondition1 = close < stopLossLevel
- exitCondition2 = close > takeProfitLevel
- exitCondition3 = not na(barsSinceEntry) and barsSinceEntry > 4 // Exit after 5 bars from entry
- // Close position if any exit condition is met
- if (exitCondition1 or exitCondition2 or exitCondition3)
- strategy.close("Long")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement