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/
- // © Maurizio-Ciullo
- // No Repainting: https://www.youtube.com/watch?v=-ZvGC0wUNAI IN QUESTA STRATEGIA E' STATA USATA LA TECNICA NO REPAINTING
- // Il trading system completo - Weekly Mean Trend ETH/USDT.P BYBIT 4H LONG E SHORT (Strategia Breakout)
- // (Sviluppo Dati Exchange = FTX)
- // (Exchange= BYBIT) (Sottostante ETH-USDT.P) (Timeframe= 4H) (Direzione= LONG E SHORT) (Swing Posizione= NO) (Esclusione Ore=NO) (Esclusione Giorni=NO) (Esclusione Mesi=NO)
- // (Take Profit Long/Short Market = NO) (Take Profit Limit Long/Short= NO)
- // (Stop Loss Limit Long= NO) (Stop Loss Limit Short= NO) (Stop Loss Market Long/Short= SI) (Trailing Stop=SI) (Stop Emergenza= NO)
- // (Rischio Operazione 2% Perdita Media) (Max Drawdown Permesso 10,94%)
- // (In Sample Dal=18/08/17 Al 30/11/21) (Out Of Sample Dal=30/11/21 Al 31/01/22)
- // (Progettatta Il=31/01/22)
- //@version=5
- strategy(title='Bot Weekly-Mean-Trend ETH/USDT.P BYBIT 4H LONG E SHORT', overlay=true,
- pyramiding=0,
- initial_capital=1000,
- commission_type=strategy.commission.percent,
- commission_value=0.1,
- slippage=3,
- default_qty_type=strategy.percent_of_equity,
- default_qty_value=34)
- // >>>>>>>>>>>>>> Start Detecting Inputs <<<<<<<<<<<<<<<<<
- swt = input(true, title='Show This Weeks OHLC?')
- input_media_W_high = input.int(title='media_W_high', defval=10, minval=1, maxval=500, group='Medie_Settimanali')
- input_media_W_low = input.int(title='media_W_low', defval=19, minval=1, maxval=500, group='Medie_Settimanali')
- input_media_W_high_smoothed = input.int(title='media_W_high_smoothed', defval=38, minval=1, maxval=500, group='Medie_Settimanali_Smoothed')
- input_media_W_low_smoothed = input.int(title='media_W_low_smoothed', defval=38, minval=1, maxval=500, group='Medie_Settimanali_Smoothed')
- onlyLong = input.bool(title='Solo long trade', defval=false, inline='1', group='Direzione')
- onlyShort = input.bool(title='Solo short trade', defval=false, inline='1', group='Direzione')
- in_atr_period_long = input.int(title='Periodicità ATR long', minval=5, maxval=500, defval=14, step=1)
- in_atr_period_short = input.int(title='Periodicità ATR short', minval=5, maxval=500, defval=9, step=1)
- in_atr_mult_long = input.float(title='atr_Mult_long', minval=0.1, maxval=5, defval=0.75, step=0.5)
- in_atr_mult_short = input.float(title='atr_Mult_short', minval=0.1, maxval=5, defval=1, step=0.5)
- dmi_min_diff_input_long = input.float(title='Distanza min dim_long', minval=-100, maxval=1000000, defval=18, step=0.1)
- dmi_min_diff_input_short = input.float(title='Distanza min dim_short', minval=-100, maxval=1000000, defval=25, step=0.1)
- // >>>>>>>>>>>>>> End Detecting Inputs <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Detecting Calcolo del range del backtest <<<<<<<<<<<<<<<<<
- startDate = input.int(title='Start Date', defval=17, minval=1, maxval=31, group='Periodo')
- startMonth = input.int(title='Start Month', defval=08, minval=1, maxval=12, group='Periodo')
- startYear = input.int(title='Start Year', defval=2000, minval=1800, maxval=2100, group='Periodo')
- endDate = input.int(title='End Date', defval=01, minval=1, maxval=31, group='Periodo')
- endMonth = input.int(title='End Month', defval=01, minval=1, maxval=12, group='Periodo')
- endYear = input.int(title='End Year', defval=2121, minval=1800, maxval=2150, group='Periodo')
- inDateRange = time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0) and time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0)
- // >>>>>>>>>>>>>> End Detecting Calcolo del range del backtest <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Detecting Hour Range Trading <<<<<<<<<<<<<<<<<
- // hourTrading = input(title='sessione valida di trading', defval='0600-2300:23456')
- // hourRangeTrading = time(timeframe.period, hourTrading)
- // >>>>>>>>>>>>>> EndDetecting Hour Range Trading <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Detecting Weekly Highs/Lows <<<<<<<<<<<<<<<<<
- wtdo = request.security(syminfo.tickerid, 'W', open) //[barstate.isconfirmed ? 0:1]
- wpdo = request.security(syminfo.tickerid, 'W', open[1])
- wpc = request.security(syminfo.tickerid, 'W', close)
- wpdc = request.security(syminfo.tickerid, 'W', close[1])
- wph = request.security(syminfo.tickerid, 'W', high[barstate.isconfirmed ? 0:1]) // NO REPAINTING [barstate.isconfirmed ? 0:1]
- wpdh = request.security(syminfo.tickerid, 'W', high[1])
- wpl = request.security(syminfo.tickerid, 'W', low[barstate.isconfirmed ? 0:1]) // NO REPAINTING [barstate.isconfirmed ? 0:1]
- wpdl = request.security(syminfo.tickerid, 'W', low[1])
- // Weekly Plots
- // plot(swt and wtdo ? wtdo : na, title="Weekly Open", style=plot.style_circles, linewidth=1, color=color.silver)
- // plot(swt and wpc ? wpc : na, title="Weekly Close", style=plot.style_circles, linewidth=1, color=color.fuchsia)
- // plot(swt and wph ? wph : na, title="Weekly High", style=plot.style_circles, linewidth=1, color=color.green)
- // plot(swt and wpl ? wpl : na, title="Weekly Low", style=plot.style_circles, linewidth=1, color=color.red)
- // >>>>>>>>>>>>>> End Detecting Weekly Highs/Lows <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Detecting Variables <<<<<<<<<<<<<<<<<
- media_high = ta.sma(wph, input_media_W_high)
- plot(close > media_high ? media_high : close < media_high ? media_high : na, color=close > media_high ? color.green : close < media_high ? color.gray :na, linewidth=1, title='media_high', style=plot.style_cross)
- media_low = ta.sma(wpl, input_media_W_low)
- plot(close < media_low ? media_low : close > media_low ? media_low : na, color=close < media_low ? color.red : close > media_low ? color.gray :na, linewidth=1, title='media_low', style=plot.style_cross)
- media_high_smoothed = ta.sma(wph, input_media_W_high_smoothed)
- plot(media_high_smoothed, color=color.new(color.green, 0), linewidth=1, title='media_high_smoothed')
- media_low_smoothed = ta.sma(wpl, input_media_W_low_smoothed)
- plot(media_low_smoothed, color=color.new(color.red, 0), linewidth=1, title='media_low_smoothed')
- // >>>>>>>>>>>>>> End Detecting Variables <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Detecting Atr Long E Short <<<<<<<<<<<<<<<<<
- // Per il calcolo dell'indicatore è stato richiamato l'indicatore originale e l'indicatore di 1na barra fa per non avere repainting usando l'operatore ternario
- valore_atr_long_origin = ta.atr(in_atr_period_long)
- valore_atr_long1 = valore_atr_long_origin[1]
- valore_atr_long = barstate.isconfirmed ? valore_atr_long_origin : valore_atr_long1 // NO REPAINTING
- atr_per_mult_long = valore_atr_long * in_atr_mult_long
- plot(atr_per_mult_long, title='atr_mult_long')
- valore_atr_short_origin = ta.atr(in_atr_period_short)
- valore_atr_short1 = valore_atr_short_origin[1]
- valore_atr_short = barstate.isconfirmed ? valore_atr_short_origin : valore_atr_short1 // NO REPAINTING
- atr_per_mult_short = valore_atr_short * in_atr_mult_short
- plot(atr_per_mult_short, title='atr_mult_short')
- // >>>>>>>>>>>>>> End Detecting Atr Long E Short <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Detecting DMI <<<<<<<<<<<<<<<<<
- // Filtro voglio che sia true se il +DI è sopra sia il -DI ed anche dell'ADX //21,3587
- // Per il calcolo dell'indicatore è stato richiamato l'indicatore originale e l'indicatore di 1na barra fa per non avere repainting usando l'operatore ternario
- //[di_pos, di_neg, adx] = ta.dmi(14, 14)
- [di_pos_origin, di_neg_origin, adx] = ta.dmi(14, 14)
- di_pos1 = di_pos_origin[1]
- di_neg1 = di_neg_origin[1]
- di_pos = barstate.isconfirmed ? di_pos_origin : di_pos1
- di_neg = barstate.isconfirmed ? di_neg_origin : di_neg1
- //dmi_total_green_cross = di_pos > di_neg and di_pos > adx? true : false
- //dmi_total_green_cross = (di_pos - di_neg) > dma_min_diff_input ? true : false
- //dmi_total_green_cross = (di_neg - di_pos) > dmi_min_diff_input ? true : false
- //dmi_total_green_cross_short = (di_neg > adx + dmi_min_diff_input_short) ? true : false
- //dmi_total_green_cross_long = (di_pos > adx + dmi_min_diff_input_long) ? true : false
- //dmi_total_green_cross = (di_pos - di_neg) < dmi_min_diff_input ? true : false
- //plot(dmi_total_green_cross ? 1 : 0, title="dmi_total_green_cross")
- // >>>>>>>>>>>>>> End Detecting DMI <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Plot Ingresso In Posizione <<<<<<<<<<<<<<<<<
- plot(strategy.position_size != 0 ? strategy.opentrades.entry_price(0) : 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
- 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
- // >>>>>>>>>>>>>> End Plot Ingresso In Posizione <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Detecting No Trading Allowed <<<<<<<<<<<<<<<<<
- // Plot No Trading Allowed giorni da 1 a 7 1 è domenica. Mesi da 1 a 12 1 è Gennaio.
- //giorni_esclusi = dayofweek(time)
- //plotshape(giorni_esclusi[1] == 2 ? giorni_esclusi : na, color=color.green, title="giorni_esclusi")
- //mesi_esclusi = month(time)
- //plotshape(mesi_esclusi[1] == 9 ? mesi_esclusi : na, color=color.yellow, title="mesi_esclusi")
- // >>>>>>>>>>>>>> Start Detecting No Trading Allowed <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Entry/Exit Conditions <<<<<<<<<<<<<<<<<
- // Condizione Entry Long: Crossover chiusura media e media maggiore altra media
- condEntryLong = ta.crossover(close, media_high) and math.abs(open - close) > valore_atr_long * in_atr_mult_long and close > open and di_pos > dmi_min_diff_input_long and not onlyShort and inDateRange
- // Condizione Exit Long: Crossunder chiusura media
- condExitLong = ta.crossunder(close, media_high)
- // Condizione Entry Short: Crossounder chiusura media e media minore altra media
- condEntryShort = ta.crossunder(close, media_low) and math.abs(open - close) > valore_atr_short * in_atr_mult_short and close < open and di_neg > dmi_min_diff_input_short and not onlyLong and inDateRange
- // Condizione Exit Short: Crossover chiusura media
- condExitShort = ta.crossover(close, media_low)
- // >>>>>>>>>>>>>> End Entry/Exit Conditions <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Buy/Sell/Close Command <<<<<<<<<<<<<<<<<
- buy_command = 'buy_command '
- sell_command = 'sell_command '
- close_command = 'close_command '
- // cancel_command = 'cancel_command'
- // >>>>>>>>>>>>>> End Buy/Sell/Close Command <<<<<<<<<<<<<<<<<
- // >>>>>>>>>>>>>> Start Execute Long And Short Open/Close Positions <<<<<<<<<<<<<<<<<
- if condEntryLong
- strategy.entry('long', strategy.long, alert_message = "Open Long Position", comment = buy_command)
- if condExitLong
- strategy.close(id='long', alert_message = "Close Long Position", comment = close_command)
- if condEntryShort
- strategy.entry('short', strategy.short, alert_message = "Open Short position", comment = sell_command)
- if condExitShort
- strategy.close(id='short', alert_message = "Close Short Position", comment = close_command)
- // >>>>>>>>>>>>>> End Execute Long And Short Open/Close Positions <<<<<<<<<<<<<<<<<
- // Nome Alert: Weekly-Mean-Trend ETH/USDT.P BYBIT 4H
- // Commento Alert: {{strategy.order.comment}}
- // ----------------- Inizio Tabella risultati mensili. 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)
- // ----------------- Fine Tabella risultati mensili. Per visualizzare andare nelle impostazioni proprietà e spuntare ad ogni tick -----------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement