Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © blackcat1402
- // Based on Original L2 Price Retracement Reminder Strategy
- //@version=4
- strategy("L2 PRRS - BO", overlay=true,
- max_bars_back = 5000,
- currency=currency.NONE,
- initial_capital=100000,
- default_qty_type=strategy.fixed,
- default_qty_value=100,
- commission_type=strategy.commission.percent,
- commission_value=0,
- process_orders_on_close=true,
- calc_on_every_tick=true,
- calc_on_order_fills=false,
- pyramiding = 0,
- slippage = 0)
- strat_dir_input = input(title="Strategy Direction", defval="all", options=["long", "short", "all"])
- strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.all
- strategy.risk.allow_entry_in(strat_dir_value)
- testStartYear = input(2020, "Backtest Start Year")
- testStartMonth = input(8, "Backtest Start Month")
- testStartDay = input(15, "Backtest Start Day")
- testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
- testPeriodValid = time >= testPeriodStart
- //function definition
- xsa(src,len,wei) =>
- sum = 0.0
- ma = 0.0
- out = 0.0
- sum := nz(sum[1]) - nz(src[len]) + src
- ma := na(src[len]) ? na : sum/len
- out := na(out[1]) ? ma : (src*wei+out[1]*(len-wei))/len
- out
- //inputs
- //moving average lookback for rsi
- xsalb = input(3, title="xsalookback", type=input.integer, minval = 1)
- //overbought threshold
- obth = input(85, title="overbought threshold", type=input.integer, minval = 1)
- // set trade expiry time manually in bars
- timeframe = input(title="Timeframe", type=input.integer, minval=1, defval=3)
- //algorithm
- //calculate RSI(3)
- lc = close[1]
- rsin = ((xsa(max((close - lc),0),xsalb,1) / xsa(abs((close - lc)),xsalb,1)) * 100)
- //define fast line as ema3 and slow line as ema21
- ff = ema(close,3)
- ma21 = ema(close,21)
- //up trend with lime and down trend with red
- barcolor(ma21>=ma21[1]?color.lime:color.red)
- barcolor(ma21<ma21[1]?color.red:color.lime)
- // use crosses to identify buy and selling points
- // plotchar(crossover(ff,ma21), char='B', location=location.belowbar, color=color.lime,size=size.small)
- // plotchar(crossunder(ff,ma21), char='S', location=location.abovebar, color=color.red,size=size.small)
- shortCondition = crossover(ff,ma21) and testPeriodValid
- longCondition = crossunder(ff,ma21) or crossunder(rsin,obth) and testPeriodValid
- // plotchar(buy, char='B', location=location.belowbar, color=color.lime,size=size.small)
- // plotchar(sel, char='S', location=location.abovebar, color=color.red,size=size.small)
- //if RSI(3) crossunder overbought threshold then it is a selling point due to retracement happen
- // plotchar(crossunder(rsin,obth), char='S', location=location.abovebar, color=color.yellow,size=size.small)
- //plot guideline and bull-bear boarder
- guideline = ema((ema(close,4)+ema(close,6)+ema(close,12)+ema(close,24))/4,2)
- boarder = sma(close,27)
- plot(guideline, color=color.white,linewidth=2)
- plot(boarder, color=color.yellow,linewidth=2)
- //strategy setting
- shortLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
- longLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
- // Determine stop loss price
- shortStopPrice = strategy.position_avg_price * (1 - longLossPerc)
- longStopPrice = strategy.position_avg_price * (1 + shortLossPerc)
- if (longCondition and guideline>boarder)
- strategy.entry("Buy", strategy.long)
- if (shortCondition and guideline>boarder)
- strategy.entry("Sell", strategy.short)
- //if (shortCondition and guideline>boarder and strategy.position_size > 0 )
- // strategy.close("Buy")
- //if (longCondition and guideline<boarder and strategy.position_size < 0 )
- // strategy.close("Sell")
- // triggers trade expiry time as set by timeframe input
- bars = barssince(strategy.opentrades == 0)
- strategy.close_all(when=(bars>=timeframe))
Add Comment
Please, Sign In to add comment