Mizo Trading Strategy Overview
Mizo Trading Strategy Overview
0 at
[Link]
// © Samuel Ralte
// whatsapp = 8787604374
//@version=5
strategy("Samuel Ralte(MIZO)", overlay=true,initial_capital = 1000,
default_qty_type = strategy.percent_of_equity, currency =
[Link],default_qty_value = 100, commission_value = 0.0)
// Functions
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = [Link]([Link](x - x[1]), t)
smoothrng = [Link](avrng, wper) * m
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r
: x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
percWidth(len, perc) => ([Link](len) - [Link](len)) * perc / 100
securityNoRep(sym, res, src) => [Link](sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on)
swingPoints(prd) =>
pivHi = [Link](prd, prd)
pivLo = [Link] (prd, prd)
last_pivHi = [Link](pivHi, pivHi, 1)
last_pivLo = [Link](pivLo, pivLo, 1)
hh = pivHi and pivHi > last_pivHi ? pivHi : na
lh = pivHi and pivHi < last_pivHi ? pivHi : na
hl = pivLo and pivLo > last_pivLo ? pivLo : na
ll = pivLo and pivLo < last_pivLo ? pivLo : na
[hh, lh, hl, ll]
f_chartTfInMinutes() =>
float _resInMinutes = [Link] * (
[Link] ? 1 :
[Link] ? 1. :
[Link] ? 60. * 24 :
[Link] ? 60. * 24 * 7 :
[Link] ? 60. * 24 * 30.4375 : na)
f_kc(src, len, sensitivity) =>
basis = [Link](src, len)
span = [Link](len)
[basis + span * sensitivity, basis - span * sensitivity]
wavetrend(src, chlLen, avgLen) =>
esa = [Link](src, chlLen)
d = [Link]([Link](src - esa), chlLen)
ci = (src - esa) / (0.015 * d)
wt1 = [Link](ci, avgLen)
wt2 = [Link](wt1, 3)
[wt1, wt2]
f_top_fractal(src) => src[4] < src[2] and src[3] < src[2] and src[2] > src[1] and
src[2] > src[0]
f_bot_fractal(src) => src[4] > src[2] and src[3] > src[2] and src[2] < src[1] and
src[2] < src[0]
f_fractalize (src) => f_top_fractal(src) ? 1 : f_bot_fractal(src) ? -1 : 0
f_findDivs(src, topLimit, botLimit) =>
fractalTop = f_fractalize(src) > 0 and src[2] >= topLimit ? src[2] : na
fractalBot = f_fractalize(src) < 0 and src[2] <= botLimit ? src[2] : na
highPrev = [Link](fractalTop, src[2], 0)[2]
highPrice = [Link](fractalTop, high[2], 0)[2]
lowPrev = [Link](fractalBot, src[2], 0)[2]
lowPrice = [Link](fractalBot, low[2], 0)[2]
bearSignal = fractalTop and high[2] > highPrice and src[2] < highPrev
bullSignal = fractalBot and low[2] < lowPrice and src[2] > lowPrev
[bearSignal, bullSignal]
// Get components
source = close
smrng1 = smoothrng(source, 27, 1.5)
smrng2 = smoothrng(source, 55, sensitivity)
smrng = (smrng1 + smrng2) / 2
filt = rngfilt(source, smrng)
up = 0.0, up := filt > filt[1] ? nz(up[1]) + 1 : filt < filt[1] ? 0 :
nz(up[1])
dn = 0.0, dn := filt < filt[1] ? nz(dn[1]) + 1 : filt > filt[1] ? 0 :
nz(dn[1])
bullCond = bool(na), bullCond := source > filt and source > source[1] and up > 0
or source > filt and source < source[1] and up > 0
bearCond = bool(na), bearCond := source < filt and source < source[1] and dn > 0
or source < filt and source > source[1] and dn > 0
lastCond = 0, lastCond := bullCond ? 1 : bearCond ? -1 : lastCond[1]
bull = bullCond and lastCond[1] == -1
bear = bearCond and lastCond[1] == 1
countBull = [Link](bull)
countBear = [Link](bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
rsi = [Link](close, 21)
rsiOb = rsi > 70 and rsi > [Link](rsi, 10)
rsiOs = rsi < 30 and rsi < [Link](rsi, 10)
dHigh = securityNoRep([Link], "D", high [1])
dLow = securityNoRep([Link], "D", low [1])
dClose = securityNoRep([Link], "D", close[1])
// Colors
cyan = #00DBFF, cyan30 = [Link](cyan, 70)
pink = #E91E63, pink30 = [Link](pink, 70)
red = #FF5252, red30 = [Link](red , 70)
// Plot
off = percWidth(300, offsetSignal)
// plotshape(bull ? low - off : na, "Buy Label" , [Link] ,
[Link], cyan, 0, "B" , [Link], size=[Link])
// plotshape(bear ? high + off : na, "Sell Label", [Link],
[Link], pink, 0, "S", [Link], size=[Link])
// Calculating
fast_ma = sma_source == "SMA" ? [Link](src, fast_length) : [Link](src, fast_length)
slow_ma = sma_source == "SMA" ? [Link](src, slow_length) : [Link](src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? [Link](macd, signal_length) : [Link](macd,
signal_length)
hist = macd - signal
buy = hist > 0
sell = hist < 0
res(Resolution) =>
if Resolution == '00 Current'
[Link]
else
if Resolution == '01 1m'
'1'
else
if Resolution == '02 3m'
'3'
else
if Resolution == '03 5m'
'5'
else
if Resolution == '04 15m'
'15'
else
if Resolution == '05 30m'
'30'
else
if Resolution == '06 45m'
'45'
else
if Resolution == '07 1h'
'60'
else
if Resolution == '08 2h'
'120'
else
if Resolution == '09 3h'
'180'
else
if Resolution == '10 4h'
'240'
else
if Resolution == '11 1D'
'1D'
else
if Resolution == '12 1W'
'1W'
else
if Resolution == '13 1M'
'1M'
//
[supertrend1, direction1] = [Link]([Link], res(time1),
[Link](factor, atrPeriod))
bodyMiddle = plot((open + close) / 2, display=[Link])
upTrend = plot(direction1 < 0 ? supertrend1 : na, 'Up Trend',
color=[Link]([Link], 0), style=plot.style_linebr)
downTrend = plot(direction1 > 0 ? supertrend1 : na, 'Down Trend',
color=[Link]([Link], 0), style=plot.style_linebr)
bool isLongSupertrendFilterEnable = enable_supertrend ? direction1 < 0 ?
supertrend1 : na : true
bool isShortSupertrendFilterEnable = enable_supertrend ? direction1 > 0 ?
supertrend1 : na : true
longCondition = long ? low - off : na
shortCondition = short ? high + off : na
plotshape(longCondition, "Buy Label" , [Link] , [Link], cyan, 0,
"Buy" , [Link], size=[Link])
plotshape(shortCondition, "Sell Label", [Link], [Link], pink,
0, "Sell", [Link], size=[Link])
// Time-Of-Day Window
//
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// Inspired from [Link]
Window-Simple-Hours-Time-of-Day-Filter/
isWithinWindowOfTime(_position) =>
currentTimeIsWithinWindowOfTime = not na(time([Link], _position +
':1234567', i_timeZone))
bool isTimeFilterEnabledAndInRange = i_timeFilterEnabled ?
isWithinWindowOfTime(i_betweenTime) : true
// Debug Start
// drawLabel(_offset, _pivot, _style, _color) =>
// if not na(_pivot)
// [Link](bar_index[_offset], _pivot, [Link](_pivot,
[Link]), style=_style, color=_color, textcolor=#131722)
//ATR
//todo determine if close should be source for both long/short or if high/low
should be used?
float ATRSLPriceWhenLongPositionEntered = [Link](openingLongPosition, close -
([Link](i_atrLengthSL) * i_atrMultiplierSL) ,0)
float ATRSLPriceWhenShortPositionEntered = [Link](openingShortPosition, close
+ ([Link](i_atrLengthSL) * i_atrMultiplierSL) ,0)
//plot(close + ([Link](i_atrLengthSL) * i_atrMultiplierSL),
color=[Link]([Link], 0))
//plot(close - ([Link](i_atrLengthSL) * i_atrMultiplierSL),
color=[Link]([Link], 0))
switch typeOfStopLoss
"Fixed %" =>
slOrderClosePrice := direction == "long" ?
fixedPercentSLPriceWhenLongPositionEntered :
fixedPercentSLPriceWhenShortPositionEntered
slOrderComment := direction == "long" ? "SL Fixed % Long" : "SL Fixed %
Short"
"Last Swing High/Low" =>
slOrderClosePrice := direction == "long" ?
swingLowPriceWhenLongPostionEntered : swingHighPriceWhenShortPositionEntered
slOrderComment := direction == "long" ? "SL Swing Low Long" : "SL Swing
High Short"
"ATR" =>
slOrderClosePrice := direction == "long" ?
ATRSLPriceWhenLongPositionEntered : ATRSLPriceWhenShortPositionEntered
slOrderComment := direction == "long" ? "SL ATR Long" : "SL ATR Short"
[slOrderClosePrice,slOrderComment]
// Multiple
//ATR
//todo determine if close should be source for both long/short or if high/low
should be used?
float ATRTPPriceWhenLongPositionEntered = [Link](openingLongPosition, close +
([Link](i_atrLengthTP) * i_atrMultiplierTP) ,0)
float ATRTPPriceWhenShortPositionEntered = [Link](openingShortPosition, close
- ([Link](i_atrLengthTP) * i_atrMultiplierTP) ,0)
// plot(close + ([Link](i_atrLengthTP) * i_atrMultiplierTP),
color=[Link]([Link], 0))
// plot(close - ([Link](i_atrLengthTP) * i_atrMultiplierTP),
color=[Link]([Link], 0))
switch typeOfTakeProfit
"Single Fixed %" =>
tpOrderClosePrice := direction == "long" ?
fixedPercentTPPriceWhenLongPositionEntered :
fixedPercentTPPriceWhenShortPositionEntered
tpOrderComment := direction == "long" ? "TP Single Fixed % Long" : "TP
Single Fixed % Short"
"Multiple Fixed %" =>
tpOrderClosePrice := na
tpOrderComment := ""
"Risk:Reward Ratio" =>
tpOrderClosePrice := direction == "long" ?
tpRRPriceWhenLongPositionEntered : tpRRPriceWhenShortPositionEntered
tpOrderComment := direction == "long" ? "TP R:R Long" : "TP R:R Short"
"ATR" =>
tpOrderClosePrice := direction == "long" ?
ATRTPPriceWhenLongPositionEntered : ATRTPPriceWhenShortPositionEntered
tpOrderComment := direction == "long" ? "TP ATR Long" : "TP ATR Short"
[tpOrderClosePrice,tpOrderComment]
// Functions
//
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
per(pcnt) =>
strategy.position_size != 0 ? [Link](pcnt / 100 *
strategy.position_avg_price / [Link]) : float(na)
// Plotting
//
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//Plot SL and TP
i_plotTPSL=input(true, title="Plot TP and SL", group='Plotting')
slbp=plot(i_plotTPSL and i_useStopLoss and strategy.position_size > 0 ?
slPriceLong[1] : na, color=[Link], style=plot.style_linebr, title="SL")
tpbp=plot(i_plotTPSL and i_useTakeProfit and strategy.position_size > 0 ?
tpPriceLong[1] : na, color=[Link], style=plot.style_linebr, title="TP")
epbp=plot(i_plotTPSL and strategy.position_size > 0 ? strategy.position_avg_price :
na, color=[Link], style=plot.style_linebr, title="Entry")
fill(slbp, epbp, color=[Link]([Link], 90))
fill(tpbp, epbp, color=[Link]([Link], 90))
// Alerts
//
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
i_alert_txt_entry_long = input.text_area(defval = "i alert message", title = "Long
Entry Message", group = "Alerts")
i_alert_txt_entry_short = input.text_area(defval = "", title = "Short Entry
Message", group = "Alerts")
i_alert_txt_exit_long = input.text_area(defval = "", title = "Long Exit Message",
group = "Alerts")
i_alert_txt_exit_short = input.text_area(defval = "", title = "Short Exit Message",
group = "Alerts")
if isStartEndPeriodsAndTimeInRange
if longConditionFinal
[Link]('Long', [Link])
alert(message=i_alert_txt_entry_long, freq=alert.freq_once_per_bar_close)
if shortConditionFinal
[Link]('Short', [Link])
alert(message=i_alert_txt_entry_short, freq=alert.freq_once_per_bar_close)
if i_useTakeProfit
//Inspired from Multiple %% profit exits example by adolgo
[Link]
if i_typeOfTakeProfit == "Multiple Fixed %"
[Link]('TP1', qty_percent=i_takeProfitQuantityPercent1,
profit=per(i_takeProfitTargetPercent1))
[Link]('TP2', qty_percent=i_takeProfitQuantityPercent2,
profit=per(i_takeProfitTargetPercent2))
[Link]('TP3', qty_percent=i_takeProfitQuantityPercent3,
profit=per(i_takeProfitTargetPercent3))
[Link]('i_takeProfitTargetPercent4',
profit=per(i_takeProfitTargetPercent4))
if tpLongCondition
[Link]('Long', alert_message = i_alert_txt_exit_long ,comment=
'tpCommentLong')
if tpShortCondition
[Link]('Short', alert_message =
i_alert_txt_exit_short ,comment= 'tpCommentShort')
if i_useStopLoss
if slLongCondition
[Link]('Long', alert_message = i_alert_txt_exit_long,
comment='slCommentLong')
if slShortCondition
[Link]('Short', comment= 'slCommentShort')
if closeLongCondition
[Link]('Long', alert_message = i_alert_txt_exit_long,
comment='Conditional Close Long')
if closeShortCondition
[Link]('Short', alert_message = i_alert_txt_exit_short,
comment='Conditional Close Short')
if newWin
lossRow := 0
winRow := winRow + 1
if winRow > maxWinRow
maxWinRow := winRow
if newLoss
winRow := 0
lossRow := lossRow + 1
if lossRow > maxLossRow
maxLossRow := lossRow
if [Link]
// Update table
lastTime = strategy.position_size == 0 ?
[Link].exit_time([Link]-1) : time
dollarReturn = [Link]
f_fillCell(dashTable, 0, 0, "Start:", [Link]("{0,date,long}",
[Link].entry_time(0)) , bgcolor, [Link]) // + [Link]("
{0,time,HH:mm}", [Link].entry_time(0))
f_fillCell(dashTable, 0, 1, "End:", [Link]("{0,date,long}", lastTime) ,
bgcolor, [Link]) // + [Link](" {0,time,HH:mm}",
[Link].entry_time(0))
_profit = ([Link] / strategy.initial_capital) * 100
f_fillCell(dashTable, 0, 2, "Net Profit:", [Link](_profit, '##.##') +
"%", _profit > 0 ? [Link] : [Link], [Link])
_numOfDaysInStrategy = (lastTime - [Link].entry_time(0)) /
(1000 * 3600 * 24)
f_fillCell(dashTable, 0, 3, "Percent Per Day", [Link](_profit /
_numOfDaysInStrategy, '#########################.#####')+"%", _profit > 0 ?
[Link] : [Link], [Link])
_winRate = ( [Link] / [Link] ) * 100
f_fillCell(dashTable, 0, 4, "Percent Profitable:", [Link](_winRate,
'##.##') + "%", _winRate < 50 ? [Link] : _winRate < 75 ? #999900 :
[Link], [Link])
f_fillCell(dashTable, 0, 5, "Profit Factor:",
[Link]([Link] / [Link], '##.###'),
[Link] > [Link] ? [Link] : [Link], [Link])
f_fillCell(dashTable, 0, 6, "Total Trades:",
[Link]([Link]), bgcolor, [Link])
f_fillCell(dashTable, 0, 7, "Winning Trades:",
[Link]([Link]), [Link] > [Link] ?
[Link] : [Link], [Link])
f_fillCell(dashTable, 0, 8, "Losing Trades:",
[Link]([Link]), [Link] < [Link] ?
[Link] : [Link], [Link])
f_fillCell(dashTable, 0, 9, "Max Wins In A Row:", [Link](maxWinRow,
'######') , bgcolor, [Link])
f_fillCell(dashTable, 0, 10, "Max Losses In A Row:",
[Link](maxLossRow, '######') , bgcolor, [Link])
if i_showMonthlyPerformance
new_month = month(time) != month(time[1])
new_year = year(time) != year(time[1])
eq = [Link]
bar_pnl = eq / eq[1] - 1
cur_month_pnl = 0.0
cur_year_pnl = 0.0
last_computed = false
[Link](month_pnl , cur_month_pnl[1])
[Link](month_time, time[1])
[Link](year_pnl , cur_year_pnl[1])
[Link](year_time, time[1])
if ([Link])
monthly_table := [Link](position.bottom_right, columns = 14, rows =
[Link](year_pnl) + 1, border_width = 1)
for yi = 0 to [Link](year_pnl) - 1
[Link](monthly_table, 0, yi + 1,
[Link](year([Link](year_time, yi))), bgcolor = #cccccc,
text_size=tableTextSize)
for mi = 0 to [Link](month_time) - 1
m_row = year([Link](month_time, mi)) - year([Link](year_time,
0)) + 1
m_col = month([Link](month_time, mi))
m_color = [Link](month_pnl, mi) > 0 ? [Link]([Link], transp =
40) : [Link]([Link], transp = 40)