gryps2/IND-code/HS-I-001-SPREAD-HISTORY/HS-I-001-SPREAD-HISTORY.mq4

140 lines
11 KiB
MQL4
Raw Permalink Normal View History

2025-05-30 14:58:21 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Spread History |
//| |
//| Copyright (c) 2024, HANDY SYSTEMS |
//| |
//| Permission is hereby granted, free of charge, to any person |
//| obtaining a copy of this software and associated documentation |
//| files (the "Software"), to deal in the Software without |
//| restriction, including without limitation the rights to use, |
//| copy, modify, merge, publish, distribute, sublicense, and/or |
//| sell copies of the Software, and to permit persons to whom the |
//| Software is furnished to do so, subject to the following |
//| conditions: |
//| |
//| The above copyright notice and this permission notice shall be |
//| included in all copies or substantial portions of the Software. |
//| |
//| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
//| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
//| OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
//| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
//| HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
//| WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
//| FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
//| OTHER DEALINGS IN THE SOFTWARE. |
//+------------------------------------------------------------------+
// Comment out from here when displaying logo on the property dialog
//#define _DO_NOT_USE_LOGO
// Comment out to here when displaying logo on the property dialog
#property copyright "Copyright (c) 2024, HANDY SYSTEMS, This software is released under the MIT License."
#ifndef _DO_NOT_USE_LOGO
#property icon "HANDY-SYSTEMS-LOGO.ico"
#endif // _DO_NOT_USE_LOGO
#property version "1.0"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2
//--- plot Spread (Actual)
#property indicator_label1 "Spread (Actual)"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot Spread (Estimated)
#property indicator_label2 "Spread (Estimated)"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
//--- indicator buffers
double SpreadBuffer[];
double EstimatedSpreadBuffer[];
//--- parameters
input string IndicatorName = "Spread History"; // Indicator Name
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, SpreadBuffer, INDICATOR_DATA);
SetIndexBuffer(1, EstimatedSpreadBuffer, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
IndicatorSetString(INDICATOR_SHORTNAME, IndicatorName);
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[])
{
double spreadValue = 0.0;
// Initialize indicator arrays
if (prev_calculated == 0)
{
ArrayInitialize(SpreadBuffer, EMPTY_VALUE);
ArrayInitialize(EstimatedSpreadBuffer, EMPTY_VALUE);
}
if (prev_calculated == rates_total) // Latest bar
{
// Get the actual spread value in real-time
spreadValue = MarketInfo(_Symbol, MODE_SPREAD) * 0.1;
// Set the value into the arrays
SpreadBuffer[0] = spreadValue;
EstimatedSpreadBuffer[0] = EMPTY_VALUE;
}
else // Historical bars
{
for (int i = prev_calculated; i < rates_total; i++)
{
if (SpreadBuffer[i] == EMPTY_VALUE) // Calculate estimated value only if actual value is not saved
{
double askPrice = iClose(NULL, 0, i);
double bidPrice = askPrice - (iHigh(NULL, 0, i) - iLow(NULL, 0, i));
// Calculate the estimated spread value from the history
spreadValue = (askPrice - bidPrice) / _Point;
// Adjust the spread value according to the price precision of the currency
if (_Digits == 3 || _Digits == 5)
spreadValue /= 10;
// Set the value into the arrays
EstimatedSpreadBuffer[i] = NormalizeDouble(spreadValue, 1);
}
else // Use the saved actual value
{
EstimatedSpreadBuffer[i] = EMPTY_VALUE;
}
}
}
return(rates_total);
}