Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //bb bands con aggiunta atr allo stop loss
- //1. Impostare la strategia con parametri strategy(........)
- //@version=4
- strategy(title="Strategia con Bollinger Bands",
- overlay=true,
- pyramiding=0,
- initial_capital=10000,
- default_qty_value=1,
- commission_type=strategy.commission.cash_per_order,
- commission_value=1,
- slippage=2)
- var stopLong = 0.0
- var stopShort = 0.0
- //2. Parametri di input della strategia (vedi funzione input(.......))
- sorgente = input(title="Source", type=input.source, defval=close)
- sma = input(title="Media mobile Semplice", type=input.integer, minval=5, maxval=200, defval=20)
- dev = input(title="Deviazione Standard", type=input.float, minval=0.1, maxval= 20, defval=2.0)
- //3. Calcolo e disegno degli indicatori / variabili
- BB_sma = sma(sorgente, sma)
- BB_offset = offset(sorgente, 1)
- BB_deviazione = dev * stdev(sorgente, sma)
- bb_bands(media_mobile, deviazione_standard) =>
- banda_sup = media_mobile + deviazione_standard
- banda_inf = media_mobile - deviazione_standard
- [banda_sup,banda_inf]
- [BB_superiore, BB_inferiore] = bb_bands(BB_sma, BB_deviazione)
- plotBB_sma = plot(BB_sma, color = color.green)
- plotBB_superiore = plot(BB_superiore, color = color.green)
- plotBB_inferiore = plot(BB_inferiore, color = color.green)
- fill(plotBB_inferiore, plotBB_superiore, color = color.green, transp = 80)
- //3.1 CALCOLO ATR
- length = input(title="Periodo ATR", defval=14, minval=1)
- atrValue =rma(tr(true), length)
- //3.2 DEFINISCO E DISEGNO I LIVELLI DI STOPlOSS PER OPERAZIONI LONG E SHORT
- if (strategy.position_size==0)
- stopLong:=0
- stopShort:=0
- if (strategy.position_size>0 and stopLong==0)
- stopLong:=BB_inferiore-atrValue
- if (strategy.position_size<0 and stopShort==0)
- stopShort:=BB_superiore+atrValue+500
- plot(stopLong,color = color.yellow, title='stoplong')
- plot(stopShort, color = color.yellow, title='stopshort')
- plot(strategy.position_size, color=color.red)
- //4. Determinazione condizioni di entrata Long
- cond_long = crossover(close, BB_inferiore)
- strategy.entry("operazioneLong", true, when = cond_long)
- //5. Determinazione condizioni di entrata Short
- cond_short= crossunder(close, BB_superiore)
- strategy.entry("operazioneShort", false, when = cond_short)
- //6. Determinazione condizioni chiusura delle operazioni Long
- cond_exitlong= crossover(close, BB_sma)
- strategy.close("operazioneLong", when = cond_exitlong)
- if(stopLong > 0)
- strategy.exit(id='chiusuraOperazioneLong',from_entry="operazioneLong", stop = stopLong)
- //7. Determinazione condizioni chiusura delle operazioni Short
- cond_exitshort=crossunder(close, BB_sma)
- strategy.close("operazioneShort", when = cond_exitshort)
- if(stopShort > 0)
- strategy.exit(id='chiusuraOperazioneShort',from_entry="operazioneShort",stop = stopShort)
Add Comment
Please, Sign In to add comment