Skip to content

Instantly share code, notes, and snippets.

@comdet
Created April 30, 2022 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comdet/6279a9bedc40dd8698053fe61408400a to your computer and use it in GitHub Desktop.
Save comdet/6279a9bedc40dd8698053fe61408400a to your computer and use it in GitHub Desktop.
//@version=4
strategy("My Strategy", overlay=true)
//////////////////////////////////////////////////////////////////////
// Testing Start dates
testStartYear = input(2020, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
//Stop date if you want to use a specific range of dates
testStopYear = input(2030, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false
// Component Code Stop
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Check Trend Strength
ha_t = syminfo.tickerid
col_red = #ff0000
col_green = #00ff00
fast_length = input(title="Fast Length", defval=3)
slow_length = input(title="Slow Length", defval=10)
src = input(title="Source", defval=close)
TSB = true
TSS = false
res_60 = input('60', title="Higher Time Frame 1 ")
trend_60 = security(ha_t, res_60, ((ema(src, fast_length) - ema(src, slow_length))/src)*100)
prev_trend_60 = security(ha_t, res_60, ((ema(src[5], fast_length) - ema(src[5], slow_length))/src[5])*100)
trend_status_60 = trend_60 > prev_trend_60
//trend_status_60 ? (TSB = true ):(TSS = true)
///////////////////////////////////////////////////////////////////
//End of checking stength
rsi_period = input(14, title="RSI period", minval = 1, step = 1)
myrsi = rsi(close, rsi_period)
rsi1 = crossunder(myrsi,70)
rsi2 = myrsi > 75
// Regression Lines
source = input(close)
length = input(100, minval=1)
offset = input(0, minval=0)
smoothing = input(17, minval=1)
mtf_val = input("", "Resolution", input.resolution)
p = input("Lime", "Up Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
q = input("Red", "Down Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
cc(x) => x=="Red"?color.red:x=="Lime"?color.lime:x=="Orange"?color.orange:x=="Teal"?
color.teal:x=="Yellow"?color.yellow:x=="Black"?color.black:color.white
data(x) => ema(security(syminfo.tickerid, mtf_val!="" ? mtf_val : timeframe.period, x), smoothing)
linreg = data(linreg(source, length, offset))
linreg_p = data(linreg(source, length, offset+1))
//plot(linreg, "Regression Line", cc(linreg>linreg[1]?p:q),linewidth=2, editable=false)
// Regression End
//COLOR of Regression Line
switchColor = input(true, "Color Regression according to trend?")
candleCol = input(false,title="Color candles based on Hull's Trend?")
visualSwitch = input(true, title="Show as a Band?")
thicknesSwitch = input(1, title="Line Thickness")
transpSwitch = input(40, title="Band Transparency",step=5)
hullColor = switchColor ? (linreg>linreg[1] ? #00ff00 : #ff0000) : #ff9800
//PLOT
///< Frame
Fi1 = plot(linreg, title="Regression Line", color=hullColor, linewidth=thicknesSwitch, transp=50)
Fi2 = plot(visualSwitch ? linreg[3] : na, title="RL", color=hullColor, linewidth=thicknesSwitch, transp=50)
///< Ending Filler
fill(Fi1, Fi2, title="Band Filler", color=hullColor, transp=transpSwitch)
// Signal
LRCOL = crossover(linreg,linreg[1]) //Linear Regression crossover long
LRCUS = crossunder(linreg,linreg[1]) //Linear Regression crossdown short
//PALR = source > linreg ? true:false // Price Above Linear Regression
//PBLR = source < linreg ? true:false // Price Below Linear Regression
PALR = true
PBLR = true
//======= FILTER ========//
// MFI
mfi_upper = sum(volume * (change(hlc3) <= 0 ? 0 : hlc3), 58)
mfi_lower = sum(volume * (change(hlc3) >= 0 ? 0 : hlc3), 58)
_mfi_rsi(mfi_upper, mfi_lower) =>
if mfi_lower == 0
100
if mfi_upper == 0
0
100.0 - (100.0 / (1.0 + mfi_upper / mfi_lower))
mf = _mfi_rsi(mfi_upper, mfi_lower)
mfi = (mf - 50) * 3
//// Plots
mfi_color = mfi > 0 ? #4CAF50 : #FF5252
//plot(mfi, "MFI Line", style=plot.style_line, color=mfi_color, linewidth = 2)
//============ EWO ==============//
ewo_src = input(close, title="source")
sma1length = input(5)
sma2length = input(35)
UsePercent = input(title="Show Dif as percent of current Candle", type=input.bool, defval=true)
smadif=iff(UsePercent,(sma(src, sma1length) - sma(src, sma2length)) / src * 100, sma(src, sma1length) - sma(src, sma2length))
longCondition = ((LRCOL and testPeriod()) and PALR and mfi > 3 and smadif > 0.3) //and trend_status_60
shortCondition = ((LRCUS and testPeriod()) and PBLR and mfi < -3 and smadif < -0.3) //and trend_status_60
var float long_tp = 0.0
var float long_sl = 0.0
var float short_tp = 0.0
var float short_sl = 0.0
longProfitPerc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=4.2) / 100
shortProfitPerc = input(title="Short Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=4.2) / 100
longExitPrice = strategy.position_avg_price * (1 + longProfitPerc)
shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)
if longCondition
strategy.entry( id = "Long", long = true)
if shortCondition
strategy.entry( id = "Short", long = false)
//Exit condition
if strategy.position_size > 0
strategy.exit("TP/SL", from_entry="Long", limit = longExitPrice, stop = shortExitPrice)
if strategy.position_size < 0
strategy.exit("TP/SL", from_entry="Short", limit =shortExitPrice, stop =longExitPrice)
if strategy.position_size > 0 and shortCondition
strategy.close("Long", comment="conflic, sell signal alert")
if strategy.position_size < 0 and longCondition
strategy.close("Short", comment="conflic, buy signal alert")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment