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/
- // © NinjaZX10R,Patrizio,Andrea
- //STRATEGIA BOLLINGER SU RIPPLE 15 MINUTI
- //SHORT DISABILITATO PERCHè NON FUNZIONANTE
- // DA SISTEMARE
- //@version=4
- strategy("Ripple Boing ", overlay=true,initial_capital=1000,
- commission_type=strategy.commission.percent,
- commission_value=0.075,
- precision=5,
- default_qty_type= strategy.percent_of_equity,
- default_qty_value=100,
- calc_on_every_tick = true)
- //entrare long dopo uscita inferiore bollinger con almeno 5 barre consecutive rosse
- // bollinger
- source_bb=input(close,type=input.source,title="source",group="bollinger")
- lenght_bb=input(20,type=input.integer,title="lenght",group="bollinger")
- offset_bb=input(2,type=input.integer,title="offset",group="bollinger")
- bb(source_bb,lenght_bb,2)
- [middle, upper, lower] = bb(source_bb,lenght_bb,2)
- //Backtest
- fromYear = input(defval = 2020, title = "Backtesting - Start Year", group = "Backtest")
- fromMonth = input(defval = 1, title = "Backtesting - Star Month",group = "Backtest")
- fromDay = input(defval = 1, title = "Backtesting - Star Day",group = "Backtest")
- thruYear = input(defval = 2112, title = "Backtesting - End Year",group = "Backtest")
- thruMonth = input(defval = 1, title = "Backtesting - End Month",group = "Backtest")
- thruDay = input(defval = 1, title = "Backtesting - End Day",group = "Backtest")
- start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start window
- finish = timestamp(thruYear, thruMonth, thruDay, 23, 59) // backtest finish window
- window() => time >= start and time <= finish ? true : false // create function "within window of time"
- //in caso di operazione persa alla barra subito prima non entrare long per evitare di cavalcare una discesa
- loss_trades = float(strategy.losstrades[0])
- loss_condition = loss_trades > float(strategy.losstrades[1])
- //input per short e long
- Short_SI=input(true,type=input.bool,title="Abilita Short")
- Long_SI=input(true,type=input.bool,title="Abilita Long")
- //Condizione che non trada se precedentemente ho avuto x perdite di fila
- perditeMaxConsecutive = input(title="perdite consecutive", type=input.integer,
- defval=2, minval=1)
- // //definizione nuova perdita
- nuova_perdita = (strategy.losstrades > strategy.losstrades[1]) and
- (strategy.wintrades == strategy.wintrades[1]) and
- (strategy.eventrades == strategy.eventrades[1])
- //contatore perdite consecutive
- streakLen = 0
- streakLen := if (nuova_perdita)
- nz(streakLen[1]) + 1
- else
- if (strategy.wintrades > strategy.wintrades[1]) or
- (strategy.eventrades > strategy.eventrades[1])
- 0
- else
- nz(streakLen[1])
- //creazione condizione per tradare senza serie negative
- okToTrade = streakLen < perditeMaxConsecutive
- ///////////////////////////////////////////////
- //condizione LONG
- barre_minime=input(5,title='numero barre minime',type=input.integer,group = "Settaggi LONG")
- barra_verde= close > open
- cond_long=close < lower and (barssince(barra_verde) > barre_minime) and window() and Long_SI //and okToTrade
- //calcolo del max relativo LONG
- indice_long = barssince(barra_verde)
- numero_sottrazione_long=input(1,type=input.integer,title="quale massimo relativo prendo,1 ultimo 2 penultimo ?",maxval=2,minval=1,group = "Settaggi LONG")
- indice_con_sottrazione_long = (indice_long - numero_sottrazione_long)
- ///////////////////////////////////////////////
- //condizione SHORT
- barre_minime_short=input(5,title='numero barre minime',type=input.integer,group = "Settaggi SHORT")
- barra_rossa=close< open
- cond_short=close > upper and (barssince(barra_rossa) > barre_minime_short) and window() and Short_SI
- //calcolo del min relativo SHORT
- indice_short = barssince(barra_rossa)
- numero_sottrazione_short=input(1,type=input.integer,title="quale minimo relativo prendo,1 ultimo 2 penultimo ?",maxval=2,minval=1,group = "Settaggi SHORT")
- indice_con_sottrazione_short = (indice_short - numero_sottrazione_short)
- //////////////////////////////////////////////
- //variabili
- var massimo_relativo_from_indice= 0.0
- var minimo_relativo_from_indice= 0.0
- var entry_price= 0.0
- //definizione entry price e max/min relativo
- if strategy.position_size == 0 and cond_long
- entry_price := close
- massimo_relativo_from_indice := high[indice_con_sottrazione_long]
- else if strategy.position_size == 0 and cond_short
- entry_price := close
- minimo_relativo_from_indice :=low[indice_con_sottrazione_short]
- //!!sl e tp long e short avranno offset diversi!!//
- //stop loss take profit iniziali LONG
- molt_tp_long=input(1.5,type=input.float,group="offset tp/sl LONG iniziale",step=0.1)
- molt_sl_long=input(1.5,type=input.float,group="offset tp/sl LONG iniziale",step=0.1)
- escursione= (massimo_relativo_from_indice - entry_price)
- take_profit = entry_price + (escursione * molt_tp_long)
- stop_loss = entry_price - (escursione * molt_sl_long)
- //definizione escursione dopo il crossover max relativo e calcolo tp e sl iniziali
- var distanza_post_crossover= 0.0
- molt_tp_long1=input(2,type=input.integer,group="Trailling Stop LONG",step=0.1)
- molt_sl_long1=input(1,type=input.integer,group="Trailling Stop LONG",step=0.1)//non utilizzato perchè dopo crossover ho deciso di andare a breakeven
- //stop loss take profit iniziali SHORT
- molt_tp_short=input(1.5,type=input.float,group="offset tp/sl SHORT iniziale",step=0.1)
- molt_sl_short=input(1.5,type=input.float,group="offset tp/sl SHORT iniziale",step=0.1)
- escursione_short= entry_price - minimo_relativo_from_indice
- take_profit_short =entry_price - (escursione_short * molt_tp_short)//il molt deve essere >= a 1 perchè sennò e minore dell'escursione stessa
- stop_loss_short = entry_price + (escursione_short * molt_sl_short)
- // //definizione escursione ,dopo il crossunder min relativo e calcolo tp e sl iniziali
- // var distanza_post_crossover_short= 0.0
- // molt_tp_short1=input(2,type=input.integer,group="Trailling Stop SHORT",step=0.1)
- // molt_sl_short1=input(1,type=input.integer,group="Trailling Stop SHORT",step=0.1)//non utilizzato perchè dopo crossunder ho deciso di andare a breakeven
- //variabili update stop loss e take profit
- var update_sl = 0.0
- var update_tp = 0.0
- //Non calcolare update_sl e update_tp se non si è in posizione
- if strategy.position_size == 0 and update_tp != 0 and update_sl != 0
- update_tp := 0.0
- update_sl := 0.0
- //update sl e tp LONG
- if strategy.position_size > 0 and update_tp > take_profit
- take_profit := update_tp
- if strategy.position_size > 0 and update_sl > stop_loss
- stop_loss := update_sl
- // //update sl e tp SHORT
- // if strategy.position_size < 0 and update_tp != 0
- // take_profit_short := update_tp
- // if strategy.position_size < 0 and update_sl != 0
- // stop_loss_short := update_sl
- //entrate
- if cond_long
- strategy.entry("Long",true,when=cond_long)
- strategy.exit("Exit","Long",stop=stop_loss,limit=take_profit)
- else if strategy.position_size > 0 and crossover(close,massimo_relativo_from_indice)
- distanza_post_crossover := massimo_relativo_from_indice - low
- update_sl := low - (distanza_post_crossover*molt_sl_long1)
- update_tp := take_profit + (distanza_post_crossover * molt_tp_long1)
- strategy.cancel_all()
- strategy.exit("Trailling Stop",limit=update_tp,stop=update_sl)
- if strategy.position_size > 0 and crossover(close,massimo_relativo_from_indice)
- label.new(bar_index,high,yloc=yloc.abovebar,text="Ccrossover Max \n" + tostring(low) +"\n" + tostring(take_profit),textcolor=color.yellow,color=color.red,style= label.style_label_center,size=size.normal)
- // if cond_short
- // strategy.entry("Short",false,when=cond_short)
- // strategy.exit("Exit","Short",stop=stop_loss_short,limit=take_profit_short)
- // else if strategy.position_size < 0 and crossunder(close,minimo_relativo_from_indice)
- // distanza_post_crossover_short := high - minimo_relativo_from_indice
- // update_sl := minimo_relativo_from_indice
- // update_tp := take_profit_short - (distanza_post_crossover_short * molt_tp_short1)
- // strategy.cancel_all()
- // strategy.exit("Trailling Stop",limit=update_tp,stop=update_sl)
- // // label.new(bar_index,y=na,yloc=yloc.belowbar,text="Update stop loss/take profit: \n" + tostring(low) +"\n" + tostring(take_profit),textcolor=color.white,color=color.white,style= label.style_none,size=size.small)
- plot(entry_price,title="Entry price",color=strategy.position_size > 0 ? color.yellow : na )
- plot(stop_loss,title="Stop loss",color=strategy.position_size > 0 ? color.red :na )
- plot(take_profit,title='take profit',color=strategy.position_size > 0 ? color.green : na )
- plot(stop_loss_short,title="Stop loss short",color=strategy.position_size < 0 ? color.red :na )
- plot(take_profit_short,title='take profit short',color=strategy.position_size < 0 ? color.green : na )
- plot(massimo_relativo_from_indice,color=strategy.position_size > 0 ? color.teal : na,title="massimo relativo")
- plot(minimo_relativo_from_indice,color=strategy.position_size < 0 ? color.teal : na,title="minimo relativo")
- // plot(update_tp,title="update_tp")
- // plot(update_sl,title="update_sl")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement