Advertisement
Guest User

Untitled

a guest
Dec 31st, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.25 KB | None | 0 0
  1. //@version=3
  2.  
  3.  
  4. study(title="strategy MTF candles by yatrader2 signals", shorttitle="[BT] Simple Template 1.0", overlay=true)
  5.  
  6.  
  7. ////////////////////////////////////////////////////////////////
  8. //* Common functions *//
  9. ////////////////////////////////////////////////////////////////
  10.  
  11. //Different options of security()
  12. get_sec(sid, res, src, look_ahead, shift_back) =>
  13. indHighTF = barstate.isrealtime ? 1 : 0
  14. indCurrTF = barstate.isrealtime ? 0 : 1
  15. x1 = security(sid, res, src[indHighTF], barmerge.gaps_off, barmerge.lookahead_off)
  16. x2 = security(sid, res, src[indHighTF], barmerge.gaps_off, barmerge.lookahead_on)
  17. x = look_ahead ? x2 : x1
  18. info = if shift_back
  19. x[indCurrTF]
  20. else
  21. x
  22.  
  23. // Check trending of a bar, i.e. Close vs Open
  24. get_trend(x, y, rerr, gerr) =>
  25. t = if x > y and abs(x - y) / y > gerr
  26. 1
  27. else
  28. if x < y and abs(x - y) / y > rerr
  29. -1
  30. else
  31. 0
  32.  
  33.  
  34. get_kama(src, length) =>
  35. xvnoise = abs(src - src[1])
  36. nfastend = 0.618
  37. nslowend = 0.0618
  38. nsignal = abs(src - src[length])
  39. nnoise = sum(xvnoise, length)
  40. nefratio = iff(nnoise != 0, nsignal / nnoise, 0)
  41. nsmooth = pow(nefratio * (nfastend - nslowend) + nslowend, 2)
  42. KAMA = na
  43. KAMA := nz(KAMA[1]) + nsmooth * (src - nz(KAMA[1]))
  44.  
  45. ////////////////////////////////////////////////////////////////
  46. //* Source and Alternative Candlestick *//
  47. ////////////////////////////////////////////////////////////////
  48.  
  49. altSrc = input("ohlc4", 'SOURCE', options=["close", "high", "low", "open", "tr", "vwap", "ohlc4", "hl2", "hlc3"])
  50.  
  51. altSLength = input(1, 'SMOOTHING LENGTH (1 to turnoff)', type= integer, minval=1)
  52. altSMethod = input("ema", '- Smoothing method', options=["swma", "sma", "ema", "rma", "wma", "vwma", "alma", "kama"])
  53.  
  54. altRes = input("1", 'ALTERNATIVE TIMEFRAME (1 to turnoff)', type=resolution)
  55. symRes = altRes == "1" ? period : altRes
  56. isHA = input(false, '- Heikin-ashi', type=bool)
  57. symID = isHA ? heikenashi(tickerid) : tickerid
  58. lookAh = input(false, '- Lookahead (disable to resolve repaint)', type=bool)
  59. shiftB = input(true, '- Shiftback (enable to resolve repaint)', type=bool)
  60.  
  61. // For any calculation relating to the actual price
  62. tv_o = open
  63. tv_h = high
  64. tv_l = low
  65. tv_c = close
  66. tv_hl2 = hl2
  67. tv_hlc3 = hlc3
  68. tv_ohlc4 = ohlc4
  69. tv_tr = tr
  70. tv_vwap = vwap
  71.  
  72. // Get specific source for the NZ center
  73. alt_src = iff(altSrc == "high", tv_h, iff(altSrc == "low", tv_l,
  74. iff(altSrc == "open", tv_o, iff(altSrc == "tr", tv_tr,
  75. iff(altSrc == "vwap", tv_vwap, iff(altSrc == "ohlc4", tv_ohlc4,
  76. iff(altSrc == "hlc3", tv_hlc3, iff(altSrc == "hl2", tv_hl2, iff(altSrc == "close", tv_c, tv_ohlc4)))))))))
  77.  
  78. // Smoothing and transform the center with the specific timeframe and candlestick
  79. alt_smoothSrc = altSLength > 1 ? iff(altSMethod == "sma", sma(alt_src, altSLength), iff(altSMethod == "ema", ema(alt_src, altSLength), iff(altSMethod == "rma", rma(alt_src, altSLength),
  80. iff(altSMethod == "wma", wma(alt_src, altSLength), iff(altSMethod == "vwma", vwma(alt_src, altSLength), iff(altSMethod == "swma", swma(alt_src),
  81. iff(altSMethod == "alma", alma(alt_src, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(alt_src, altSLength), sma(alt_src, altSLength))))))))) : alt_src
  82. alt_s = altRes == "1" ? alt_smoothSrc : get_sec(symID, symRes, alt_smoothSrc, lookAh, shiftB)
  83.  
  84.  
  85. // Smoothing and transform the high with the specific timeframe and candlestick
  86. alt_smoothHigh = altSLength > 1 ? iff(altSMethod == "sma", sma(tv_h, altSLength), iff(altSMethod == "ema", ema(tv_h, altSLength), iff(altSMethod == "rma", rma(tv_h, altSLength),
  87. iff(altSMethod == "wma", wma(tv_h, altSLength), iff(altSMethod == "vwma", vwma(tv_h, altSLength), iff(altSMethod == "swma", swma(tv_h),
  88. iff(altSMethod == "alma", alma(tv_h, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(tv_h, altSLength), sma(tv_h, altSLength))))))))) : tv_h
  89. alt_h = altRes == "1" ? alt_smoothHigh : get_sec(symID, symRes, alt_smoothHigh, lookAh, shiftB)
  90.  
  91.  
  92. // Smoothing and transform the low with the specific timeframe and candlestick
  93. alt_smoothLow = altSLength > 1 ? iff(altSMethod == "sma", sma(tv_l, altSLength), iff(altSMethod == "ema", ema(tv_l, altSLength), iff(altSMethod == "rma", rma(tv_l, altSLength),
  94. iff(altSMethod == "wma", wma(tv_l, altSLength), iff(altSMethod == "vwma", vwma(tv_l, altSLength), iff(altSMethod == "swma", swma(tv_l),
  95. iff(altSMethod == "alma", alma(tv_l, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(tv_l, altSLength), sma(tv_l, altSLength))))))))) : tv_l
  96. alt_l = altRes == "1" ? alt_smoothLow : get_sec(symID, symRes, alt_smoothLow, lookAh, shiftB)
  97.  
  98. // Get specific source for the center
  99. alt_srcCenter = iff(altSrc == "high", tv_h, iff(altSrc == "low", tv_l,
  100. iff(altSrc == "open", tv_o, iff(altSrc == "tr", tv_hlc3 + tv_tr,
  101. iff(altSrc == "vwap", tv_vwap, iff(altSrc == "ohlc4", tv_ohlc4,
  102. iff(altSrc == "hlc3", tv_hlc3, iff(altSrc == "hl2", tv_hl2, iff(altSrc == "close", tv_c, tv_ohlc4)))))))))
  103.  
  104. // Smoothing and transform the center with the specific timeframe and candlestick
  105. alt_smoothCenter = altSLength > 1 ? iff(altSMethod == "sma", sma(alt_srcCenter, altSLength), iff(altSMethod == "ema", ema(alt_srcCenter, altSLength), iff(altSMethod == "rma", rma(alt_srcCenter, altSLength),
  106. iff(altSMethod == "wma", wma(alt_srcCenter, altSLength), iff(altSMethod == "vwma", vwma(alt_srcCenter, altSLength), iff(altSMethod == "swma", swma(alt_srcCenter),
  107. iff(altSMethod == "alma", alma(alt_srcCenter, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(alt_srcCenter, altSLength), sma(alt_srcCenter, altSLength))))))))) : alt_src
  108. alt_center = altRes == "1" ? alt_smoothCenter : get_sec(symID, symRes, alt_smoothCenter, lookAh, shiftB)
  109.  
  110. ////////////////////////////////////////////////////////////////
  111. //* Common Variables *//
  112. ////////////////////////////////////////////////////////////////
  113.  
  114. // Transform setting for plotting with overlay=false
  115. plotCenter = 0
  116. plot_rangeWidth = 300
  117. plot_signalWidth = 200
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. intv=security(tickerid, altRes , isintraday?interval:interval*1440)
  125. o = security(tickerid, altRes , open, barmerge.gaps_off, barmerge.lookahead_on)
  126. c = security(tickerid, altRes , close, barmerge.gaps_off, barmerge.lookahead_on)
  127. h = security(tickerid, altRes , high, barmerge.gaps_off, barmerge.lookahead_on)
  128. l = security(tickerid, altRes , low, barmerge.gaps_off, barmerge.lookahead_on)
  129.  
  130. min_of_day = (hour*60)+minute
  131. midpoint=floor(intv/2)
  132. wickwidth=input(1.0, title="Wick width in bars")*interval/2
  133. step= input(true, title="Gray Borders with square edges of candles")
  134. topsbots= input(true, title="Color top/bottom edge of candles")
  135. pointyline= input(false, title="Colored Borders with non-square edges of candles")
  136.  
  137. wicktime= ((min_of_day % intv > midpoint-wickwidth) and (min_of_day % intv <= midpoint+wickwidth)) ?true:false
  138.  
  139. col = c >= o ? lime : red
  140.  
  141. // why so many options... see below
  142. plot(not pointyline?na:wicktime?o>c?h:l:o, color=col, title="Open", style=line, transp=40, title="pointyline open")
  143. plot(not pointyline?na:wicktime?o<=c?h:l:c, color=col, title="Close", style=line, transp=40, title="pointyline close")
  144. plot(not step?na:wicktime?o>c?h:l:o, color=gray, title="Open", style=stepline, transp=20, title="stepline open")
  145. plot(not step?na:wicktime?o<=c?h:l:c, color=gray, title="Close", style=stepline, transp=20, title="stepline close")
  146. plot(not topsbots?na:wicktime?o>c?h:l:o, color=col, title="Open", style=circles, transp=80)
  147. plot(not topsbots?na:wicktime?o<=c?h:l:c, color=col, title="Close", style=circles, transp=80)
  148.  
  149.  
  150. po = plot(wicktime?o>c?h:l:o, color=col, title="Open", style=stepline, transp=100, title="hidden open", editable=false)
  151. pc = plot(wicktime?o<=c?h:l:c, color=col, title="Close", style=stepline, transp=100, title="hidden close", editable=false)
  152. // wick tips
  153. plot(wicktime and h>c?h:na, color=col, title="High", style=circles, transp=60)
  154. plot(wicktime and l<c?l:na, color=col, title="Low", style=circles, transp=60)
  155.  
  156. fill(po, pc, col)
  157.  
  158.  
  159.  
  160.  
  161. // INPUTS
  162.  
  163. useRenko = input(false, title="This a RENKO Chart?")
  164. // WARNING: Using Heikin-Ashi may result in inaccurate backtest results
  165. useHeikin = input(false, title="Use Heikin-Ashi Candles? (BT WILL BE UNRELIABLE)")
  166. // Use Alternate Anchor TF for MAs
  167. anchor = input(0,minval=0,maxval=1440,title="Alternate TimeFrame Multiplier (0=none)")
  168. // If have anchor specified, calculate the base multiplier.
  169. //mult = isintraday ? anchor==0 or interval<=0 or interval>=anchor or anchor>1440? 1 : round(anchor/interval) : 1
  170. //mult := isdwm? 1 : mult // Only available Daily or less
  171. mult = anchor>0 ? anchor : 1
  172. open_ = useHeikin ? security(heikinashi(tickerid), period, open) : security(tickerid, period, open)
  173. close_ = useHeikin ? security(heikinashi(tickerid), period, close) : security(tickerid, period, close)
  174. high_ = useRenko? max(close_,open_) : useHeikin ? security(heikinashi(tickerid), period, high) : security(tickerid, period, high)
  175. low_ = useRenko? min(close_,open_) : useHeikin ? security(heikinashi(tickerid), period, low) : security(tickerid, period, low)
  176. uPrice = input(title="Price", defval="close", options=["close", "high", "low"])
  177. src= uPrice=="close" ? close_ : uPrice=="high" ? high_ : uPrice=="low" ? low_ : na
  178.  
  179.  
  180.  
  181. // Conditions
  182.  
  183. longCond = na
  184. shortCond = na
  185. longCond := c >= o and c[1] <= o[1]
  186. shortCond := c <= o and c[1] >= o[1]
  187.  
  188. // Count your long short conditions for more control with Pyramiding
  189.  
  190. sectionLongs = 0
  191. sectionLongs := nz(sectionLongs[1])
  192. sectionShorts = 0
  193. sectionShorts := nz(sectionShorts[1])
  194.  
  195. if longCond
  196. sectionLongs := sectionLongs + 1
  197. sectionShorts := 0
  198.  
  199. if shortCond
  200. sectionLongs := 0
  201. sectionShorts := sectionShorts + 1
  202.  
  203. // Pyramiding
  204.  
  205. pyrl = 1
  206.  
  207. // These check to see your signal and cross references it against the pyramiding settings above
  208.  
  209. longCondition = longCond and sectionLongs <= pyrl
  210. shortCondition = shortCond and sectionShorts <= pyrl
  211.  
  212. // Get the price of the last opened long or short
  213.  
  214. last_open_longCondition = na
  215. last_open_shortCondition = na
  216. last_open_longCondition := longCondition ? open : nz(last_open_longCondition[1])
  217. last_open_shortCondition := shortCondition ? open : nz(last_open_shortCondition[1])
  218.  
  219. // Check if your last postion was a long or a short
  220.  
  221. last_longCondition = na
  222. last_shortCondition = na
  223. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  224. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  225.  
  226. in_longCondition = last_longCondition > last_shortCondition
  227. in_shortCondition = last_shortCondition > last_longCondition
  228.  
  229. // Take profit
  230.  
  231. isTPl = input(false, "Take Profit Long")
  232. isTPs = input(false, "Take Profit Short")
  233. tp = input(5, "Take Profit %", type=float)
  234. long_tp = isTPl and crossover(high, (1+(tp/100))*last_open_longCondition) and longCondition == 0 and in_longCondition == 1
  235. short_tp = isTPs and crossunder(low, (1-(tp/100))*last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1
  236.  
  237. // Stop Loss
  238.  
  239. isSLl = input(false, "Stop Loss Long")
  240. isSLs = input(false, "Stop Loss Short")
  241. sl= 0.0
  242. sl := input(3, "Stop Loss %", type=float)
  243. long_sl = isSLl and crossunder(low, (1-(sl/100))*last_open_longCondition) and longCondition == 0 and in_longCondition == 1
  244. short_sl = isSLs and crossover(high, (1+(sl/100))*last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1
  245.  
  246. // Create a single close for all the different closing conditions.
  247.  
  248. long_close = long_tp or long_sl ? 1 : 0
  249. short_close = short_tp or short_sl ? 1 : 0
  250.  
  251. // Get the time of the last close
  252.  
  253. last_long_close = na
  254. last_short_close = na
  255. last_long_close := long_close ? time : nz(last_long_close[1])
  256. last_short_close := short_close ? time : nz(last_short_close[1])
  257.  
  258. // Alerts & Signals
  259.  
  260. bton(b) => b ? 1 : 0
  261. plotshape(longCondition, title = "Buy Signal", text = "B", style=shape.triangleup, location=location.belowbar, color = green, editable = false, transp = 0)
  262. plotshape(shortCondition, title = "Sell Signal", text = "S", style=shape.triangledown, location=location.abovebar, color = red, editable = false, transp = 0)
  263.  
  264. plotshape(long_tp and last_longCondition > nz(last_long_close[1]), text ="TP", title="Take Profit Long", style=shape.triangledown,
  265. location=location.abovebar, color = red, editable = false, transp = 0)
  266. plotshape(short_tp and last_shortCondition > nz(last_short_close[1]) , text ="TP", title="Take Profit Short", style=shape.triangleup,
  267. location=location.belowbar, color = lime, editable = false, transp = 0)
  268.  
  269. ltp = iff(long_tp and last_longCondition > nz(last_long_close[1]), (1+(tp/100))*last_open_longCondition, na)
  270. plot(ltp, style=cross, linewidth=3, color = white, editable = false)
  271. stp = iff(short_tp and last_shortCondition > nz(last_short_close[1]), (1-(tp/100))*last_open_shortCondition, na)
  272. plot(stp, style = cross, linewidth=3, color = white, editable = false)
  273.  
  274. plotshape(long_sl and last_longCondition > nz(last_long_close[1]), text ="SL", title="Stop Loss Long", style=shape.triangledown,
  275. location=location.abovebar, color = red, editable = false, transp = 0)
  276. plotshape(short_sl and last_shortCondition > nz(last_short_close[1]), text ="SL", title="Stop Loss Short", style=shape.triangleup,
  277. location=location.belowbar, color = lime, editable = false, transp = 0)
  278.  
  279. lsl = iff(long_sl and last_longCondition > nz(last_long_close[1]), (1-(sl/100))*last_open_longCondition, na)
  280. plot(lsl, style=cross, linewidth=3, color = white, editable = false)
  281. ssl = iff(short_sl and last_shortCondition > nz(last_short_close[1]), (1+(sl/100))*last_open_shortCondition, na)
  282. plot(ssl, style = cross, linewidth=3, color = white, editable = false)
  283.  
  284. alertcondition(bton(longCondition), title="Buy Alert")
  285. alertcondition(bton(shortCondition), title="Sell Alert")
  286. alertcondition(bton(long_tp and last_longCondition > nz(last_long_close[1])), title="Take Profit Long")
  287. alertcondition(bton(short_tp and last_shortCondition > nz(last_short_close[1])), title="Take Profit Short")
  288. alertcondition(bton(long_sl and last_longCondition > nz(last_long_close[1])), title="Stop Loss Long")
  289. alertcondition(bton(short_sl and last_shortCondition > nz(last_short_close[1])), title="Stop Loss Short")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement