Hello there I am attempting to create a pine strategy that will exit a position once the price has gone 3% up or 1% down, can any one guide this is what I have so far
//@version=5
startDate = input(title="Start Date",defval=15)
startMonth = input(title="Start Month",defval=1)
startYear = input(title="Start Year",defval=2023)
afterStartDate = (time >= timestamp(syminfo.timezone,startYear, startMonth, startDate, 0, 0))
investment = input.int(defval=1000, title='INVESTMENT USDT')
comm=0.08
strategy('BS WAB with lines', shorttitle='WAB', overlay=true, default_qty_value = 1000, commission_type = strategy.commission.percent, commission_value = comm)
// Definitions: Price and Timeframes
src = input(close, title='Source')
resolution = timeframe.period
price = request.security(syminfo.tickerid, resolution, src)
// Defintions: Moving Average periods and types
ma1Period = input.int(defval=8, title='Period', inline='1', group='Moving Averages: Zone 1')
ma1Type = input.string(defval='EMA', title='Type', options=['EMA', 'SMA', 'WMA', 'HMA', 'LIN'], tooltip='MA Smoothing', inline='1', group='Moving Averages: Zone 1')
ma2Period = input.int(defval=21, title='Period', inline='2', group='Moving Averages: Zone 1')
ma2Type = input.string(defval='EMA', title='Type', options=['EMA', 'SMA', 'WMA', 'HMA', 'LIN'], tooltip='MA Smoothing', inline='2', group='Moving Averages: Zone 1')
// Moving Average Calculation
ma1 = ma1Type == 'EMA' ? ta.ema(price, ma1Period) : ma1Type == 'SMA' ? ta.sma(price, ma1Period) : ma1Type == 'WMA' ? ta.wma(price, ma1Period) : ma1Type == 'HMA' ? ta.hma(price, ma1Period) : ma1Type == 'LIN' ? ta.linreg(price, ma1Period, 0) : na
ma2 = ma2Type == 'EMA' ? ta.ema(price, ma2Period) : ma2Type == 'SMA' ? ta.sma(price, ma2Period) : ma2Type == 'WMA' ? ta.wma(price, ma2Period) : ma2Type == 'HMA' ? ta.hma(price, ma2Period) : ma2Type == 'LIN' ? ta.linreg(price, ma2Period, 0) : na
// Definitions: Trends
TrendUp1() =>
ma1 > ma2
TrendDown1() =>
ma1 < ma2
trendColor1 = TrendUp1() ? color.green : TrendDown1() ? color.red : color.blue
p_ma1 = plot(ma1)
p_ma2 = plot(ma2)
fill(p_ma1, p_ma2, color=trendColor1)
plotshape(ma1 > ma2 and ma1[1] < ma2[1], style=shape.triangleup, color= color.rgb(59, 255, 108), size=size.small, text="B", location=location.belowbar, title='Buy shades', offset=1)
plotshape(ma1 < ma2 and ma1[1] > ma2[1], style=shape.triangledown, color= color.rgb(255, 0, 0), size=size.small, text="S", location=location.abovebar, title='Sell shades', offset=1)
mainComment=str.tostring(high[1])
strategyAmount=investment/close
longCondition = ma1 > ma2 and ma1[1] < ma2[1]
if (longCondition and afterStartDate)
strategy.entry("BUY", strategy.long, qty=strategyAmount, comment = mainComment)
shortCondition = ma1 < ma2 and ma1[1] > ma2[1]
if (shortCondition and afterStartDate)
strategy.entry("SELL", strategy.short, qty=strategyAmount, comment = mainComment)
if anyone coud help me, create an exit on this example it will be highly appreciated
You want percentage based TP and SL basically.
Use strategy.position_avg_price
to get your average position size and then calculate the 3% up and 1% down from there. Then use the strategy.exit()
function to place your exit order.
For a long position, it would look like below:
tp_per = 0.03
sl_per = 0.01
tp_price = strategy.position_avg_price * (1 + tp_per)
sl_price = strategy.position_avg_price * (1 - sl_per)
if (strategy.position_size > 0)
strategy.exit("LE", "BUY", stop=sl_price, limit=tp_price)