OPEN-SOURCE SCRIPT

56

//version=5
strategy('Naser_M4PIVOT_R', overlay=true, default_qty_value=5, currency=currency.USD, default_qty_type=strategy.percent_of_equity, initial_capital=10000, max_bars_back=4000)

//===== TIMEFRAME ==========================================================================//
factorA = input.float(0.8, title='حساسیت ربات 1', step=0.1)
periodA = input.int(1, minval=1, maxval=100, title='پریود ربات 1')

factor = input.float(2.5, title='حساسیت ربات 2', step=0.1)
period = input.int(1, minval=1, maxval=100, title='پریود ربات 2')

use_sl = input(title='حد ضرر رو فعال کن', defval=false)
initial_sl_pct = input.float(defval=5, minval=0.0, title='حد ضرر %')



from_day = input.int(defval=1, title='از روز', minval=1, maxval=31)
from_month = input.int(defval=1, title='از ماه', minval=1, maxval=12)
from_year = input.int(defval=2019, title='از سال', minval=2017)
to_day = input.int(defval=1, title='تا روز', minval=1, maxval=31)
to_month = input.int(defval=1, title='تا ماه', minval=1, maxval=12)
to_year = input.int(defval=9999, title='تا سال', minval=2017)
start = timestamp(from_year, from_month, from_day, 00, 00)
finish = timestamp(to_year, to_month, to_day, 23, 59)
window() =>
time >= start and time <= finish ? true : false

//===== INPUTS ==========================================================================//


upA = (high + low) / 2 - factorA * ta.atr(periodA)
downA = (high + low) / 2 + factorA * ta.atr(periodA)
trend_upA = 0.0
trend_upA := close[1] > trend_upA[1] ? math.max(upA, trend_upA[1]) : upA
trend_downA = 0.0
trend_downA := close[1] < trend_downA[1] ? math.min(downA, trend_downA[1]) : downA
trendA = 0.0
trendA := close > trend_downA[1] ? 1 : close < trend_upA[1] ? -1 : nz(trendA[1], 1)
tslA = trendA == 1 ? trend_upA : trend_downA
line_colorA = trendA == 1 ? 'green' : 'red'
long_signalA = trendA == 1 and trendA[1] == -1
short_signalA = trendA == -1 and trendA[1] == 1
backgroundA = input(title=' پس زمینه ربات 1', defval=true)



//===== PLOTS ==========================================================================//

