Nav apraksta

atr_momentum_indicator_mt4.mq4 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //+------------------------------------------------------------------+
  2. //| atr_momentum_indicator_mt4.mq4 |
  3. //| Copyright 2025, MQL Development |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2025, MQL Development"
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.00"
  9. #property strict
  10. #property indicator_separate_window
  11. #property indicator_buffers 3
  12. #property indicator_minimum 0.0
  13. //+------------------------------------------------------------------+
  14. //| Custom indicator initialization function |
  15. //+------------------------------------------------------------------+
  16. input string string_1 = "<><><><><><> General SETTINGS <><><><><><>"; //__
  17. input double threshold = 10.0; // Threshold Value
  18. sinput string atrSettings = " <><><><><> Atr Settings <><><><><> "; //_
  19. input int atrPeriod = 14; // ATR Period
  20. input string string_2_2 = "<><><><><><> Color Setting <><><><><><>"; //__
  21. input color atr_line_color = clrAqua; // Atr Line Color
  22. input color signal_color = clrLimeGreen; // Signal Color
  23. input color no_signal_color = clrSlateGray; // No Signal Color
  24. // Global Variables
  25. double atrPercentageBuffer[];
  26. double signalBuffer[];
  27. double noSignalBuffer[];
  28. string short_name="";
  29. int OnInit()
  30. {
  31. //--- indicator buffers mapping
  32. SetIndexBuffer(0, atrPercentageBuffer);
  33. SetIndexBuffer(1, signalBuffer);
  34. SetIndexBuffer(2, noSignalBuffer);
  35. //--- Set buffer names
  36. SetIndexLabel(0, "Atr %");
  37. SetIndexLabel(1, "Above threshold");
  38. SetIndexLabel(2, "Below threshold");
  39. SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, atr_line_color);
  40. SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 4, signal_color);
  41. SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, 4, no_signal_color);
  42. short_name = "atr_momentum_indicator_mt4";
  43. IndicatorShortName(short_name);
  44. //---
  45. return(INIT_SUCCEEDED);
  46. }
  47. //+------------------------------------------------------------------+
  48. //| Custom indicator iteration function |
  49. //+------------------------------------------------------------------+
  50. int OnCalculate(const int rates_total,
  51. const int prev_calculated,
  52. const datetime &time[],
  53. const double &open[],
  54. const double &high[],
  55. const double &low[],
  56. const double &close[],
  57. const long &tick_volume[],
  58. const long &volume[],
  59. const int &spread[])
  60. {
  61. //---
  62. int i, Counted_bars;
  63. Counted_bars=IndicatorCounted(); // Number of counted bars
  64. if(prev_calculated == 0)
  65. i=Bars-Counted_bars-10;
  66. else
  67. i=Bars-Counted_bars-1;
  68. while(i >= 0)
  69. {
  70. double atrValueIs = atrValue(i);
  71. double candle_0 = iClose(Symbol(), PERIOD_CURRENT, i);
  72. double candle_1 = iClose(Symbol(), PERIOD_CURRENT, i + 1);
  73. double candle_2 = iClose(Symbol(), PERIOD_CURRENT, i + 2);
  74. double candle_percentage_0 = atrPercentageValue(i);
  75. double candle_percentage_1 = atrPercentageValue(i + 1);
  76. double candle_percentage_2 = atrPercentageValue(i + 2);
  77. double value_1 = 0, value_2 = 0, value_3 = 0;
  78. if(candle_percentage_0 != 0 && candle_percentage_1 != 0 && candle_percentage_2 != 0)
  79. {
  80. value_1 = (((candle_percentage_1 - candle_percentage_2) / candle_percentage_2) * 100);
  81. value_2 = (((candle_percentage_0 - candle_percentage_1) / candle_percentage_1) * 100);
  82. value_3 = (((candle_percentage_0 - candle_percentage_2) / candle_percentage_2) * 100);
  83. }
  84. double totalSum = value_1 + value_2 + value_3;
  85. atrPercentageBuffer[i] = ((atrValueIs / candle_1) * 100);
  86. //Print(" atrValueIs: ",atrValueIs," candle_1: ",candle_1," final: ",atrPercentageBuffer[i]);
  87. atrPercentageBuffer[i] = atrPercentageBuffer[i] * 100;
  88. if(totalSum > threshold)
  89. {
  90. signalBuffer[i] = totalSum;
  91. noSignalBuffer[i] = 0;
  92. }
  93. else
  94. {
  95. noSignalBuffer[i] = totalSum;
  96. signalBuffer[i] = 0;
  97. }
  98. //Print("Time Current Candle: ", iClose(Symbol(), PERIOD_CURRENT,0),
  99. // " | Time Candle 1: ", iClose(Symbol(), PERIOD_CURRENT, 0 + 1),
  100. // " | Time Candle 2: ", iClose(Symbol(), PERIOD_CURRENT, 0 + 2),
  101. // " | ATR Value: ", atrValueIs,
  102. // " | value_1: ", value_1,
  103. // " | value_2: ", value_2,
  104. // " | value_3: ", value_3,
  105. // " | Sum: ", NormalizeDouble(totalSum, Digits()),
  106. // " | i: ", i);
  107. i--;
  108. }
  109. //--- return value of prev_calculated for next call
  110. return(rates_total);
  111. }
  112. //+------------------------------------------------------------------+
  113. //+------------------------------------------------------------------+
  114. //| |
  115. //+------------------------------------------------------------------+
  116. double atrValue(int index)
  117. {
  118. return NormalizeDouble(iATR(Symbol(), PERIOD_CURRENT, atrPeriod, index), Digits());
  119. }
  120. //+------------------------------------------------------------------+
  121. //| |
  122. //+------------------------------------------------------------------+
  123. double atrPercentageValue(int index)
  124. {
  125. // Print(" Atr Percentage: ", (iATR(Symbol(), PERIOD_CURRENT, atrPeriod, index) / iClose(Symbol(), PERIOD_CURRENT, index)) * 100, " i: ", index);
  126. return (iATR(Symbol(), PERIOD_CURRENT, atrPeriod, index) / iClose(Symbol(), PERIOD_CURRENT, index + 1)) * 100;
  127. }
  128. //+------------------------------------------------------------------+
  129. //| |
  130. //+------------------------------------------------------------------+
  131. //+------------------------------------------------------------------+