Files
-mql5_scripts/Average Intraday Range - script for MetaTrader 5/average_intraday_range.mq5
T

86 lines
6.6 KiB
Plaintext

//+==================================================================+
//| Average Intraday Range |
//| Vinicius Oliveira |
//+==================================================================+
//=== Program properties
#property copyright "Copyright © 2021, Vinicius Oliveira - V1N1"
#property link "https://www.mql5.com/en/users/vinicius-fx"
#property version "1.00"
#property strict
#property script_show_inputs
//=== Input parameters
input int iFromHour = 8; // From (Hour)
input int iToHour = 17; // To (Hour)
input int iDaysNum = 5; // Days
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Local variables
int Start = 1, Idx, FromIdxAdd = 0, FromHour, ToHour, DaysNum = iDaysNum, FromShift, ToShift;
double ValueAIR = 0.0, MaxPrice, MinPrice;
datetime FromTime, ToTime;
//--- Mql Structures
MqlDateTime CurHr;
//--- Checks parameters
if(iFromHour < 0 || iFromHour > 23 || iToHour < 0 || iToHour > 23)
{
Print("Invalid period ...");
return;
}
if(iDaysNum < 1)
{
Print("Invalid number of days ...");
return;
}
//--- Checks if the current day will be counted
if(!TimeToStruct(iTime(_Symbol, PERIOD_H1, 0), CurHr))
{
Print("Datetime conversion error ...");
return;
}
if(CurHr.hour > iToHour)
{
Start--;
DaysNum--;
}
//--- Checks if the period is from the previous day
if(iFromHour >= iToHour) {FromIdxAdd++;}
//--- Converts hours to seconds
FromHour = iFromHour * PeriodSeconds(PERIOD_H1);
ToHour = iToHour * PeriodSeconds(PERIOD_H1);
//--- Calculates the sum of intraday range
for(Idx = Start; Idx <= DaysNum; Idx++)
{
//--- Gets the period
FromTime = iTime(_Symbol, PERIOD_D1, Idx + FromIdxAdd) + FromHour;
ToTime = iTime(_Symbol, PERIOD_D1, Idx) + ToHour;
//--- Gets the top and the bottom of the period
FromShift = iBarShift(_Symbol, PERIOD_H1, FromTime, false); if(FromShift < 0) {Print("Time not found in available history ..."); return;}
ToShift = iBarShift(_Symbol, PERIOD_H1, ToTime, false); if(ToShift < 0) {Print("Time not found in available history ..."); return;}
MaxPrice = iHigh(_Symbol, PERIOD_H1, iHighest(_Symbol, PERIOD_H1, MODE_HIGH, FromShift - ToShift, ToShift + 1));
MinPrice = iLow (_Symbol, PERIOD_H1, iLowest (_Symbol, PERIOD_H1, MODE_LOW, FromShift - ToShift, ToShift + 1));
//--- Updates the sum of intraday range
ValueAIR += MaxPrice - MinPrice;
}
//--- Calculates the average intraday range (in points)
ValueAIR /= iDaysNum;
Alert("AIR = ", int(MathRound(ValueAIR / _Point)), " points.");
Print("AIR = ", int(MathRound(ValueAIR / _Point)), " points.");
}
//+------------------------------------------------------------------+
//| Script End |
//+------------------------------------------------------------------+