Skip to content

Instantly share code, notes, and snippets.

@cedricfarinazzo
Created February 28, 2021 01:57
Show Gist options
  • Save cedricfarinazzo/b42cfd152667f622463b829c6611422a to your computer and use it in GitHub Desktop.
Save cedricfarinazzo/b42cfd152667f622463b829c6611422a to your computer and use it in GitHub Desktop.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sed101010
//@version=4
strategy("POC 1", overlay=true, initial_capital=2000)
// INPUT
positionType = input(defval="LONG", title="Position Type", options=["LONG", "SHORT"])
longLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=5) * 0.01
shortLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=5) * 0.01
// DATA
sar_data = sar(0.02, 0.02, .2)
[bb_middle, bb_upper, bb_lower] = bb(close, 20, 2)
rsi_data = rsi(close, 14)
// COMPUTE
hasEntryLongConditional() => (crossover(close, bb_lower) or rsi_data < 30) and volume > 0
hasCloseLongConditional() => (crossunder(close, bb_upper) or rsi_data > 90) and volume > 0
hasEntryShortConditional() => 1 == 1
hasCloseShortConditional() => 1 != 1
longStopPrice = strategy.position_avg_price * (1 - longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)
// FUNCTION
hasOpenTrade() => strategy.opentrades != 0
notHasOpenTrade() => strategy.opentrades == 0
strategyClose() =>
if positionType == "LONG"
strategy.close("LONG", qty_percent=100, when=true)
else
strategy.close("SHORT", qty_percent=100, when=true)
strategyOpen() =>
if positionType == "LONG"
strategy.entry("LONG", strategy.long, 100, when=true)
else
strategy.entry("SHORT", strategy.short, 100, when=true)
hitStopLoss() =>
if positionType == "LONG"
longStopPrice > close
else
shortStopPrice < close
// ACT
if (notHasOpenTrade() and hasEntryLongConditional())
strategyOpen()
if (hasOpenTrade() and hasCloseLongConditional())
strategyClose()
if (hasOpenTrade() and hitStopLoss())
strategyClose()
// PLOTING
plot(sar_data, style=plot.style_circles, linewidth=1)
bb_upper_plot = plot(bb_upper, color=color.aqua)
bb_lower_plot = plot(bb_lower, color=color.aqua)
fill(bb_lower_plot, bb_upper_plot, color=color.aqua)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment