Professional Documents
Culture Documents
//
VERSION = 'v11-Test'// 2024.3.20
strategy(
'ALGOX',
shorttitle = 'ALGOX ' + VERSION,
overlay = true,
explicit_plot_zorder = true,
pyramiding = 0,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 50,
calc_on_every_tick = false,
process_orders_on_close = true,
max_bars_back = 500,
initial_capital = 5000,
commission_type = strategy.commission.percent,
commission_value = 0.02
)
//
// === INPUTS ===
// ————— Converts current chart timeframe into a float minutes value.
//f_tfInMinutes() =>
// _tfInMinutes = (
// timeframe.period == '1' ? '3' :
// timeframe.period == '3' ? '5' :
// timeframe.period == '5' ? '15' :
// timeframe.period == '15' ? '30' :
// timeframe.period == '30' ? '240' :
// timeframe.period == '60' ? '480' : 'D')
//my_time = f_tfInMinutes()
f_tfInMinutes() =>
_tfInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1. / 60 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
my_time = f_tfInMinutes()
tradeDateIsAllowed = not enableFilter or (time >= fromDate and time <= toDate)
/////////////////////////////////////////////
// Create non-repainting security function
rp_security(_symbol, _res, _src) =>
request.security(_symbol, _res, _src[barstate.isconfirmed ? 0 : 1])
// Main Indicator
securityNoRep(sym, res, src) => request.security(sym, res, src[barstate.isconfirmed
? 0 : 1], barmerge.gaps_off, barmerge.lookahead_on)
////////////////////////////////////////////////////////
// === BASE FUNCTIONS ===
// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len, offSig, offALMA) =>
v1 = ta.sma(src, len) // Simple
v2 = ta.ema(src, len) // Exponential
v3 = 2 * v2 - ta.ema(v2, len) // Double Exponential
v4 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len) // Triple
Exponential
v5 = ta.wma(src, len) // Weighted
v6 = ta.vwma(src, len) // Volume Weighted
v7 = 0.0
sma_1 = ta.sma(src, len) // Smoothed
v7 := na(v7[1]) ? sma_1 : (v7[1] * (len - 1) + src) / len
v8 = ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len),
math.round(math.sqrt(len))) // Hull
v9 = ta.linreg(src, len, offSig) // Least Squares
v10 = ta.alma(src, len, offALMA, offSig) // Arnaud Legoux
v11 = ta.sma(v1, len) // Triangular (extreme smooth)
// SuperSmoother filter
// © 2013 John F. Ehlers
a1 = math.exp(-1.414 * 3.14159 / len)
b1 = 2 * a1 * math.cos(1.414 * 3.14159 / len)
c2 = b1
c3 = -a1 * a1
c1 = 1 - c2 - c3
v12 = 0.0
v12 := c1 * (src + nz(src[1])) / 2 + c2 * nz(v12[1]) + c3 * nz(v12[2])
type == 'EMA' ? v2 : type == 'DEMA' ? v3 : type == 'TEMA' ? v4 : type ==
'WMA' ? v5 : type == 'VWMA' ? v6 : type == 'SMMA' ? v7 : type == 'HullMA' ? v8 :
type == 'LSMA' ? v9 : type == 'ALMA' ? v10 : type == 'TMA' ? v11 : type == 'SSMA' ?
v12 : v1
//stratRes
//
trendColour = closeSeriesAlt > openSeriesAlt ? color.green : color.red
bcolour = closeSeriesAlt > openSeriesAlt ? lime100 : red100
barcolor(scolor ? bcolour : na, title='Bar Colours')
closeP = plot(almaRibbon ? closeSeriesAlt : na, title='Close Series',
color=color.new(trendColour, 20), linewidth=2, style=plot.style_line)
openP = plot(almaRibbon ? openSeriesAlt : na, title='Open Series',
color=color.new(trendColour, 20), linewidth=2, style=plot.style_line)
fill(closeP, openP, color=color.new(trendColour, 80))
//
//<triggers>
lxTrigger = false
sxTrigger = false
leTrigger = ta.crossover (closeSeriesAlt, openSeriesAlt)
seTrigger = ta.crossunder(closeSeriesAlt, openSeriesAlt)
if barstate.isconfirmed
wasLong := false
else
if buy
wasLong := true
if barstate.isconfirmed
wasShort := false
else
if sell
wasShort := true
if buy
buyFlag := true
alert("BUY")
if sell
sellFlag := true
alert("SELL")
if barstate.isconfirmed
if buy
alert("BUY_CONFIRMED")
buyFlag:= false
else
if buyFlag
alert("BUY_DISAPPEAR")
buyFlag:= false
if sell
alert("SELL_CONFIRMED")
sellFlag:= false
else
if sellFlag
alert("SELL_DISAPPEAR")
sellFlag:= false
//tradeType
if buy and TPSType == "Trailing" and tradeDateIsAllowed
strategy.close("Short" , alert_message = i_alert_txt_exit_short)
strategy.entry("Long" , strategy.long , alert_message = i_alert_txt_entry_long)
//tradeType
if buy and TPSType == "Options" and tradeDateIsAllowed
// strategy.close("Short" , alert_message = i_alert_txt_exit_short)
strategy.entry("Long" , strategy.long , alert_message = i_alert_txt_entry_long)
// Calculate ATR
tpatrValue = ta.atr(atrLength)
// Calculate take profit and stop loss levels for buy signals
takeProfit1_buy = 1 * profitFactor * tpatrValue //close + profitFactor * atrValue
takeProfit2_buy = 2 * profitFactor * tpatrValue //close + 2 * profitFactor *
atrValue
takeProfit3_buy = 3 * profitFactor * tpatrValue //close + 3 * profitFactor *
atrValue
stopLoss_buy = close - takeProfit1_buy //stopFactor * tpatrValue
// Calculate take profit and stop loss levels for sell signals
takeProfit1_sell = 1 * profitFactor * tpatrValue //close - profitFactor * atrValue
takeProfit2_sell = 2 * profitFactor * tpatrValue //close - 2 * profitFactor *
atrValue
takeProfit3_sell = 3 * profitFactor * tpatrValue //close - 3 * profitFactor *
atrValue
stopLoss_sell = close + takeProfit1_sell //stopFactor * tpatrValue
// ——————————— <constant_declarations>
//Tooltip
T_LVL = '(%) Exit Level'
T_QTY = '(%) Adjust trade exit volume'
T_MSG = 'Paste JSON message for your bot'
//Webhook Message
O_LEMSG = 'Long Entry'
O_LXMSGSL = 'Long SL'
O_LXMSGTP1 = 'Long TP1'
O_LXMSGTP2 = 'Long TP2'
O_LXMSGTP3 = 'Long TP3'
O_LXMSG = 'Long Exit'
O_SEMSG = 'Short Entry'
O_SXMSGSL = 'Short SL'
O_SXMSGA = 'Short TP1'
O_SXMSGB = 'Short TP2'
O_SXMSGC = 'Short TP3'
O_SXMSGX = 'Short Exit'
i_sxLvlTP1 = i_lxLvlTP1
i_sxQtyTP1 = i_lxQtyTP1
i_sxLvlTP2 = i_lxLvlTP2
i_sxQtyTP2 = i_lxQtyTP2
i_sxLvlTP3 = i_lxLvlTP3
i_sxQtyTP3 = i_lxQtyTP3
i_sxLvlSL = i_lxLvlSL
G_DISPLAY = 'Display'
//<display>
i_alertOn = input.bool (true, 'Alert Labels On/Off' , group
= G_DISPLAY)
i_barColOn = input.bool (true, 'Bar Color On/Off' , group
= G_DISPLAY)
// ——————————— <function_declarations>
// @function Calculate the Take Profit line, and the crossover or crossunder
f_tp(_condition, _conditionValue, _leTrigger, _seTrigger, _src, _lxLvlTP,
_sxLvlTP)=>
var float _tpLine = 0.0
_topLvl = _src + _lxLvlTP //TPSType == "Fixed %" ? _src + (_src * (_lxLvlTP
/ 100)) : _src + _lxLvlTP
_botLvl = _src - _lxLvlTP //TPSType == "Fixed %" ? _src - (_src * (_sxLvlTP
/ 100)) : _src - _sxLvlTP
_tpLine := _condition[1] != _conditionValue and _leTrigger ? _topLvl :
_condition[1] != -_conditionValue and _seTrigger ? _botLvl :
nz(_tpLine[1])
[_tpLine]
// ——————————— <calculations>
//<set initial values>
var float condition = 0.0
var float slLine = 0.0
var float entryLine = 0.0
switch
leTrigger and condition[1] <= 0.0 => condition := 1.0
seTrigger and condition[1] >= 0.0 => condition := -1.0
tp3Long and condition[1] == 1.2 => condition := 1.3
tp3Short and condition[1] == -1.2 => condition := -1.3
tp2Long and condition[1] == 1.1 => condition := 1.2
tp2Short and condition[1] == -1.1 => condition := -1.2
tp1Long and condition[1] == 1.0 => condition := 1.1
tp1Short and condition[1] == -1.0 => condition := -1.1
slLong and condition[1] >= 1.0 => condition := 0.0
slShort and condition[1] <= -1.0 => condition := 0.0
lxTrigger and condition[1] >= 1.0 => condition := 0.0
sxTrigger and condition[1] <= -1.0 => condition := 0.0
// ——————————— <strategy_calls> {
//<long orders>
if strategy.position_size <= 0 and longE and TPSType == "ATR" and
tradeDateIsAllowed
strategy.entry( 'Long', strategy.long, alert_message =
i_leMsg, comment = 'LE')
if strategy.position_size > 0 and condition == 1.0 and TPSType == "ATR" and
tradeDateIsAllowed
strategy.exit( id = 'LXTP1', from_entry = 'Long',
qty_percent = i_lxQtyTP1, limit = tp1Line, stop
= slLine, comment_profit = 'LXTP1', comment_loss = 'SL',
alert_profit = i_lxMsgTP1, alert_loss = i_lxMsgSL)
if strategy.position_size > 0 and condition == 1.1 and TPSType == "ATR" and
tradeDateIsAllowed
strategy.exit( id = 'LXTP2', from_entry = 'Long',
qty_percent = i_lxQtyTP2, limit = tp2Line, stop
= slLine, comment_profit = 'LXTP2', comment_loss = 'SL',
alert_profit = i_lxMsgTP2, alert_loss = i_lxMsgSL)
if strategy.position_size > 0 and condition == 1.2 and TPSType == "ATR" and
tradeDateIsAllowed
strategy.exit( id = 'LXTP3', from_entry = 'Long',
qty_percent = i_lxQtyTP3, limit = tp3Line, stop
= slLine, comment_profit = 'LXTP3', comment_loss = 'SL',
alert_profit = i_lxMsgTP3, alert_loss = i_lxMsgSL)
if longX and tradeDateIsAllowed
strategy.close( 'Long', alert_message = i_lxMsg, comment
= 'LX')
//<short orders>
if strategy.position_size >= 0 and shortE and TPSType == "ATR" and
tradeDateIsAllowed
strategy.entry( 'Short', strategy.short, alert_message =
i_leMsg, comment = 'SE')
if strategy.position_size < 0 and condition == -1.0 and TPSType == "ATR" and
tradeDateIsAllowed
strategy.exit( id = 'SXTP1', from_entry =
'Short', qty_percent = i_sxQtyTP1, limit = tp1Line,
stop = slLine, comment_profit = 'SXTP1', comment_loss =
'SL', alert_profit = i_sxMsgTP1, alert_loss = i_sxMsgSL)
if strategy.position_size < 0 and condition == -1.1 and TPSType == "ATR" and
tradeDateIsAllowed
strategy.exit( id = 'SXTP2', from_entry =
'Short', qty_percent = i_sxQtyTP2, limit = tp2Line,
stop = slLine, comment_profit = 'SXTP2', comment_loss =
'SL', alert_profit = i_sxMsgTP2, alert_loss = i_sxMsgSL)
if strategy.position_size < 0 and condition == -1.2 and TPSType == "ATR" and
tradeDateIsAllowed
strategy.exit( id = 'SXTP3', from_entry =
'Short', qty_percent = i_sxQtyTP3, limit = tp3Line,
stop = slLine, comment_profit = 'SXTP3', comment_loss =
'SL', alert_profit = i_sxMsgTP3, alert_loss = i_sxMsgSL)
if shortX and tradeDateIsAllowed
strategy.close( 'Short', alert_message = i_sxMsg, comment
= 'SX')
// ——————————— <visuals>
c_tp = leTrigger or seTrigger ? na :
condition == 0.0 ? na : color.green
c_entry = leTrigger or seTrigger ? na :
condition == 0.0 ? na : color.blue
c_sl = leTrigger or seTrigger ? na :
condition == 0.0 ? na : color.red
p_tp1Line = plot ( condition == 1.0 or condition == -1.0 ? tp1Line : na,
title = "TP Line 1", color = c_tp, linewidth = 1, style =
plot.style_linebr)
p_tp2Line = plot ( condition == 1.0 or condition == -1.0 or condition ==
1.1 or condition == -1.1 ? tp2Line : na, title = "TP Line 2", color =
c_tp, linewidth = 1, style = plot.style_linebr)
p_tp3Line = plot ( condition == 1.0 or condition == -1.0 or condition ==
1.1 or condition == -1.1 or condition == 1.2 or condition == -1.2 ? tp3Line :
na, title = "TP Line 3", color = c_tp, linewidth = 1, style =
plot.style_linebr)
p_entryLine = plot ( condition >= 1.0 or condition <= -1.0 ? entryLine : na,
title = "Entry Line", color = c_entry, linewidth = 1, style =
plot.style_linebr)
p_slLine = plot ( condition == 1.0 or condition == -1.0 or condition ==
1.1 or condition == -1.1 or condition == 1.2 or condition == -1.2 ? slLine :
na, title = "SL Line", color = c_sl, linewidth = 1, style =
plot.style_linebr)
//<alerts labels>
plotshape( i_alertOn and longE, title = 'Long', text = 'Long',
textcolor = color.white, color = color.green, style = shape.labelup,
size = size.tiny, location = location.belowbar)
plotshape( i_alertOn and shortE, title = 'Short', text = 'Short',
textcolor = color.white, color = color.red, style = shape.labeldown,
size = size.tiny, location = location.abovebar)
plotshape( i_alertOn and (longX or shortX) ? close : na, title = 'Close',
text = 'Close', textcolor = color.white, color = color.gray, style
= shape.labelup, size = size.tiny, location = location.absolute)
l_tp = i_alertOn and (longTP1 or shortTP1) ? close : na
//<debug>
plot( na, title = "─── <debug> ───", editable = false, display =
display.data_window)
plot( condition, title = "condition", editable = false, display =
display.data_window)
plot( strategy.position_size * 100, title = ".position_size", editable =
false, display = display.data_window)
//#endregion }
// ——————————— <↑↑↑ G_RISK ↑↑↑>
if TPSType == "ATR"
line.delete( f_qDq(a_slLine, line.new( entryIndex, slLine,
last_bar_index + l_right, slLine, style = line.style_solid, color = c_sl)))
if TPSType == "ATR"
line.delete( f_qDq(a_entryLine, line.new( entryIndex, entryLine,
last_bar_index + l_right, entryLine, style = line.style_solid, color =
color.blue)))
if TPSType == "ATR"
line.delete( f_qDq(a_tp3Line, line.new( entryIndex, tp3Line,
last_bar_index + l_right, tp3Line, style = line.style_solid, color = c_tp)))
if TPSType == "ATR"
line.delete( f_qDq(a_tp2Line, line.new( entryIndex, tp2Line,
last_bar_index + l_right, tp2Line, style = line.style_solid, color = c_tp)))
if TPSType == "ATR"
line.delete( f_qDq(a_tp1Line, line.new( entryIndex, tp1Line,
last_bar_index + l_right, tp1Line, style = line.style_solid, color = c_tp)))
if TPSType == "ATR"
label.delete( f_qDq(a_slLabel, label.new( last_bar_index + l_right,
slLine, 'SL: ' + str.tostring(slLine, '##.###'), style =
label.style_label_left, textcolor = color.white, color = c_sl)))
if TPSType == "ATR"
label.delete( f_qDq(a_entryLabel, label.new( last_bar_index + l_right,
entryLine, 'Entry: ' + str.tostring(entryLine, '##.###'), style =
label.style_label_left, textcolor = color.white, color = color.blue)))
if TPSType == "ATR"
label.delete( f_qDq(a_tp3label, label.new( last_bar_index + l_right,
tp3Line, 'TP3: ' + str.tostring(tp3Line, '##.###') + " - Target Pips : - " +
str.tostring(longE ? tp3Line - entryLine : entryLine - tp3Line, "#.##"), style =
label.style_label_left, textcolor = color.white, color = c_tp)))
if TPSType == "ATR"
label.delete( f_qDq(a_tp2label, label.new( last_bar_index + l_right,
tp2Line, 'TP2: ' + str.tostring(tp2Line, '##.###'), style =
label.style_label_left, textcolor = color.white, color = c_tp)))
if TPSType == "ATR"
label.delete( f_qDq(a_tp1label, label.new( last_bar_index + l_right,
tp1Line, 'TP1: ' + str.tostring(tp1Line, '##.###'), style =
label.style_label_left, textcolor = color.white, color = c_tp)))
//#endregion }
// ——————————— <↑↑↑ G_SCRIPT02 ↑↑↑>
// ——————————— <alerts>
//<any_alert_function_call>
if longE or shortE or longX or shortX
alert(message = 'Any Alert', freq = alert.freq_once_per_bar_close)
if longE
alert(message = 'Long Entry', freq = alert.freq_once_per_bar_close)
if shortE
alert(message = 'Short Entry', freq = alert.freq_once_per_bar_close)
if longX
alert(message = 'Long Exit', freq = alert.freq_once_per_bar_close)
if shortX
alert(message = 'Short Exit', freq = alert.freq_once_per_bar_close)
//#endregion }
// ——————————— <↑↑↑ G_SCRIPT03 ↑↑↑>
// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// © TraderHalai
// This script was born out of my quest to be able to display strategy back test
statistics on charts to allow for easier backtesting on devices that do not
natively support backtest engine (such as mobile phones, when I am backtesting from
away from my computer). There are already a few good ones on TradingView, but
most / many are too complicated for my needs.
//
//Found an excellent display backtest engine by 'The Art of Trading'. This script
is a snippet of his hard work, with some very minor tweaks and changes. Much
respect to the original author.
//
//Full credit to the original author of this script. It can be found here:
https://www.tradingview.com/script/t776tkZv-Hammers-Stars-Strategy/?
offer_id=10&aff_id=15271
//
// This script can be copied and airlifted onto existing strategy scripts of your
own, and integrates out of the box without implementation of additional functions.
I've also added Max Runup, Average Win and Average Loss per trade to the orignal
script.
//
//Will look to add in more performance metrics in future, as I further develop this
script.
//
//Feel free to use this display panel in your scripts and strategies.
//DEMO basic strategy - Use your own strategy here - Jaws Mean Reversion from my
profile used here
//source = input(title = "Source", defval = close)
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)
table.cell(monthly_table, 0, 0, "", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 1, 0, "Jan", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 2, 0, "Feb", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 3, 0, "Mar", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 4, 0, "Apr", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 5, 0, "May", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 6, 0, "Jun", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 7, 0, "Jul", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 8, 0, "Aug", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 9, 0, "Sep", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 10, 0, "Oct", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 11, 0, "Nov", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 12, 0, "Dec", bgcolor = #cccccc,
text_size=tableTextSize)
table.cell(monthly_table, 13, 0, "Year", bgcolor = #999999,
text_size=tableTextSize)
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)
//@version=5
//strategy("Table to filter trades per day", overlay=true, use_bar_magnifier =
true, initial_capital = 5000, calc_on_every_tick = true, calc_on_order_fills =
true, commission_type = strategy.commission.cash_per_contract)
//~ ___________________________________________________________________________
//~ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//~ !!!!!!!!!!!!!!!_________________ START _________________!!!!!!!!!!!!!!!!!
f_strategy_closedtrades_hour() =>
switch
i_check_open_close =="Closed" =>
dayofweek(strategy.closedtrades.exit_time(trade_number), timezone_setting)
i_check_open_close =="Opened" =>
dayofweek(strategy.closedtrades.entry_time(trade_number), timezone_setting)
f_data(_i) =>
var _closed_trades = 0
var _loss_trades = 0
var _win_trades = 0
var _txt_closed_trades = ""
var _txt_loss_trades = ""
var _txt_win_trades = ""
//__________________________
var string[] array1 = array.new_string(5)
var string[] array2 = array.new_string(5)
var string[] array3 = array.new_string(5)
var string[] array4 = array.new_string(5)
var string[] array5 = array.new_string(5)
var string[] array6 = array.new_string(5)
var string[] array7 = array.new_string(5)
for i = 1 to 7
switch
i == 1 => f_pass_data_to_array(i,array1)
i == 2 => f_pass_data_to_array(i,array2)
i == 3 => f_pass_data_to_array(i,array3)
i == 4 => f_pass_data_to_array(i,array4)
i == 5 => f_pass_data_to_array(i,array5)
i == 6 => f_pass_data_to_array(i,array6)
i == 7 => f_pass_data_to_array(i,array7)
/////////////////////////////////////////////////////////////// ©
BackQuant ///////////////////////////////////////////////////////////////
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
at https://mozilla.org/MPL/2.0/
// © BackQuant
import TradingView/ta/4 as ta
//@version=5
//indicator(
// title="DEMA Adjusted Average True Range [BackQuant]",
// shorttitle = "DEMA ATR [BackQuant]",
// overlay=true,
// timeframe="",
// timeframe_gaps=true
// )
atr = ta.atr(lookback)
trueRange = atr * atrFactor
DemaAtr = demaOut
DemaAtr := nz(DemaAtr[1], DemaAtr)
// Function Out
DemaAtr = DemaAtrWithBands(periodDema, source, periodAtr, factorAtr)
/////////////////////////////////////////////////////////////// ©
BackQuant ///////////////////////////////////////////////////////////////
// Conditions
DemaAtrLong = DemaAtr > DemaAtr[1]
DemaAtrShort = DemaAtr < DemaAtr[1]
// Colour Condtions
var color Trend = #ffffff
if DemaAtrLong
Trend := longColour
if DemaAtrShort
Trend := shortColour
// Plotting
plot(
showAtr ? DemaAtr : na,
"ATR",
color=Trend,
linewidth = 2
)
//
═══════════════════════════════════════════════════════════════════════════════════
═══════════════ //
//# *
═══════════════════════════════════════════════════════════════════════════════════
═══════════
//# *
//# * Study : Backtest Framework
//# * Author : © dgtrd
//# * Purpose : Ability to optimize a study and observe trade simulation
statistics accordingly
//# *
//# * Revision History
//# * Release : Nov 21, 2020 : Initial Release
//# * Update : Mar 13, 2021 : Enchanced Backtest Framework
//# * - long/short/stoploss conditions enchaced
//# * - early warning ability added (label + alert)
//# *
//# *
═══════════════════════════════════════════════════════════════════════════════════
═══════════
//
═══════════════════════════════════════════════════════════════════════════════════
═══════════════ //
// -Inputs
═══════════════════════════════════════════════════════════════════════════════════
═══════ //
// -Calculations
═══════════════════════════════════════════════════════════════════════════════════
═ //
if isBackTest
if lblInOutSL
label longLabel = label.new(bar_index, low, text='L', tooltip='entry
price : ' + str.tostring(entryPrice) + '\nentry value : ' + str.tostring(capital,
'#.##'), color=color.green, style=label.style_label_up, textcolor=color.white,
textalign=text.align_center, size=size.tiny)
longLabel
if lblInOutSL
text_1 = stopLossCondition ? 'SL' : 'TP'
label shortLabel = label.new(bar_index, high, text=text_1,
tooltip='change .......... : ' + str.tostring((exitPrice / entryPrice - 1) * 100,
'#.##') + '%\nentry/exit price : ' + str.tostring(entryPrice) + ' / ' +
str.tostring(exitPrice) + '\nnew capital ..... : ' + str.tostring(capital, '#.##'),
color=color.red, style=label.style_label_down, textcolor=color.white,
textalign=text.align_center, size=size.tiny)
shortLabel
if ta.change(time)
label.delete(wLabel[1])
if stopLossCondition
alert('stop loss condition, price ' + str.tostring(close),
alert.freq_once_per_bar)
if lblTrdStat
var years = (timenow - time) / 31556952000
// -Plotting
═══════════════════════════════════════════════════════════════════════════════════
═ //