Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("GG 101", overlay=true)
- smalengt50 = 50
- emalengt12 = 12
- emalengt21 = 21
- sma50 = ta.sma(close, 50)
- ema12 = ta.ema(close, 12)
- ema21 = ta.ema(close, 21)
- // Declare stopLoss and takeProfit variables
- var float stopLoss = na
- var float takeProfit = na
- // 1. Put these "na"s.
- var line stopLossLine = na
- var line takeProfitLine = na
- // 2. Adding logic to manually reset or extend the lines based on if we are in a position or not.
- if strategy.position_size == 0
- stopLossLine := na
- takeProfitLine := na
- else
- stopLossLine.set_x2(bar_index)
- takeProfitLine.set_x2(bar_index)
- longCondition = ta.crossover(ema12, ema21)
- shortCondition = ta.crossunder(ema12, ema21)
- if longCondition and strategy.position_size == 0
- stopLoss := close * (1 - 0.03)
- takeProfit := close * (1 + 0.03)
- // 3. Draw the start of the lines when we enter a position.
- stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
- takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
- // Open a long position at market price
- strategy.entry("Long", strategy.long)
- // Set stop loss and take profit levels
- strategy.exit("Take Profit/Stop Loss", from_entry="Long", stop=stopLoss, limit=takeProfit)
- if shortCondition and strategy.position_size == 0
- stopLoss := close * (1 + 0.03)
- takeProfit := close * (1 - 0.03)
- // 3. Draw the start of the lines when we enter a position.
- stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
- takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
- // Open a short position at market price
- strategy.entry("Short", strategy.short)
- // Set stop loss and take profit levels
- strategy.exit("Take Profit/Stop Loss", from_entry="Short", stop=stopLoss, limit=takeProfit)
- bandColor50 = sma50 < sma50[1] ? #ffa73c : color.rgb(33, 184, 243)
- bandColor = ema12 > ema21 ? color.green : color.red
- plot(ema12, color=bandColor)
- plot(ema21, color=bandColor)
- plot(sma50, color=bandColor50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement