Advertisement
mossmanpete

Renko+Moving Average+RMI Alert R3 by JustUncleL

Feb 13th, 2018
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.34 KB | None | 0 0
  1. //@version=3
  2. // Author: JustUncleL
  3. //
  4. // Description:
  5. // This script idea is designed to be used with 10pip brick (recommened) Renko charts. It combines the Renko
  6. // price action with a directional coloured EMA (default length 6) and a RMI (instead of the usual RSI) indicator
  7. // to provide entry and exit signals. RMI is bit like RSI with a built-in momentum factor and works well with Renko.
  8. // Signals can optionally be filtered by Daily or Weekly Open, where by only trade long above open and short below
  9. // open (this option is enabled by default).
  10. // Exit occur when EMA or RMI reverses direction, or optionally (disabled by default) when the Renko prints
  11. // a brick in the reverse direction.
  12. // Each Entry and Exit signal creates an Alertcondition that can be picked up by the TradingView Alarm system.
  13. //
  14. // TIP: To get 10pip Bricks set Renko to "Traditional" type bricks and 0.001 for non-JPY currency pairs, and
  15. // 0.1 for JPY currency pairs. Also set chart Time frame to 5min or 15mins.
  16. //
  17. // References:
  18. // - TheLark Relative Momentum Index (RMI)
  19. strategy("Renko Magic", overlay=true, precision=8, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.2)
  20.  
  21.  
  22. // === INPUTS
  23.  
  24. // Medium Fast MA - type, source, length
  25. ma_type = input(defval="EMA", title="MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])
  26. ma_len = input(defval=6, title="MA - Length", minval=1)
  27. ma_src = input(close, title="MA - Source")
  28. exitfirst = input(false,title="Exit Trade on 1st Opposite Renko Brick")
  29. // inputs for RMI function
  30. rmilen = input(4, title="RMI/RSI MA Length")
  31. mom = input(5, title="Momentum Length",minval=0)
  32. ob = input(70,title="Overbought")
  33. os = input(30,title="Oversold")
  34. uRSI = input(false,title="Use RSI instead of RMI")
  35. udopen = input(true,title="Use Daily Open Line Filter")
  36. dperiod = input("1D", title="Period for Daily Open Line", options=["1D","1W","1M"])
  37. //
  38. // === /INPUTS
  39.  
  40. // Constants colours that include fully non-transparent option.
  41. green100 = #008000FF
  42. lime100 = #00FF00FF
  43. red100 = #FF0000FF
  44. blue100 = #0000FFFF
  45. aqua100 = #00FFFFFF
  46. darkred100 = #8B0000FF
  47. gray100 = #808080FF
  48.  
  49. // === FUNCTIONS
  50.  
  51. // - variant(type, src, len)
  52. // Returns MA input selection variant, default to SMA if blank or typo.
  53.  
  54. // SuperSmoother filter
  55. // © 2013 John F. Ehlers
  56. variant_supersmoother(src,len) =>
  57. a1 = exp(-1.414*3.14159 / len)
  58. b1 = 2*a1*cos(1.414*3.14159 / len)
  59. c2 = b1
  60. c3 = (-a1)*a1
  61. c1 = 1 - c2 - c3
  62. v9 = 0.0
  63. v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  64. v9
  65.  
  66. variant_smoothed(src,len) =>
  67. v5 = 0.0
  68. v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
  69. v5
  70.  
  71. variant_zerolagema(src,len) =>
  72. ema1 = ema(src, len)
  73. ema2 = ema(ema1, len)
  74. v10 = ema1+(ema1-ema2)
  75. v10
  76.  
  77. variant_doubleema(src,len) =>
  78. v2 = ema(src, len)
  79. v6 = 2 * v2 - ema(v2, len)
  80. v6
  81.  
  82. variant_tripleema(src,len) =>
  83. v2 = ema(src, len)
  84. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  85. v7
  86.  
  87. // return variant, defaults to SMA
  88. variant(type, src, len) =>
  89. type=="EMA" ? ema(src,len) :
  90. type=="WMA" ? wma(src,len):
  91. type=="VWMA" ? vwma(src,len) :
  92. type=="SMMA" ? variant_smoothed(src,len) :
  93. type=="DEMA" ? variant_doubleema(src,len):
  94. type=="TEMA" ? variant_tripleema(src,len):
  95. type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
  96. type=="SSMA" ? variant_supersmoother(src,len) :
  97. type=="ZEMA" ? variant_zerolagema(src,len) :
  98. type=="TMA" ? sma(sma(src,len),len) : sma(src,len)
  99.  
  100. // - /variant
  101. // === /FUNCTIONS
  102.  
  103.  
  104. dodgerblue = #1E90FF
  105.  
  106. // === Moving Average
  107. ma_series = variant(ma_type,ma_src,ma_len)
  108.  
  109. // Get direction based on MA
  110. direction = 0
  111. direction := rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : nz(direction[1])
  112.  
  113. // Plot MA series and color it according too direction
  114. pcol = direction>0 ? lime : direction<0 ? red : na
  115. plot(ma_series, title="MA Plot", color=pcol, linewidth=2,style=line,join=true, transp=10)
  116.  
  117. // === /Moving Average
  118.  
  119.  
  120. // === DAILY OPEN LINE
  121. //
  122. // Test for new Daily Session or start of new month for Daily.
  123. start = security(ticker, dperiod, time, barmerge.gaps_off, barmerge.lookahead_on)
  124. newDay = change(start)
  125.  
  126. // Calculate Annualised Volatility
  127. dopen = 0.0
  128. dopen_ = valuewhen(start[1]<=time, ma_src, 0)
  129. dopen := newDay ? dopen_ : nz(dopen[1],dopen_)
  130. // Plot all Days open
  131. plot(dopen, color = newDay?na:purple, title = "Daily Open",linewidth=2, transp=40)
  132.  
  133. // === /DAILY OPEN LINE
  134.  
  135. // === RMI (Relative Momentum Index)
  136. //
  137. // "... The Relative Momentum Index was developed by Roger Altman
  138. // and was introduced in his article in the February, 1993 issue of
  139. // Technical Analysis of Stocks & Commodities magazine. "
  140. // "... While RSI counts up and down days from close to close, the Relative
  141. // Momentum Index counts up and down days from the close relative to a
  142. // close x number of days ago. "
  143.  
  144. //calc
  145. up = ema(max(ma_src- ma_src[mom],0),rmilen)
  146. dn = ema(max(ma_src[mom] - ma_src,0),rmilen)
  147. //rmi =
  148. rmi = dn == 0 ? 0 : 100 - 100 / (1 + up / dn)
  149.  
  150. // Use RSI instead of RMI
  151. rmi := uRSI ? rsi(ma_src, rmilen) : rmi
  152.  
  153. // === /RMI
  154.  
  155. // === Calculate Alerts
  156. PA = (close>open) and (not udopen or close>dopen)? 1 : (close<open) and (not udopen or close<dopen)? -1 : 0
  157. HAS = direction
  158. RMI = rmi>=rmi[1] and rmi>=ob ? 1 : rmi<=rmi[1] and rmi<=os ? -1 : 0
  159.  
  160. long = PA==1 and HAS==1 and RMI==1
  161. short = PA==-1 and HAS==-1 and RMI==-1
  162.  
  163. olong = 0
  164. olong := olong[1]>0 and (HAS<0 or (not uRSI and RMI<=0) or (exitfirst and (close<open and close[1]>open[1])))? 0 : nz(olong[1])==0 and long? 1 : olong[1]>0 and HAS==1? olong[1]+1 : 0
  165. oshort = 0
  166. oshort := oshort[1]>0 and (HAS>1 or (not uRSI and RMI>=0) or (exitfirst and (close>open and close[1]<open[1])))? 0 : nz(oshort[1])==0 and short? 1 : oshort[1]>0 and HAS==-1? oshort[1]+1 : 0
  167.  
  168. // debug
  169. //plotshape(PA,location=location.bottom)
  170. //plotshape(HAS,location=location.bottom)
  171. //plotshape(RMI,location=location.bottom)
  172. //plotshape(olong,location=location.bottom)
  173. //plotshape(oshort,location=location.bottom)
  174.  
  175.  
  176. // === Upgraded Conditions Framework ===
  177.  
  178. ////////////////////////////////////////////////////////////////////////////
  179.  
  180. long_entry = olong==1 //Long Or Buy Condition Here
  181.  
  182. short_entry = oshort==1 //Short Or Sell Condition Here
  183.  
  184. long_exit = olong[1]>0 and olong==0 //Close Long Condition Here (Optional)
  185.  
  186. short_exit = oshort[1]>0 and oshort==0 //Close Short Condition Here (Optional)
  187.  
  188. ///////////////////////////////////////////////////////////////////////////
  189.  
  190. // init these values here, they will get updated later as more decisions are made
  191. last_long_close = na
  192. last_short_close = na
  193.  
  194. // === Long position detection ===
  195. // longs open
  196. longo = 0
  197. longo := nz(longo[1])
  198. // longs closed
  199. longc = 0
  200. longc := nz(longc[1])
  201. if long_entry
  202. longo := longo + 1
  203. longc := 0
  204. if long_exit
  205. longc := longc + 1
  206. longo := 0
  207. // === /END
  208.  
  209. // === Short position detection ===
  210. shorto = 0
  211. shorto := nz(shorto[1])
  212. shortc = 0
  213. shortc := nz(shortc[1])
  214. if short_entry
  215. shorto := shorto + 1
  216. shortc := 0
  217. if short_exit
  218. shortc := shortc + 1
  219. shorto := 0
  220. // === /END
  221.  
  222. // === Pyramiding Settings ===
  223. pyr = input(1, title="Pyramiding Setting")
  224. //pyr = 1
  225. longCondition = long_entry and longo <= pyr
  226. longX = long_exit and longc <= pyr
  227. shortCondition = short_entry and shorto <=pyr
  228. shortX = short_exit and shortc <=pyr
  229. // === /END
  230.  
  231. // === Get Last Position Price ===
  232. last_open_longCondition = na
  233. last_open_shortCondition = na
  234. // last open prices
  235. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  236. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  237. // === /END
  238.  
  239. // === Check For Long/Short ===
  240. last_longCondition = na
  241. last_shortCondition = na
  242. // last open times
  243. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  244. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  245. last_longClose = longX ? time : nz(last_long_close[1])
  246. last_shortClose = shortX ? time : nz(last_short_close[1])
  247.  
  248. in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
  249. in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
  250. // === /END
  251.  
  252. // === Stop Loss (Long) ===
  253. isSLl = input(false, "Stop Loss (Long)")
  254. sll = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  255. long_call_sl = last_open_longCondition * (1 - sll)
  256. long_sl = isSLl and low <= long_call_sl and longCondition == 0
  257. // === /END
  258.  
  259. // === Stop Loss (Short) ===
  260. isSLs = input(false, "Stop Loss (Short)")
  261. sls = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
  262. short_call_sl = last_open_shortCondition * (1 + sls)
  263. short_sl = isSLs and high >= short_call_sl and shortCondition == 0
  264. // === /END
  265.  
  266. // === Trailing Stop ===
  267. last_high = na
  268. last_low = na
  269. last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
  270. last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
  271. isTSl = input(false, "Trailing Stop Long")
  272. tsil = input(25, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  273. tsl = input(8, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
  274. long_call_ts = last_high * (1 - tsl)
  275. long_call_tsi = last_open_longCondition * (1 + tsil)
  276. long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
  277. isTSs = input(false, "Trailing Stop Short")
  278. tsis = input(25, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  279. tss = input(8, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
  280. short_call_ts = last_low * (1 + tss)
  281. short_call_tsi = last_open_shortCondition * (1 - tsis)
  282. short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
  283. // === /END
  284.  
  285. // === Create Single Close For All Closing Conditions ===
  286. closelong = long_sl or long_ts or longX
  287. closeshort = short_sl or short_ts or shortX
  288.  
  289. // Get Last Close
  290. last_long_close := closelong ? time : nz(last_long_close[1])
  291. last_short_close := closeshort ? time : nz(last_short_close[1])
  292.  
  293. // Check For Close Since Last Open
  294. if closelong and last_long_close[1] > last_longCondition
  295. closelong := 0
  296.  
  297. if closeshort and last_short_close[1] > last_shortCondition
  298. closeshort := 0
  299. // === /END
  300.  
  301. ////////////////////////////////////////////////////////////////////////////
  302.  
  303. // === Alarm Settings ===
  304. //alertcondition(longCondition==1, title='LONG', message='LONG')
  305. //alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
  306. //alertcondition(shortCondition==1, title='SHORT', message='SHORT')
  307. //alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
  308. // === /END
  309.  
  310. ////////////////////////////////////////////////////////////////////////////
  311.  
  312. // === Visuals & Debugs Here ===
  313. //Remove "//" To Check/Debug The Code Above
  314. // Signal Shapes
  315. //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
  316. //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
  317. //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
  318. //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
  319. // SL Plot
  320. //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
  321. //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
  322. //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
  323. // TP Plot
  324. //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
  325. //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
  326. //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
  327. // TS Plot
  328. //tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
  329. //tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
  330. //plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
  331. //plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
  332. //plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
  333. //plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
  334. // === /END
  335.  
  336. ////////////////////////////////////////////////////////////////////////////
  337. // //
  338. // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
  339. // //
  340. ////////////////////////////////////////////////////////////////////////////
  341.  
  342. // === Strategy Direction Switch ===
  343. dir = input(title = "Strategy Direction", defval="Both", options=["Both", "Long", "Short"])
  344. // === /END
  345.  
  346. // === Backtesting Dates ===
  347. testPeriodSwitch = input(true, "Custom Backtesting Dates")
  348. testStartYear = input(2017, "Backtest Start Year")
  349. testStartMonth = input(1, "Backtest Start Month")
  350. testStartDay = input(1, "Backtest Start Day")
  351. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  352. testStopYear = input(9999, "Backtest Stop Year")
  353. testStopMonth = input(1, "Backtest Stop Month")
  354. testStopDay = input(1, "Backtest Stop Day")
  355. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  356. testPeriod() =>
  357. time >= testPeriodStart and time <= testPeriodStop ? true : false
  358. isPeriod = testPeriodSwitch == true ? testPeriod() : true
  359. // === /END
  360.  
  361. // === Strategy ===
  362. if isPeriod and dir=="Both"
  363. if (longCondition)
  364. strategy.entry("Long",strategy.long)
  365. if (closelong) and not shortCondition
  366. strategy.close("Long")
  367. if (shortCondition)
  368. strategy.entry("Short",strategy.short)
  369. if (closeshort) and not longCondition
  370. strategy.close("Short")
  371.  
  372. if isPeriod and dir=="Long"
  373. if (longCondition)
  374. strategy.entry("Long",strategy.long)
  375. if (closelong)
  376. strategy.close("Long")
  377.  
  378. if isPeriod and dir=="Short"
  379. if (shortCondition)
  380. strategy.entry("Short",strategy.short)
  381. if (closeshort)
  382. strategy.close("Short")
  383. // === /END
  384.  
  385. ////////////////////////////////////////////////////////////////////////////
  386. // //
  387. // ULTIMATE PINE INJECTOR V1.2 //
  388. // //
  389. //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement