Advertisement
Guest User

Tradingview BCH/BTC 2Hour

a guest
Feb 3rd, 2018
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.42 KB | None | 0 0
  1. //@version=3
  2. //study(title = "Alerts", shorttitle = "AL", overlay = true)
  3. strategy(title = "MACD Strategy V2.0 BCH/BTC 2H", shorttitle = "Strategy BCH/BTC", overlay=false,currency=currency.USD, initial_capital=100, default_qty_type=strategy.percent_of_equity,default_qty_value=100, commission_type=strategy.commission.percent, commission_value = 0.2)
  4. fastLength = input(21, minval=1, title="First Tuning")
  5. slowLength=input(27,minval=1, title="Second Tuning")
  6. // === Main Script Here ===
  7.  
  8. source = close
  9. useCurrentRes = input(true, title="Use Current Chart Resolution?")
  10. resCustom = input(title="Use Different Timeframe? Uncheck Box Above", type=resolution, defval="60")
  11. smd = input(true, title="Show MacD & Signal Line? Also Turn Off Dots Below")
  12. sd = input(true, title="Show Dots When MacD Crosses Signal Line?")
  13. sh = input(true, title="Show Histogram?")
  14. macd_colorChange = input(true,title="Change MacD Line Color-Signal Line Cross?")
  15. hist_colorChange = input(true,title="MacD Histogram 4 Colors?")
  16.  
  17. res = useCurrentRes ? period : resCustom
  18. signalLength = input(9,minval=1)
  19.  
  20. fastMA = ema(source, fastLength)
  21. slowMA = ema(source, slowLength)
  22.  
  23. macd = fastMA - slowMA
  24. signal = sma(macd, signalLength)
  25. hist = macd - signal
  26.  
  27. outMacD = security(tickerid, res, macd)
  28. outSignal = security(tickerid, res, signal)
  29. outHist = security(tickerid, res, hist)
  30.  
  31. histA_IsUp = outHist > outHist[1] and outHist > 0
  32. histA_IsDown = outHist < outHist[1] and outHist > 0
  33. histB_IsDown = outHist < outHist[1] and outHist <= 0
  34. histB_IsUp = outHist > outHist[1] and outHist <= 0
  35.  
  36. //MacD Color Definitions
  37. macd_IsAbove = outMacD >= outSignal
  38. macd_IsBelow = outMacD < outSignal
  39.  
  40. plot_color = hist_colorChange ? histA_IsUp ? aqua : histA_IsDown ? blue : histB_IsDown ? red : histB_IsUp ? maroon :yellow :gray
  41. macd_color = macd_colorChange ? macd_IsAbove ? lime : red : red
  42. signal_color = macd_colorChange ? macd_IsAbove ? blue : blue : lime
  43.  
  44. circleYPosition = outSignal
  45. long = crossover(outMacD, outSignal)
  46. short = crossunder(outMacD, outSignal)
  47.  
  48. plot(smd and outMacD ? outMacD : na, title="MACD", color=macd_color, linewidth=4)
  49. plot(smd and outSignal ? outSignal : na, title="Signal Line", color=signal_color, style=line ,linewidth=2)
  50. plot(sh and outHist ? outHist : na, title="Histogram", color=plot_color, style=histogram, linewidth=4)
  51. plot(sd and cross(outMacD, outSignal) ? circleYPosition : na, title="Cross", style=circles, linewidth=4, color=macd_color)
  52. hline(0, '0 Line', linestyle=solid, linewidth=2, color=white)
  53.  
  54. ////////////////////////////////////////////////////////////////////////////
  55. // //
  56. // INSERT SCRIPT HERE //
  57. // //
  58. ////////////////////////////////////////////////////////////////////////////
  59.  
  60. // === /END
  61.  
  62. ////////////////////////===ANION=CODE====///////////////////////////////////
  63. // //
  64. // ULTIMATE PINE INJECTOR V1.2 + rs fix //
  65. // //
  66. ////////////////////////////////////////////////////////////////////////////
  67.  
  68. // === Conditions ===
  69.  
  70. ////////////////////////////////////////////////////////////////////////////
  71.  
  72. long_entry = long //Long Or Buy Condition Here
  73.  
  74. short_entry = short //Short Or Sell Condition Here
  75.  
  76. ////////////////////////////////////////////////////////////////////////////
  77.  
  78. //Replace Only If Valid Exit Conditions
  79.  
  80. long_exit = short_entry //Close Long Condition Here (Optional)
  81.  
  82. short_exit = long_entry //Close Short Condition Here (Optional)
  83.  
  84. // === /END
  85.  
  86. ///////////////////////////////////////////////////////////////////////////
  87.  
  88. // init these values here, they will get updated later as more decisions are made
  89. last_long_close = na
  90. last_short_close = na
  91.  
  92. // === Long position detection ===
  93. // longs open
  94. longo = 0
  95. longo := nz(longo[1])
  96. // longs closed
  97. longc = 0
  98. longc := nz(longc[1])
  99. if long_entry
  100. longo := longo + 1
  101. longc := 0
  102. if long_exit
  103. longc := longc + 1
  104. longo := 0
  105. // === /END
  106.  
  107. // === Short position detection ===
  108. shorto = 0
  109. shorto := nz(shorto[1])
  110. shortc = 0
  111. shortc := nz(shortc[1])
  112. if short_entry
  113. shorto := shorto + 1
  114. shortc := 0
  115. if short_exit
  116. shortc := shortc + 1
  117. shorto := 0
  118. // === /END
  119.  
  120. // === Pyramiding Settings ===
  121. //pyr = input(1, title="Pyramiding Setting")
  122. pyr = 1
  123. longCondition = long_entry and longo <= pyr
  124. longX = long_exit and longc <= pyr
  125. shortCondition = short_entry and shorto <=pyr
  126. shortX = short_exit and shortc <=pyr
  127. // === /END
  128.  
  129. // === Get Last Position Price ===
  130. last_open_longCondition = na
  131. last_open_shortCondition = na
  132. // last open prices
  133. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  134. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  135. // === /END
  136.  
  137. // === Check For Long/Short ===
  138. last_longCondition = na
  139. last_shortCondition = na
  140. // last open times
  141. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  142. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  143. last_longClose = longX ? time : nz(last_long_close[1])
  144. last_shortClose = shortX ? time : nz(last_short_close[1])
  145.  
  146. in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
  147. in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
  148. // === /END
  149.  
  150. // === Stop Loss (Long) ===
  151. isSLl = input(false, "Stop Loss (Long)")
  152. sll = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  153. long_call_sl = last_open_longCondition * (1 - sll)
  154. long_sl = isSLl and low <= long_call_sl and longCondition == 0
  155. // === /END
  156.  
  157. // === Stop Loss (Short) ===
  158. isSLs = input(false, "Stop Loss (Short)")
  159. sls = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  160. short_call_sl = last_open_shortCondition * (1 + sls)
  161. short_sl = isSLs and high >= short_call_sl and shortCondition == 0
  162. // === /END
  163.  
  164. // === Trailing Stop ===
  165. last_high = na
  166. last_low = na
  167. last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
  168. last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
  169. isTSl = input(false, "Trailing Stop Long")
  170. tsil = input(19, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  171. tsl = input(2, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  172. long_call_ts = last_high * (1 - tsl)
  173. long_call_tsi = last_open_longCondition * (1 + tsil)
  174. long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
  175. isTSs = input(false, "Trailing Stop Short")
  176. tsis = input(19, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  177. tss = input(2, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  178. short_call_ts = last_low * (1 + tss)
  179. short_call_tsi = last_open_shortCondition * (1 - tsis)
  180. short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
  181. // === /END
  182.  
  183. // === Create Single Close For All Closing Conditions ===
  184. closelong = long_sl or long_ts or longX
  185. closeshort = short_sl or short_ts or shortX
  186.  
  187. // Get Last Close
  188. last_long_close := closelong ? time : nz(last_long_close[1])
  189. last_short_close := closeshort ? time : nz(last_short_close[1])
  190.  
  191. // Check For Close Since Last Open
  192. if closelong and last_long_close[1] > last_longCondition
  193. closelong := 0
  194.  
  195. if closeshort and last_short_close[1] > last_shortCondition
  196. closeshort := 0
  197. // === /END
  198.  
  199. ////////////////////////////////////////////////////////////////////////////
  200.  
  201. // === Alarm Settings ===
  202. alertcondition(longCondition==1, title='LONG', message='LONG')
  203. alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
  204. alertcondition(shortCondition==1, title='SHORT', message='SHORT')
  205. alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
  206. // === /END
  207.  
  208. ////////////////////////////////////////////////////////////////////////////
  209.  
  210. // === Debugs Here ===
  211. //Remove "//" To Check/Debug The Code Above
  212. // Signal Shapes
  213. //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
  214. //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
  215. //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
  216. //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
  217. // SL Plot
  218. //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
  219. //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
  220. //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
  221. // TP Plot
  222. //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
  223. //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
  224. //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
  225. // TS Plot
  226. tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
  227. tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
  228. plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
  229. plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
  230. plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
  231. plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
  232. // === /END
  233.  
  234. ////////////////////////////////////////////////////////////////////////////
  235. // //
  236. // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
  237. // //
  238. ////////////////////////////////////////////////////////////////////////////
  239.  
  240. // === Strategy Direction Switch ===
  241. direction = input(title = "Strategy Direction", defval="Both", options=["Both", "Long", "Short"])
  242. // === /END
  243.  
  244. // === Backtesting Dates ===
  245. testPeriodSwitch = input(true, "Custom Backtesting Dates")
  246. testStartYear = input(2017, "Backtest Start Year")
  247. testStartMonth = input(8, "Backtest Start Month")
  248. testStartDay = input(1, "Backtest Start Day")
  249. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  250. testStopYear = input(2018, "Backtest Stop Year")
  251. testStopMonth = input(12, "Backtest Stop Month")
  252. testStopDay = input(31, "Backtest Stop Day")
  253. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  254. testPeriod() =>
  255. time >= testPeriodStart and time <= testPeriodStop ? true : false
  256. isPeriod = testPeriodSwitch == true ? testPeriod() : true
  257. // === /END
  258.  
  259. // === Strategy ===
  260. if isPeriod and direction=="Both"
  261. if (longCondition)
  262. strategy.entry("Long",strategy.long)
  263. if (closelong) and not shortCondition
  264. strategy.close("Long")
  265. if (shortCondition)
  266. strategy.entry("Short",strategy.short)
  267. if (closeshort) and not longCondition
  268. strategy.close("Short")
  269.  
  270. if isPeriod and direction=="Long"
  271. if (longCondition)
  272. strategy.entry("Long",strategy.long)
  273. if (closelong)
  274. strategy.close("Long")
  275.  
  276. if isPeriod and direction=="Short"
  277. if (shortCondition)
  278. strategy.entry("Short",strategy.short)
  279. if (closeshort)
  280. strategy.close("Short")
  281. // === /END
  282.  
  283. ////////////////////////////////////////////////////////////////////////////
  284. // //
  285. // ULTIMATE PINE INJECTOR V1.2 //
  286. // //
  287. //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement