Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- //Il trading system completo - Bot Break-in-New Ver-5 (Strategia Breakout) - parte 2
- // (Exchange= FTX) (Sottostante ETH-PERP) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= SI) (Esclusione Ore=NO) (Esclusione Giorni=LUNEDI') (Esclusione Mesi=SETTEMBRE)
- // (Take Profit Long/Short Market = Non Definito) (Take Profit Limit Long/Short= +20% Tradingview-Hub) (Stop Loss Limit Long/Short= -11% Tradingview-Hub) (Stop Loss Market Long/Short= -11%) (Stop Emergenza= No)
- //@version=5
- //1. Strategia
- strategy(title='Bot Break-in-New Ver-5 ETH/PERP FTX 4H LONG E SHORT', overlay=true,
- pyramiding=0,
- initial_capital=150,
- commission_type=strategy.commission.percent,
- commission_value=0.1,
- slippage=3,
- default_qty_type=strategy.percent_of_equity,
- default_qty_value=27.5)
- //input SL e TP Long e Short
- input_stop_loss_long = input.float(title='stop_loss_long', defval=11, minval=0, maxval=100, step=0.1, group='Stop')
- input_take_profit_long = input.float(title='take_profit_long', defval=20, minval=0, maxval=100, step=0.1, group='Stop')
- input_stop_loss_short = input.float(title='stop_loss_short', defval=11, minval=0, maxval=100, step=0.1, group='Stop')
- input_take_profit_short = input.float(title='take_profit_short', defval=20, minval=0, maxval=100, step=0.1, group='Stop')
- input_lunghezza_ema = input.int(title='lunghezza ema', defval=125, minval=0, maxval=300, step=5)
- riskPerTrade = input.float(title='Risk Per Trade %', minval=0.1, maxval=100, defval=27.5, step=0.5)
- length = input.int(title="Length", defval=10, minval=1)
- smoothing = input.string(title="Smoothing", defval="SMA", options=["RMA", "SMA", "EMA", "WMA"])
- ma_function(source, length) =>
- switch smoothing
- "RMA" => ta.rma(source, length)
- "SMA" => ta.sma(source, length)
- "EMA" => ta.ema(source, length)
- => ta.wma(source, length)
- plot(ma_function(ta.tr(true), length), title = "ATR", color=color.new(#B71C1C, 0))
- in_atr_Mult = input.float(title='atr_Mult', minval=1, maxval=5, defval=1, step=0.5)
- //in_tpPips = input(title="TP", type=input.integer, defval=40)
- //in_slPips = input(title="SL", type=input.integer, defval=30)
- //Input solo long o solo short
- in_solo_long = input.bool(title='Solo long', defval=false, inline='1', group='Direzione')
- in_solo_short = input.bool(title='Solo short', defval=false, inline='1', group='Direzione')
- atr = ma_function(ta.tr(true), length)
- media = ta.ema(close, input_lunghezza_ema)
- plot(media, title = "media", color=color.blue)
- hourTrading = input(title='sessione valida di trading', defval='0600-2300:23456')
- rangeTrading = time(timeframe.period, hourTrading)
- //calcolo della size per lo SL e TP
- size = strategy.equity * riskPerTrade / 100
- nr_contratti = size / close
- stop_loss_long = math.round(size / syminfo.mintick / 100 * input_stop_loss_long) / nr_contratti
- take_profit_long = math.round(size / syminfo.mintick / 100 * input_take_profit_long) / nr_contratti
- stop_loss_short = math.round(size / syminfo.mintick / 100 * input_stop_loss_short) / nr_contratti
- take_profit_short = math.round(size / syminfo.mintick / 100 * input_take_profit_short) / nr_contratti
- plot(strategy.position_size != 0 ? strategy.position_avg_price : na , color=strategy.position_size > 0 ? color.blue : strategy.position_size < 0 ? color.red : na, style=plot.style_linebr, title="entry_price") // stampa l'entry price in rosso se short in blu se long
- plot(strategy.position_size > 0 ? strategy.position_avg_price + take_profit_long * 0.01 * 10: strategy.position_size < 0 ? strategy.position_avg_price - take_profit_short * 0.01 * 10: na, color=color.green, style=plot.style_cross, linewidth=2, title="tk_limit")
- plot(strategy.position_size > 0 ? strategy.position_avg_price - stop_loss_long * 0.01 * 10: strategy.position_size < 0 ? strategy.position_avg_price + stop_loss_short * 0.01 * 10 : na, color=color.red, style=plot.style_cross, linewidth=2, title="sl_limit")
- bgcolor(strategy.position_size > 0 ? color.green : strategy.position_size < 0 ? color.red : na, transp=90) // sfondo verde quando siamo long, sfondo rosso quando siamo short, no sfondo quando non siamo in posizione
- // giorni da 1 a 7 1 Γ¨ domenica
- //Condizione Long
- cond_long = math.abs(open - close) > atr * in_atr_Mult and close > open and close > media and month != 9 and dayofweek != 2 and not in_solo_short
- //Condizione Short
- cond_short = math.abs(open - close) > atr * in_atr_Mult and close < open and close < media and month != 9 and dayofweek != 2 and not in_solo_long
- //entrata e uscita Long
- if cond_long// and strategy.opentrades == 0
- strategy.entry('operazioneLong', strategy.long)
- strategy.exit('SL e TP', from_entry='operazioneLong', loss=stop_loss_long, profit=take_profit_long)
- //entrata e uscita Short
- if cond_short// and strategy.opentrades == 0
- strategy.entry('operazioneShort', strategy.short)
- strategy.exit('SL e TP', from_entry='operazioneShort', loss=stop_loss_short, profit=take_profit_short)
- // Emergency!!!
- lastEntryPrice = strategy.opentrades.entry_price(0)
- //chiusura di sicurezza Long
- prezzo_uscita_emergenza_long = lastEntryPrice - math.round(size / 100 * input_stop_loss_long) / nr_contratti
- plot(prezzo_uscita_emergenza_long,color=color.blue,linewidth=3, title="Stop_Emerg_Long")
- condExitLong = close < prezzo_uscita_emergenza_long
- if strategy.opentrades ==1 and condExitLong
- strategy.close(id = 'operazioneLong')
- barcolor(cond_long ? color.lime : cond_short ? color.purple : na)
- //chiusura di sicurezza Short
- //plot(lastEntryPrice, color=color.fuchsia, style=plot.style_circles, linewidth=3, title="Last entry price")
- prezzo_uscita_emergenza_short = lastEntryPrice + math.round(size / 100 * input_stop_loss_short) / nr_contratti
- plot(prezzo_uscita_emergenza_short,color=color.red,linewidth=3, title="Stop_Emerg_Short")
- condExitShort = close > prezzo_uscita_emergenza_short
- if strategy.opentrades ==1 and condExitShort
- strategy.close(id = 'operazioneShort')
- // Tabella risultati mensili by QuantNomad (TSC Boilerplate) Per visualizzare andare nelle impostazioni proprietΓ e spuntare ad ogni tick
- new_month = month(time) != month(time[1])
- new_year = year(time) != year(time[1])
- eq = strategy.equity
- bar_pnl = eq / eq[1] - 1
- cur_month_pnl = 0.0
- cur_year_pnl = 0.0
- // Current Monthly P&L
- cur_month_pnl := new_month ? 0.0 :
- (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1
- // Current Yearly P&L
- cur_year_pnl := new_year ? 0.0 :
- (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1
- // Arrays to store Yearly and Monthly P&Ls
- var month_pnl = array.new_float(0)
- var month_time = array.new_int(0)
- var year_pnl = array.new_float(0)
- var year_time = array.new_int(0)
- last_computed = false
- if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
- if (last_computed[1])
- array.pop(month_pnl)
- array.pop(month_time)
- array.push(month_pnl , cur_month_pnl[1])
- array.push(month_time, time[1])
- if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
- if (last_computed[1])
- array.pop(year_pnl)
- array.pop(year_time)
- array.push(year_pnl , cur_year_pnl[1])
- array.push(year_time, time[1])
- last_computed := barstate.islast ? true : nz(last_computed[1])
- // Monthly P&L Table
- var monthly_table = table(na)
- prec = input(2, title = "Return Precision")
- if (barstate.islast)
- monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, bgcolor=#0F0F0F,border_width=1,border_color=#000000)
- table.cell(monthly_table, 0, 0, "", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 1, 0, "Jan", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 2, 0, "Feb", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 3, 0, "Mar", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 4, 0, "Apr", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 5, 0, "May", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 6, 0, "Jun", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 7, 0, "Jul", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 8, 0, "Aug", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 9, 0, "Sep", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 10, 0, "Oct", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 11, 0, "Nov", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 12, 0, "Dec", text_color=#D3D3D3, bgcolor=#0F0F0F)
- table.cell(monthly_table, 13, 0, "Year", text_color=#D3D3D3, bgcolor=#0F0F0F)
- for yi = 0 to array.size(year_pnl) - 1
- table.cell(monthly_table, 0, yi + 1, str.tostring(year(array.get(year_time, yi))), text_color=#D3D3D3, bgcolor=#0F0F0F)
- y_color = array.get(year_pnl, yi) > 0 ? color.lime : color.red
- table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
- 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.lime : color.red
- table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement