Advertisement
Maurizio-Ciullo

STRATEGIA BOLLINGER SU RIPPLE 15 MINUTI DA SISTEMARE

Nov 1st, 2021
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © NinjaZX10R,Patrizio,Andrea
  3. //STRATEGIA BOLLINGER SU RIPPLE 15 MINUTI
  4. //SHORT DISABILITATO PERCHè NON FUNZIONANTE
  5.  
  6. // DA SISTEMARE
  7.  
  8. //@version=4
  9. strategy("Ripple Boing ", overlay=true,initial_capital=1000,
  10.      commission_type=strategy.commission.percent,
  11.      commission_value=0.075,
  12.      precision=5,
  13.      default_qty_type= strategy.percent_of_equity,
  14.      default_qty_value=100,
  15.      calc_on_every_tick = true)
  16.    
  17. //entrare long dopo uscita inferiore bollinger con almeno 5 barre consecutive rosse
  18.  
  19. // bollinger
  20. source_bb=input(close,type=input.source,title="source",group="bollinger")
  21. lenght_bb=input(20,type=input.integer,title="lenght",group="bollinger")
  22. offset_bb=input(2,type=input.integer,title="offset",group="bollinger")
  23. bb(source_bb,lenght_bb,2)
  24. [middle, upper, lower] = bb(source_bb,lenght_bb,2)
  25.  
  26. //Backtest
  27. fromYear  = input(defval = 2020, title = "Backtesting - Start Year", group = "Backtest")      
  28. fromMonth = input(defval = 1,  title = "Backtesting - Star Month",group = "Backtest")    
  29. fromDay   = input(defval = 1,    title = "Backtesting - Star Day",group = "Backtest")  
  30. thruYear  = input(defval = 2112, title = "Backtesting - End Year",group = "Backtest")      
  31. thruMonth = input(defval = 1,    title = "Backtesting - End Month",group = "Backtest")    
  32. thruDay   = input(defval = 1,    title = "Backtesting - End Day",group = "Backtest")    
  33.    
  34. start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
  35. finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
  36. window()  => time >= start and time <= finish ? true : false       // create function "within window of time"
  37.  
  38. //in caso di operazione persa alla barra subito prima non entrare long per evitare di cavalcare una discesa
  39. loss_trades = float(strategy.losstrades[0])
  40. loss_condition = loss_trades > float(strategy.losstrades[1])
  41.  
  42.  
  43. //input per short e long
  44. Short_SI=input(true,type=input.bool,title="Abilita Short")
  45. Long_SI=input(true,type=input.bool,title="Abilita Long")
  46.  
  47. //Condizione che non trada se precedentemente ho avuto x perdite di fila
  48.  
  49. perditeMaxConsecutive = input(title="perdite consecutive", type=input.integer,
  50.      defval=2, minval=1)
  51.      
  52. // //definizione nuova perdita
  53.  
  54. nuova_perdita = (strategy.losstrades > strategy.losstrades[1]) and
  55.      (strategy.wintrades == strategy.wintrades[1]) and
  56.      (strategy.eventrades == strategy.eventrades[1])
  57. //contatore perdite consecutive
  58.  
  59. streakLen = 0
  60.  
  61. streakLen := if (nuova_perdita)
  62.     nz(streakLen[1]) + 1
  63. else
  64.     if (strategy.wintrades > strategy.wintrades[1]) or
  65.          (strategy.eventrades > strategy.eventrades[1])
  66.         0
  67.     else
  68.         nz(streakLen[1])
  69. //creazione condizione per tradare senza serie negative
  70.  
  71. okToTrade = streakLen < perditeMaxConsecutive
  72.  
  73. ///////////////////////////////////////////////
  74.  
  75. //condizione LONG
  76. barre_minime=input(5,title='numero barre minime',type=input.integer,group = "Settaggi LONG")
  77. barra_verde= close > open
  78. cond_long=close < lower and (barssince(barra_verde) > barre_minime) and window() and Long_SI //and okToTrade
  79.  
  80. //calcolo del  max relativo LONG
  81. indice_long = barssince(barra_verde)
  82. numero_sottrazione_long=input(1,type=input.integer,title="quale massimo relativo prendo,1 ultimo 2 penultimo ?",maxval=2,minval=1,group = "Settaggi LONG")
  83. indice_con_sottrazione_long = (indice_long - numero_sottrazione_long)
  84.  
  85. ///////////////////////////////////////////////
  86.  
  87. //condizione SHORT
  88. barre_minime_short=input(5,title='numero barre minime',type=input.integer,group = "Settaggi SHORT")
  89. barra_rossa=close< open
  90. cond_short=close > upper and (barssince(barra_rossa) > barre_minime_short) and window() and Short_SI
  91.  
  92. //calcolo del  min relativo SHORT
  93. indice_short = barssince(barra_rossa)
  94. numero_sottrazione_short=input(1,type=input.integer,title="quale minimo relativo prendo,1 ultimo 2 penultimo ?",maxval=2,minval=1,group = "Settaggi SHORT")
  95. indice_con_sottrazione_short = (indice_short - numero_sottrazione_short)
  96.  
  97. //////////////////////////////////////////////
  98.  
  99. //variabili
  100. var massimo_relativo_from_indice= 0.0
  101. var minimo_relativo_from_indice= 0.0
  102. var entry_price= 0.0
  103.  
  104. //definizione entry price e max/min relativo
  105. if strategy.position_size == 0 and cond_long  
  106.     entry_price := close
  107.     massimo_relativo_from_indice := high[indice_con_sottrazione_long]
  108. else if strategy.position_size == 0 and cond_short
  109.     entry_price := close
  110.     minimo_relativo_from_indice :=low[indice_con_sottrazione_short]
  111.    
  112.  
  113.    
  114. //!!sl e tp long e short avranno offset diversi!!//
  115.  
  116. //stop loss take profit iniziali LONG
  117.  
  118. molt_tp_long=input(1.5,type=input.float,group="offset tp/sl LONG iniziale",step=0.1)
  119. molt_sl_long=input(1.5,type=input.float,group="offset tp/sl LONG iniziale",step=0.1)
  120. escursione=  (massimo_relativo_from_indice - entry_price)
  121. take_profit = entry_price + (escursione * molt_tp_long)
  122. stop_loss = entry_price - (escursione * molt_sl_long)
  123.  
  124. //definizione escursione dopo il crossover max relativo  e calcolo tp e sl iniziali
  125. var distanza_post_crossover= 0.0
  126. molt_tp_long1=input(2,type=input.integer,group="Trailling Stop LONG",step=0.1)
  127. 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
  128.  
  129. //stop loss take profit iniziali SHORT
  130. molt_tp_short=input(1.5,type=input.float,group="offset tp/sl SHORT iniziale",step=0.1)
  131. molt_sl_short=input(1.5,type=input.float,group="offset tp/sl SHORT iniziale",step=0.1)
  132. escursione_short=  entry_price - minimo_relativo_from_indice
  133. take_profit_short =entry_price - (escursione_short * molt_tp_short)//il molt deve essere >= a 1 perchè sennò e minore dell'escursione stessa
  134. stop_loss_short = entry_price + (escursione_short * molt_sl_short)  
  135.  
  136. // //definizione escursione ,dopo il crossunder min relativo  e calcolo tp e sl iniziali
  137. // var distanza_post_crossover_short= 0.0
  138. // molt_tp_short1=input(2,type=input.integer,group="Trailling Stop SHORT",step=0.1)
  139. // 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
  140.  
  141. //variabili update stop loss e take profit
  142. var update_sl = 0.0
  143. var update_tp = 0.0
  144.  
  145. //Non calcolare update_sl e update_tp se non si è in posizione
  146. if strategy.position_size == 0 and update_tp != 0 and update_sl != 0
  147.     update_tp := 0.0
  148.     update_sl := 0.0
  149.  
  150. //update sl e tp LONG
  151. if strategy.position_size > 0 and update_tp > take_profit
  152.     take_profit := update_tp
  153. if  strategy.position_size > 0 and update_sl > stop_loss
  154.     stop_loss := update_sl
  155.  
  156.  
  157. // //update sl e tp SHORT    
  158. // if strategy.position_size < 0  and update_tp != 0
  159. //     take_profit_short := update_tp
  160. // if strategy.position_size < 0 and update_sl != 0
  161. //     stop_loss_short := update_sl
  162.  
  163.    
  164. //entrate  
  165. if cond_long
  166.     strategy.entry("Long",true,when=cond_long)
  167.     strategy.exit("Exit","Long",stop=stop_loss,limit=take_profit)
  168. else if strategy.position_size > 0 and crossover(close,massimo_relativo_from_indice)
  169.     distanza_post_crossover := massimo_relativo_from_indice - low
  170.     update_sl := low - (distanza_post_crossover*molt_sl_long1)
  171.     update_tp := take_profit + (distanza_post_crossover * molt_tp_long1)
  172.     strategy.cancel_all()
  173.     strategy.exit("Trailling Stop",limit=update_tp,stop=update_sl)
  174.    
  175. if strategy.position_size > 0 and crossover(close,massimo_relativo_from_indice)
  176.     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)
  177.  
  178. // if cond_short
  179. //     strategy.entry("Short",false,when=cond_short)
  180. //     strategy.exit("Exit","Short",stop=stop_loss_short,limit=take_profit_short)
  181. // else if strategy.position_size < 0 and crossunder(close,minimo_relativo_from_indice)  
  182. //     distanza_post_crossover_short := high - minimo_relativo_from_indice  
  183. //     update_sl := minimo_relativo_from_indice
  184. //     update_tp := take_profit_short - (distanza_post_crossover_short * molt_tp_short1)
  185. //     strategy.cancel_all()
  186. //     strategy.exit("Trailling Stop",limit=update_tp,stop=update_sl)
  187. //     // 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)
  188.  
  189.  
  190.    
  191. plot(entry_price,title="Entry price",color=strategy.position_size > 0 ? color.yellow : na )
  192. plot(stop_loss,title="Stop loss",color=strategy.position_size > 0 ?  color.red :na )
  193. plot(take_profit,title='take profit',color=strategy.position_size > 0 ? color.green : na )
  194. plot(stop_loss_short,title="Stop loss short",color=strategy.position_size < 0 ?  color.red :na )
  195. plot(take_profit_short,title='take profit short',color=strategy.position_size < 0 ? color.green : na )
  196. plot(massimo_relativo_from_indice,color=strategy.position_size > 0 ? color.teal : na,title="massimo relativo")    
  197. plot(minimo_relativo_from_indice,color=strategy.position_size < 0  ? color.teal : na,title="minimo relativo")
  198. // plot(update_tp,title="update_tp")
  199. // plot(update_sl,title="update_sl")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement