Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.76 KB | None | 0 0
  1. //@version=3
  2.  
  3. //
  4. //▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ VOLATTITLITY PRICE ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  5.  
  6.  
  7. sTitle = "Multi-Timeframe Volatility Bands"
  8. sShort = "Volatility Bands"
  9. sRelease = "2018-06-22"
  10. sOverlay = true
  11. sScale = scale.right
  12. sPrecision = 0
  13. sInitial_capital = 10000
  14. sCommission_value = 0.05
  15. sPyramiding = 1
  16. sDefault_qty_type = strategy.percent_of_equity
  17. sDefault_qty_value = 100
  18. sCalc_on_order_fills = true
  19. sCalc_on_every_tick = true
  20. strategy(title=sTitle+" [v"+sRelease+"]", shorttitle=sShort, overlay=sOverlay, scale=sScale, precision=sPrecision,initial_capital=sInitial_capital, commission_value=sCommission_value, pyramiding=sPyramiding,calc_on_every_tick=sCalc_on_every_tick, default_qty_value=sDefault_qty_value, calc_on_order_fills=sCalc_on_order_fills)
  21.  
  22.  
  23.  
  24. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  25. // ♣DEFAULT SETTINGS♣
  26. //
  27. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  28.  
  29. defLongLen = 390
  30. defLongTF = 0
  31. defShortLen = 70
  32. defShortTF = 0
  33. //INPUTS
  34.  
  35.  
  36.  
  37. defLongDev = "price"
  38. defLongDev1 = 1.28
  39. defLongDev2 = 2.01
  40. defLongDev3 = 2.51
  41. defLongDev4 = 3.09
  42. defLongDev5 = 4.01
  43. defLongFine = 1
  44.  
  45. defShortDev = "price"
  46. defShortDev1 = 0
  47. defShortDev2 = 0
  48. defShortDev3 = 0
  49. defShortDev4 = 0
  50. defShortDev5 = 0
  51. defShortFine = 1
  52.  
  53. defShowLongBands = "[4] Standard to Bronze"
  54. defShowShortBands = "[4] Standard to Bronze"
  55.  
  56. defThemeLongShade = "Dark"
  57. defThemeColorAboveLong = "10 Cyan"
  58. defThemeColorBelowLong = "08 Green"
  59. defThemeLongCenter = "Light Grey"
  60.  
  61. defThemeShortShade = "Dark"
  62. defThemeColorAboveShort = "10 Cyan"
  63. defThemeColorBelowShort = "08 Green"
  64. defThemeShortCenter = "Light Grey"
  65.  
  66. //
  67. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  68. // ♣FUNCTION INPUTS♣
  69. //
  70. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  71.  
  72. // Source for price and volume
  73. source = close
  74.  
  75. //________________________________
  76. // LONG TIMEFRAME SETTINGS
  77. enableLong = input(true, title="-------- LONG VOLATILITY BANDS -------- [uncheck to disable]")
  78.  
  79. // VWAP Length (average over N bars back)
  80. longLength = input( title="Length (bars back)", type=integer, defval=defLongLen, minval=1 )
  81.  
  82. // Long resolution
  83. longCustomRes = input(title="Timeframe (minutes) [0 to use current chart's]", type=integer, defval=defLongTF, minval=0, step=100)
  84. longRes = longCustomRes==0 ? period : tostring(longCustomRes)
  85.  
  86.  
  87. //
  88. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  89. // ♣LONG DEVIATION INPUT SETTINGS♣
  90. //
  91. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  92.  
  93. showLongBands = input(title="How many deviation bands to display" , type=string, defval=defShowLongBands,
  94. options=["VWAP only", "[1] Standard Deviation", "[2] Standard & Golden", "[3] Stardard to Silver", "[4] Standard to Bronze", "[5] All"])
  95.  
  96. // Long Standard Deviations Parameters (default: Metallic Means)
  97. longDev1 = input(title='[1] "Standard" Deviation (default: 1 SD)', defval=defLongDev1) // Zero ratio (Fib ratio n=0)
  98. longDev2 = input(title='[2] "Golden" Deviation (1.618 SD)' , defval=defLongDev2) // Golden ratio (Fib ratio n=1)
  99. longDev3 = input(title='[3] "Silver" Deviation (2.414 SD)' , defval=defLongDev3) // Silver ratio (Fib ratio n=2)
  100. longDev4 = input(title='[4] "Bronze" Deviation (3.303 SD)' , defval=defLongDev4) // Bronze ratio (Fib ratio n=3)
  101. longDev5 = input(title='[5] "Copper" Deviation (4.236 SD)' , defval=defLongDev5) // Copper ratio (Fib ratio n=4)
  102. // Fine-tune volume weight and deviations
  103. longFinetune = input(title="[all] × fine-tuning coefficient", type=float, step=0.01, minval=0.001, defval=defLongFine)
  104.  
  105. //
  106. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  107. // ♣LONG TIMEFRAME INPUTS♣
  108. //
  109. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  110. themeLongShade = input(title="Color shade", type=string, defval=defThemeLongShade,
  111. options=["Dark", "Light"])
  112.  
  113. // Long: Color selection for each side
  114. themeColorAboveLong = input(title="Color above VWAP", type=string, defval=defThemeColorAboveLong,
  115. options=["01 Red","08 Green", "10 Cyan","02 Deep Orange","12 Blue", "16 Blue Grey", "00 Grey"])
  116. themeColorBelowLong = input(title="Color below VWAP", type=string, defval=defThemeColorBelowLong,
  117. options=["01 Red", "08 Green","02 Deep Orange","10 Cyan", "12 Blue", "16 Blue Grey", "00 Grey"])
  118.  
  119. // Long alternating fill selector (bullish/bearish)
  120. longFillAlt = input(false, title="Bullish/Bearish alternating fill")
  121.  
  122. // Center and "opposite" bands (other side of the mean)
  123. themeLongCenter = input(title="Center & opposite bands", type=string, defval=defThemeLongCenter,
  124. options=["Light Grey", "Dark Grey", "Red", "Blue"])
  125.  
  126.  
  127. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  128. // ♣SHORT TIMEFRAME INPUT♣
  129. //
  130. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  131. enableShort = input(true, title="-------- SHORT VOLATILITY BANDS -------- [uncheck to disable]")
  132.  
  133. // VWAP Length
  134. shortLength = input( title="Length (bars back)", type=integer, defval=defShortLen, minval=1 )
  135.  
  136. // Short resolution
  137. shortCustomRes = input(title="Timeframe (minutes) [0 to use current chart's]", type=float, defval=defShortTF, minval=0, step=1)
  138. shortRes = shortCustomRes==0 ? period : tostring(shortCustomRes)
  139.  
  140. //
  141. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  142. // ♣DEVIATION INPUTS♣
  143. //
  144. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  145. showShortBands = input(title="How many deviation bands to display", type=string, defval=defShowShortBands,
  146. options=["VWAP only", "[1] Standard Deviation", "[2] Standard & Golden", "[3] Stardard to Silver", "[4] Standard to Bronze", "[5] All"])
  147.  
  148. // Standard Deviations Parameters (default: Metallic Means)type = float, step = 0.1)
  149. shortDev1 = input(title='[1] "Standard" Deviation [0 = same as long]', minval=0, step=0.1, defval=defShortDev1) // Zero ratio (Fib ratio n=0)
  150. shortDev2 = input(title='[2] "Golden" Deviation [0 = same as long]' , minval=0, step=0.1, defval=defShortDev2) // Golden ratio (Fib ratio n=1)
  151. shortDev3 = input(title='[3] "Silver" Deviation [0 = same as long]' , minval=0, step=0.1, defval=defShortDev3) // Silver ratio (Fib ratio n=2)
  152. shortDev4 = input(title='[4] "Bronze" Deviation [0 = same as long]' , minval=0, step=0.1, defval=defShortDev4) // Bronze ratio (Fib ratio n=3)
  153. shortDev5 = input(title='[5] "Copper" Deviation [0 = same as long]' , minval=0, step=0.1, defval=defShortDev5) // Copper ratio (Fib ratio n=4)
  154. // Fine-tune volume weight and deviations
  155. shortFinetune = input(title="[all] × fine-tuning coefficient [0 = same as long]", type=float, step=0.01, minval=0.001, defval=defShortFine)
  156.  
  157. //
  158. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  159. // ♣THEMES♣
  160. //
  161. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  162. // Short shades (Dark to Light)
  163. themeShortShade = input(title="Color shade", type=string, defval=defThemeShortShade,
  164. options=["Dark", "Light"])
  165.  
  166. // Short: Color selection for each side
  167. themeColorAboveShort = input(title="Color above VWAP", type=string, defval=defThemeColorAboveShort,
  168. options=["01 Red", "08 Green","02 Deep Orange", "10 Cyan", "12 Blue", "16 Blue Grey", "00 Grey"])
  169. themeColorBelowShort = input(title="Color below VWAP", type=string, defval=defThemeColorBelowShort,
  170. options=["01 Red","08 Green","02 Deep Orange", "10 Cyan", "12 Blue", "16 Blue Grey", "00 Grey"])
  171.  
  172. // Short alternating fill selector (bullish/bearish)
  173. shortFillAlt = input(false, title="Bullish/Bearish alternating fill")
  174.  
  175. // Center and "opposite" bands (other side of the mean)
  176. themeShortCenter = input(title="Center & opposite bands", type=string, defval=defThemeShortCenter,
  177. options=["Light Grey", "Dark Grey", "Red", "Blue"])
  178.  
  179. //
  180. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  181. // ♣STRATEGY INPUTS♣
  182. //
  183. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  184. FromYear = input( defval = 2018, title = "Strategy from Year", type=integer, minval = 2011 )
  185. FromMonth = input( defval = 03, title = "... Month", type=integer, minval = 1 )
  186. FromDay = input( defval = 01, title = "... Day", type=integer, minval = 1 )
  187. ToYear = input( defval = 2018, title = "Strategy to Year", type=integer, minval = 2011 )
  188. ToMonth = input( defval = 04, title = "... Month", type=integer, minval = 1 )
  189. ToDay = input( defval = 30, title = "... Day", type=integer, minval = 1 )
  190. isTimeRange = ( time > timestamp( FromYear, FromMonth, FromDay, 00, 00 ) ) and ( time < timestamp( ToYear, ToMonth, ToDay, 23, 59 ) )
  191. useTrueRange = input(defval="Auto", options=["Always", "Auto", "Never"], title="Use True Range instead of Volume")
  192. vol = useTrueRange == "Always" or (useTrueRange == "Auto" and na(time))? volume : time
  193. //
  194. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  195. // ♣FUNCTIONS♣
  196. //
  197. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  198. fnVWAP(Price, Weight, Length) => sum(Price * Weight, Length ) / sum(Weight, Length)
  199. longSMA = sma(source,longLength)
  200. shortSMA = sma(source,shortLength)
  201. // fnWeightedStDev: Weighted Standard Deviation (WSD)
  202. // Calculate deviation based on the sliding Length (N minutes)
  203. longfnWeightedStDev(Price, Weight, Length) => sqrt( sum(Weight * pow(Price-longSMA, 2), Length ) / sum(Weight, Length) )
  204. shortfnWeightedStDev(Price1, Weight1, Length1) => sqrt( sum(Weight1 * pow(Price1-shortSMA , 2), Length1 ) / sum(Weight1, Length1) )
  205.  
  206. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  207. // ♣ CALCULATE♣
  208.  
  209. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  210.  
  211. // Evaluate if long and short deviation multipliers are the same (otherwise use short value).
  212. shortDev1 := shortDev1 == 0 ? longDev1 : shortDev1
  213. shortDev2 := shortDev2 == 0 ? longDev2 : shortDev2
  214. shortDev3 := shortDev3 == 0 ? longDev3 : shortDev3
  215. shortDev4 := shortDev4 == 0 ? longDev4 : shortDev4
  216. shortDev5 := shortDev5 == 0 ? longDev5 : shortDev5
  217. shortFinetune := shortFinetune == 0 ? longFinetune : shortFinetune
  218.  
  219. longPrice = security(tickerid, longRes, close)
  220. longVolume = security(tickerid, longRes, volume)
  221. shortPrice = security(tickerid, shortRes, close)
  222. shortVolume = security(tickerid, shortRes, volume)
  223. shortTime = security(tickerid, shortRes, time)
  224. longTime = security(tickerid, longRes, time)
  225. // VWAP
  226. longMean = enableLong ==true ? fnVWAP( longPrice, longVolume, longLength) : na
  227. shortMean = enableShort==true ? fnVWAP(shortPrice, shortVolume, shortLength) : na
  228.  
  229. // WEIGHTED STANDARD DEVIATION
  230. longDeviation = longfnWeightedStDev( longPrice, longTime, longLength)
  231. shortDeviation = shortfnWeightedStDev(shortPrice, shortTime,shortLength)
  232.  
  233. //
  234. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  235. // ♣Deviations♣
  236. //
  237. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  238.  
  239. // Which long lines to calculate and draw
  240. showLongDev5 = showLongBands=="[5] All"
  241. showLongDev4 = showLongBands=="[4] Standard to Bronze" or showLongDev5
  242. showLongDev3 = showLongBands=="[3] Stardard to Silver" or showLongDev4
  243. showLongDev2 = showLongBands=="[2] Standard & Golden" or showLongDev3
  244. showLongDev1 = showLongBands=="[1] Standard Deviation" or showLongDev2
  245. showLongMean = showLongBands=="VWAP only" or showLongDev1
  246.  
  247. // Which short lines to calculate and draw
  248. showShortDev5 = showShortBands=="[5] All"
  249. showShortDev4 = showShortBands=="[4] Standard to Bronze" or showShortDev5
  250. showShortDev3 = showShortBands=="[3] Stardard to Silver" or showShortDev4
  251. showShortDev2 = showShortBands=="[2] Standard & Golden" or showShortDev3
  252. showShortDev1 = showShortBands=="[1] Standard Deviation" or showShortDev2
  253. showShortMean = showShortBands=="VWAP only" or showShortDev1
  254.  
  255. // Long Timeframe above
  256. longDevLineAbove1 = showLongDev1 ? longMean + ( longDev1 * longDeviation ) * longFinetune : na
  257. longDevLineAbove2 = showLongDev2 ? longMean + ( longDev2 * longDeviation ) * longFinetune : na
  258. longDevLineAbove3 = showLongDev3 ? longMean + ( longDev3 * longDeviation ) * longFinetune : na
  259. longDevLineAbove4 = showLongDev4 ? longMean + ( longDev4 * longDeviation ) * longFinetune : na
  260. longDevLineAbove5 = showLongDev5 ? longMean + ( longDev5 * longDeviation ) * longFinetune : na
  261. // Long Timeframe below
  262. longDevLineBelow1 = showLongDev1 ? longMean - ( longDev1 * longDeviation ) * longFinetune : na
  263. longDevLineBelow2 = showLongDev2 ? longMean - ( longDev2 * longDeviation ) * longFinetune : na
  264. longDevLineBelow3 = showLongDev3 ? longMean - ( longDev3 * longDeviation ) * longFinetune : na
  265. longDevLineBelow4 = showLongDev4 ? longMean - ( longDev4 * longDeviation ) * longFinetune : na
  266. longDevLineBelow5 = showLongDev5 ? longMean - ( longDev5 * longDeviation ) * longFinetune : na
  267.  
  268. // Short Timeframe above
  269. shortDevLineAbove1 = showShortDev1 ? shortMean + ( shortDev1 * shortDeviation ) * shortFinetune : na
  270. shortDevLineAbove2 = showShortDev2 ? shortMean + ( shortDev2 * shortDeviation ) * shortFinetune : na
  271. shortDevLineAbove3 = showShortDev3 ? shortMean + ( shortDev3 * shortDeviation ) * shortFinetune : na
  272. shortDevLineAbove4 = showShortDev4 ? shortMean + ( shortDev4 * shortDeviation ) * shortFinetune : na
  273. shortDevLineAbove5 = showShortDev5 ? shortMean + ( shortDev5 * shortDeviation ) * shortFinetune : na
  274. // Long Timeframe below
  275. shortDevLineBelow1 = showShortDev1 ? shortMean - ( shortDev1 * shortDeviation ) * shortFinetune : na
  276. shortDevLineBelow2 = showShortDev2 ? shortMean - ( shortDev2 * shortDeviation ) * shortFinetune : na
  277. shortDevLineBelow3 = showShortDev3 ? shortMean - ( shortDev3 * shortDeviation ) * shortFinetune : na
  278. shortDevLineBelow4 = showShortDev4 ? shortMean - ( shortDev4 * shortDeviation ) * shortFinetune : na
  279. shortDevLineBelow5 = showShortDev5 ? shortMean - ( shortDev5 * shortDeviation ) * shortFinetune : na
  280.  
  281. //
  282. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  283. // ♣STRATEGY♣
  284. //
  285. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  286.  
  287. //priceOverNegStDev1 = crossover(low, longDevLineBelow1)
  288. //priceOverPosStDev1 = crossunder(high, longDevLineAbove1)
  289. //long Signal
  290. isCrossO(val,toCrs) => crossunder(val, toCrs)
  291. isCrossU(val,toCrs) => crossunder(val, toCrs)
  292.  
  293.  
  294.  
  295. bullLong0Cond = isCrossO(low,longMean)
  296. bullLong1Cond = isCrossO(high,longDevLineBelow1)
  297. bullLong2Cond = isCrossO(high,longDevLineBelow2)
  298. bullLong3Cond = isCrossO(high,longDevLineBelow3)
  299. bullLong4Cond = isCrossO(high,longDevLineBelow4)
  300. bullLong5Cond = isCrossO(high,longDevLineBelow5)
  301.  
  302.  
  303.  
  304. //bearLong0Cond = isCrossU(low,longMean)
  305. bearLong1Cond = isCrossU(close,longDevLineAbove1)
  306. bearLong2Cond = isCrossU(close,longDevLineAbove2)
  307. bearLong3Cond = isCrossU(close,longDevLineAbove3)
  308. bearLong4Cond = isCrossU(close,longDevLineAbove4)
  309. bearLong5Cond = isCrossU(close,longDevLineAbove5)
  310.  
  311.  
  312. longBullSignal = bullLong0Cond or bullLong1Cond or bullLong2Cond or bullLong3Cond or bullLong4Cond or bullLong5Cond ? true : false
  313. longBearSignal = bearLong1Cond or bearLong2Cond or bearLong3Cond or bearLong4Cond or bearLong5Cond ? true : false
  314.  
  315.  
  316.  
  317.  
  318. //Short Signal
  319. sbLL0Cond = isCrossO(low,shortMean)
  320. sbLL1Cond = isCrossO(high,shortDevLineBelow1)
  321. sbLL2Cond = isCrossO(high,shortDevLineBelow2)
  322. sbLL3Cond = isCrossO(high,shortDevLineBelow3)
  323. sbLL4Cond = isCrossO(high,shortDevLineBelow4)
  324. sbLL5Cond = isCrossO(high,shortDevLineBelow5)
  325.  
  326.  
  327.  
  328. //sbL0Cond = isCrossU(low,shortMean)
  329. sbL1Cond = isCrossU(close,shortDevLineAbove1)
  330. sbL2Cond = isCrossU(close,shortDevLineAbove2)
  331. sbL3Cond = isCrossU(close,shortDevLineAbove3)
  332. sbL4Cond = isCrossU(close,shortDevLineAbove4)
  333. sbL5Cond = isCrossU(close,shortDevLineAbove5)
  334.  
  335. shortBullSignal = sbLL1Cond or sbLL2Cond or sbLL3Cond or sbLL4Cond or sbLL5Cond ? true : false
  336. shortBearSignal = sbL1Cond or sbL2Cond or sbL3Cond or sbL4Cond or sbL5Cond ? true : false
  337.  
  338.  
  339. // LONG STRATEGY
  340. // BULL TRADE
  341. strategy.entry( "BULL", strategy.long, when=( longBullSignal and time ) )
  342. strategy.exit( "BULL", when=( longBearSignal and time ) )
  343.  
  344.  
  345.  
  346. // BEAR TRADE
  347. strategy.entry( "BEAR", strategy.short, when=( longBearSignal and time ) )
  348. strategy.exit( "BEAR", when=( longBullSignal and time ) )
  349.  
  350. //SHORT STRATEGY
  351. // BULL TRADE
  352. strategy.entry( "BULL", strategy.long, when=( shortBullSignal and time) )
  353. strategy.exit( "BULL", when=( shortBearSignal and time ) )
  354.  
  355. // BEAR TRADE
  356. strategy.entry( "BEAR", strategy.short, when=( shortBearSignal and time ) )
  357. strategy.exit( "BEAR", when=( shortBullSignal and time) )
  358. // End of STRATEGY
  359. // *****************************************************************************
  360. // -----------------------------------------------------------------------------
  361.  
  362.  
  363. //
  364. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  365. // ♣DESIGN♣
  366. //
  367. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  368.  
  369. // Red, Pink, Purple, Deep Purple
  370. red20 = #EF9A9A , pink20 = #F48FB1 , purple20 = #CE93D8 , deepPurple20 = #B39DDB
  371. red30 = #E57373 , pink30 = #F06292 , purple30 = #BA68C8 , deepPurple30 = #9575CD
  372. red50 = #F44336 , pink50 = #E91E63 , purple50 = #9C27B0 , deepPurple50 = #673AB7
  373. red70 = #D32F2F , pink70 = #C2185B , purple70 = #7B1FA2 , deepPurple70 = #512DA8
  374. red90 = #B71C1C , pink90 = #880E4F , purple90 = #D500F9 , deepPurple90 = #311B92
  375. // Indigo, Blue, Light Blue, Cyan
  376. indigo20 = #9FA8DA , blue20 = #90CAF9 , lightBlue20 = #81D4FA , cyan20 = #80DEEA
  377. indigo30 = #7986CB , blue30 = #64B5F6 , lightBlue30 = #4FC3F7 , cyan30 = #4DD0E1
  378. indigo50 = #3F51B5 , blue50 = #2196F3 , lightBlue50 = #03A9F4 , cyan50 = #E57373
  379. indigo70 = #303F9F , blue70 = #1976D2 , lightBlue70 = #0288D1 , cyan70 = #E57373
  380. indigo90 = #1A237E , blue90 = #0D47A1 , lightBlue90 = #01579B , cyan90 = #E57373
  381. // Teal, Green, Light Green, Lime
  382. teal20 = #80CBC4 , green20 = #A5D6A7 , lightGreen20 = #C5E1A5 , lime20 = #E6EE9C
  383. teal30 = #4DB6AC , green30 = #81C784 , lightGreen30 = #AED581 , lime30 = #DCE775
  384. teal50 = #009688 , green50 = #4CAF50 , lightGreen50 = #8BC34A , lime50 = #CDDC39
  385. teal70 = #00796B , green70 = #A5D6A7 , lightGreen70 = #689F38 , lime70 = #AFB42B
  386. teal90 = #004D40 , green90 = #4CAF50 , lightGreen90 = #33691E , lime90 = #827717
  387. // Yellow, Amber, Orange, Deep Orange
  388. yellow20 = #FFF59D , amber20 = #FFE082 , orange20 = #FFCC80 , deepOrange20 = #FFAB91
  389. yellow30 = #FFF176 , amber30 = #FFD54F , orange30 = #FFB74D , deepOrange30 = #FF8A65
  390. yellow50 = #FFEB3B , amber50 = #FFC107 , orange50 = #FF9800 , deepOrange50 = #FF5722
  391. yellow70 = #FBC02D , amber70 = #FFA000 , orange70 = #F57C00 , deepOrange70 = #E64A19
  392. yellow90 = #F57F17 , amber90 = #FF6F00 , orange90 = #E65100 , deepOrange90 = #BF360C
  393. // Grey,blue gray
  394. grey20 = #EEEEEE, bluegrey20 = #B0BEC5
  395. grey30 = #E0E0E0, bluegrey40 = #78909C
  396. grey40 = #BDBDBD, bluegrey60 = #546E7A
  397. grey50 = #9E9E9E, bluegrey70 = #455A64
  398. grey70 = #616161, bluegrey80 = #37474F
  399. grey80 = #424242, bluegrey90 = #26C6DA
  400. grey90 = #212121
  401.  
  402. //
  403. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  404. // ♣COLOR THEMES♣
  405. //
  406. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  407.  
  408. // work out the shade here first, then plug that variable into colorAbove and colorBelow
  409. colorLongRed = themeLongShade=="Dark" ? red90 : red70
  410. colorLongDeepOr = themeLongShade=="Dark" ? deepOrange70 : deepOrange50
  411.  
  412. colorLongBlueGrey = themeLongShade=="Dark" ? bluegrey60 : bluegrey20
  413. colorLongGreen = themeLongShade=="Dark" ? green90 : green70
  414. colorLongCyan = themeLongShade=="Dark" ? cyan70 : cyan50
  415. colorLongBlue = themeLongShade=="Dark" ? blue90 : blue70
  416.  
  417. colorLongPink = themeLongShade=="Dark" ? pink70 : pink50
  418. colorLongGrey = themeLongShade=="Dark" ? grey80 : grey50
  419.  
  420. colorShortRed = themeShortShade=="Dark" ? red90 : red70
  421. colorShortDeepOr = themeShortShade=="Dark" ? deepOrange70 : deepOrange50
  422.  
  423. colorShortGreen = themeShortShade=="Dark" ? green90 : green70
  424. colorShortCyan = themeShortShade=="Dark" ? cyan70 : cyan50
  425. colorShortBlue = themeShortShade=="Dark" ? blue90 : blue70
  426. colorShortBlueGrey = themeShortShade=="Dark" ? bluegrey80 : bluegrey60
  427.  
  428. colorShortPink = themeShortShade=="Dark" ? pink70 : pink50
  429. colorShortGrey = themeShortShade=="Dark" ? grey80 : grey50
  430.  
  431. colorAboveLong =
  432. themeColorAboveLong=="01 Red" ? colorLongRed :
  433. themeColorAboveLong=="02 Deep Orange" ? colorLongDeepOr :
  434.  
  435. themeColorAboveLong=="08 Green" ? colorLongGreen :
  436. themeColorAboveLong=="10 Cyan" ? colorLongCyan :
  437. themeColorAboveLong=="12 Blue" ? colorLongBlue :
  438.  
  439. themeColorAboveLong=="16 Blue Grey" ? colorLongBlueGrey :
  440. colorLongGrey
  441.  
  442. colorBelowLong =
  443. themeColorBelowLong=="01 Red" ? colorLongRed :
  444. themeColorBelowLong=="02 Deep Orange" ? colorLongDeepOr :
  445.  
  446. themeColorBelowLong=="08 Green" ? colorLongGreen :
  447.  
  448. themeColorBelowLong=="10 Cyan" ? colorLongCyan :
  449. themeColorBelowLong=="12 Blue" ? colorLongBlue :
  450.  
  451. themeColorBelowLong=="16 Blue Grey" ? colorLongBlueGrey :
  452. colorLongGrey
  453.  
  454. colorAboveShort =
  455. themeColorAboveShort=="01 Red" ? colorShortRed :
  456. themeColorAboveShort=="02 Deep Orange" ? colorShortDeepOr :
  457.  
  458. themeColorAboveShort=="08 Green" ? colorShortGreen :
  459. themeColorAboveShort=="10 Cyan" ? colorShortCyan :
  460.  
  461. themeColorAboveShort=="12 Blue" ? colorShortBlue :
  462.  
  463. themeColorAboveShort=="16 Blue Grey" ? colorShortBlueGrey :
  464. colorShortGrey
  465.  
  466. colorBelowShort =
  467. themeColorBelowShort=="01 Red" ? colorShortRed :
  468. themeColorBelowShort=="02 Deep Orange" ? colorShortDeepOr :
  469.  
  470. themeColorBelowShort=="08 Green" ? colorShortGreen :
  471. // themeColorBelowShort=="09 Teal" ? colorShortTeal :
  472. themeColorBelowShort=="10 Cyan" ? colorShortCyan :
  473. // themeColorBelowShort=="11 Light Blue" ? colorShortLiteBl :
  474. themeColorBelowShort=="12 Blue" ? colorShortBlue :
  475.  
  476. themeColorBelowShort=="16 Blue Grey" ? colorShortBlueGrey :
  477. colorShortGrey
  478.  
  479.  
  480. //
  481. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  482. // ♣FILL POINTS FOR BULLISH/BEARISH♣
  483. //
  484. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  485. longFillCenter = themeLongCenter=="Light Grey" ? grey40 : themeLongCenter=="Dark Grey" ? grey90 : themeLongCenter=="Red" ? red : blue
  486. shortFillCenter = themeShortCenter=="Light Grey" ? grey40 : themeLongCenter=="Dark Grey" ? grey90 : themeLongCenter=="Red" ? red : blue
  487.  
  488. // Long bands fill color
  489. isLongBullish = close > longMean
  490. fillLongAbove = not(isLongBullish) and longFillAlt ? longFillCenter : colorAboveLong
  491. fillLongBelow = isLongBullish and longFillAlt ? longFillCenter : colorBelowLong
  492.  
  493. // Short bands fill color
  494. isShortBullish = close > shortMean
  495. fillShortAbove = not(isShortBullish) and shortFillAlt ? shortFillCenter : colorAboveShort
  496. fillShortBelow = isShortBullish and shortFillAlt ? shortFillCenter : colorBelowShort
  497.  
  498.  
  499.  
  500. //
  501. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  502. // ♣PLOT♣
  503. //
  504. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  505.  
  506.  
  507. // Means
  508. mainLongMean = plot(longMean , title="------ MEAN (LONG) -------", linewidth=1, style=circles, join=false, transp=0, color=grey80)
  509. mainShortMean =plot(shortMean, title="------ MEAN (SHORT) ------", linewidth=1, style=circles , join=false, transp=0, color=grey80)
  510.  
  511. // Long Deviation Lines
  512. longAbove1 = plot( longDevLineAbove1, title="[1🔼] Standard Deviation (SELL?)", style=circles, join=false, transp=0 , color=grey50 )
  513. longAbove2 = plot( longDevLineAbove2, title="[2🔼] Golden Dev. Above ", style=circles, join=false, transp=0 ,color=colorAboveLong )
  514. longAbove3 = plot( longDevLineAbove3, title="[3🔼] Silver Dev. Above ", style=circles, join=false, transp=0 , color=colorAboveLong )
  515. longAbove4 = plot( longDevLineAbove4, title="[4🔼] Bronze Dev. Above ", style=circles, join=false, transp=0 , color=colorAboveLong )
  516. longAbove5 = plot( longDevLineAbove5, title="[5🔼] Copper Dev. Above ", style=circles, join=false, transp=100, color=colorAboveLong )
  517. longBelow1 = plot( longDevLineBelow1, title="[1🔽] Standard Deviation (BUY?)", style=circles, join=false, transp=0 , color=grey50)
  518. longBelow2 = plot( longDevLineBelow2, title="[2🔽] Golden Dev. Below ", style=circles, join=false, transp=0 , color=colorBelowLong )
  519. longBelow3 = plot( longDevLineBelow3, title="[3🔽] Silver Dev. Below ", style=circles, join=false, transp=0 , color=colorBelowLong )
  520. longBelow4 = plot( longDevLineBelow4, title="[4🔽] Bronze Dev. Below ", style=circles, join=false, transp=0 , color=colorBelowLong )
  521. longBelow5 = plot( longDevLineBelow5, title="[5🔽] Copper Dev. Below ", style=circles, join=false, transp=100, color=colorBelowLong )
  522.  
  523. // Short Deviation Lines
  524. shortAbove1 = plot( shortDevLineAbove1, title="[1🔼] Standard Deviation (SELL?)", style=circles, join=false, transp=0, color=grey50 )
  525. shortAbove2 = plot( shortDevLineAbove2, title="[2🔼] Golden Dev. Above ", style=circles, join=false, transp=0, color=colorAboveShort )
  526. shortAbove3 = plot( shortDevLineAbove3, title="[3🔼] Silver Dev. Above ", style=circles, join=false, transp=0, color=colorAboveShort )
  527. shortAbove4 = plot( shortDevLineAbove4, title="[4🔼] Bronze Dev. Above ", style=circles, join=false, transp=0, color=colorAboveShort )
  528. shortAbove5 = plot( shortDevLineAbove5, title="[5🔼] Copper Dev. Above ", style=circles, join=false, transp=0, color=colorAboveShort )
  529. shortBelow1 = plot( shortDevLineBelow1, title="[1🔽] Standard Deviation (BUY?)", style=circles, join=false, transp=0, color=grey50 )
  530. shortBelow2 = plot( shortDevLineBelow2, title="[2🔽] Golden Dev. Below ", style=circles, join=false, transp=0, color=colorBelowShort )
  531. shortBelow3 = plot( shortDevLineBelow3, title="[3🔽] Silver Dev. Below ", style=circles, join=false, transp=0, color=colorBelowShort )
  532. shortBelow4 = plot( shortDevLineBelow4, title="[4🔽] Bronze Dev. Below ", style=circles, join=false, transp=0, color=colorBelowShort )
  533. shortBelow5 = plot( shortDevLineBelow5, title="[5🔽] Copper Dev. Below ", style=circles, join=false, transp=0, color=colorBelowShort )
  534.  
  535.  
  536. //
  537. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  538. // FILL PLOT
  539. //
  540. //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  541.  
  542. // Long Bands Fill
  543. fill ( longBelow1, longAbove1, transp=90, color=longFillCenter , title="------ Fill: 1 SD Band around Long Mean ------")
  544. fill ( longAbove1, longAbove2, transp=80, color=fillLongAbove, title="[2🔼] Long Overbought (Golden Band)" )
  545. fill ( longAbove2, longAbove3, transp=80, color=fillLongAbove, title="[3🔼] Long Overbought (Silver Band)" )
  546. fill ( longAbove3, longAbove4, transp=80, color=fillLongAbove, title="[4🔼] Long Overbought (Bronze Band)" )
  547. fill ( longAbove4, longAbove5, transp=80, color=fillLongAbove, title="[5🔼] Long Overbought (Copper Band)" )
  548. fill ( longBelow1, longBelow2, transp=80, color=fillLongBelow, title="[2🔽] Long Oversold (Golden Band)" )
  549. fill ( longBelow2, longBelow3, transp=80, color=fillLongBelow, title="[3🔽] Long Oversold (Silver Band)" )
  550. fill ( longBelow3, longBelow4, transp=80, color=fillLongBelow, title="[4🔽] Long Oversold (Bronze Band)" )
  551. fill ( longBelow4, longBelow5, transp=80, color=fillLongBelow, title="[5🔽] Long Oversold (Copper Band)" )
  552.  
  553. // Short Bands fill
  554. fill ( shortBelow1, shortAbove1, transp=90, color=shortFillCenter , title="------ Fill 1 SD Band around Short Mean ------")
  555. fill ( shortAbove1, shortAbove2, transp=80, color=fillShortAbove, title="[2🔼] Short Overbought (Golden Band)" )
  556. fill ( shortAbove2, shortAbove3, transp=80, color=fillShortAbove, title="[3🔼] Short Overbought (Silver Band)" )
  557. fill ( shortAbove3, shortAbove4, transp=80, color=fillShortAbove, title="[4🔼] Short Overbought (Bronze Band)" )
  558. fill ( shortAbove4, shortAbove5, transp=80, color=fillShortAbove, title="[5🔼] Short Overbought (Copper Band)" )
  559. fill ( shortBelow1, shortBelow2, transp=80, color=fillShortBelow, title="[2🔽] Short Oversold (Golden Band)" )
  560. fill ( shortBelow2, shortBelow3, transp=80, color=fillShortBelow, title="[3🔽] Short Oversold (Silver Band)" )
  561. fill ( shortBelow3, shortBelow4, transp=80, color=fillShortBelow, title="[4🔽] Short Oversold (Bronze Band)" )
  562. fill ( shortBelow4, shortBelow5, transp=80, color=fillShortBelow, title="[5🔽] Short Oversold (Copper Band)" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement