Advertisement
--SubZer0--

Intraday PriceLevelMarker v2.0 - For ThinkOrSwim

Dec 31st, 2022 (edited)
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.32 KB | Source Code | 0 0
  1. ###########################################################################
  2. #PriceLevelMarker - Automatically Plot Important Price Levels in ToS
  3. #Creator: u/--SubZer0--
  4. #Version: 2.0
  5. #Last Updated: 12.31.2022
  6. #############################################################################
  7.  
  8. #hint showOnlyLastPeriod: Yes will show plots only for current day. <br />No will show it for all days visible on your chart timeframe
  9. #hint showPlotLabels: Display labels to clearly identify each plot. The position of these chart labels is controlled by plotLabelOffset
  10. #hint plotLabelOffset: This value determines where to show the chart label. The default is 5, which means the chart label will be displayed on the far right of the chart at the price axis and the distance between the latest candle and the label will be 5 bars. Note: This number cannot be greater than the <b>Expansion Area</b> defined in "Chart Style -> Settings -> Time Axis -> Expansion Area" otherwise the label will not be displayed.
  11.  
  12. #User input
  13. input showOnlyLastPeriod = yes;
  14. input showPlotLabels = yes;
  15. input plotLabelOffset = 0;
  16.  
  17. input showOvernightGapRegion = yes;
  18. input ShowOvernightGapBoundary = no;
  19. input ShowOvernightGapMean = Yes;
  20. input showTodayOpen = yes;
  21. input showTodayHigh = yes;
  22. input showTodayLow = yes;
  23. input showYClose = yes;
  24. input showYHigh = yes;
  25. input showYLow = yes;
  26. input showYYHigh = yes;
  27. input showYYLow = yes;
  28. input show52WkHigh = yes;
  29. input show52WkLow = yes;
  30. input show5YHigh = yes;
  31. input show5YLow = yes;
  32. input showDaily50SMA = yes;
  33. input showDaily100SMA = yes;
  34. input showDaily200SMA = yes;
  35.  
  36.  
  37. #Default constants
  38. def paintStrategyLine = PaintingStrategy.LINE;
  39. def paintStrategyDashes = PaintingStrategy.DASHES;
  40. def styleSolid = Curve.FIRM;
  41. def styleShortDash = Curve.SHORT_DASH;
  42. def styleMediumDash = Curve.MEDIUM_DASH;
  43. def styleLongDash = Curve.LONG_DASH;
  44. def stylePoints = Curve.POINTS;
  45. def lineWeight = 1;
  46. DefineGlobalColor("OvernightGapRegion", Color.LIGHT_GRAY);
  47.  
  48. def aggregationPeriod = AggregationPeriod.DAY;
  49. def maPriceType = FundamentalType.CLOSE;
  50. def sma50Length = 50;
  51. def sma100Length = 100;
  52. def sma200Length = 200;
  53. def length = 1;
  54. def nan = Double.NaN;
  55. def labelLocBar = !IsNaN(close) and IsNaN(close[-1]);
  56. def displace0 = 0;
  57. def displace1 = 1;
  58. def displace2 = 2;
  59.  
  60.  
  61. ###########################################################################
  62. ##
  63. ## Plot Open (Today's Open Price)
  64. ##
  65. ###########################################################################
  66.  
  67. plot TodayOpen;
  68.  
  69. if !showTodayOpen or (showOnlyLastPeriod and !IsNaN(open(period = aggregationPeriod)[-1]))
  70. {
  71.     TodayOpen = Double.NaN;
  72. }
  73. else
  74. {
  75.     TodayOpen = open(period = aggregationPeriod)[displace0];
  76. }
  77.  
  78. TodayOpen.SetDefaultColor(Color.BLUE);
  79. TodayOpen.SetPaintingStrategy(paintStrategyDashes);
  80. TodayOpen.SetLineWeight(lineWeight);
  81.  
  82. TodayOpen.HideBubble();
  83.  
  84. AddChartBubble(showTodayOpen and showPlotLabels and labelLocBar[plotLabelOffset], TodayOpen[plotLabelOffset], "Op", Color.CYAN);
  85.  
  86.  
  87.  
  88. ###########################################################################
  89. ##
  90. ## Plot High and Low (High and Low of Today)
  91. ##
  92. ###########################################################################
  93.  
  94. plot High;
  95. plot Low;
  96.  
  97. if !showTodayHigh or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  98. {
  99.     High = nan;
  100. }
  101. else
  102. {
  103.     High = Highest(high(period = aggregationPeriod)[displace0], length);
  104. }
  105.  
  106. if !showTodayLow or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  107. {
  108.     Low = nan;
  109. }
  110. else
  111. {
  112.     Low = Lowest(low(period = aggregationPeriod)[displace0], length);
  113. }
  114.  
  115. High.SetDefaultColor(Color.BLUE);
  116. High.SetPaintingStrategy(paintStrategyLine);
  117. High.SetStyle(styleLongDash);
  118. High.SetLineWeight(lineWeight);
  119.  
  120. Low.SetDefaultColor(Color.BLUE);
  121. Low.SetPaintingStrategy(paintStrategyLine);
  122. Low.SetStyle(styleShortDash);
  123. Low.SetLineWeight(lineWeight);
  124.  
  125. High.HideBubble();
  126. Low.HideBubble();
  127.  
  128. AddChartBubble(showTodayHigh and showPlotLabels and labelLocBar[plotLabelOffset], High[plotLabelOffset], "Hi", Color.Light_GRAY, 0);
  129. AddChartBubble(showTodayLow and showPlotLabels and labelLocBar[plotLabelOffset], Low[plotLabelOffset], "Lo", Color.LIGHT_GRAY, 0);
  130.  
  131.  
  132. ###########################################################################
  133. ##
  134. ## Plot YClose (Previous day close)
  135. ##
  136. ###########################################################################
  137.  
  138. plot YClose;
  139. if !showYClose
  140.         or IsNaN(close(period = aggregationPeriod))
  141.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  142. {
  143.     YClose = Double.NaN;
  144. }
  145. else
  146. {
  147.     YClose = close(period = aggregationPeriod)[displace1];
  148. }
  149.  
  150. YClose.SetDefaultColor(Color.ORANGE);
  151. YClose.SetPaintingStrategy(paintStrategyDashes);
  152. YClose.SetLineWeight(lineWeight);
  153. YClose.HideBubble();
  154.  
  155. AddChartBubble(showYClose and showPlotLabels and labelLocBar[plotLabelOffset], YClose[plotLabelOffset], "YCl", Color.LIGHT_ORANGE);
  156.  
  157.  
  158. ###########################################################################
  159. ##
  160. ## Plot Overnight Gap (Gap between previous day close and today's open)
  161. ##
  162. ###########################################################################
  163.  
  164. plot UpperGapBoundary = if showOvernightGapBoundary then if YClose > TodayOpen then YClose else TodayOpen else Double.NaN;
  165. plot LowerGapBoundary = if showOvernightGapBoundary then if YClose > TodayOpen then TodayOpen else YClose else Double.NaN;
  166. plot GapMean = if showOvernightGapMean then (YClose + TodayOpen) / 2 else Double.NaN;
  167.  
  168. AddCloud(if showOvernightGapRegion then YClose else Double.NaN, if showOvernightGapRegion then TodayOpen else Double.NaN, GlobalColor("OvernightGapRegion"), GlobalColor("OvernightGapRegion"));
  169.  
  170. UpperGapBoundary.SetStyle(Curve.POINTS);
  171. UpperGapBoundary.SetDefaultColor(Color.GRAY);
  172. UpperGapBoundary.SetHiding(!showOvernightGapBoundary);
  173. UpperGapBoundary.HideBubble();
  174.  
  175. LowerGapBoundary.SetStyle(Curve.POINTS);
  176. LowerGapBoundary.SetDefaultColor(Color.GRAY);
  177. LowerGapBoundary.SetHiding(!showOvernightGapBoundary);
  178. LowerGapBoundary.HideBubble();
  179.  
  180. GapMean.SetStyle(Curve.POINTS);
  181. GapMean.SetDefaultColor(Color.GRAY);
  182. GapMean.SetHiding(!showOvernightGapMean);
  183. GapMean.HideBubble();
  184.  
  185.  
  186. ###########################################################################
  187. ##
  188. ## Plot YHigh and YLow (High and Low of Yesterday)
  189. ##
  190. ###########################################################################
  191.  
  192. plot YHigh;
  193. plot YLow;
  194.  
  195. if !showYHigh        
  196.         or IsNaN(close(period = aggregationPeriod))
  197.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  198. {
  199.     YHigh = nan;
  200. }
  201. else
  202. {
  203.     YHigh = high(period = aggregationPeriod)[displace1];
  204. }
  205.  
  206. if !showYLow
  207.         or IsNaN(close(period = aggregationPeriod))
  208.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  209. {
  210.     YLow = nan;
  211. }
  212. else
  213. {
  214.     YLow = low(period = aggregationPeriod)[displace1];
  215. }
  216.  
  217. YHigh.SetDefaultColor(Color.ORANGE);
  218. YHigh.SetPaintingStrategy(paintStrategyLine);
  219. YHigh.SetStyle(styleLongDash);
  220. YHigh.SetLineWeight(lineWeight);
  221.  
  222. YLow.SetDefaultColor(Color.ORANGE);
  223. YLow.SetPaintingStrategy(paintStrategyLine);
  224. YLow.SetStyle(styleShortDash);
  225. YLow.SetLineWeight(lineWeight);
  226.  
  227. YHigh.HideBubble();
  228. YLow.HideBubble();
  229.  
  230. AddChartBubble(showYHigh and showPlotLabels and labelLocBar[plotLabelOffset], YHigh[plotLabelOffset], "YHi", Color.Light_GRAY);
  231. AddChartBubble(showYLow and showPlotLabels and labelLocBar[plotLabelOffset], YLow[plotLabelOffset], "YLo", Color.LIGHT_GRAY, 0);
  232.  
  233.  
  234. ###########################################################################
  235. ##
  236. ## Plot YYHigh and YYLow (High and Low Two days ago)
  237. ##
  238. ###########################################################################
  239.  
  240. plot YYHigh;
  241. plot YYLow;
  242.  
  243. if !showYYHigh
  244.         or IsNaN(close(period = aggregationPeriod))
  245.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  246. {
  247.     YYHigh = nan;
  248. }
  249. else
  250. {
  251.     YYHigh = Highest(high(period = aggregationPeriod)[displace2], length);
  252. }
  253.  
  254. if !showYYLow
  255.         or IsNaN(close(period = aggregationPeriod))
  256.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  257. {
  258.     YYLow = nan;
  259. }
  260. else
  261. {
  262.     YYLow = Lowest(low(period = aggregationPeriod)[displace2], length);
  263. }
  264.  
  265. YYHigh.SetDefaultColor(Color.MAGENTA);
  266. YYHigh.SetPaintingStrategy(paintStrategyLine);
  267. YYHigh.SetStyle(styleLongDash);
  268. YYHigh.SetLineWeight(lineWeight);
  269.  
  270. YYLow.SetDefaultColor(Color.MAGENTA);
  271. YYLow.SetPaintingStrategy(paintStrategyLine);
  272. YYLow.SetStyle(styleShortDash);
  273. YYLow.SetLineWeight(lineWeight);
  274.  
  275. YYHigh.HideBubble();
  276. YYLow.HideBubble();
  277.  
  278. AddChartBubble(showYYHigh and showPlotLabels and labelLocBar[plotLabelOffset], YYHigh[plotLabelOffset], "YYHi", Color.Light_GRAY);
  279. AddChartBubble(showYYLow and showPlotLabels and labelLocBar[plotLabelOffset], YYLow[plotLabelOffset], "YYLo", Color.LIGHT_GRAY, 0);
  280.  
  281.  
  282. ###########################################################################
  283. ##
  284. ## Plot _52WkHigh and _52WkLow - (Highest and lowest during the last 52 weeks)
  285. ##
  286. ###########################################################################
  287.  
  288. plot _52WkHigh;
  289. plot _52WkLow;
  290.  
  291. if !show52WkHigh        
  292.         or IsNaN(close(period = aggregationPeriod))
  293.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  294. {
  295.     _52WkHigh = nan;
  296.  }
  297. else
  298. {
  299.     _52WkHigh = Highest(high(period = aggregationPeriod)[displace0], 252);
  300.  }
  301.  
  302. if !show52WkLow        
  303.         or IsNaN(close(period = aggregationPeriod))
  304.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  305. {
  306.      _52WkLow = nan;
  307. }
  308. else
  309. {
  310.      _52WkLow = Lowest(low(period = aggregationPeriod)[displace0], 252);
  311. }
  312.  
  313. _52WkHigh.SetDefaultColor(Color.PLUM);
  314. _52WkHigh.SetPaintingStrategy(paintStrategyLine);
  315. _52WkHigh.SetStyle(styleLongDash);
  316. _52WkHigh.SetLineWeight(lineWeight);
  317.  
  318. _52WkLow.SetDefaultColor(Color.PLUM);
  319. _52WkLow.SetPaintingStrategy(paintStrategyLine);
  320. _52WkLow.SetStyle(styleShortDash);
  321. _52WkLow.SetLineWeight(lineWeight);
  322.  
  323. _52WkHigh.HideBubble();
  324. _52WkLow.HideBubble();
  325.  
  326. AddChartBubble(show52WkHigh and showPlotLabels and labelLocBar[plotLabelOffset], _52WkHigh[plotLabelOffset], "52Hi", Color.Light_RED);
  327. AddChartBubble(show52WkLow and showPlotLabels and labelLocBar[plotLabelOffset], _52WkLow[plotLabelOffset], "52Lo", Color.LIGHT_GREEN, 0);
  328.  
  329.  
  330. ###########################################################################
  331. ##
  332. ## Plot 5YHigh and 5YLow - (Highest and lowest during the last 5 years)
  333. ## ToS does not support plotting "all time high" or "all time low" because
  334. ## of data limitations within ToS.
  335. ##
  336. ###########################################################################
  337.  
  338. plot _5YHigh;
  339. plot _5YLow;
  340. def years = 5;
  341.  
  342. if !show5YHigh        
  343.         or IsNaN(close(period = aggregationPeriod))
  344.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  345. {
  346.     _5YHigh = nan;
  347. }
  348. else
  349. {
  350.     _5YHigh = Highest(high(period = aggregationPeriod)[displace1], 252*years);
  351. }
  352.  
  353. if !show5YLow        
  354.         or IsNaN(close(period = aggregationPeriod))
  355.         or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  356. {
  357.     _5YLow = nan;
  358. }
  359. else
  360. {
  361.     _5YLow = Lowest(low(period = aggregationPeriod)[displace1], 252*years);
  362. }
  363.  
  364. _5YHigh.SetDefaultColor(Color.GRAY);
  365. _5YHigh.SetPaintingStrategy(paintStrategyLine);
  366. _5YHigh.SetStyle(styleLongDash);
  367. _5YHigh.SetLineWeight(lineWeight);
  368.  
  369. _5YLow.SetDefaultColor(Color.GRAY);
  370. _5YLow.SetPaintingStrategy(paintStrategyLine);
  371. _5YLow.SetStyle(styleShortDash);
  372. _5YLow.SetLineWeight(lineWeight);
  373.  
  374. _5YHigh.HideBubble();
  375. _5YLow.HideBubble();
  376.  
  377. AddChartBubble(show5YHigh and showPlotLabels and labelLocBar[plotLabelOffset], _5YHigh[plotLabelOffset], "5Hi", Color.Light_RED);
  378. AddChartBubble(show5YLow and showPlotLabels and labelLocBar[plotLabelOffset], _5YLow[plotLabelOffset], "5Lo", Color.LIGHT_GREEN, 0);
  379.  
  380.  
  381.  
  382. ###########################################################################
  383. # Plot Daily 50SMA on Intra-Day Charts
  384. #
  385. #############################################################################
  386. plot Daily50SMA;
  387. if !showDaily50SMA or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  388. {
  389.     Daily50SMA = Double.NaN;
  390. }
  391. else
  392. {
  393.     Daily50SMA = Average(fundamental(maPriceType, period = aggregationPeriod)[-displace0], sma50Length);
  394. }
  395.  
  396. Daily50SMA.SetDefaultColor(Color.PINK);
  397. Daily50SMA.SetPaintingStrategy(paintStrategyLine);
  398. Daily50SMA.SetStyle(styleSolid);
  399. Daily50SMA.SetLineWeight(lineWeight+1);
  400.  
  401. Daily50SMA.HideBubble();
  402.  
  403. AddChartBubble(showDaily50SMA and showPlotLabels and labelLocBar[plotLabelOffset], Daily50SMA[plotLabelOffset], sma50Length + "", Color.PINK);
  404.  
  405.  
  406. ###########################################################################
  407. # Plot Daily 100SMA on Intra-Day Charts
  408. #
  409. #############################################################################
  410. plot Daily100SMA;
  411. if !showDaily100SMA or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  412. {
  413.     Daily100SMA = Double.NaN;
  414. }
  415. else
  416. {
  417.     Daily100SMA = Average(fundamental(maPriceType, period = aggregationPeriod)[-displace0], sma100Length);
  418. }
  419.  
  420. Daily100SMA.SetDefaultColor(Color.BLACK);
  421. Daily100SMA.SetPaintingStrategy(paintStrategyLine);
  422. Daily100SMA.SetStyle(styleSolid);
  423. Daily100SMA.SetLineWeight(lineWeight+1);
  424.  
  425. Daily100SMA.HideBubble();
  426.  
  427. AddChartBubble(showDaily100SMA and showPlotLabels and labelLocBar[plotLabelOffset], Daily100SMA[plotLabelOffset], sma100Length + "", Color.CYAN);
  428.  
  429.  
  430. ###########################################################################
  431. # Plot Daily 200SMA on Intra-Day Charts
  432. #
  433. #############################################################################
  434. plot Daily200SMA;
  435. if !showDaily200SMA or (showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]))
  436. {
  437.     Daily200SMA = Double.NaN;
  438. }
  439. else
  440. {
  441.     Daily200SMA = Average(fundamental(maPriceType, period = aggregationPeriod)[-displace0], sma200Length);
  442. }
  443.  
  444. Daily200SMA.SetDefaultColor(Color.MAGENTA);
  445. Daily200SMA.SetPaintingStrategy(paintStrategyLine);
  446. Daily200SMA.SetStyle(styleSolid);
  447. Daily200SMA.SetLineWeight(lineWeight+1);
  448.  
  449. Daily200SMA.HideBubble();
  450.  
  451. AddChartBubble(showDaily200SMA and showPlotLabels and labelLocBar[plotLabelOffset], Daily200SMA[plotLabelOffset], sma200Length + "", Color.MAGENTA);
  452.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement