xmd79

MANNU MATRIX

Oct 7th, 2021
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.93 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © MANNU
  3. //@version=4
  4.  
  5. study(title="MANNU MATRIX", shorttitle="MANNU MATRIX", overlay=true)
  6. showpivot = input(defval = false, title="Show CPR")
  7. showVWAP = input(title="Show VWAP", type=input.bool, defval=false)
  8. showEMA20 = input(title="Show EMA-20", type=input.bool, defval=false)
  9. showEMA50 = input(title="Show EMA-50", type=input.bool, defval=false)
  10. showEMA200 = input(title="Show EMA-200", type=input.bool, defval=false)
  11. showSMA50 = input(title="Show SMA-50", type=input.bool, defval=false)
  12. showSMA200 = input(title="Show SMA-200", type=input.bool, defval=false)
  13. showBB = input(title="Show BB", type=input.bool, defval=false)
  14.  
  15. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. //////////////////////////////////////////////// EMA - 5/20/50/100/200 ///////////////////////////////////////////////
  17. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18.  
  19. EMA20_Len = input(20, minval=1, title="EMA20_Length")
  20. //EMA20_src = input(close, title="EMA20_Source")
  21. //EMA20_offset = input(title="EMA20_Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  22. EMA20_out = ema(close, EMA20_Len)
  23. plot(showEMA20 ? EMA20_out:na, title="EMA20", color=color.red, offset=0,linewidth=2)
  24.  
  25. EMA50_Len = input(50, minval=1, title="EMA50_Length")
  26. //EMA50_src = input(close, title="EMA50_Source")
  27. //EMA50_offset = input(title="EMA50_Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  28. EMA50_out = ema(close, EMA50_Len)
  29. plot(showEMA50 ? EMA50_out:na, title="EMA50", color=color.blue, offset=0,linewidth=2)
  30.  
  31. EMA200_Len = input(200, minval=1, title="EMA200_Length")
  32. //EMA200_src = input(close, title="EMA200_Source")
  33. //EMA200_offset = input(title="EMA200_Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  34. EMA200_out = ema(close, EMA200_Len)
  35. plot(showEMA200 ? EMA200_out:na, title="EMA200", color=color.yellow, offset=0,linewidth=2)
  36.  
  37. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. //////////////////////////////////////////////// SMA - 9/20/50/100/200 ///////////////////////////////////////////////
  39. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40.  
  41. SMA50_Len = input(50, minval=1, title="SMA50_Length")
  42. //SMA50_src = input(close, title="SMA50_Source")
  43. //SMA50_offset = input(title="SMA50_Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  44. SMA50_out = sma(close, SMA50_Len)
  45. plot(showSMA50 ? SMA50_out:na, title="SMA50", color=color.maroon, offset=0,linewidth=2)
  46.  
  47. SMA200_Len = input(200, minval=1, title="SMA200_Length")
  48. //SMA200_src = input(close, title="SMA200_Source")
  49. //SMA200_offset = input(title="SMA200_Offset", type=input.integer, defval=0, minval=-500, maxval=500)
  50. SMA200_out = sma(close, SMA200_Len)
  51. plot(showSMA200 ? SMA200_out:na, title="SMA200", color=color.aqua, offset=0,linewidth=2)
  52.  
  53. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. /////////////////////////////////////////////////// VWAP /////////////////////////////////////////////////////////
  55. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. //vwaplength = input(title="VWAP Length", type=input.integer, defval=1)
  57. //cvwap = ema(vwap,vwaplength)
  58. cvwap1 = vwap(hlc3)
  59. plotvwap = plot(showVWAP ? cvwap1 : na,title="VWAP",color=color.black, transp=0, linewidth=2)
  60.  
  61.  
  62. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. ///////////////////////////////////// CENTRAL PIVOT RANGE (CPR) //////////////////////////////////////////////////
  64. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. res = input(title="Resolution", type=input.resolution, defval="D")
  66. res1 = input(title="Resolution", type=input.resolution, defval="W")
  67. res2 = input(title="Resolution", type=input.resolution, defval="M")
  68. H = security(syminfo.tickerid, res, high[1], barmerge.gaps_off, barmerge.lookahead_on)
  69. L = security(syminfo.tickerid, res, low[1], barmerge.gaps_off, barmerge.lookahead_on)
  70. C = security(syminfo.tickerid, res, close[1], barmerge.gaps_off, barmerge.lookahead_on)
  71. WH = security(syminfo.tickerid, res1, high[1], barmerge.gaps_off, barmerge.lookahead_on)
  72. WL = security(syminfo.tickerid, res1, low[1], barmerge.gaps_off, barmerge.lookahead_on)
  73. WC = security(syminfo.tickerid, res1, close[1], barmerge.gaps_off, barmerge.lookahead_on)
  74. MH = security(syminfo.tickerid, res2, high[1], barmerge.gaps_off, barmerge.lookahead_on)
  75. ML = security(syminfo.tickerid, res2, low[1], barmerge.gaps_off, barmerge.lookahead_on)
  76. MC = security(syminfo.tickerid, res2, close[1], barmerge.gaps_off, barmerge.lookahead_on)
  77. TTH = security(syminfo.tickerid, res, high, barmerge.gaps_off, barmerge.lookahead_on)
  78. TTL = security(syminfo.tickerid, res, low, barmerge.gaps_off, barmerge.lookahead_on)
  79. TTC = security(syminfo.tickerid, res, close, barmerge.gaps_off, barmerge.lookahead_on)
  80.  
  81. PP = (H + L + C) / 3
  82. TC = (H + L)/2
  83. BC = (PP - TC) + PP
  84. R1 = (PP * 2) - L
  85. R2 = PP + (H - L)
  86. R3 = H + 2*(PP - L)
  87. R4 = PP * 3 + (H - 3 * L)
  88. S1 = (PP * 2) - H
  89. S2 = PP - (H - L)
  90. S3 = L - 2*(H - PP)
  91. S4 = PP*3 - (3*H - L)
  92. WPP = (WH + WL + WC)/3
  93. WTC = (WH + WL)/2
  94. WBC = (WPP - WTC) + WPP
  95. MPP = (MH + ML + MC)/3
  96. MTC = (MH + ML)/2
  97. MBC = (MPP - MTC) + MPP
  98. TOPP = (TTH + TTL + TTC)/3
  99. TOTC = (TTH + TTL)/2
  100. TOBC = (TOPP - TOTC) + TOPP
  101. TR1 = (TOPP * 2) - TTL
  102. TR2 = TOPP + (TTH - TTL)
  103. TS1 = (TOPP * 2) - TTH
  104. TS2 = TOPP - (TTH - TTL)
  105. plot(showpivot ? PP : na, title="PP", style=plot.style_stepline, color=color.blue, linewidth=2)
  106. plot(showpivot ? TC : na, title="TC", style=plot.style_circles, color=color.blue, linewidth=1)
  107. plot(showpivot ? BC : na, title="BC", style=plot.style_circles, color=color.blue, linewidth=1)
  108.  
  109. plot(showpivot ? WPP : na, title="WPP", style=plot.style_stepline, color=color.black, linewidth=2)
  110. plot(showpivot ? WTC : na, title="WTC", style=plot.style_stepline, color=color.black, linewidth=2)
  111. plot(showpivot ? WBC : na, title="WBC", style=plot.style_stepline, color=color.black, linewidth=2)
  112.  
  113. plot(showpivot ? MPP : na, title="MPP", style=plot.style_stepline, color=color.black, linewidth=2)
  114. plot(showpivot ? MTC : na, title="MTC", style=plot.style_stepline, color=color.black, linewidth=2)
  115. plot(showpivot ? MBC : na, title="MBC", style=plot.style_stepline, color=color.black, linewidth=2)
  116.  
  117. plot(showpivot ? S1 : na, title="S1", style=plot.style_stepline, color=color.green, linewidth=1)
  118. plot(showpivot ? S2 : na, title="S2", style=plot.style_stepline, color=color.green, linewidth=1)
  119. plot(showpivot ? S3 : na, title="S3", style=plot.style_stepline, color=color.green, linewidth=1)
  120. plot(showpivot ? S4 : na, title="S4", style=plot.style_stepline, color=color.green, linewidth=1)
  121.  
  122. plot(showpivot ? R1 : na, title="R1", style=plot.style_stepline, color=color.red, linewidth=1)
  123. plot(showpivot ? R2 : na, title="R2", style=plot.style_stepline, color=color.red, linewidth=1)
  124. plot(showpivot ? R3 : na, title="R3", style=plot.style_stepline, color=color.red, linewidth=1)
  125. plot(showpivot ? R4 : na, title="R4", style=plot.style_stepline, color=color.red, linewidth=1)
  126.  
  127. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. /////////////////////////////////////////////////// PREVIOUS DAY /////////////////////////////////////////////////
  129. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  130. prevDayHigh = security(syminfo.tickerid, 'D', high[1], lookahead=true)
  131. prevDayLow = security(syminfo.tickerid, 'D', low[1], lookahead=true)
  132. plot( showpivot and prevDayHigh ? prevDayHigh : na, title="Prev Day High", style=plot.style_circles, linewidth=2, color=color.red, transp=0)
  133. plot( showpivot and prevDayLow ? prevDayLow : na, title="Prev Day Low", style=plot.style_circles, linewidth=2, color=color.green, transp=0)
  134.  
  135. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  136. ///////////////////////////////////////////////// PREVIOUS WEEKLY ///////////////////////////////////////////////
  137. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  138. prevWeekHigh = security(syminfo.tickerid, 'W', high[1], lookahead=true)
  139. prevWeekLow = security(syminfo.tickerid, 'W', low[1], lookahead=true)
  140. plot( showpivot and prevWeekHigh ? prevWeekHigh : na, title="Prev Week High", style=plot.style_stepline, linewidth=2, color=color.red, transp=0)
  141. plot( showpivot and prevWeekLow ? prevWeekLow : na, title="Prev Week Low", style=plot.style_stepline, linewidth=2, color=color.green, transp=0)
  142.  
  143. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144. ///////////////////////////////////////////////// PREVIOUS MONTH /////////////////////////////////////////////////
  145. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  146. prevMonthHigh = security(syminfo.tickerid, 'M', high[1], lookahead=true)
  147. prevMonthLow = security(syminfo.tickerid, 'M', low[1], lookahead=true)
  148. plot( showpivot and prevMonthHigh ? prevMonthHigh : na, title="Prev Month High", style=plot.style_stepline, linewidth=2, color=color.red, transp=0)
  149. plot( showpivot and prevMonthLow ? prevMonthLow : na, title="Prev Month Low", style=plot.style_stepline, linewidth=2, color=color.green, transp=0)
  150.  
  151.  
  152.  
  153. //HAlftrend
  154.  
  155. amplitude = input(title="Amplitude", defval=3)
  156. channelDeviation = input(title="Channel Deviation", defval=2)
  157. showArrows = input(title="Show Arrows", defval=true)
  158. showChannels = input(title="Show Channels", defval=false)
  159.  
  160. var int trend1 = 0
  161. var int nextTrend = 0
  162. var float maxLowPrice = nz(low[1], low)
  163. var float minHighPrice = nz(high[1], high)
  164.  
  165. var float up = 0.0
  166. var float down = 0.0
  167. float atrHigh = 0.0
  168. float atrLow = 0.0
  169. float arrowUp = na
  170. float arrowDown = na
  171.  
  172. atr2 = atr(100) / 2
  173. dev1 = channelDeviation * atr2
  174.  
  175. highPrice = high[abs(highestbars(amplitude))]
  176. lowPrice = low[abs(lowestbars(amplitude))]
  177. highma = sma(high, amplitude)
  178. lowma = sma(low, amplitude)
  179.  
  180. if nextTrend == 1
  181. maxLowPrice := max(lowPrice, maxLowPrice)
  182.  
  183. if highma < maxLowPrice and close < nz(low[1], low)
  184. trend1 := 1
  185. nextTrend := 0
  186. minHighPrice := highPrice
  187. else
  188. minHighPrice := min(highPrice, minHighPrice)
  189.  
  190. if lowma > minHighPrice and close > nz(high[1], high)
  191. trend1 := 0
  192. nextTrend := 1
  193. maxLowPrice := lowPrice
  194.  
  195. if trend1 == 0
  196. if not na(trend1[1]) and trend1[1] != 0
  197. up := na(down[1]) ? down : down[1]
  198. arrowUp := up - atr2
  199. else
  200. up := na(up[1]) ? maxLowPrice : max(maxLowPrice, up[1])
  201. atrHigh := up + dev1
  202. atrLow := up - dev1
  203. else
  204. if not na(trend1[1]) and trend1[1] != 1
  205. down := na(up[1]) ? up : up[1]
  206. arrowDown := down + atr2
  207. else
  208. down := na(down[1]) ? minHighPrice : min(minHighPrice, down[1])
  209. atrHigh := down + dev1
  210. atrLow := down - dev1
  211.  
  212. ht = trend1 == 0 ? up : down
  213.  
  214. var color buyColor = color.green
  215. var color sellColor = color.red
  216.  
  217. htColor = trend1 == 0 ? buyColor : sellColor
  218. htPlot = plot(ht, title="HalfTrend", linewidth=2, color=htColor)
  219.  
  220. //atrHighPlot = plot(showChannels ? atrHigh : na, title="ATR High", style=plot.style_circles, color=sellColor)
  221. //atrLowPlot = plot(showChannels ? atrLow : na, title="ATR Low", style=plot.style_circles, color=buyColor)
  222.  
  223. //fill(htPlot, atrHighPlot, title="ATR High Ribbon", color=sellColor)
  224. //fill(htPlot, atrLowPlot, title="ATR Low Ribbon", color=buyColor)
  225.  
  226. buySignal = not na(arrowUp) and (trend1 == 0 and trend1[1] == 1)
  227. sellSignal = not na(arrowDown) and (trend1 == 1 and trend1[1] == 0)
  228.  
  229. plotshape(showArrows and buySignal ? atrLow : na, title="Arrow Up" ,text="Buy" , style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, location=location.absolute, size=size.small,color=buyColor)
  230. //plotshape(showArrows and sellSignal ? atrHigh : na, title="Arrow Down", style=shape.triangledown, location=location.absolute, size=size.tiny, color=sellColor)
  231.  
  232. plotshape(showArrows and sellSignal ? atrHigh : na, title="Arrow Down",text="Sell", size=size.tiny, color=color.red, textcolor=color.white,style=shape.labeldown , location=location.absolute, size=size.small, color=sellColor)
  233. alertcondition(buySignal, title="Alert: Buy", message="Buy")
  234. alertcondition(sellSignal, title="Alert: Sell", message="Sell")
  235.  
  236. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  237. ///////////////////////////////////////////////// BOLLINGER BANDS/////////////////////////////////////////////////
  238. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  239.  
  240. length1 = 20 //input(20, minval=1)
  241. src1 = close //input(close, title="BB_Source")
  242. mult = 2 //input(2.0, minval=0.001, maxval=50, title="BB_StdDev")
  243. basis = sma(src1, length1)
  244. dev = mult * stdev(src1, length1)
  245. upper = basis + dev
  246. lower = basis - dev
  247. p1 = plot(showBB ? upper : na, "Upper", color=color.red, editable=true)
  248. p2 = plot(showBB ? lower: na, "Lower", color=color.green, editable=true)
  249. plot(showBB ? basis : na, "Basis", color=#872323, editable=true)
  250. fill(p1, p2, title = "Background", color=#198787, transp=95)
  251.  
  252. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  253. ///////////////////////////////////////////////// FIB LEVELS /////////////////////////////////////////////////////
  254. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  255.  
  256.  
  257. //Fib levels
  258.  
  259.  
  260. //------------------------------------------------------------------------------
  261. extendType =input(true, title="Extend lines?") ? extend.right : extend.none
  262. show_price =input(true, title="show price?")
  263. Reverse =input(false, title="Reverse Direction?")
  264. //------------------------------------------------------------------------------
  265.  
  266. FPeriod = input(100, title="lookback length")
  267. Fhigh = highest(FPeriod)
  268. Flow = lowest(FPeriod)
  269. SwingHigh = highestbars(high, FPeriod)
  270. SwingLow = lowestbars(low, FPeriod)
  271. downfib = Reverse ? SwingHigh > SwingLow : SwingHigh < SwingLow
  272.  
  273. X1_offset = downfib ? SwingHigh : SwingLow
  274. X2_offset =15
  275.  
  276. bar_index_duration = time - time[1] // time between bars
  277.  
  278. X1 = X1_offset * bar_index_duration // starting level x position
  279. X2 = X2_offset * bar_index_duration // ending level x position
  280. lbl_X2 = ((show_price ? 15 : 6) + X2_offset) * bar_index_duration // label x position
  281.  
  282. F000 =downfib ? Flow : Fhigh
  283. F236 =downfib ? (Fhigh-Flow) * 0.236 + Flow : Fhigh - (Fhigh-Flow) * 0.236
  284. F382 =downfib ? (Fhigh-Flow) * 0.382 + Flow : Fhigh - (Fhigh-Flow) * 0.382
  285. F500 =downfib ? (Fhigh-Flow) * 0.500 + Flow : Fhigh - (Fhigh-Flow) * 0.500
  286. F618 =downfib ? (Fhigh-Flow) * 0.618 + Flow : Fhigh - (Fhigh-Flow) * 0.618
  287. F786 =downfib ? (Fhigh-Flow) * 0.786 + Flow : Fhigh - (Fhigh-Flow) * 0.786
  288. F100 =downfib ? Fhigh : Flow
  289.  
  290. F000_color =color.new(color.orange,0)
  291. F236_color =color.new(color.orange,0)
  292. F382_color =color.new(color.orange,0)
  293. F500_color =color.new(color.orange,0)
  294. F618_color =color.new(color.orange,0)
  295. F786_color =color.new(color.orange,0)
  296. F100_color =color.new(color.orange,0)
  297.  
  298. F000_lbl =show_price ? "0% [" + tostring(F000) + " ]" : "0%"
  299. F236_lbl =show_price ? "23.6% [" + tostring(F236) + " ]" : "23.6%"
  300. F382_lbl =show_price ? "38.2% [" + tostring(F382) + " ]" : "38.2%"
  301. F500_lbl =show_price ? "50% [" + tostring(F500) + " ]" : "50%"
  302. F618_lbl =show_price ? "61.8% [" + tostring(F618) + " ]" : "61.8%"
  303. F786_lbl =show_price ? "78.6% [" + tostring(F786) + " ]" : "78.6%"
  304. F100_lbl =show_price ? "100% [" + tostring(F100) + " ]" : "100%"
  305.  
  306. //------------------------------------------------------------------------------
  307. //Setup line & Label Appearance
  308.  
  309. line_style =line.style_solid
  310. label_style =label.style_none
  311. label_size =size.normal
  312.  
  313. //------------------------------------------------------------------------------
  314. // 0 line
  315. if true
  316. ln = line.new(
  317. x1 = time + X1,
  318. y1 = F000,
  319. x2 = time + X2,
  320. y2 = F000,
  321. xloc =xloc.bar_time,
  322. extend =extendType,
  323. color =F000_color,
  324. style =line_style,
  325. width =1
  326. )
  327. line.delete(ln[1])
  328. if true
  329. lbl = label.new(
  330. x =time + lbl_X2,
  331. y =F000,
  332. xloc =xloc.bar_time,
  333. yloc =yloc.price,
  334. text =F000_lbl,
  335. color =F000_color,
  336. textcolor =F000_color,
  337. style =label_style,
  338. size =label_size
  339. )
  340. label.delete(lbl[1])
  341.  
  342. //------------------------------------------------------------------------------
  343. //236 line
  344. if true
  345. ln = line.new(
  346. x1 = time + X1,
  347. y1 = F236,
  348. x2 = time + X2,
  349. y2 = F236,
  350. xloc =xloc.bar_time,
  351. extend =extendType,
  352. color =F236_color,
  353. style =line_style,
  354. width =1
  355. )
  356. line.delete(ln[1])
  357. if true
  358. lbl = label.new(
  359. x =time + lbl_X2,
  360. y =F236,
  361. xloc =xloc.bar_time,
  362. yloc =yloc.price,
  363. text =F236_lbl,
  364. color =F236_color,
  365. textcolor =F236_color,
  366. style =label_style,
  367. size =label_size
  368. )
  369. label.delete(lbl[1])
  370.  
  371. //------------------------------------------------------------------------------
  372. // 382 line
  373. if true
  374. ln = line.new(
  375. x1 = time + X1,
  376. y1 = F382,
  377. x2 = time + X2,
  378. y2 = F382,
  379. xloc =xloc.bar_time,
  380. extend =extendType,
  381. color =F382_color,
  382. style =line_style,
  383. width =1
  384. )
  385. line.delete(ln[1])
  386. if true
  387. lbl = label.new(
  388. x =time + lbl_X2,
  389. y =F382,
  390. xloc =xloc.bar_time,
  391. yloc =yloc.price,
  392. text =F382_lbl,
  393. color =F382_color,
  394. textcolor =F382_color,
  395. style =label_style,
  396. size =label_size
  397. )
  398. label.delete(lbl[1])
  399. //------------------------------------------------------------------------------
  400. //500 line
  401. if true
  402. ln = line.new(
  403. x1 = time + X1,
  404. y1 = F500,
  405. x2 = time + X2,
  406. y2 = F500,
  407. xloc =xloc.bar_time,
  408. extend =extendType,
  409. color =F500_color,
  410. style =line_style,
  411. width =1
  412. )
  413. line.delete(ln[1])
  414. if true
  415. lbl = label.new(
  416. x =time + lbl_X2,
  417. y =F500,
  418. xloc =xloc.bar_time,
  419. yloc =yloc.price,
  420. text =F500_lbl,
  421. color =F500_color,
  422. textcolor =F500_color,
  423. style =label_style,
  424. size =label_size
  425. )
  426. label.delete(lbl[1])
  427. //------------------------------------------------------------------------------
  428. //618 line
  429. if true
  430. ln = line.new(
  431. x1 = time + X1,
  432. y1 = F618,
  433. x2 = time + X2,
  434. y2 = F618,
  435. xloc =xloc.bar_time,
  436. extend =extendType,
  437. color =F618_color,
  438. style =line_style,
  439. width =1
  440. )
  441. line.delete(ln[1])
  442. if true
  443. lbl = label.new(
  444. x =time + lbl_X2,
  445. y =F618,
  446. xloc =xloc.bar_time,
  447. yloc =yloc.price,
  448. text =F618_lbl,
  449. color =F618_color,
  450. textcolor =F618_color,
  451. style =label_style,
  452. size =label_size
  453. )
  454. label.delete(lbl[1])
  455. //------------------------------------------------------------------------------
  456.  
  457. //786 line
  458. if true
  459. ln = line.new(
  460. x1 = time + X1,
  461. y1 = F786,
  462. x2 = time + X2,
  463. y2 = F786,
  464. xloc =xloc.bar_time,
  465. extend =extendType,
  466. color =F786_color,
  467. style =line_style,
  468. width =1
  469. )
  470. line.delete(ln[1])
  471. if true
  472. lbl = label.new(
  473. x =time + lbl_X2,
  474. y =F786,
  475. xloc =xloc.bar_time,
  476. yloc =yloc.price,
  477. text =F786_lbl,
  478. color =F786_color,
  479. textcolor =F786_color,
  480. style =label_style,
  481. size =label_size
  482. )
  483. label.delete(lbl[1])
  484. //------------------------------------------------------------------------------
  485. //100 line
  486. if true
  487. ln = line.new(
  488. x1 = time + X1,
  489. y1 = F100,
  490. x2 = time + X2,
  491. y2 = F100,
  492. xloc =xloc.bar_time,
  493. extend =extendType,
  494. color =F100_color,
  495. style =line_style,
  496. width =1
  497. )
  498. line.delete(ln[1])
  499. if true
  500. lbl = label.new(
  501. x =time + lbl_X2,
  502. y =F100,
  503. xloc =xloc.bar_time,
  504. yloc =yloc.price,
  505. text =F100_lbl,
  506. color =F100_color,
  507. textcolor =F100_color,
  508. style =label_style,
  509. size =label_size
  510. )
  511. label.delete(lbl[1])
  512.  
  513. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  514. ///////////////////////////////////////////////// Multiple HMA////////////////////////////////////////////////////
  515. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  516.  
  517.  
  518. //inputs
  519. src=close
  520. useCurrentRes = input(true, title="Use Current Chart Resolution?")
  521. resCustom = input(title="Use Different Timeframe? Uncheck Box Above", type=input.resolution, defval="D")
  522. len = input(33, title="Moving Average Length - LookBack Period")
  523. //periodT3 = input(defval=7, title="Tilson T3 Period", minval=1)
  524. factorT3 = input(defval=7, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
  525. atype = input(4,minval=1,maxval=8,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA, 8=Tilson T3")
  526. spc=input(false, title="Show Price Crossing 1st Mov Avg - Highlight Bar?")
  527. cc = input(true,title="Change Color Based On Direction?")
  528. smoothe = input(2, minval=1, maxval=10, title="Color Smoothing - Setting 1 = No Smoothing")
  529. doma2 = input(false, title="Optional 2nd Moving Average")
  530. spc2=input(false, title="Show Price Crossing 2nd Mov Avg?")
  531. len2 = input(50, title="Moving Average Length - Optional 2nd MA")
  532. sfactorT3 = input(defval=7, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
  533. atype2 = input(1,minval=1,maxval=8,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA, 8=Tilson T3")
  534. cc2 = input(true,title="Change Color Based On Direction 2nd MA?")
  535. warn = input(false, title="***You Can Turn On The Show Dots Parameter Below Without Plotting 2nd MA to See Crosses***")
  536. warn2 = input(false, title="***If Using Cross Feature W/O Plotting 2ndMA - Make Sure 2ndMA Parameters are Set Correctly***")
  537. sd = input(false, title="Show Dots on Cross of Both MA's")
  538.  
  539. res_MHMA = useCurrentRes ? timeframe.period : resCustom
  540. //hull ma definition
  541. hullma = wma(2*wma(src, len/2)-wma(src, len), round(sqrt(len)))
  542. //TEMA definition
  543. ema1 = ema(src, len)
  544. ema2 = ema(ema1, len)
  545. ema3 = ema(ema2, len)
  546. tema = 3 * (ema1 - ema2) + ema3
  547.  
  548. //Tilson T3
  549. factor = factorT3 *.10
  550. gd(src, len, factor) => ema(src, len) * (1 + factor) - ema(ema(src, len), len) * factor
  551. t3(src, len, factor) => gd(gd(gd(src, len, factor), len, factor), len, factor)
  552. tilT3 = t3(src, len, factor)
  553.  
  554.  
  555. avg = atype == 1 ? sma(src,len) : atype == 2 ? ema(src,len) : atype == 3 ? wma(src,len) : atype == 4 ? hullma : atype == 5 ? vwma(src, len) : atype == 6 ? rma(src,len) : atype == 7 ? 3 * (ema1 - ema2) + ema3 : tilT3
  556. //2nd Ma - hull ma definition
  557. hullma2 = wma(2*wma(src, len2/2)-wma(src, len2), round(sqrt(len2)))
  558. //2nd MA TEMA definition
  559. sema1 = ema(src, len2)
  560. sema2 = ema(sema1, len2)
  561. sema3 = ema(sema2, len2)
  562. stema = 3 * (sema1 - sema2) + sema3
  563.  
  564. //2nd MA Tilson T3
  565. sfactor = sfactorT3 *.10
  566. sgd(src, len2, sfactor) => ema(src, len2) * (1 + sfactor) - ema(ema(src, len2), len2) * sfactor
  567. st3(src, len2, sfactor) => sgd(sgd(gd(src, len2, sfactor), len2, sfactor), len2, sfactor)
  568. stilT3 = st3(src, len2, sfactor)
  569.  
  570. avg2 = atype2 == 1 ? sma(src,len2) : atype2 == 2 ? ema(src,len2) : atype2 == 3 ? wma(src,len2) : atype2 == 4 ? hullma2 : atype2 == 5 ? vwma(src, len2) : atype2 == 6 ? rma(src,len2) : atype2 == 7 ? 3 * (ema1 - ema2) + ema3 : stilT3
  571.  
  572. out = avg
  573. out_two = avg2
  574.  
  575. out1 = security(syminfo.tickerid, res_MHMA, out)
  576. out2 = security(syminfo.tickerid, res_MHMA, out_two)
  577.  
  578. //Formula for Price Crossing Moving Average #1
  579. cr_up = open < out1 and close > out1
  580. cr_Down = open > out1 and close < out1
  581. //Formula for Price Crossing Moving Average #2
  582. cr_up2 = open < out2 and close > out2
  583. cr_Down2 = open > out2 and close < out2
  584.  
  585. ma_up = out1 >= out1[smoothe]
  586. ma_down = out1 < out1[smoothe]
  587.  
  588. col = cc ? ma_up ? color.lime : ma_down ? color.red : color.aqua : color.aqua
  589. col2 = cc2 ? ma_up ? color.lime : ma_down ? color.red : color.aqua : color.white
  590.  
  591. circleYPosition = out2
  592.  
  593. plot(out1, title="Multi-Timeframe Moving Avg", style=plot.style_line, linewidth=2, color = col)
  594. plot(doma2 and out2 ? out2 : na, title="2nd Multi-TimeFrame Moving Average", style=plot.style_circles, linewidth=2, color=col2)
  595.  
  596. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  597. ///////////////////////////////////////////////// YGS Scalper/////////////////////////////////////////////////////
  598. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  599.  
  600. ShowPAC = input(true, title="Show wave")
  601. ShowTrendIndi = input(true, title="Show Trend Indicator")
  602. PACLen = input(50,minval=2,title="EMA ")
  603. src_YGS = input(close,title="Source for Wave centre EMA")
  604.  
  605. // --- CONSTANTS ---
  606. DodgerBlue = #1E90FF
  607.  
  608. // === /INPUTS ===
  609. // Constants colours that include fully non-transparent option.
  610. lime100 = #00FF00FF
  611. blue100 = #0000FFFF
  612. aqua100 = #00FFFFFF
  613. darkred100 = #8B0000FF
  614.  
  615. // === SERIES SETUP ===
  616.  
  617. // Price action channel (Wave)
  618.  
  619. pacLo = ema(low, PACLen)
  620. pacHi = ema(high, PACLen)
  621.  
  622.  
  623. // === /SERIES ===
  624.  
  625. // === PLOTTING ===
  626. //
  627. // If selected, Plot the Wave Channel based on EMA high,low and close
  628. L1=plot(ShowPAC ?pacLo:na, color=color.black, linewidth=1, title="up",transp=20)
  629. H1=plot(ShowPAC ?pacHi:na, color=color.black, linewidth=1, title="down",transp=20)
  630.  
  631. fill(L1,H1, color=color.gray,transp=92,title="Fill Channel")
  632.  
  633. // Show trend direction indication on the bottom
  634. wcolor = high>pacHi and low>pacHi? color.lime : low<pacLo and high<pacLo ? color.red : color.gray
  635. plotshape(ShowTrendIndi?src_YGS:na, color=wcolor,location=location.bottom,style=shape.square,size=size.normal,transp=20,title="Trend Direction")
  636.  
  637. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  638. ////////////////////////////////////////////////////Trend Strength//////////////////////////////////////////////////////
  639. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  640.  
  641. //Trend Strength
  642.  
  643. t = input(13 , "Length")
  644. p = input(233 , "Plotting Length")
  645. d = input(false, "On Balance Volume Momentum Acceleration (on daily)")
  646.  
  647. f_speedy(_d, _t) =>
  648. v = sma(change(_d, _t) / _t, 3)
  649. a = change(v, _t) / _t
  650. v - a
  651.  
  652. source = close
  653. pvr = d and nz(volume) and timeframe.isdaily ? sma(source, t) / sma(volume, t) : na
  654. psgval = f_speedy(source, t)
  655.  
  656. plotarrow(psgval, title="Momentum Acceleration Of Price", colorup=color.aqua, colordown=color.orange, transp=80, show_last = p)
  657.  
  658. plot(nz(volume) * pvr * .041, color=open > close ? color.red : color.green, style=plot.style_columns, title="Volume Histogram", transp=35, show_last = p)
  659. plot(f_speedy(obv,t) * pvr * .34, color=color.aqua, title="Momentum Acceleration of OBV", show_last = p)
  660.  
  661. // -Alerts ══════════════════════════════════════════════════════════════════════════════════════ //
  662. bothAlertCondition = cross(psgval, 0)
  663. alertcondition(bothAlertCondition , "Early Warning" , "SpeedyGonzales : Probable Trade Opportunity\n{{exchange}}:{{ticker}}->\nPrice = {{close}},\nTime = {{time}}")
  664. alertcondition(bothAlertCondition[1] , "Trading Opportunity" , "SpeedyGonzales : Probable Trade Opportunity\n{{exchange}}:{{ticker}}->\nPrice = {{close}},\nTime = {{time}}")
  665.  
  666. longAlertCondition = crossover(psgval, 0)
  667. alertcondition(longAlertCondition , "Long : Early Warning" , "SpeedyGonzales - Not Confirmed Probable Long Trade Opportunity\n{{exchange}}:{{ticker}}->\nPrice = {{close}},\nTime = {{time}}")
  668. alertcondition(longAlertCondition[1] , "Long : Trading Opportunity" , "SpeedyGonzales - Probable Long Trade Opportunity\n{{exchange}}:{{ticker}}->\nPrice = {{close}},\nTime = {{time}}")
  669.  
  670. shortAlertCondition = crossunder(psgval, 0)
  671. alertcondition(shortAlertCondition , "Short : Early Warning" , "SpeedyGonzales - Not Confirmed Probable Short Trade Opportunity\n{{exchange}}:{{ticker}}->\nPrice = {{close}},\nTime = {{time}}")
  672. alertcondition(shortAlertCondition[1], "Short : Trading Opportunity", "SpeedyGonzales - Probable Short Trade Opportunity\n{{exchange}}:{{ticker}}->\nPrice = {{close}},\nTime = {{time}}")
  673.  
  674. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  675. //# * ══════════════════════════════════════════════════════════════════════════════════════════════
  676. //# *
  677. //# * Purpose : Ability to optimize a study and observe trade simulation statistics accordingly
  678. //# * ══════════════════════════════════════════════════════════════════════════════════════════════
  679. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  680.  
  681. // -Inputs ══════════════════════════════════════════════════════════════════════════════════════════ //
  682.  
  683. isBackTest = input(false, "Backtest On/Off" , group = "Backtest Framework")
  684. dasCapital = input(1000., "Initial Capital" , inline = "BT1" , group = "Backtest Framework")
  685. lenBckTst = input(1, "Period (Year)", minval=0, step = .1 , inline = "BT1" , group = "Backtest Framework")
  686. isStopLoss = input(false, "Apply Stop Loss, with Stop Loss Set To %" , inline = "BT2" , group = "Backtest Framework")
  687. stopLoss = input(1., "", step=.1, minval = 0 , inline = "BT2" , group = "Backtest Framework") / 100
  688. isBull = input(false, "Long : Candle Direction as Confirmation : Short" , inline = "BT3" , group = "Backtest Framework")
  689. isBear = input(false, "" , inline = "BT3" , group = "Backtest Framework")
  690. isSudden = input(true, "Avoid Sudden Price Changes" , group = "Backtest Framework")
  691. isTest = input(false, "❗❗❗ Simulate Trade on Next Bar : Only For Test Purpose (REPAINTS)", group = "Backtest Framework")
  692. lblInOutSL = input(true, "Trade Entry/Exit Labels  Trade Statistics Label" , inline = "BT4" , group = "Backtest Framework")
  693. lblTrdStat = input(true, "" , inline = "BT4" , group = "Backtest Framework")
  694.  
  695. // -Calculations ════════════════════════════════════════════════════════════════════════════════════ //
  696.  
  697. startBckTst = time > timenow - lenBckTst * 31556952000
  698.  
  699. var inTrade = false
  700. var entryPrice = 0.
  701. var exitPrice = 0.
  702.  
  703. if isBackTest
  704.  
  705. var capital = dasCapital
  706. var trades = 0
  707. var win = 0
  708. var loss = 0
  709.  
  710. bullCandle = close > open
  711. bearCandle = close < open
  712. stopLossTrigger = crossunder(close, entryPrice * (1 - stopLoss))
  713.  
  714. longCondition = isTest ?
  715. isBull ?
  716. isSudden ?
  717. longAlertCondition [1] and not shortAlertCondition and bullCandle :
  718. longAlertCondition [1] and bullCandle :
  719. isSudden ?
  720. longAlertCondition [1] and not shortAlertCondition :
  721. longAlertCondition [1] :
  722. isBull ?
  723. isSudden ?
  724. longAlertCondition [2] and not shortAlertCondition[1] and bullCandle[1] :
  725. longAlertCondition [2] and bullCandle[1] :
  726. isSudden ?
  727. longAlertCondition [2] and not shortAlertCondition[1] :
  728. longAlertCondition [1]
  729.  
  730. shortCondition = isTest ?
  731. isBear ?
  732. isSudden ?
  733. shortAlertCondition[1] and not longAlertCondition and bearCandle :
  734. shortAlertCondition[1] and bearCandle :
  735. isSudden ?
  736. shortAlertCondition[1] and not longAlertCondition :
  737. shortAlertCondition[1] :
  738. isBear ?
  739. isSudden ?
  740. shortAlertCondition[2] and not longAlertCondition [1] and bearCandle[1] :
  741. shortAlertCondition[2] and bearCandle[1] :
  742. isSudden ?
  743. shortAlertCondition[2] and not longAlertCondition [1] :
  744. shortAlertCondition[1]
  745.  
  746. stopLossCondition = isStopLoss ? inTrade and not shortCondition ? stopLossTrigger : 0 : 0
  747.  
  748. if startBckTst and longCondition and not inTrade
  749. entryPrice := open
  750. inTrade := true
  751. trades := trades + 1
  752.  
  753. if lblInOutSL
  754. label longLabel = label.new(bar_index, low, text="L"
  755. ,tooltip="entry price : " + tostring(entryPrice) + "\nentry value : " + tostring(capital, "#.##")
  756. ,color=color.green, style=label.style_label_up, textcolor=color.white, textalign=text.align_center, size=size.tiny)
  757.  
  758. alert("long : probable trading opportunity, price " + tostring(close), alert.freq_once_per_bar)
  759.  
  760.  
  761. if (shortCondition or stopLossCondition) and inTrade
  762. exitPrice := stopLossCondition ? close : open
  763. inTrade := false
  764. capital := capital * (exitPrice / entryPrice)
  765.  
  766. if exitPrice > entryPrice
  767. win := win + 1
  768. else
  769. loss := loss + 1
  770.  
  771. if lblInOutSL
  772. text = stopLossCondition ? "SL" : "TP"
  773. label shortLabel = label.new(bar_index, high, text = text
  774. ,tooltip="change .......... : " + tostring((exitPrice / entryPrice - 1) * 100, "#.##") + "%\nentry/exit price : " + tostring(entryPrice) + " / " + tostring(exitPrice) + "\nnew capital ..... : " + tostring(capital, "#.##")
  775. ,color=color.red, style=label.style_label_down, textcolor=color.white, textalign=text.align_center, size=size.tiny)
  776.  
  777. alert("short : probable trading opportunity, price " + tostring(close), alert.freq_once_per_bar)
  778.  
  779.  
  780. var label wLabel = na
  781.  
  782. if not inTrade and longAlertCondition[1] and not shortAlertCondition
  783. wLabel := label.new(bar_index, low, text="⚠️"
  784. ,tooltip="probable long trading opportunity \nawaiting confirmation (next candle)\nif confirmed, backtest tool will execute trade with open price of the canlde"
  785. ,color=color.green, style=label.style_none, textcolor=color.white, textalign=text.align_center, size=size.huge)
  786. label.delete(wLabel[1])
  787.  
  788. alert("long : early warning : probable trading opportunity, awaiting confirmation (next candle), price " + tostring(close), alert.freq_once_per_bar)
  789.  
  790. if inTrade and shortAlertCondition[1] and not longAlertCondition
  791. wLabel := label.new(bar_index, high, text="⚠️"
  792. ,tooltip="probable short/take profit trading opportunity \nawaiting confirmation (next candle)\nif confirmed, backtest tool will execute trade with open price of the canlde"
  793. ,color=color.green, style=label.style_none, textcolor=color.white, textalign=text.align_center, size=size.huge)
  794. label.delete(wLabel[1])
  795.  
  796. alert("short : early warning : probable trading opportunity, awaiting confirmation (next candle), price " + tostring(close), alert.freq_once_per_bar)
  797.  
  798. if change(time)
  799. label.delete(wLabel[1])
  800.  
  801. if stopLossCondition
  802. alert("stop loss condition, price " + tostring(close), alert.freq_once_per_bar)
  803.  
  804.  
  805. if lblTrdStat
  806. var years = (timenow - time) / 31556952000
  807.  
  808. var yearsTxt = ""
  809. var remarks = ""
  810.  
  811. if years < lenBckTst
  812. lenBckTst := years
  813. yearsTxt := tostring(lenBckTst, "#.##") + " Years***"
  814. remarks := "\n\n*longs only\n**final value, if trade active displays estimated final value\n***max available data for selected timeframe : # of bars - " + tostring(bar_index)
  815. else
  816. yearsTxt := tostring(lenBckTst, "#.##") + " Year(s)"
  817. remarks := "\n\n*longs only\n**final value - if in trade, displays estimated final value"
  818.  
  819. inTradeTxt = inTrade ? "inTrade" : "not inTrade"
  820. estimated = inTrade ? capital * (close / entryPrice) : capital
  821. entryTxt = inTrade ? tostring(entryPrice) : "not inTrade"
  822. lastTrdTxt = inTrade ? ", Gain/Loss " + tostring((estimated/capital - 1) * 100, "#.##") + "%, Stop Loss " + tostring(isStopLoss ? entryPrice * (1 - stopLoss) : na) : ""
  823. stopLossTxt = isStopLoss ? "if last value falls by " + tostring(stopLoss * 100) + "% of entry price" : "not applied"
  824.  
  825. tooltipTxt = "entires/exit caclulations\n" +
  826. "-long entry , on next bar when arrows below bar pointing up\n" +
  827. "-take profit, on next bar when arrows above bar pointing down\n" +
  828. "-stop loss " + stopLossTxt + remarks
  829.  
  830. label indiLabel = label.new(time, close
  831. ,text="☼☾ Trade Statistics*, Trade Period - " + yearsTxt +
  832. "\n═════════════════════════════════════" +
  833. "\nSuccess Ratio ...... : " + tostring((win/trades)*100, "#") + "%" + ", # of Trades - " + tostring(trades) + ", Win/Loss - " + tostring(win) + "/" + tostring(loss) +
  834. "\nGain/Loss % ........ : " + tostring((estimated/dasCapital - 1) * 100, "#") + "%" + ", Initial/Final Value** - " + tostring(dasCapital) + " / " + tostring(estimated, "#") +
  835. "\n\nCurrent TradeStatus - " + inTradeTxt + lastTrdTxt +
  836. "\n═════════════════════════════════════" +
  837. "\nEntry Price/Value . : " + entryTxt + " / " + tostring(capital, "#.##") + " " + inTradeTxt +
  838. "\nLast Price/Value ... : " + tostring(close) + " / " + tostring(estimated , "#.##") + " " + inTradeTxt
  839. ,tooltip=tooltipTxt
  840. ,color=inTrade ? estimated/dasCapital > 1 ? color.teal : color.maroon : color.gray, xloc=xloc.bar_time, style=label.style_label_left, textcolor=color.white, textalign=text.align_left)
  841.  
  842. label.set_x(indiLabel, label.get_x(indiLabel) + round(change(time) * 5))
  843. label.delete(indiLabel[1])
  844.  
  845. // -Plotting ════════════════════════════════════════════════════════════════════════════════════ //
  846.  
  847. bgcolor(isBackTest and startBckTst and startBckTst != startBckTst[1] ? color.blue : na)
  848. plot(inTrade ? entryPrice : exitPrice > 0 ? exitPrice : na, title="Entry/Exit Price Line", color=inTrade ? color.green : color.red, style = plot.style_circles)
Advertisement
Add Comment
Please, Sign In to add comment