Professional Documents
Culture Documents
Strategy Test
Strategy Test
Strategy Test
0 at
https://mozilla.org/MPL/2.0/
// © Samuel Ralte
//@version=5
strategy("Strategy TEST1", overlay=true, initial_capital = 1000, default_qty_type =
strategy.percent_of_equity, default_qty_value = 100, commission_value = 0.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]
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]
//
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
per(pcnt) =>
strategy.position_size != 0 ? math.round(pcnt / 100 *
strategy.position_avg_price / syminfo.mintick) : float(na)
if shortConditionFinal
strategy.entry('Short', strategy.short)
// alert(message=alertSyntaxBase + 'side:short',
freq=alert.freq_once_per_bar_close)
if i_useTakeProfit
//Inspired from Multiple %% profit exits example by adolgo
https://www.tradingview.com/script/kHhCik9f-Multiple-profit-exits-example/
if i_typeOfTakeProfit == "Multiple Fixed %"
strategy.exit('TP1', qty_percent=i_takeProfitQuantityPercent1,
profit=per(i_takeProfitTargetPercent1))
strategy.exit('TP2', qty_percent=i_takeProfitQuantityPercent2,
profit=per(i_takeProfitTargetPercent2))
strategy.exit('TP3', qty_percent=i_takeProfitQuantityPercent3,
profit=per(i_takeProfitTargetPercent3))
strategy.exit('i_takeProfitTargetPercent4',
profit=per(i_takeProfitTargetPercent4))
if tpLongCondition
strategy.close('Long', comment= 'tpCommentLong')
if tpShortCondition
strategy.close('Short', comment= 'tpCommentShort')
if i_useStopLoss
if slLongCondition
strategy.close('Long', comment='slCommentLong')
if slShortCondition
strategy.close('Short', comment= 'slCommentShort')
if closeLongCondition
strategy.close('Long', comment='Conditional Close Long')
if closeShortCondition
strategy.close('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 barstate.islastconfirmedhistory
// Update table
lastTime = strategy.position_size == 0 ?
strategy.closedtrades.exit_time(strategy.closedtrades-1) : time
dollarReturn = strategy.netprofit
f_fillCell(dashTable, 0, 0, "Start:", str.format("{0,date,long}",
strategy.closedtrades.entry_time(0)) , bgcolor, color.white) // + str.format("
{0,time,HH:mm}", strategy.closedtrades.entry_time(0))
f_fillCell(dashTable, 0, 1, "End:", str.format("{0,date,long}", lastTime) ,
bgcolor, color.white) // + str.format(" {0,time,HH:mm}",
strategy.opentrades.entry_time(0))
_profit = (strategy.netprofit / strategy.initial_capital) * 100
f_fillCell(dashTable, 0, 2, "Net Profit:", str.tostring(_profit, '##.##') +
"%", _profit > 0 ? color.teal : color.maroon, color.white)
_numOfDaysInStrategy = (lastTime - strategy.closedtrades.entry_time(0)) /
(1000 * 3600 * 24)
f_fillCell(dashTable, 0, 3, "Percent Per Day", str.tostring(_profit /
_numOfDaysInStrategy, '#########################.#####')+"%", _profit > 0 ?
color.teal : color.maroon, color.white)
_winRate = ( strategy.wintrades / strategy.closedtrades ) * 100
f_fillCell(dashTable, 0, 4, "Percent Profitable:", str.tostring(_winRate,
'##.##') + "%", _winRate < 50 ? color.maroon : _winRate < 75 ? #999900 :
color.teal, color.white)
f_fillCell(dashTable, 0, 5, "Profit Factor:",
str.tostring(strategy.grossprofit / strategy.grossloss, '##.###'),
strategy.grossprofit > strategy.grossloss ? color.teal : color.maroon, color.white)
f_fillCell(dashTable, 0, 6, "Total Trades:",
str.tostring(strategy.closedtrades), bgcolor, color.white)
f_fillCell(dashTable, 0, 7, "Winning Trades:",
str.tostring(strategy.wintrades), strategy.wintrades > strategy.losstrades ?
color.teal : color.maroon, color.white)
f_fillCell(dashTable, 0, 8, "Losing Trades:",
str.tostring(strategy.losstrades), strategy.wintrades < strategy.losstrades ?
color.teal : color.maroon, color.white)
f_fillCell(dashTable, 0, 9, "Max Wins In A Row:", str.tostring(maxWinRow,
'######') , bgcolor, color.white)
f_fillCell(dashTable, 0, 10, "Max Losses In A Row:",
str.tostring(maxLossRow, '######') , bgcolor, color.white)
i_showMonthlyPerformance = input.bool(true, 'Monthly Performance',
group='Dashboards', inline="Show Dashboards")
i_monthlyReturnPercision = 2
if i_showMonthlyPerformance
new_month = month(time) != month(time[1])
new_year = year(time) != year(time[1])
eq = strategy.equity
bar_pnl = eq / eq[1] - 1
cur_month_pnl = 0.0
cur_year_pnl = 0.0
last_computed = false
array.push(month_pnl , cur_month_pnl[1])
array.push(month_time, time[1])
array.push(year_pnl , cur_year_pnl[1])
array.push(year_time, time[1])
if (barstate.islastconfirmedhistory)
monthly_table := table.new(position.bottom_right, columns = 14, rows =
array.size(year_pnl) + 1, border_width = 1)
for yi = 0 to array.size(year_pnl) - 1
table.cell(monthly_table, 0, yi + 1,
str.tostring(year(array.get(year_time, yi))), bgcolor = #cccccc,
text_size=tableTextSize)
for mi = 0 to array.size(month_time) - 1
m_row = year(array.get(month_time, mi)) - year(array.get(year_time,
0)) + 1
m_col = month(array.get(month_time, mi))
m_color = array.get(month_pnl, mi) > 0 ? color.new(color.teal, transp =
40) : color.new(color.maroon, transp = 40)