mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-20 15:58:07 +00:00
refactor: Added Percent mode
This commit is contained in:
@@ -4,76 +4,66 @@
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
#property link ""
|
||||
#property version "2.00"
|
||||
#property description "Professional Average True Range (ATR) with selectable"
|
||||
#property description "candle source (Standard or Heikin Ashi)."
|
||||
#property version "2.10" // Added Percent mode
|
||||
#property description "Professional Average True Range (ATR) with selectable display mode."
|
||||
|
||||
//--- Indicator Window and Plot Properties ---
|
||||
#property indicator_separate_window
|
||||
#property indicator_buffers 1
|
||||
#property indicator_plots 1
|
||||
|
||||
//--- Plot 1: ATR line
|
||||
#property indicator_label1 "ATR"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrDodgerBlue
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
|
||||
//--- Include the calculator engine ---
|
||||
#include <MyIncludes\ATR_Calculator.mqh>
|
||||
|
||||
//--- Enum for selecting the candle source for calculation ---
|
||||
enum ENUM_CANDLE_SOURCE
|
||||
{
|
||||
CANDLE_STANDARD, // Use standard OHLC data
|
||||
CANDLE_HEIKIN_ASHI // Use Heikin Ashi smoothed data
|
||||
};
|
||||
|
||||
//--- Input Parameters ---
|
||||
input int InpAtrPeriod = 14; // ATR Period
|
||||
input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; // Candle source
|
||||
input int InpAtrPeriod = 14; // ATR Period
|
||||
input ENUM_ATR_DISPLAY_MODE InpDisplayMode = ATR_POINTS; // Display Mode (Points or Percent)
|
||||
input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; // Candle source
|
||||
|
||||
//--- Indicator Buffers ---
|
||||
double BufferATR[];
|
||||
|
||||
//--- Global calculator object (as a base class pointer) ---
|
||||
//--- Global calculator object ---
|
||||
CATRCalculator *g_calculator;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Custom indicator initialization function. |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit()
|
||||
{
|
||||
//--- Map the buffer and set as non-timeseries
|
||||
SetIndexBuffer(0, BufferATR, INDICATOR_DATA);
|
||||
ArraySetAsSeries(BufferATR, false);
|
||||
|
||||
//--- Dynamically create the appropriate calculator instance
|
||||
switch(InpCandleSource)
|
||||
{
|
||||
case CANDLE_HEIKIN_ASHI:
|
||||
g_calculator = new CATRCalculator_HA();
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("ATR HA(%d)", InpAtrPeriod));
|
||||
break;
|
||||
default: // CANDLE_STANDARD
|
||||
default:
|
||||
g_calculator = new CATRCalculator();
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("ATR(%d)", InpAtrPeriod));
|
||||
break;
|
||||
}
|
||||
|
||||
//--- Check if creation was successful and initialize
|
||||
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpAtrPeriod))
|
||||
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpAtrPeriod, InpDisplayMode))
|
||||
{
|
||||
Print("Failed to create or initialize ATR Calculator object.");
|
||||
return(INIT_FAILED);
|
||||
}
|
||||
|
||||
//--- Set indicator display properties
|
||||
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
||||
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_calculator.GetPeriod());
|
||||
if(InpDisplayMode == ATR_PERCENT)
|
||||
{
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("ATR%% %s(%d)", (InpCandleSource == CANDLE_HEIKIN_ASHI ? "HA " : ""), InpAtrPeriod));
|
||||
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("ATR %s(%d)", (InpCandleSource == CANDLE_HEIKIN_ASHI ? "HA " : ""), InpAtrPeriod));
|
||||
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
||||
}
|
||||
|
||||
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_calculator.GetPeriod());
|
||||
return(INIT_SUCCEEDED);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user