Maurizio-Ciullo

bb bands con aggiunta atr allo stop loss

Jul 6th, 2021 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //bb bands con aggiunta atr allo stop loss
  2.  
  3. //1. Impostare la strategia con parametri strategy(........)
  4. //@version=4
  5. strategy(title="Strategia con Bollinger Bands",
  6.      overlay=true,
  7.      pyramiding=0,
  8.      initial_capital=10000,
  9.      default_qty_value=1,
  10.      commission_type=strategy.commission.cash_per_order,
  11.      commission_value=1,
  12.      slippage=2)
  13.  
  14. var stopLong = 0.0
  15. var stopShort = 0.0
  16.  
  17. //2. Parametri di input della strategia (vedi funzione input(.......))
  18. sorgente = input(title="Source", type=input.source, defval=close)
  19. sma = input(title="Media mobile Semplice", type=input.integer, minval=5, maxval=200, defval=20)
  20. dev = input(title="Deviazione Standard", type=input.float, minval=0.1, maxval= 20, defval=2.0)
  21.  
  22. //3. Calcolo e disegno degli indicatori / variabili
  23. BB_sma = sma(sorgente, sma)
  24. BB_offset = offset(sorgente, 1)
  25. BB_deviazione = dev * stdev(sorgente, sma)
  26.  
  27. bb_bands(media_mobile, deviazione_standard) =>
  28.     banda_sup = media_mobile + deviazione_standard
  29.     banda_inf = media_mobile - deviazione_standard
  30.     [banda_sup,banda_inf]
  31.  
  32. [BB_superiore, BB_inferiore] = bb_bands(BB_sma, BB_deviazione)
  33.  
  34. plotBB_sma = plot(BB_sma, color = color.green)
  35. plotBB_superiore = plot(BB_superiore, color = color.green)
  36. plotBB_inferiore = plot(BB_inferiore, color = color.green)
  37. fill(plotBB_inferiore, plotBB_superiore, color = color.green, transp = 80)
  38.  
  39. //3.1 CALCOLO ATR
  40. length = input(title="Periodo ATR", defval=14, minval=1)
  41. atrValue =rma(tr(true), length)
  42.  
  43. //3.2 DEFINISCO E DISEGNO I LIVELLI DI STOPlOSS PER OPERAZIONI LONG E SHORT
  44. if (strategy.position_size==0)
  45.     stopLong:=0
  46.     stopShort:=0
  47.    
  48. if (strategy.position_size>0 and stopLong==0)
  49.     stopLong:=BB_inferiore-atrValue
  50.  
  51. if (strategy.position_size<0 and stopShort==0)
  52.     stopShort:=BB_superiore+atrValue+500
  53.  
  54. plot(stopLong,color = color.yellow, title='stoplong')
  55. plot(stopShort, color = color.yellow, title='stopshort')
  56. plot(strategy.position_size, color=color.red)
  57.  
  58. //4. Determinazione condizioni di entrata Long
  59. cond_long = crossover(close, BB_inferiore)
  60. strategy.entry("operazioneLong", true, when = cond_long)
  61.  
  62. //5. Determinazione condizioni di entrata Short
  63. cond_short= crossunder(close, BB_superiore)
  64. strategy.entry("operazioneShort", false, when = cond_short)
  65.  
  66. //6. Determinazione condizioni chiusura delle operazioni Long
  67. cond_exitlong= crossover(close, BB_sma)
  68. strategy.close("operazioneLong", when = cond_exitlong)
  69. if(stopLong > 0)
  70.     strategy.exit(id='chiusuraOperazioneLong',from_entry="operazioneLong", stop = stopLong)
  71.  
  72. //7. Determinazione condizioni chiusura delle operazioni Short
  73. cond_exitshort=crossunder(close, BB_sma)
  74. strategy.close("operazioneShort", when = cond_exitshort)
  75. if(stopShort > 0)
  76.     strategy.exit(id='chiusuraOperazioneShort',from_entry="operazioneShort",stop = stopShort)
  77.  
Add Comment
Please, Sign In to add comment