Advertisement
Guest User

SM_MultipleIndicatorLabels

a guest
Jun 28th, 2018
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. #
  2. # SM_MultipleIndicatorLabels
  3. #
  4. #
  5. # version 1.0
  6. #
  7.  
  8. input Show_Impulse_Extras = yes;
  9. input Show_ATR_Trailing_Stop = yes;
  10. input Show_MACD_Trend = yes;
  11.  
  12. ####################################
  13. #
  14. # Exponential moving average (uses 13 period by default)
  15. #
  16. ####################################
  17.  
  18. input EMA_Length = 13;
  19. def EMA_price = close;
  20. def EMA_calculation = ExpAverage(EMA_price, EMA_Length);
  21. def EMA_above = if close > EMA_calculation then 1 else 0;
  22. def EMA_below = if close <= EMA_calculation then 1 else 0;
  23.  
  24. ####################################
  25. #
  26. # MACD + MACD Histogram
  27. #
  28. ####################################
  29.  
  30. input MACD_fastLength = 12;
  31. input MACD_slowLength = 26;
  32. input MACDLength = 9;
  33. input MACD_averageType = AverageType.EXPONENTIAL;
  34. def MACD_Value = MovingAverage(MACD_averageType, close, MACD_fastLength) - MovingAverage(MACD_averageType, close, MACD_slowLength);
  35. def MACD_Avg = MovingAverage(MACD_averageType, MACD_Value, MACDLength);
  36. def MACD_Diff = MACD_Value - MACD_Avg;
  37. def ZeroLine = 0;
  38.  
  39. def MACD_PositiveAndUp;
  40. def MACD_PositiveAndDown;
  41. def MACD_NegativeAndDown;
  42. def MACD_NegativeAndUp;
  43. def MACD_GoingUp;
  44. def MACD_GoingDown;
  45. if MACD_Diff >= 0 {
  46. if MACD_Diff > MACD_Diff[1] {
  47. MACD_PositiveAndUp = 1;
  48. MACD_PositiveAndDown = 0;
  49. MACD_NegativeAndUp = 0;
  50. MACD_NegativeAndDown = 0;
  51. MACD_GoingUp = 1;
  52. MACD_GoingDown = 0;
  53. } else {
  54. MACD_PositiveAndUp = 0;
  55. MACD_PositiveAndDown = 1;
  56. MACD_NegativeAndUp = 0;
  57. MACD_NegativeAndDown = 0;
  58. MACD_GoingUp = 0;
  59. MACD_GoingDown = 1;
  60. }
  61. } else {
  62. if MACD_Diff < MACD_Diff[1] {
  63. MACD_PositiveAndUp = 0;
  64. MACD_PositiveAndDown = 0;
  65. MACD_NegativeAndUp = 0;
  66. MACD_NegativeAndDown = 1;
  67. MACD_GoingUp = 0;
  68. MACD_GoingDown = 1;
  69. } else {
  70. MACD_PositiveAndUp = 0;
  71. MACD_PositiveAndDown = 0;
  72. MACD_NegativeAndUp = 1;
  73. MACD_NegativeAndDown = 0;
  74. MACD_GoingUp = 1;
  75. MACD_GoingDown = 0;
  76. }
  77. }
  78.  
  79. ####################################
  80. #
  81. # Impulse
  82. #
  83. ####################################
  84.  
  85. def Impulse_Up;
  86. def Impulse_Down;
  87. def Impulse_Neutral;
  88.  
  89. if EMA_above == 1 { # cases 1, 2, 3
  90. Impulse_Down = 0; # Impulse down requires EMA_Down
  91. Impulse_Up = if MACD_GoingUp == 1 then 1 else 0;
  92. Impulse_Neutral = if MACD_GoingDown == 1 then 1 else 0;
  93. } else { # Cases 4, 5, 6
  94. Impulse_Up = 0;
  95. Impulse_Down = if MACD_GoingDown == 1 then 1 else 0;
  96. Impulse_Neutral = if MACD_GoingUp == 1 then 1 else 0;
  97. }
  98. AddLabel(Impulse_Up, "IMPULSE", COLOR.DARK_GREEN);
  99. AddLabel(Impulse_Down, "IMPULSE", COLOR.DARK_RED);
  100. AddLabel(Impulse_Neutral, "IMPULSE", COLOR.GRAY);
  101.  
  102.  
  103. ####################################
  104. #
  105. # EMA Label
  106. #
  107. ####################################
  108.  
  109. def show_EMA_up;
  110. def show_EMA_down;
  111. if Show_Impulse_Extras == yes {
  112. show_EMA_up = if EMA_above == 1 then 1 else 0;
  113. show_EMA_down = if EMA_below == 1 then 1 else 0;
  114. } else {
  115. show_EMA_up = 0;
  116. show_EMA_down = 0;
  117. }
  118.  
  119. AddLabel(show_EMA_up, "EMA", COLOR.DARK_GREEN);
  120. AddLabel(show_EMA_down, "EMA", COLOR.DARK_RED);
  121.  
  122. ####################################
  123. # MACD Histogram label
  124. ####################################
  125.  
  126. def show_MACD_PU;
  127. def show_MACD_PD;
  128. def show_MACD_NU;
  129. def show_MACD_ND;
  130.  
  131. if Show_Impulse_Extras == yes {
  132. show_MACD_PU = if MACD_PositiveAndUp == 1 then 1 else 0;
  133. show_MACD_PD = if MACD_PositiveAndDown == 1 then 1 else 0;
  134. show_MACD_NU = if MACD_NegativeAndUp == 1 then 1 else 0;
  135. show_MACD_ND = if MACD_NegativeAndDown == 1 then 1 else 0;
  136. } else {
  137. show_MACD_PU = 0;
  138. show_MACD_PD = 0;
  139. show_MACD_NU = 0;
  140. show_MACD_ND = 0;
  141. }
  142.  
  143. AddLabel(show_MACD_PU, "MACD Histogram", COLOR.DARK_GREEN);
  144. AddLabel(show_MACD_PD, "MACD Histogram", COLOR.DARK_RED);
  145. AddLabel(show_MACD_NU, "MACD Histogram", COLOR.DARK_GREEN);
  146. AddLabel(show_MACD_ND, "MACD Histogram", COLOR.DARK_RED);
  147.  
  148. ####################################
  149. # MACD Trend label
  150. ####################################
  151.  
  152. def MACD_Positive = if MACD_Diff > 0 then 1 else 0;
  153. def MACD_Negative = if MACD_Diff <= 0 then 1 else 0;
  154. def show_MACD_P;
  155. def show_MACD_N;
  156.  
  157. if Show_MACD_Trend == yes {
  158. show_MACD_P = if MACD_Positive == 1 then 1 else 0;
  159. show_MACD_N = if MACD_Negative == 1 then 1 else 0;
  160. } else {
  161. show_MACD_P = 0;
  162. show_MACD_N = 0;
  163. }
  164.  
  165. AddLabel(show_MACD_P, "MACD Trend", Color.DARK_GREEN);
  166. AddLabel(show_MACD_N, "MACD Trend", COLOR.DARK_RED);
  167.  
  168.  
  169. ####################################
  170. #
  171. # ATR Trailing Stop
  172. #
  173. ####################################
  174.  
  175. input trailType = {default modified, unmodified};
  176. input ATRPeriod = 9;
  177. input ATRFactor = 2.9;
  178. input firstTrade = {default long, short};
  179. input averageType = AverageType.EXPONENTIAL;
  180. Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);
  181. def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
  182. def HRef = if low <= high[1] then high - close[1] else (high - close[1]) - 0.5 * (low - high[1]); def LRef = if high >= low[1]
  183. then close[1] - low
  184. else (close[1] - low) - 0.5 * (low[1] - high);
  185. def trueRange;
  186. switch (trailType) {
  187. case modified:
  188. trueRange = Max(HiLo, Max(HRef, LRef));
  189. case unmodified:
  190. trueRange = trueRange(high, close, low);
  191. }
  192. def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
  193. def state = {default init, long, short};
  194. def trail;
  195. switch (state[1]) {
  196. case init:
  197. if (!IsNaN(loss)) {
  198. switch (firstTrade) {
  199. case long:
  200. state = state.long;
  201. trail = close - loss;
  202. case short:
  203. state = state.short;
  204. trail = close + loss;
  205. }
  206. } else {
  207. state = state.init;
  208. trail = Double.NaN;
  209. }
  210. case long:
  211. if (close > trail[1]) {
  212. state = state.long;
  213. trail = Max(trail[1], close - loss);
  214. } else {
  215. state = state.short;
  216. trail = close + loss;
  217. }
  218. case short:
  219. if (close < trail[1]) {
  220. state = state.short;
  221. trail = Min(trail[1], close + loss);
  222. } else {
  223. state = state.long;
  224. trail = close - loss;
  225. }
  226. }
  227.  
  228. def ATRLabelLong = if state == state.long then 1 else 0;
  229. def ATRLabelShort = if state != state.long then 1 else 0;
  230. def ShowATRLabelLong;
  231. def ShowATRLabelShort;
  232.  
  233. if Show_ATR_Trailing_Stop == yes {
  234. ShowATRLabelLong = if ATRLabelLong == 1 then 1 else 0;
  235. ShowATRLabelShort = if ATRLabelShort == 1 then 1 else 0;
  236. } else {
  237. ShowATRLabelLong = 0;
  238. ShowATRLabelShort = 0;
  239. }
  240.  
  241. AddLabel(ShowATRLabelLong, "ATR Trailing Stop", COLOR.DARK_GREEN);
  242. AddLabel(ShowATRLabelShort, "ATR Trailing Stop", COLOR.DARK_RED);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement