| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //+------------------------------------------------------------------+
- //| atr_momentum_indicator_mt4.mq4 |
- //| Copyright 2025, MQL Development |
- //| https://www.mqldevelopment.com/ |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2025, MQL Development"
- #property link "https://www.mqldevelopment.com/"
- #property version "1.00"
- #property strict
- #property indicator_separate_window
- #property indicator_buffers 3
- #property indicator_minimum 0.0
- //+------------------------------------------------------------------+
- //| Custom indicator initialization function |
- //+------------------------------------------------------------------+
- input string string_1 = "<><><><><><> General SETTINGS <><><><><><>"; //__
- input double threshold = 10.0; // Threshold Value
- sinput string atrSettings = " <><><><><> Atr Settings <><><><><> "; //_
- input int atrPeriod = 14; // ATR Period
- input string string_2_2 = "<><><><><><> Color Setting <><><><><><>"; //__
- input color atr_line_color = clrAqua; // Atr Line Color
- input color signal_color = clrLimeGreen; // Signal Color
- input color no_signal_color = clrSlateGray; // No Signal Color
- // Global Variables
- double atrPercentageBuffer[];
- double signalBuffer[];
- double noSignalBuffer[];
- string short_name="";
- int OnInit()
- {
- //--- indicator buffers mapping
- SetIndexBuffer(0, atrPercentageBuffer);
- SetIndexBuffer(1, signalBuffer);
- SetIndexBuffer(2, noSignalBuffer);
- //--- Set buffer names
- SetIndexLabel(0, "Atr %");
- SetIndexLabel(1, "Above threshold");
- SetIndexLabel(2, "Below threshold");
- SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, atr_line_color);
- SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 4, signal_color);
- SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, 4, no_signal_color);
- short_name = "atr_momentum_indicator_mt4";
- IndicatorShortName(short_name);
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Custom indicator iteration function |
- //+------------------------------------------------------------------+
- int OnCalculate(const int rates_total,
- const int prev_calculated,
- const datetime &time[],
- const double &open[],
- const double &high[],
- const double &low[],
- const double &close[],
- const long &tick_volume[],
- const long &volume[],
- const int &spread[])
- {
- //---
- int i, Counted_bars;
- Counted_bars=IndicatorCounted(); // Number of counted bars
- if(prev_calculated == 0)
- i=Bars-Counted_bars-10;
- else
- i=Bars-Counted_bars-1;
- while(i >= 0)
- {
- double atrValueIs = atrValue(i);
- double candle_0 = iClose(Symbol(), PERIOD_CURRENT, i);
- double candle_1 = iClose(Symbol(), PERIOD_CURRENT, i + 1);
- double candle_2 = iClose(Symbol(), PERIOD_CURRENT, i + 2);
- double candle_percentage_0 = atrPercentageValue(i);
- double candle_percentage_1 = atrPercentageValue(i + 1);
- double candle_percentage_2 = atrPercentageValue(i + 2);
- double value_1 = 0, value_2 = 0, value_3 = 0;
- if(candle_percentage_0 != 0 && candle_percentage_1 != 0 && candle_percentage_2 != 0)
- {
- value_1 = (((candle_percentage_1 - candle_percentage_2) / candle_percentage_2) * 100);
- value_2 = (((candle_percentage_0 - candle_percentage_1) / candle_percentage_1) * 100);
- value_3 = (((candle_percentage_0 - candle_percentage_2) / candle_percentage_2) * 100);
- }
- double totalSum = value_1 + value_2 + value_3;
- atrPercentageBuffer[i] = ((atrValueIs / candle_1) * 100);
- //Print(" atrValueIs: ",atrValueIs," candle_1: ",candle_1," final: ",atrPercentageBuffer[i]);
- atrPercentageBuffer[i] = atrPercentageBuffer[i] * 100;
- if(totalSum > threshold)
- {
- signalBuffer[i] = totalSum;
- noSignalBuffer[i] = 0;
- }
- else
- {
- noSignalBuffer[i] = totalSum;
- signalBuffer[i] = 0;
- }
- //Print("Time Current Candle: ", iClose(Symbol(), PERIOD_CURRENT,0),
- // " | Time Candle 1: ", iClose(Symbol(), PERIOD_CURRENT, 0 + 1),
- // " | Time Candle 2: ", iClose(Symbol(), PERIOD_CURRENT, 0 + 2),
- // " | ATR Value: ", atrValueIs,
- // " | value_1: ", value_1,
- // " | value_2: ", value_2,
- // " | value_3: ", value_3,
- // " | Sum: ", NormalizeDouble(totalSum, Digits()),
- // " | i: ", i);
- i--;
- }
- //--- return value of prev_calculated for next call
- return(rates_total);
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double atrValue(int index)
- {
- return NormalizeDouble(iATR(Symbol(), PERIOD_CURRENT, atrPeriod, index), Digits());
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double atrPercentageValue(int index)
- {
- // Print(" Atr Percentage: ", (iATR(Symbol(), PERIOD_CURRENT, atrPeriod, index) / iClose(Symbol(), PERIOD_CURRENT, index)) * 100, " i: ", index);
- return (iATR(Symbol(), PERIOD_CURRENT, atrPeriod, index) / iClose(Symbol(), PERIOD_CURRENT, index + 1)) * 100;
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
|