Algo Strategy LTF
Algo Strategy LTF
// Inputs
sensitivity = input.float(200, title='Sensitive', step=1, group="Main")
start_date_input = input.time(defval=timestamp("1 Jan 2024"), title="Start
calculating date", group="Main")
// FIB LVL
_236 = input.float(0.236, title='Final Long', step=0.001, group='FIB LVL')
_382 = input.float(0.382, title='Sensitive', step=0.001, group='FIB LVL')
_5 = input.float(0.5, title='trend line', step=0.001, group='FIB LVL')
_456 = input.float(-0.456, title='SL line', step=0.001, group='FIB LVL')
_618 = input.float(0.618, title='Sensitive', step=0.001, group='FIB LVL')
_786 = input.float(0.786, title='final short', step=0.001, group='FIB LVL')
// Strategy TP SL
useSession = input.bool(defval=false, title='Sessione', inline='Sessione',
group="INPUT settings")
closeAtSessionEnd = input.bool(defval=false, title='Close all sessions at the end',
group="INPUT settings", tooltip='Close all positions at the market price at the end
of each session')
// OUTPUT settings
multiprofit = input.bool(true, title="Use Multi Profit", group="OUTPUT settings")
dcaAct = input.bool(true, title="Use Dollar Cost Averaging:", group="OUTPUT
settings", inline='DCA1')
dacValue = input.string("5", title="Number of entry", options=["2", "3", "5"],
group="OUTPUT settings", inline='DCA1')
TP1 = input.float(0.9, title="TP1 %", step=0.1, group="OUTPUT settings",
inline='1')
TP2 = input.float(1.8, title="TP2 %", step=0.1, group="OUTPUT settings",
inline='2')
TP3 = input.float(3.6, title="TP3 %", step=0.1, group="OUTPUT settings",
inline='3')
TP4 = input.float(5, title="TP4 %", step=0.1, group="OUTPUT settings", inline='4')
qty1 = input.float(30, title="% EXIT", step=1, group="OUTPUT settings", inline='1')
qty2 = input.float(30, title="% EXIT", step=1, group="OUTPUT settings", inline='2')
qty3 = input.float(15, title="% EXIT", step=1, group="OUTPUT settings", inline='3')
qty4 = input.float(15, title="% EXIT", step=1, group="OUTPUT settings", inline='4')
SL = input.float(3, title="Stoploss %", step=0.1, group="OUTPUT settings",
inline='6')
movestoploss = input.bool(title="If TP1 is reached: Move Stoploss to entry price",
group="OUTPUT settings", defval=false)
active_trailing = input.bool(title="If TP1 is reached: Activate Trailing Stoploss",
group="OUTPUT settings", defval=false)
sl_type = input.string('ATR', title='Tipo di Trailing', options=['ATR', '%', 'FIB
LVL'], inline='Trailing1')
atrLength = input.int(14, title='Durata ATR', inline='Trailing1')
stopLoss = input.int(title='% Trailing', defval=5, minval=1, inline='Trailing2')
atrMultiplier = input.int(5, title='ATR Multi Trailing', inline='Trailing2')
// Plot Dashboard
plotDashboard = input.bool(true, group="groupBacktest", title="Plot Dashboard",
inline="UX Backtest")
DashLabel = input.int(defval=35, title='Label X Offset', minval=0, maxval=205,
group="groupBacktest", inline="UX Backtest")
tradeIdeeAct = input.bool(true, group="groupBacktest", title="Show Trade Idee",
inline="UX Backtest")
oppositeSignalACT = input.bool(true, group="groupBacktest", title="Stop or opposite
Signal", inline="UX Backtest")
trailingConfigurationACT = input.bool(true, title="Trailing Configuration ON/OFF",
inline="trailing", group="groupBacktest")
trailingConfigurationType = input.string("Breakeven", title="Trailing Type",
options=["Breakeven", "Moving Target", "Moving 2-Target", "Percent Below Triggers",
"Percent Below Highest"], inline="trailing", group="groupBacktest")
movingTarget = input.int(1, title="Moving Target (BE)", minval=1,
group="groupBacktest", inline="SignalG")
// Strategy variables
var bool is_long_trend_started = false
var bool is_short_trend_started = false
var bool is_trend_change = na
var bool is_long_trend = false
var bool is_short_trend = false
var bool redy_long = false
var bool can_long = false
var bool redy_short = false
var bool can_short = false
var float imba_entry_long_line = na
var float imba_entry_short_line = na
var float STLLong = na
var float STLShort = na
// Truncate function
truncate(number, decimals) =>
factor = math.pow(10, decimals)
int(number * factor) / factor
// Strategy logic
high_line = ta.highest(high, int(sensitivity))
low_line = ta.lowest(low, int(sensitivity))
channel_range = high_line - low_line
fib_236 = high_line - channel_range * (_236)
fib_382 = high_line - channel_range * (_382)
fib_5 = high_line - channel_range * (_5)
fib_456 = high_line - channel_range * (_456)
fib_618 = high_line - channel_range * (_618)
fib_786 = high_line - channel_range * (_786)
imba_trend_line = fib_5
SL_Line = fib_456
// Filtering
RSI = truncate(ta.rsi(close, input.int(14, group='RSI Filterring')), 2)
atra = ta.atr(atrLen)
atrMa = atrMaType == 'EMA' ? ta.ema(atra, atrMaLen) : ta.sma(atra, atrMaLen)
cndSidwayss1 = atra >= atrMa
cndSidwayss2 = RSI > toplimitrsi or RSI < botlimitrsi
cndSidways = cndSidwayss1 or cndSidwayss2
cndSidways1 = cndSidwayss1 and cndSidwayss2
Sidwayss1 = atra <= atrMa
Sidwayss2 = RSI < toplimitrsi and RSI > botlimitrsi
Sidways = Sidwayss1 or Sidwayss2
Sidways1 = Sidwayss1 and Sidwayss2
trendType = typefilter == 'Filter with Atr' ? cndSidwayss1 : typefilter == 'Filter
with RSI' ? cndSidwayss2 : typefilter == 'Atr or RSI' ? cndSidways : typefilter ==
'Atr and RSI' ? cndSidways1 : typefilter == 'No Filtering' ? RSI > 0 : typefilter
== 'Entry Only in flat market(By ATR or RSI)' ? Sidways : typefilter == 'Entry Only
in flat market(By ATR and RSI)' ? Sidways1 : na
// CAN LONG/SHORT
if can_long
is_long_trend := true
is_short_trend := false
is_long_trend_started := is_long_trend_started ? false : true
else if can_short
is_short_trend := true
is_long_trend := false
is_short_trend_started := is_short_trend_started ? false : true
else
is_trend_change := false
can_long := false
can_short := false
is_short_trend_started := false
is_long_trend_started := false
// Plotting TP & SL
plot(strategy.position_size > 0 ? long_sl_lv : na, color=color.new(#ca14af, 20),
style=plot.style_linebr, title="SL Long")
plot(strategy.position_size < 0 ? short_sl_lv : na, color=color.new(#ca14af, 20),
style=plot.style_linebr, title="SL Short")
plot(strategy.position_size > 0 ? strategy.position_avg_price : na,
color=color.new(#2759cd, 30), style=plot.style_linebr, title="Entrie Top")
plot(strategy.position_size < 0 ? strategy.position_avg_price : na,
color=color.new(#2759cd, 30), style=plot.style_linebr, title="Entrie Bottom")
entryLongThree := math.round_to_mintick(math.avg(strategy.position_avg_price,
imba_entry_long_line))
entryLongTwo := math.round_to_mintick(math.avg(strategy.position_avg_price,
entryLongThree))
entryLongFour := math.round_to_mintick(math.avg(entryLongThree,
imba_entry_long_line))
entryShortThree := math.round_to_mintick(math.avg(strategy.position_avg_price,
imba_entry_short_line))
entryShortTwo := math.round_to_mintick(math.avg(strategy.position_avg_price,
entryShortThree))
entryShortFour := math.round_to_mintick(math.avg(entryShortThree,
imba_entry_short_line))
// Plotting TP levels
plot(multiprofit == true and strategy.position_size > 0 ?
strategy.position_avg_price + percentTPSL(TP1)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Long TP1")
plot(multiprofit == true and strategy.position_size > 0 ?
strategy.position_avg_price + percentTPSL(TP2)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Long TP2")
plot(multiprofit == true and strategy.position_size > 0 ?
strategy.position_avg_price + percentTPSL(TP3)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Long TP3")
plot(multiprofit == true and strategy.position_size > 0 ?
strategy.position_avg_price + percentTPSL(TP4)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Long TP4")
plot(multiprofit == true and strategy.position_size > 0 ?
strategy.position_avg_price + percentTPSL(TP4)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Long TP5")
plot(multiprofit == true and strategy.position_size < 0 ?
strategy.position_avg_price - percentTPSL(TP1)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Short TP1")
plot(multiprofit == true and strategy.position_size < 0 ?
strategy.position_avg_price - percentTPSL(TP2)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Short TP2")
plot(multiprofit == true and strategy.position_size < 0 ?
strategy.position_avg_price - percentTPSL(TP3)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Short TP3")
plot(multiprofit == true and strategy.position_size < 0 ?
strategy.position_avg_price - percentTPSL(TP4)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Short TP4")
plot(multiprofit == true and strategy.position_size < 0 ?
strategy.position_avg_price - percentTPSL(TP4)*syminfo.mintick : na,
color=color.new(#14ca3b, 30), style=plot.style_linebr, title="Short TP5")
// Plot Dashboard
var float xval = na
xval := timeframe.period == "1" ? timenow + 300000 : timeframe.period == "3" ?
timenow + (15*60000) : timeframe.period == "5" ? timenow + (25*60000) :
timeframe.period == "15" ? timenow + (75*60000) : timeframe.period == "30" ?
timenow + (150*60000) : timeframe.period == "45" ? timenow + (225*60000) :
timeframe.period == "60" ? timenow + (300*60000) : timeframe.period == "180" ?
timenow + (600*60000) : timeframe.period == "240" ? timenow + (1200*60000) :
timeframe.period == "D" ? timenow + (7200*60000) : timenow + (50400*60000)
if plotDashboard
var float closedTrades = na
var float winTrades = na
var float lossTrades = na
var int maxWinRow = na
var int maxLossRow = na
label la = na
label.delete(la[1])
string txt = 'Total Trade: ' + str.tostring(strategy.closedtrades, '##.##') +
'\n'
txt += 'Total Win: ' + str.tostring(strategy.wintrades, '##.##') + '\n'
txt += 'Total Loss: ' + str.tostring(strategy.losstrades, '##.##') + '\n'
txt += 'Max Win in a Row: ' + str.tostring(maxWinRow, '######') + '\n'
txt += 'Max Loss in a Row: ' + str.tostring(maxLossRow, '######') + '\n'
la := label.new(x=time + DashLabel * (time - time[1]), xloc=xloc.bar_time,
yloc=yloc.price, y=close, text=txt, color=color.new(color.black, 50),
style=label.style_label_left, textcolor=color.white, size=size.normal,
textalign=text.align_left)