0% found this document useful (0 votes)
25 views7 pages

Algo Strategy LTF

The document outlines a trading strategy script for a platform using Pine Script version 5, named 'Algo Strategy LTF'. It includes various inputs for sensitivity, filtering options, Fibonacci levels, take-profit and stop-loss settings, and entry/exit logic based on market conditions. The strategy aims to identify long and short trends, manage trades with multiple profit-taking options, and adjust stop-loss levels dynamically.

Uploaded by

fcgh383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Algo Strategy LTF

The document outlines a trading strategy script for a platform using Pine Script version 5, named 'Algo Strategy LTF'. It includes various inputs for sensitivity, filtering options, Fibonacci levels, take-profit and stop-loss settings, and entry/exit logic based on market conditions. The strategy aims to identify long and short trends, manage trades with multiple profit-taking options, and adjust stop-loss levels dynamically.

Uploaded by

fcgh383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

//@version=5

strategy('Algo Strategy LTF', shorttitle='AlgoStrategy LTF', overlay=true,


max_lines_count=500, max_labels_count=500, max_bars_back=1)

// 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")

// Sideways Filtering Input


filterAct = input.bool(true, title="Atr and RSI", group='Sideways Filtering Input',
inline="1")
typefilter = input.string('Atr and RSI', title='Atr and RSI', options=['Filter with
Atr', 'Filter with RSI', 'Atr or RSI', 'Atr and RSI', 'No Filtering', 'Entry Only
in flat market(By ATR or RSI)', 'Entry Only in flat market(By ATR and RSI)'],
group='Sideways Filtering Input', inline="1")
atrLen = input.int(5, minval=1, title='atr Length', group='Sideways Filtering
Input')
atrMaType = input.string('SMA', options=['SMA', 'EMA'], group='Sideways Filtering
Input', title='atr Moving Average Type')
atrMaLen = input.int(5, minval=1, title='atr MA Length', group='Sideways Filtering
Input')
adxLen = input.float(5, minval=1, maxval=50, title="ADX SMOOTHing", group='Sideways
Filtering Input')
diLen = input.float(14, minval=1, title="DI Length", group='Sideways Filtering
Input')
adxLim = input.float(22, minval=1, title="ADX Limit", group='Sideways Filtering
Input')
SMOOTH = input.float(3, minval=1, maxval=5, title="SMOOTHing Factor",
group='Sideways Filtering Input')
lag = input.float(8, minval=0, maxval=15, title="Lag", group='Sideways Filtering
Input')
toplimitrsi = input.int(45, title='TOP Limit', group='RSI Filterring')
botlimitrsi = input.int(10, title='BOT Limit', group='RSI Filterring')

// 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

if time >= start_date_input


can_long := close >= imba_trend_line and close >= fib_236 and not is_long_trend
and trendType
can_short := close <= imba_trend_line and close <= fib_786 and not
is_short_trend and trendType

// 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

is_trend_change := is_short_trend_started or is_long_trend_started


plotshape(is_long_trend and is_long_trend_started ? imba_trend_line : na,
title="Long", style=shape.triangleup, location=location.belowbar,
color=color.green, size=size.small)
plotshape(is_short_trend and is_short_trend_started ? imba_trend_line : na,
title="Short", style=shape.triangledown, location=location.abovebar,
color=color.red, size=size.small)
plot(imba_trend_line, color=is_long_trend[1] ? color.new(#14ca3b, 0) :
is_short_trend ? color.new(#ff0000, 0) : color.new(#000000, 100), linewidth=3)

// Strategy entry logic


if (is_long_trend and is_long_trend_started)
strategy.entry("Long", strategy.long)
imba_entry_long_line := imba_trend_line
if (is_short_trend and is_short_trend_started)
strategy.entry("Short", strategy.short)
imba_entry_short_line := imba_trend_line

// Calculate stop-loss and take-profit levels


percentTPSL(x) =>
strategy.position_size != 0 ? math.round(x / 100 *
strategy.position_avg_price / syminfo.mintick) : float(na)
sl_val = sl_type == 'ATR' ? atrMultiplier * ta.atr(atrLength) : sl_type == 'FIB
LVL' ? SL_Line : sl_type == 'PERCENTAGE' ? close * stopLoss / 100 : 0.00
trailing_sl_long = strategy.position_size > 0 ? imba_trend_line : na
var float trailing_sl_short = na
trailing_sl_short := strategy.position_size < 0 and strategy.position_size[1] >=
0 ? high + sl_val : strategy.position_size < 0 and strategy.position_size[1] < 0 ?
math.min(high + sl_val, nz(trailing_sl_short[1])) : na
STLLong := (strategy.position_size > 0 and movestoploss) ?
strategy.position_avg_price : imba_entry_long_line -
percentTPSL(SL)*syminfo.mintick
STLShort := (strategy.position_size < 0 and movestoploss) ?
strategy.position_avg_price : imba_entry_short_line +
percentTPSL(SL)*syminfo.mintick

long_sl_lv = (strategy.position_size > 0 and active_trailing) ? trailing_sl_long :


STLLong
short_sl_lv = (strategy.position_size < 0 and active_trailing) ?
trailing_sl_short : STLShort
long_tp1_lv = strategy.position_avg_price + percentTPSL(TP1)*syminfo.mintick
long_tp2_lv = strategy.position_avg_price + percentTPSL(TP2)*syminfo.mintick
long_tp3_lv = strategy.position_avg_price + percentTPSL(TP3)*syminfo.mintick
long_tp4_lv = strategy.position_avg_price + percentTPSL(TP4)*syminfo.mintick
short_tp1_lv = strategy.position_avg_price - percentTPSL(TP1)*syminfo.mintick
short_tp2_lv = strategy.position_avg_price - percentTPSL(TP2)*syminfo.mintick
short_tp3_lv = strategy.position_avg_price - percentTPSL(TP3)*syminfo.mintick
short_tp4_lv = strategy.position_avg_price - percentTPSL(TP4)*syminfo.mintick

// Exit logic for multiprofit


if multiprofit
strategy.exit("TP1", "Long", qty_percent=qty1, profit=percentTPSL(TP1),
comment="Tp1🎯")
strategy.exit("TP2", "Long", qty_percent=qty2, profit=percentTPSL(TP2),
comment="Tp2🎯")
strategy.exit("TP3", "Long", qty_percent=qty3, profit=percentTPSL(TP3),
comment="Tp3🎯")
strategy.exit("TP4", "Long", qty_percent=qty4, profit=percentTPSL(TP4),
comment="Tp4🎯")
strategy.exit("TP5", "Long", qty_percent=qty4, profit=percentTPSL(TP4),
comment="Tp5🎯")
if (strategy.position_size > 0 and low < long_sl_lv)
strategy.close("Long", comment="CLOSED")
strategy.exit("TP1", "Short", qty_percent=qty1, profit=percentTPSL(TP1),
comment="Tp1🎯")
strategy.exit("TP2", "Short", qty_percent=qty2, profit=percentTPSL(TP2),
comment="Tp2🎯")
strategy.exit("TP3", "Short", qty_percent=qty3, profit=percentTPSL(TP3),
comment="Tp3🎯")
strategy.exit("TP4", "Short", qty_percent=qty4, profit=percentTPSL(TP4),
comment="Tp4🎯")
strategy.exit("TP5", "Short", qty_percent=qty4, profit=percentTPSL(TP4),
comment="Tp5🎯")
if (strategy.position_size < 0 and high > short_sl_lv)
strategy.close("Short", comment="CLOSED")

// Close all positions at the end of the session


if closeAtSessionEnd
strategy.close_all(comment='Fine Sessione')

// Exit logic for simple profit-taking


if not multiprofit
strategy.exit("SL", "Long", loss=STLLong, comment="SL")
strategy.exit("SL", "Short", loss=STLShort, comment="SL")

// 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")

// Plot DCA entries


var float entryLongThree = na
var float entryLongTwo = na
var float entryLongFour = na
var float entryShortThree = na
var float entryShortTwo = na
var float entryShortFour = na

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))

plot(strategy.position_size > 0 ? dcaAct ? dacValue == "2" ? na : entryLongThree :


na : na, color=color.new(#004cff, 30), style=plot.style_linebr, title="Long Entry
3")
plot(strategy.position_size > 0 ? dcaAct ? dacValue == "2" or dacValue == "3" ?
na : entryLongTwo : na : na, color=color.new(#004cff, 30), style=plot.style_linebr,
title="Long Entry 2")
plot(strategy.position_size > 0 ? dcaAct ? dacValue == "2" or dacValue == "3" ?
na : entryLongFour : na : na, color=color.new(#004cff, 30),
style=plot.style_linebr, title="Long Entry 4")
plot(strategy.position_size < 0 ? dcaAct ? dacValue == "2" ? na : entryShortThree :
na : na, color=color.new(#004cff, 30), style=plot.style_linebr, title="Short Entry
3")
plot(strategy.position_size < 0 ? dcaAct ? dacValue == "2" or dacValue == "3" ?
na : entryShortTwo : na : na, color=color.new(#004cff, 30),
style=plot.style_linebr, title="Short Entry 2")
plot(strategy.position_size < 0 ? dcaAct ? dacValue == "2" or dacValue == "3" ?
na : entryShortFour : na : na, color=color.new(#004cff, 30),
style=plot.style_linebr, title="Short Entry 4")
plot(strategy.position_size == 0 ? na : dcaAct ? dacValue == "2" or dacValue == "3"
or dacValue == "5" ? imba_entry_long_line : na : na , color=is_long_trend[1] ?
color.new(#004cff, 0) : color.new(#000000, 100), linewidth=1,
style=plot.style_linebr)
plot(strategy.position_size == 0 ? na : dcaAct ? dacValue == "2" or dacValue == "3"
or dacValue == "5" ? imba_entry_short_line : na : na , color=is_short_trend[1] ?
color.new(#004cff, 0) : color.new(#000000, 100), linewidth=1,
style=plot.style_linebr)

// 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)

You might also like