Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("POPCAT EMA Crossover Strategy", overlay=true, calc_on_every_tick=true)
- // Input for stop loss and take profit percentages
- stopLossPercentage = input.float(3, "Stop Loss Percentage") * 0.005
- takeProfitPercentage = input.float(3, "Take Profit Percentage") * 0.02
- // EMA settings
- emaFastPeriod = 12
- emaSlowPeriod = 21
- emaFast = ta.ema(close, emaFastPeriod)
- emaSlow = ta.ema(close, emaSlowPeriod)
- // Entry amount input
- entryAmount = input.float(10, title="Entry Amount", minval=10)
- // Variables for stop loss and take profit lines
- var line stopLossLine = na
- var line takeProfitLine = na
- // Reset lines if no position
- if strategy.position_size == 0
- stopLossLine := na
- takeProfitLine := na
- else
- stopLossLine.set_x2(bar_index)
- takeProfitLine.set_x2(bar_index)
- // Buy condition
- if ta.crossover(emaFast, emaSlow) and strategy.position_size <= 0
- stopLoss = close * (1 - stopLossPercentage)
- takeProfit = close * (1 + takeProfitPercentage)
- stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
- takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
- strategy.entry("Long", strategy.long, qty=entryAmount)
- strategy.exit("SL / TP", "Long", stop=stopLoss, limit=takeProfit)
- // Sell condition
- if ta.crossunder(emaFast, emaSlow) and strategy.position_size >= 0
- stopLoss = close * (1 + stopLossPercentage)
- takeProfit = close * (1 - takeProfitPercentage)
- stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
- takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
- strategy.entry("Short", strategy.short, qty=entryAmount)
- strategy.exit("SL / TP", "Short", stop=stopLoss, limit=takeProfit)
- // Plot EMAs on chart
- bandColor = emaFast > emaSlow ? color.green : color.red
- plotFast = plot(emaFast, color=bandColor, linewidth=1, title="EMA 12")
- plotSlow = plot(emaSlow, color=bandColor, linewidth=3, title="EMA 21")
- // Fill the area between the EMAs with 25% opacity
- fill(plotFast, plotSlow, color=emaFast > emaSlow ? color.new(color.green, 75) : color.new(color.red, 75))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement