Advertisement
Guest User

SM_MultipleIndicatorLabels v1.1

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