Advertisement
Guest User

Untitled

a guest
Aug 5th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 KB | None | 0 0
  1. #property strict
  2. #property indicator_separate_window
  3. #property indicator_minimum 0
  4. #property indicator_buffers 2
  5. #property indicator_plots 2
  6. //--- plot upVolume
  7. #property indicator_label1 "upVolume"
  8. #property indicator_type1 DRAW_HISTOGRAM
  9. #property indicator_color1 clrGreen
  10. #property indicator_style1 STYLE_SOLID
  11. #property indicator_width1 2
  12. //--- plot dnVolume
  13. #property indicator_label2 "dnVolume"
  14. #property indicator_type2 DRAW_HISTOGRAM
  15. #property indicator_color2 clrFireBrick
  16. #property indicator_style2 STYLE_SOLID
  17. #property indicator_width2 2
  18. //--- input parameters
  19. input int Difference = 300;
  20. input int LabelShift = 150;
  21. input bool ShowVolumeLabels = true;
  22. input int DivBy = 1;
  23. input color WaveColor = clrWheat;
  24. input int WaveWidth = 1;
  25. input int TriggerCandle = 1;
  26. input bool EnableNativeAlerts = true;
  27. input bool EnableSoundAlerts = true;
  28. input bool EnableEmailAlerts = true;
  29. input bool EnablePushAlerts = true;
  30. input string AlertEmailSubject = "";
  31. input string AlertText = "";
  32. input string SoundFileName = "alert.wav";
  33.  
  34. datetime LastAlertTime = D'01.01.1970';
  35. int LastAlertDirection = 0;
  36.  
  37. //--- indicator buffers
  38. double upVolumeBuffer[];
  39. double dnVolumeBuffer[];
  40. double barDirection[];
  41. double trendDirection[];
  42. double waveDirection[];
  43. long volumeTracker = 0;
  44.  
  45. double highestHigh = EMPTY_VALUE;
  46. double lowestLow = EMPTY_VALUE;
  47. int hhBar = EMPTY_VALUE;
  48. int llBar = EMPTY_VALUE;
  49.  
  50. //+------------------------------------------------------------------+
  51. //| Custom indicator initialization function |
  52. //+------------------------------------------------------------------+
  53. int OnInit()
  54. {
  55. //--- indicator buffers mapping
  56. IndicatorBuffers(5);
  57.  
  58. SetIndexBuffer(0, upVolumeBuffer);
  59. SetIndexBuffer(1, dnVolumeBuffer);
  60. SetIndexBuffer(2, barDirection);
  61. SetIndexBuffer(3, trendDirection);
  62. SetIndexBuffer(4, waveDirection);
  63.  
  64. //---
  65. return(INIT_SUCCEEDED);
  66. }
  67. //+------------------------------------------------------------------+
  68. //| Custom indicator deinitialization function |
  69. //+------------------------------------------------------------------+
  70. int deinit()
  71. {
  72. for(int i = ObjectsTotal() - 1; i >= 0; i--)
  73. {
  74. string Label = ObjectName(i);
  75. if (StringCompare("ED8847DC", StringSubstr(Label, 0, 8), true) == 0) {
  76. ObjectDelete(Label);
  77. }
  78. }
  79. if (((TriggerCandle > 0) && (Time[0] > LastAlertTime)) || (TriggerCandle == 0))
  80. {
  81. string Text;
  82. // Above Zero Alert
  83. if (((Coppock[TriggerCandle] > 0) && (Coppock[TriggerCandle + 1] <= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
  84. {
  85. Text = AlertText + "Coppock: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Above Zero.";
  86. if (EnableNativeAlerts) Alert(Text);
  87. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Coppock Alert", Text);
  88. if (EnableSoundAlerts) PlaySound(SoundFileName);
  89. if (EnablePushAlerts) SendNotification(Text);
  90. LastAlertTime = Time[0];
  91. LastAlertDirection = 1;
  92. }
  93. // Below Zero Alert
  94. if (((Coppock[TriggerCandle] < 0) && (Coppock[TriggerCandle + 1] >= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
  95. {
  96. Text = AlertText + "Coppock: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Below Zero.";
  97. if (EnableNativeAlerts) Alert(Text);
  98. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Coppock Alert", Text);
  99. if (EnableSoundAlerts) PlaySound(SoundFileName);
  100. if (EnablePushAlerts) SendNotification(Text);
  101. LastAlertTime = Time[0];
  102. LastAlertDirection = -1;
  103. }
  104. }
  105. }
  106. return(0);
  107. }
  108. //+------------------------------------------------------------------+
  109. //| Custom indicator iteration function |
  110. //+------------------------------------------------------------------+
  111. int OnCalculate(const int rates_total,
  112. const int prev_calculated,
  113. const datetime &time[],
  114. const double &open[],
  115. const double &high[],
  116. const double &low[],
  117. const double &close[],
  118. const long &tick_volume[],
  119. const long &volume[],
  120. const int &spread[])
  121. {
  122. //---
  123. // Only compute bars on new bar
  124. if (rates_total == prev_calculated) return(rates_total);
  125. RefreshRates();
  126. int limit = rates_total - 1;
  127.  
  128. int waveChangeBar = limit - 1;
  129.  
  130. // Initialise values
  131. if (highestHigh == EMPTY_VALUE) highestHigh = close[waveChangeBar];
  132. if (lowestLow == EMPTY_VALUE) lowestLow = close[waveChangeBar];
  133. if (hhBar == EMPTY_VALUE) hhBar = waveChangeBar;
  134. if (llBar == EMPTY_VALUE) llBar = waveChangeBar;
  135.  
  136. string waveID = "ED8847DC-" + TimeToString(time[waveChangeBar], TIME_DATE|TIME_MINUTES) + "-TL";
  137. if (!ObjectFind(0, waveID)) {
  138. ObjectCreate(0, waveID, OBJ_TREND, 0, time[waveChangeBar], close[waveChangeBar], time[waveChangeBar], close[waveChangeBar]);
  139. ObjectSet(waveID, OBJPROP_RAY, false);
  140. ObjectSet(waveID, OBJPROP_WIDTH, WaveWidth);
  141. ObjectSet(waveID, OBJPROP_COLOR, WaveColor);
  142. }
  143. double shift = LabelShift / MathPow(10, Digits);
  144.  
  145. for(int i=limit-1; i>=0; i--) {
  146. // Determine this bar's direction
  147. if (close[i] - close[i+1] > 0) barDirection[i] = 1; // current close higher
  148. if (close[i] - close[i+1] == 0) barDirection[i] = 0; // current close equal
  149. if (close[i] - close[i+1] < 0) barDirection[i] = -1; // current close lower
  150.  
  151. if (barDirection[limit] == EMPTY_VALUE) barDirection[limit] = barDirection[i];
  152. if (trendDirection[limit] == EMPTY_VALUE) trendDirection[limit] = barDirection[i];
  153. if (waveDirection[limit] == EMPTY_VALUE) waveDirection[limit] = barDirection[i];
  154.  
  155. // Determine highset high and lowest low
  156. if (close[i] > highestHigh) {
  157. highestHigh = close[i];
  158. hhBar = i;
  159. }
  160. else if (close[i] < lowestLow) {
  161. lowestLow = close[i];
  162. llBar = i;
  163. }
  164. // Determine if this bar has started a new trend
  165. if ((barDirection[i] != 0) && (barDirection[i] != barDirection[i+1]))
  166. trendDirection[i] = barDirection[i];
  167. else trendDirection[i] = trendDirection[i+1];
  168.  
  169. // Determine if this bar has started a new wave
  170. double waveTest = 0.0;
  171. if (waveDirection[i+1] == 1) {
  172. waveTest = highestHigh;
  173. }
  174. if (waveDirection[i+1] == -1) {
  175. waveTest = lowestLow;
  176. }
  177. double waveDifference = (MathAbs(waveTest - close[i])) * MathPow(10, Digits);
  178. if (trendDirection[i] != waveDirection[i+1]) {
  179. if (waveDifference >= Difference) waveDirection[i] = trendDirection[i];
  180. else waveDirection[i] = waveDirection[i+1];
  181. }
  182. else waveDirection[i] = waveDirection[i+1];
  183.  
  184. // Determine if we have started a new wave
  185. if (waveDirection[i] != waveDirection[i+1]) {
  186. if (waveDirection[i] == 1) {
  187. highestHigh = close[i];
  188. hhBar = i;
  189. waveChangeBar = llBar;
  190. }
  191. else {
  192. lowestLow = close[i];
  193. llBar = i;
  194. waveChangeBar = hhBar;
  195. }
  196.  
  197. ObjectSet(waveID, OBJPROP_PRICE2, close[waveChangeBar]);
  198. ObjectSet(waveID, OBJPROP_TIME2, time[waveChangeBar]);
  199. waveID = "ED8847DC-" + TimeToString(time[waveChangeBar], TIME_DATE|TIME_MINUTES) + "-TL";
  200. ObjectCreate(0, waveID, OBJ_TREND, 0, time[waveChangeBar], close[waveChangeBar], time[i], close[i]);
  201. ObjectSet(waveID, OBJPROP_RAY, false);
  202. ObjectSet(waveID, OBJPROP_WIDTH, WaveWidth);
  203. ObjectSet(waveID, OBJPROP_COLOR, WaveColor);
  204.  
  205. volumeTracker = 0;
  206. for (int k=waveChangeBar-1; k>=i; k--) {
  207. volumeTracker += tick_volume[k];
  208. if (waveDirection[i] == 1) {
  209. upVolumeBuffer[k] = volumeTracker;
  210. dnVolumeBuffer[k] = 0;
  211. }
  212. if (waveDirection[i] == -1) {
  213. upVolumeBuffer[k] = 0;
  214. dnVolumeBuffer[k] = volumeTracker;
  215. }
  216. }
  217.  
  218. if (ShowVolumeLabels == true) {
  219. string volLabel = "ED8847DC-" + TimeToString(time[waveChangeBar], TIME_DATE|TIME_MINUTES) + "-VOL";
  220. if (waveDirection[i] == 1) {
  221. ObjectCreate(0, volLabel, OBJ_TEXT, 0, time[waveChangeBar], low[waveChangeBar]-shift);
  222. ObjectSet(volLabel, OBJPROP_ANGLE, -0);
  223. ObjectSet(volLabel, OBJPROP_ANCHOR, ANCHOR_LEFT);
  224. ObjectSetText(volLabel, DoubleToString(dnVolumeBuffer[waveChangeBar]/DivBy, 0), 8, NULL, clrLightPink);
  225. }
  226. else{
  227. ObjectCreate(0, volLabel, OBJ_TEXT, 0, time[waveChangeBar], high[waveChangeBar]+shift);
  228. ObjectSet(volLabel, OBJPROP_ANGLE, 0);
  229. ObjectSet(volLabel, OBJPROP_ANCHOR, ANCHOR_LEFT);
  230. ObjectSetText(volLabel, DoubleToString(upVolumeBuffer[waveChangeBar]/DivBy, 0), 8, NULL, clrLightGreen);
  231. }
  232. }
  233. }
  234. else {
  235. volumeTracker += tick_volume[i];
  236. }
  237.  
  238. // Add the volume of this bar to the wave, or start again
  239. // if (waveDirection[i] == waveDirection[i+1]) {
  240. // volumeTracker += tick_volume[i];
  241. // }
  242. // else {
  243. //volumeTracker = 0;
  244. //for (int k=waveChangeBar-1; k>=i; k--) {
  245. // volumeTracker += tick_volume[k];
  246. // if (waveDirection[i] == 1) {
  247. // upVolumeBuffer[k] = volumeTracker;
  248. // dnVolumeBuffer[k] = 0;
  249. // }
  250. // if (waveDirection[i] == -1) {
  251. // upVolumeBuffer[k] = 0;
  252. // dnVolumeBuffer[k] = volumeTracker;
  253. // }
  254. //}
  255. // }
  256.  
  257. // Set the indicators
  258. if (waveDirection[i] == 1) {
  259. upVolumeBuffer[i] = volumeTracker;
  260. dnVolumeBuffer[i] = 0;
  261. }
  262. if (waveDirection[i] == -1) {
  263. upVolumeBuffer[i] = 0;
  264. dnVolumeBuffer[i] = volumeTracker;
  265. }
  266. }
  267.  
  268. ObjectSet(waveID, OBJPROP_PRICE2, close[0]);
  269. ObjectSet(waveID, OBJPROP_TIME2, time[0]);
  270.  
  271. //--- return value of prev_calculated for next call
  272. return(rates_total);
  273. }
  274.  
  275. string StringPadLeft (string inStr, ushort padStr, int totalStrLen) {
  276. string result;
  277. StringInit(result, totalStrLen, padStr);
  278. result = StringConcatenate(result, inStr);
  279.  
  280. int pos = StringLen(inStr);
  281.  
  282. return StringSubstr(result, pos, totalStrLen);
  283. }
  284. //+------------------------------------------------------------------+
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement