AlgoX Non-Repainting - Bar Mag False
AlgoX Non-Repainting - Bar Mag False
strategy(
'AlgoX - Non-Repainting',
overlay = true,
explicit_plot_zorder = true,
pyramiding = 0,
default_qty_value = 3,
calc_on_every_tick = true,
process_orders_on_close = true,
max_bars_back = 500,
initial_capital = 10000,
commission_type = strategy.commission.cash_per_contract,
commission_value = 2
)
//Truncate Function
truncate(number, decimals) =>
factor = math.pow(10, decimals)
int(number * factor) / factor
//
// === INPUTS ===
TPSType = input.string('ATR', 'What TPS should be taken : ', options = ['ATR',
'Trailing', 'Options'])
setupType = input.string('Renko', title='What Trading Setup should be taken : ',
options=['Open/Close', 'Renko'])
scolor = input(true, title='Show coloured Bars to indicate Trend?')
almaRibbon = input(false, title='Enable Ribbon?')
//
f_tfInMinutes() =>
_tfInMinutes = timeframe.period == '1' ? '3' : timeframe.period == '3' ? '5' :
timeframe.period == '5' ? '15' : timeframe.period == '15' ? '30' : timeframe.period
== '30' ? '60' : timeframe.period == '60' ? '240' : 'D'
_tfInMinutes
my_time1 = f_tfInMinutes()
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1. / 60. :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 1440. :
timeframe.isweekly ? 10080. :
timeframe.ismonthly ? 43800. : na)
my_time = str.tostring(f_resInMinutes()*tfmult)
//filtering
atra = request.security(syminfo.tickerid, '', ta.atr(atrfiltLen)
[1],barmerge.gaps_off,barmerge.lookahead_off)
atrMa = atrMaType == 'EM' ? ta.ema(atra, atrMaLen) : ta.sma(atra, atrMaLen)
updm = ta.change(high)
downdm = -ta.change(low)
plusdm = na(updm) ? na : updm > downdm and updm > 0 ? updm : 0
minusdm = na(downdm) ? na : downdm > updm and downdm > 0 ? downdm : 0
tradLen = tradLen1 * 1
//============================================
//
EMA_CROSS--------------------------------------------------------------------------
------------------------------------------------------ // Signal 4
//============================================
a = ta.ema(renko_close, EMA1_length)
b = ta.ema(renko_close, EMA2_length)
//<triggers>
lxTrigger = false
sxTrigger = false
leTrigger = buy_entry
seTrigger = sel_entry
if barstate.isconfirmed
wasLong := false
else
if buy
wasLong := true
if barstate.isconfirmed
wasShort := false
else
if sell
wasShort := true
// 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 = true //input.bool (true, 'Alert Labels On/Off' ,
group = G_DISPLAY)
i_barColOn = true //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
//Long Orders
if longE
strategy.entry('Long',strategy.long, alert_message = '{' +'"ticker": "ES", '
+'"action": "buy", ' + '"quantity": 3'+'}')
strategy.exit('L-TP1', 'Long', qty_percent = i_lxQtyTP1, limit =
tp1Line, alert_message = '{' +'"ticker": "ES", ' +'"action": "sell", ' +
'"quantity": 1, ' + '"orderType": "limit", ' + '"limitPrice":
'+str.tostring(math.round_to_mintick(tp1Line),'##.##')+ '}')
strategy.exit('L-TP2', 'Long', qty_percent = i_lxQtyTP2, limit =
tp2Line, alert_message = '{' +'"ticker": "ES", ' +'"action": "sell", ' +
'"quantity": 1'+'}')
strategy.exit('L-TP3', 'Long', qty_percent = i_lxQtyTP3, limit =
tp3Line, alert_message = '{' +'"ticker": "ES", ' +'"action": "sell", ' +
'"quantity": 1'+'}')
strategy.exit('L-Stop Loss', 'Long', qty_percent = 100, stop = slLine,
alert_message = '{' +'"ticker": "ES", ' +'"action": "exit"'+'}')
//Short Orders
if shortE
strategy.entry('Short', strategy.short, alert_message = '{' +'"ticker": "ES", '
+'"action": "sell", ' + '"quantity": 3'+'}')
strategy.exit('S-TP1', 'Short', qty_percent = i_sxQtyTP1, limit = tp1Line,
alert_message = '{' +'"ticker": "ES", ' +'"action": "buy", ' + '"quantity": 1'+'}')
strategy.exit('S-TP2', 'Short', qty_percent = i_sxQtyTP2, limit = tp2Line,
alert_message = '{' +'"ticker": "ES", ' +'"action": "buy", ' + '"quantity": 1'+'}')
strategy.exit('S-TP3', 'Short', qty_percent = i_sxQtyTP3, limit = tp3Line,
alert_message = '{' +'"ticker": "ES", ' +'"action": "buy", ' + '"quantity": 1'+'}')
strategy.exit('S-Stop Loss', 'Short', qty_percent = 100, stop = slLine,
alert_message = '{' +'"ticker": "ES", ' +'"action": "exit"'+'}')
// ——————————— <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, '##.##'), 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)))
hide = timeframe.isintraday
// EMA calculations
ema = request.security(syminfo.tickerid, matimeframe, ta.ema(close, emaPeriod)
[1],barmerge.gaps_off,barmerge.lookahead_off)
ema2 = request.security(syminfo.tickerid, matimeframe, ta.ema(close,emaPeriod2)
[1],barmerge.gaps_off,barmerge.lookahead_off)
ema3 = request.security(syminfo.tickerid, matimeframe,ta.ema(close, emaPeriod3)
[1],barmerge.gaps_off,barmerge.lookahead_off)
emaColor = close > ema3 ? color.new(color.rgb(56, 142, 60, 63), 50) :
color.new(color.rgb(147, 40, 51, 38), 50)
// Plotting EMA's
plot_ema1 = plot(hide ? ema : na, style=plot.style_line,
color=color.new(color.rgb(255, 255, 255, 100), 50), title="EMA", linewidth=2)
plot_ema2 = plot(hide ? ema2 : na, style=plot.style_line,
color=color.new(color.rgb(255, 255, 255, 100), 50), title="EMA", linewidth=1)
plot_ema3 = plot(ema3, style=plot.style_line, color=emaColor, title="EMA",
linewidth=1)
// EMA Cloud
cloudColor = ema2 > ema ? color.new(#0f8513, 80) : color.new(#a81414, 80)
cloudColor2 = ema2 > ema3 ? color.new(#0f8513, 50) : color.new(#a81414, 50)
cloudColor := showcloud ? cloudColor : na
fill(plot_ema1, plot_ema2, color=cloudColor, title="EMA Cloud")
fill(plot_ema3, plot_ema2, color=cloudColor, title="EMA Cloud")
import TradingView/ta/8 as ta
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 Trendcolor = #ffffff
if DemaAtrLong
Trendcolor := longColour
if DemaAtrShort
Trendcolor := shortColour
// Plotting
plot( showAtr ? DemaAtr : na, "ATR", color=Trendcolor, linewidth = 2 )