Files
mql5/Indicators/MyIndicators/FisherTransform.mq5
T
2025-08-13 15:42:02 +02:00

181 lines
12 KiB
Plaintext

//+------------------------------------------------------------------+
//| FisherTransform.mq5 |
//| Copyright 2025, xxxxxxxx |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
#property version "1.01" // Switched to line drawing for clarity
#property description "Fisher Transform Oscillator"
//--- Indicator Window and Level Properties ---
#property indicator_separate_window
#property indicator_level1 1.5
#property indicator_level2 0.75
#property indicator_level3 0.0
#property indicator_level4 -0.75
#property indicator_level5 -1.5
#property indicator_levelstyle STYLE_DOT
//--- Buffers and Plots ---
// --- FIX: Reduced buffers as color buffer is no longer needed ---
#property indicator_buffers 3 // Fisher, Trigger, and 1 calculation buffer
#property indicator_plots 2
//--- Plot 1: Fisher line
#property indicator_label1 "Fisher"
#property indicator_type1 DRAW_LINE // FIX: Changed to DRAW_LINE
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- Plot 2: Trigger line
#property indicator_label2 "Trigger"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrange
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- Input Parameters ---
input int InpLength = 9; // Length
//--- Indicator Buffers ---
double BufferFisher[];
double BufferTrigger[];
double BufferValue[]; // Calculation buffer for the intermediate 'value'
//--- Global Variables ---
int ExtLength;
//--- Forward declarations for helper functions ---
double Highest(const double &array[], int period, int current_pos);
double Lowest(const double &array[], int period, int current_pos);
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+
void OnInit()
{
//--- Validate and store input
ExtLength = (InpLength < 1) ? 1 : InpLength;
//--- Map the buffers
SetIndexBuffer(0, BufferFisher, INDICATOR_DATA);
SetIndexBuffer(1, BufferTrigger, INDICATOR_DATA);
SetIndexBuffer(2, BufferValue, INDICATOR_CALCULATIONS);
//--- Set all buffers to non-timeseries for stable calculation
ArraySetAsSeries(BufferFisher, false);
ArraySetAsSeries(BufferTrigger, false);
ArraySetAsSeries(BufferValue, false);
//--- Set indicator properties
IndicatorSetInteger(INDICATOR_DIGITS, 4); // Increased precision for better accuracy
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtLength);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, ExtLength + 1);
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Fisher(%d)", ExtLength));
}
//+------------------------------------------------------------------+
//| Fisher Transform calculation 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[])
{
//--- Check for enough data
if(rates_total < ExtLength)
return(0);
//--- Create a buffer for HL2 price
double hl2[];
ArrayResize(hl2, rates_total);
for(int i=0; i<rates_total; i++)
{
hl2[i] = (high[i] + low[i]) / 2.0;
}
//--- Main calculation loop (full recalculation for stability)
for(int i = 1; i < rates_total; i++) // Start from 1 to access i-1
{
if(i < ExtLength)
{
BufferValue[i] = 0;
BufferFisher[i] = 0;
continue;
}
double high_ = Highest(hl2, ExtLength, i);
double low_ = Lowest(hl2, ExtLength, i);
double range = high_ - low_;
if(range < _Point)
range = _Point;
double price_pos = 0;
if(range > 0)
price_pos = (hl2[i] - low_) / range - 0.5;
// Recursive smoothing for 'value'
BufferValue[i] = 0.33 * 2 * price_pos + 0.67 * BufferValue[i-1];
// Clamp the value to prevent log() errors
if(BufferValue[i] > 0.999)
BufferValue[i] = 0.999;
if(BufferValue[i] < -0.999)
BufferValue[i] = -0.999;
// Fisher Transform with recursive smoothing
double log_val = 0.5 * MathLog((1 + BufferValue[i]) / (1 - BufferValue[i]));
BufferFisher[i] = log_val + 0.5 * BufferFisher[i-1];
// The trigger is the previous Fisher value
BufferTrigger[i] = BufferFisher[i-1];
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Finds the highest value in a given period of an array. |
//+------------------------------------------------------------------+
double Highest(const double &array[], int period, int current_pos)
{
double res = array[current_pos];
for(int i = 1; i < period; i++)
{
int index = current_pos - i;
if(index < 0)
break;
if(res < array[index])
res = array[index];
}
return(res);
}
//+------------------------------------------------------------------+
//| Finds the lowest value in a given period of an array. |
//+------------------------------------------------------------------+
double Lowest(const double &array[], int period, int current_pos)
{
double res = array[current_pos];
for(int i = 1; i < period; i++)
{
int index = current_pos - i;
if(index < 0)
break;
if(res > array[index])
res = array[index];
}
return(res);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+