// Line
line_plotA = plot(tslA, color=trendA == 1 ? color.green : color.red, linewidth=2, title='Trend Line')
// Labels
plotshape(long_signalA and window() ? upA : na, location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
plotshape(short_signalA and window() ? downA : na, location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
// Circles
plotshape(long_signalA and window() ? upA : na, title='Uptrend starts', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(short_signalA and window() ? downA : na, title='Downtrend starts', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
// Background
long_fillA = backgroundA ? trendA == 1 ? color.green : na : na
short_fillA = backgroundA ? trendA == -1 ? color.red : na : na
candle_plotA = plot(ohlc4, title='Price Line', color=trendA == 1 ? long_fillA : short_fillA, linewidth=2, transp=90)
fill(candle_plotA, line_plotA, title='Long Background', color=long_fillA, transp=90)
fill(candle_plotA, line_plotA, title='Short Background', color=short_fillA, transp=90)

//===== GLOBAL ==========================================================================//

var entry_price = 0.0
var updated_entry_price = 0.0
var sl_price = 0.0

has_open_trade() =>
strategy.position_size != 0
has_no_open_trade() =>
strategy.position_size == 0

is_long() =>
strategy.position_size > 0 ? true : false
is_short() =>
strategy.position_size < 0 ? true : false

plot(use_sl ? has_no_open_trade() ? close : sl_price : na, color=has_no_open_trade() ? na : color.blue, title='Stop Loss')

strategy_close() =>
if is_long()
strategy.close('خرید')
if is_short()
strategy.close('فروش')

strategy_long() =>
strategy.entry('خرید', strategy.long)

strategy_short() =>
strategy.entry('فروش', strategy.short)

sl_pct = initial_sl_pct
if long_signalA or is_long() and not(short_signalA or is_short())
sl_pct := initial_sl_pct * -1
sl_pct

//===== STRATEGY ==========================================================================//

crossed_sl = false
if is_long() and use_sl
crossed_sl := close <= sl_price
crossed_sl
if is_short() and use_sl
crossed_sl := close >= sl_price
crossed_sl

terminate_operation = window() and has_open_trade() and crossed_sl

if terminate_operation and not(long_signalA or short_signalA) // Do not close position if trend is flipping anyways.
entry_price := 0.0
updated_entry_price := entry_price
sl_price := 0.0
strategy_close()

start_operation = window() and (long_signalA or short_signalA)

if start_operation
entry_price := close
updated_entry_price := entry_price
sl_price := entry_price + entry_price * sl_pct / 100
if long_signalA
strategy_long()
if short_signalA
strategy_short()

//===== TRAILING ==========================================================================//

if is_long() and use_sl
strategy_pct = (close - updated_entry_price) / updated_entry_price * 100.00
if strategy_pct > 1
sl_pct += strategy_pct - 1.0
new_sl_price = updated_entry_price + updated_entry_price * sl_pct / 100
sl_price := math.max(sl_price, new_sl_price)
updated_entry_price := sl_price
updated_entry_price

if is_short() and use_sl
strategy_pct = (close - updated_entry_price) / updated_entry_price * 100.00
if strategy_pct < -1
sl_pct += strategy_pct + 1.0
new_sl_price = updated_entry_price + updated_entry_price * sl_pct / 100
sl_price := math.min(sl_price, new_sl_price)
updated_entry_price := sl_price
updated_entry_price





//===== INPUTS ==========================================================================//

up = (high + low) / 2 - factor * ta.atr(period)
down = (high + low) / 2 + factor * ta.atr(period)
trend_up = 0.0
trend_up := close[1] > trend_up[1] ? math.max(up, trend_up[1]) : up
trend_down = 0.0
trend_down := close[1] < trend_down[1] ? math.min(down, trend_down[1]) : down
trend = 0.0
trend := close > trend_down[1] ? 1 : close < trend_up[1] ? -1 : nz(trend[1], 1)
tsl = trend == 1 ? trend_up : trend_down
line_color = trend == 1 ? 'green' : 'red'
long_signal = trend == 1 and trend[1] == -1
short_signal = trend == -1 and trend[1] == 1
background = input(title='پس زمینه ربات 2', defval=true)


//===== PLOTS ==========================================================================//

// Line
line_plot = plot(tsl, color=trend == 1 ? color.green : color.red, linewidth=2, title='Trend Line')
// Labels
plotshape(long_signal and window() ? up : na, location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
plotshape(short_signal and window() ? down : na, location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
// Circles
plotshape(long_signal and window() ? up : na, title='Uptrend starts', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(short_signal and window() ? down : na, title='Downtrend starts', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
// Background
long_fill = background ? trend == 1 ? color.green : na : na
short_fill = background ? trend == -1 ? color.red : na : na
candle_plot = plot(ohlc4, title='Price Line', color=trend == 1 ? long_fill : short_fill, linewidth=2, transp=90)
fill(candle_plot, line_plot, title='Long Background', color=long_fill, transp=90)
fill(candle_plot, line_plot, title='Short Background', color=short_fill, transp=90)

//===== GLOBAL ==========================================================================//

var entry_priceB = 0.0
var updated_entry_priceB = 0.0
var sl_priceB = 0.0



plot(use_sl ? has_no_open_trade() ? close : sl_price : na, color=has_no_open_trade() ? na : color.blue, title='Stop Loss')

strategy_closeB() =>
if is_long()
strategy.close('خرید')
if is_short()
strategy.close('فروش')



sl_pctB = initial_sl_pct
if long_signal or is_long() and not(short_signal or is_short())
sl_pct := initial_sl_pct * -1
sl_pct

//===== STRATEGY ==========================================================================//

crossed_slB = false
if is_long() and use_sl
crossed_sl := close <= sl_price
crossed_sl
if is_short() and use_sl
crossed_sl := close >= sl_price
crossed_sl

terminate_operationB = window() and has_open_trade() and crossed_sl

if terminate_operation and not(long_signal or short_signal) // Do not close position if trend is flipping anyways.
entry_price := 0.0
updated_entry_price := entry_price
sl_price := 0.0
strategy_close()

start_operationB = window() and (long_signal or short_signal)

if start_operation
entry_price := close
updated_entry_price := entry_price
sl_price := entry_price + entry_price * sl_pct / 100
if long_signal
strategy_long()
if short_signal
strategy_short()

//===== TRAILING ==========================================================================//

if is_long() and use_sl
strategy_pct = (close - updated_entry_price) / updated_entry_price * 100.00
if strategy_pct > 1
sl_pct += strategy_pct - 1.0
new_sl_price = updated_entry_price + updated_entry_price * sl_pct / 100
sl_price := math.max(sl_price, new_sl_price)
updated_entry_price := sl_price
updated_entry_price

if is_short() and use_sl
strategy_pct = (close - updated_entry_price) / updated_entry_price * 100.00
if strategy_pct < -1
sl_pct += strategy_pct + 1.0
new_sl_price = updated_entry_price + updated_entry_price * sl_pct / 100
sl_price := math.min(sl_price, new_sl_price)
updated_entry_price := sl_price
updated_entry_price
Bands and Channels

Script open-source

In pieno spirito TradingView, l'autore di questo script lo ha pubblicato open-source, in modo che i trader possano comprenderlo e verificarlo. Un saluto all'autore! È possibile utilizzarlo gratuitamente, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.

Vuoi usare questo script sui tuoi grafici?

Declinazione di responsabilità