0% found this document useful (0 votes)
61 views17 pages

StatMap Code

The document is a script for a trading indicator called CombinedLevels, designed for the MetaTrader 5 platform. It calculates and plots daily, weekly, 4-hour, and monthly levels based on historical price data, allowing traders to visualize potential support and resistance levels. The script includes various input parameters for customization and functions to draw trend lines and labels on the chart based on calculated averages and market conditions.

Uploaded by

Jonathan Austin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views17 pages

StatMap Code

The document is a script for a trading indicator called CombinedLevels, designed for the MetaTrader 5 platform. It calculates and plots daily, weekly, 4-hour, and monthly levels based on historical price data, allowing traders to visualize potential support and resistance levels. The script includes various input parameters for customization and functions to draw trend lines and labels on the chart based on calculated averages and market conditions.

Uploaded by

Jonathan Austin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

//+------------------------------------------------------------------+

//| CombinedLevels.mq5 |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window

input int ADRPeriod = 10; // For daily/weekly levels


input int DaysPeriod = 60; // Number of days to look back for 4H levels
input int MonthsPeriod = 60; // Number of months to look back for monthly levels
input int DayOpenHour = 7;
input int DayCloseHour = 19;
input int DayOpenMinute = 30;
bool DailyLevels = true;
bool WeeklyLevels = true;

MqlRates rates[], rates1[], ratesW[], ratesH4[], ratesMN[];


MqlDateTime TimeStruct, TimeStruct1;
int Number_Days = ADRPeriod;
datetime glLastBarTimeD1, glLastBarTimeM15, glLastBarTimeW, glLastBarTimeH4,
glLastBarTimeMN, positionOpenTime;
bool glBuyPlaced, glSellPlaced;

// Define 4-hour open times in NY time (UTC-5 or UTC-4 depending on DST)


int OpenTimes[] = {18, 22, 2, 6, 10, 14}; // UTC equivalent: 23, 3, 7, 11, 15, 19
(assuming UTC)

int OnInit()
{
ArraySetAsSeries(rates, true);
ArraySetAsSeries(rates1, true);
ArraySetAsSeries(ratesW, true);
ArraySetAsSeries(ratesH4, true);
ArraySetAsSeries(ratesMN, true);
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)


{
ObjectsDeleteAll(0, "StatMap");
ObjectsDeleteAll(0, "Avg4HLevel");
ObjectsDeleteAll(0, "MonthlyLevels");
}

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[])
{
CopyRates(_Symbol, PERIOD_D1, 0, Number_Days, rates);
CopyRates(_Symbol, PERIOD_W1, 0, Number_Days, ratesW);
CopyRates(_Symbol, PERIOD_M15, 0, 700, rates1);
CopyRates(_Symbol, PERIOD_H4, 0, DaysPeriod * 6, ratesH4); // Assuming 6
candles per day (24 hours / 4 hours)
CopyRates(_Symbol, PERIOD_MN1, 0, MonthsPeriod, ratesMN);

TimeToStruct(rates[1].time, TimeStruct);
TimeCurrent(TimeStruct1);

bool newBarD1 = false;


if (glLastBarTimeD1 != rates[0].time)
{
if (glLastBarTimeD1 > 0)
newBarD1 = true;
glLastBarTimeD1 = rates[0].time;
}

bool newBarW = false;


if (glLastBarTimeW != ratesW[0].time)
{
if (glLastBarTimeW > 0)
newBarW = true;
glLastBarTimeW = ratesW[0].time;
}

bool newBarM15 = false;


if (glLastBarTimeM15 != rates1[0].time)
{
if (glLastBarTimeM15 > 0)
newBarM15 = true;
glLastBarTimeM15 = rates1[0].time;
}

bool newBarH4 = false;


if (glLastBarTimeH4 != ratesH4[0].time)
{
if (glLastBarTimeH4 > 0)
newBarH4 = true;
glLastBarTimeH4 = ratesH4[0].time;
}

bool newBarMN = false;


if (glLastBarTimeMN != ratesMN[0].time)
{
if (glLastBarTimeMN > 0)
newBarMN = true;
glLastBarTimeMN = ratesMN[0].time;
}

// Calculate averages for daily/weekly levels


double averageBullDist = NormalizeDouble(AverageOfBullDist(Number_Days),
_Digits);
double averageBearMani = NormalizeDouble(AverageOfBearMani(Number_Days),
_Digits);
double averageBearDist = NormalizeDouble(AverageOfBearDist(Number_Days),
_Digits);
double averageBullMani = NormalizeDouble(AverageOfBullMani(Number_Days),
_Digits);
double averageBullOpen = NormalizeDouble(AverageOfBullOpens(Number_Days),
_Digits);
double averageBearOpen = NormalizeDouble(AverageOfBearOpens(Number_Days),
_Digits);
double averageWeekBullDist =
NormalizeDouble(AverageOfWeekBullDist(Number_Days), _Digits);
double averageWeekBearMani =
NormalizeDouble(AverageOfWeekBearMani(Number_Days), _Digits);
double averageWeekBearDist =
NormalizeDouble(AverageOfWeekBearDist(Number_Days), _Digits);
double averageWeekBullMani =
NormalizeDouble(AverageOfWeekBullMani(Number_Days), _Digits);
double averageWeekBullOpen =
NormalizeDouble(AverageOfWeekBullOpens(Number_Days), _Digits);
double averageWeekBearOpen =
NormalizeDouble(AverageOfWeekBearOpens(Number_Days), _Digits);

datetime HighOpenTime = StructToTime(TimeStruct1);


datetime HighCloseTime = HighOpenTime + ((DayCloseHour - DayOpenHour) *
PeriodSeconds(PERIOD_H1));
datetime HighWeekCloseTime = HighOpenTime + 5 * PeriodSeconds(PERIOD_D1);

// Plot daily/weekly levels


if (TimeStruct1.hour == DayOpenHour && TimeStruct1.min == DayOpenMinute &&
TimeStruct1.sec == 00 && DailyLevels == true)
{
double DayOpen = iOpen(_Symbol, PERIOD_M15, 1);
double TrueBullDist = DayOpen + (averageBullDist - averageBullOpen);
double TrueBearMan = DayOpen + (averageBearMani - averageBearOpen);
double TrueBearDist = DayOpen - (averageBearOpen - averageBearDist);
double TrueBullMan = DayOpen - (averageBullOpen - averageBullMani);

DrawTrendLine("TrueBullDist", HighOpenTime, TrueBullDist, HighCloseTime,


TrueBullDist, clrRed, STYLE_SOLID);
DrawText("BullDist", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueBullDist, "1D +D", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("TrueBearMan", HighOpenTime, TrueBearMan, HighCloseTime,


TrueBearMan, clrGray, STYLE_SOLID);
DrawText("BearMan", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueBearMan, "1D -M", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("TrueBullMan", HighOpenTime, TrueBullMan, HighCloseTime,


TrueBullMan, clrGray, STYLE_SOLID);
DrawText("BullMan", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueBullMan, "1D +M", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("TrueBearDist", HighOpenTime, TrueBearDist, HighCloseTime,


TrueBearDist, clrRed, STYLE_SOLID);
DrawText("BearDist", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueBearDist, "1D -D", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("OpenLine", HighOpenTime, DayOpen, HighCloseTime, DayOpen,


clrGreen, STYLE_DOT);
DrawText("OpenLabel", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
DayOpen, "1D 0", clrBlack, 8, ANCHOR_CENTER);
}
if (newBarD1 == true)
{
ObjectsDeleteAll(0, "StatMap");
}

if (TimeStruct1.day_of_week == 1 && TimeStruct1.hour == DayOpenHour &&


TimeStruct1.min == DayOpenMinute && TimeStruct1.sec == 00 && WeeklyLevels == true)
{
double DayOpen = iOpen(_Symbol, PERIOD_M15, 1);
double TrueWeekBullDist = DayOpen + (averageWeekBullDist -
averageWeekBullOpen);
double TrueWeekBearMan = DayOpen + (averageWeekBearMani -
averageWeekBearOpen);
double TrueWeekBearDist = DayOpen - (averageWeekBearOpen -
averageWeekBearDist);
double TrueWeekBullMan = DayOpen - (averageWeekBullOpen -
averageWeekBullMani);

DrawTrendLine("WeekTrueBullDist", HighOpenTime, TrueWeekBullDist,


HighWeekCloseTime, TrueWeekBullDist, clrRed, STYLE_SOLID);
DrawText("WeekBullDist", HighWeekCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueWeekBullDist, "1W +D", clrBlack, 8, ANCHOR_CENTER, false, false);

DrawTrendLine("WeekTrueBearMan", HighOpenTime, TrueWeekBearMan,


HighWeekCloseTime, TrueWeekBearMan, clrGray, STYLE_SOLID);
DrawText("WeekBearMan", HighWeekCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueWeekBearMan, "1W -M", clrBlack, 8, ANCHOR_CENTER, false, false);

DrawTrendLine("WeekTrueBullMan", HighOpenTime, TrueWeekBullMan,


HighWeekCloseTime, TrueWeekBullMan, clrGray, STYLE_SOLID);
DrawText("WeekBullMan", HighWeekCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueWeekBullMan, "1W +M", clrBlack, 8, ANCHOR_CENTER, false, false);

DrawTrendLine("WeekTrueBearDist", HighOpenTime, TrueWeekBearDist,


HighWeekCloseTime, TrueWeekBearDist, clrRed, STYLE_SOLID);
DrawText("WeekBearDist", HighWeekCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueWeekBearDist, "1W -D", clrBlack, 8, ANCHOR_CENTER, false, false);

DrawTrendLine("WeekOpenLine", HighOpenTime, DayOpen, HighWeekCloseTime,


DayOpen, clrGreen, STYLE_DOT);
DrawText("WeekOpenLabel", HighWeekCloseTime + 3 * PeriodSeconds(PERIOD_H1),
DayOpen, "1W 0", clrBlack, 8, ANCHOR_CENTER, false, false);
}
if (newBarW == true)
{
ObjectsDeleteAll(0, "StatMapWeek");
}

// Plot 4-hour levels


for (int i = 0; i < ArraySize(OpenTimes); i++)
{
datetime H4OpenTime = NormalizeTime(HighOpenTime, OpenTimes[i]);
datetime H4CloseTime = H4OpenTime + PeriodSeconds(PERIOD_H4);

double averageBullDistH4 = NormalizeDouble(AverageOfBullDistH4(DaysPeriod,


OpenTimes[i]), _Digits);
double averageBearManiH4 = NormalizeDouble(AverageOfBearManiH4(DaysPeriod,
OpenTimes[i]), _Digits);
double averageBearDistH4 = NormalizeDouble(AverageOfBearDistH4(DaysPeriod,
OpenTimes[i]), _Digits);
double averageBullManiH4 = NormalizeDouble(AverageOfBullManiH4(DaysPeriod,
OpenTimes[i]), _Digits);
double averageBullOpenH4 = NormalizeDouble(AverageOfBullOpensH4(DaysPeriod,
OpenTimes[i]), _Digits);
double averageBearOpenH4 = NormalizeDouble(AverageOfBearOpensH4(DaysPeriod,
OpenTimes[i]), _Digits);

double DayOpenH4 = iOpen(_Symbol, PERIOD_H4, 1);


double TrueBullDistH4 = DayOpenH4 + (averageBullDistH4 -
averageBullOpenH4);
double TrueBearManH4 = DayOpenH4 + (averageBearManiH4 - averageBearOpenH4);
double TrueBearDistH4 = DayOpenH4 - (averageBearOpenH4 -
averageBearDistH4);
double TrueBullManH4 = DayOpenH4 - (averageBullOpenH4 - averageBullManiH4);

DrawTrendLine("Avg4HLevelTrueBullDist" + string(i), H4OpenTime,


TrueBullDistH4, H4CloseTime, TrueBullDistH4, clrRed, STYLE_SOLID);
DrawText("Avg4HLevelBullDist" + string(i), H4CloseTime + 3 *
PeriodSeconds(PERIOD_H1), TrueBullDistH4, "4H +D", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("Avg4HLevelTrueBearMan" + string(i), H4OpenTime,


TrueBearManH4, H4CloseTime, TrueBearManH4, clrGray, STYLE_SOLID);
DrawText("Avg4HLevelBearMan" + string(i), H4CloseTime + 3 *
PeriodSeconds(PERIOD_H1), TrueBearManH4, "4H -M", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("Avg4HLevelTrueBullMan" + string(i), H4OpenTime,


TrueBullManH4, H4CloseTime, TrueBullManH4, clrGray, STYLE_SOLID);
DrawText("Avg4HLevelBullMan" + string(i), H4CloseTime + 3 *
PeriodSeconds(PERIOD_H1), TrueBullManH4, "4H +M", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("Avg4HLevelTrueBearDist" + string(i), H4OpenTime,


TrueBearDistH4, H4CloseTime, TrueBearDistH4, clrRed, STYLE_SOLID);
DrawText("Avg4HLevelBearDist" + string(i), H4CloseTime + 3 *
PeriodSeconds(PERIOD_H1), TrueBearDistH4, "4H -D", clrBlack, 8, ANCHOR_CENTER);
}

// Plot monthly levels using percentage change


if (TimeStruct1.day == 1 && TimeStruct1.hour == DayOpenHour && TimeStruct1.min
== DayOpenMinute && TimeStruct1.sec == 00)
{
double DayOpen = iOpen(_Symbol, PERIOD_MN1, 1);
double TrueMonthBullDist = DayOpen * (1 +
AverageOfMonthlyPercentageChange(MonthsPeriod, true, true) / 100);
double TrueMonthBearMan = DayOpen * (1 +
AverageOfMonthlyPercentageChange(MonthsPeriod, false, true) / 100);
double TrueMonthBearDist = DayOpen * (1 +
AverageOfMonthlyPercentageChange(MonthsPeriod, false, false) / 100);
double TrueMonthBullMan = DayOpen * (1 +
AverageOfMonthlyPercentageChange(MonthsPeriod, true, false) / 100);

DrawTrendLine("MonthTrueBullDist", HighOpenTime, TrueMonthBullDist,


HighCloseTime, TrueMonthBullDist, clrRed, STYLE_SOLID);
DrawText("MonthBullDist", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueMonthBullDist, "1M +D", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("MonthTrueBearMan", HighOpenTime, TrueMonthBearMan,


HighCloseTime, TrueMonthBearMan, clrGray, STYLE_SOLID);
DrawText("MonthBearMan", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueMonthBearMan, "1M -M", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("MonthTrueBullMan", HighOpenTime, TrueMonthBullMan,


HighCloseTime, TrueMonthBullMan, clrGray, STYLE_SOLID);
DrawText("MonthBullMan", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueMonthBullMan, "1M +M", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("MonthTrueBearDist", HighOpenTime, TrueMonthBearDist,


HighCloseTime, TrueMonthBearDist, clrRed, STYLE_SOLID);
DrawText("MonthBearDist", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
TrueMonthBearDist, "1M -D", clrBlack, 8, ANCHOR_CENTER);

DrawTrendLine("MonthOpenLine", HighOpenTime, DayOpen, HighCloseTime,


DayOpen, clrGreen, STYLE_DOT);
DrawText("MonthOpenLabel", HighCloseTime + 3 * PeriodSeconds(PERIOD_H1),
DayOpen, "1M 0", clrBlack, 8, ANCHOR_CENTER);
}
if (newBarMN == true)
{
ObjectsDeleteAll(0, "MonthlyLevels");
}

return(rates_total);
}

void DrawTrendLine(string name, datetime time1, double price1, datetime time2,


double price2, color col, ENUM_LINE_STYLE style)
{
ObjectCreate(0, name, OBJ_TREND, 0, time1, price1, time2, price2);
ObjectSetInteger(0, name, OBJPROP_COLOR, col);
ObjectSetInteger(0, name, OBJPROP_STYLE, style);
}

void DrawText(string name, datetime time, double price, string text, color col, int
fontsize, ENUM_ANCHOR_POINT anchor, bool selectable = true, bool selected = true)
{
ObjectCreate(0, name, OBJ_TEXT, 0, time, price);
ObjectSetString(0, name, OBJPROP_TEXT, text);
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, fontsize);
ObjectSetInteger(0, name, OBJPROP_ANCHOR, anchor);
ObjectSetInteger(0, name, OBJPROP_COLOR, col);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, selectable);
ObjectSetInteger(0, name, OBJPROP_SELECTED, selected);
}

double AverageOfBullDist(int pNumberOfDays)


{
int bars = iBars(_Symbol, PERIOD_D1);
int count = 0;
double dayhighs;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_D1, i);
TimeToStruct(barTime, TimeStruct);
if (TimeStruct.day_of_week == TimeStruct1.day_of_week && iOpen(_Symbol,
PERIOD_D1, i) < iClose(_Symbol, PERIOD_D1, i))
{
dayhighs = iHigh(_Symbol, PERIOD_D1, i);
sum += dayhighs;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBullDist(int pNumberOfWeeks)


{
int bars = iBars(_Symbol, PERIOD_W1);
int count = 0;
double weekhighs;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfWeeks; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_W1, i);
TimeToStruct(barTime, TimeStruct);
if (iOpen(_Symbol, PERIOD_W1, i) < iClose(_Symbol, PERIOD_W1, i))
{
weekhighs = iHigh(_Symbol, PERIOD_W1, i);
sum += weekhighs;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBearMani(int pNumberOfDays)


{
int bars = iBars(_Symbol, PERIOD_D1);
int count = 0;
double dayhighs;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_D1, i);
TimeToStruct(barTime, TimeStruct);
if (TimeStruct.day_of_week == TimeStruct1.day_of_week && iOpen(_Symbol,
PERIOD_D1, i) > iClose(_Symbol, PERIOD_D1, i))
{
dayhighs = iHigh(_Symbol, PERIOD_D1, i);
sum += dayhighs;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBearMani(int pNumberOfWeeks)


{
int bars = iBars(_Symbol, PERIOD_W1);
int count = 0;
double weekhighs;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfWeeks; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_W1, i);
TimeToStruct(barTime, TimeStruct);
if (iOpen(_Symbol, PERIOD_W1, i) > iClose(_Symbol, PERIOD_W1, i))
{
weekhighs = iHigh(_Symbol, PERIOD_W1, i);
sum += weekhighs;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBearDist(int pNumberOfDays)


{
int bars = iBars(_Symbol, PERIOD_D1);
int count = 0;
double daylows;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_D1, i);
TimeToStruct(barTime, TimeStruct);
if (TimeStruct.day_of_week == TimeStruct1.day_of_week && iOpen(_Symbol,
PERIOD_D1, i) > iClose(_Symbol, PERIOD_D1, i))
{
daylows = iLow(_Symbol, PERIOD_D1, i);
sum += daylows;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBearDist(int pNumberOfWeeks)


{
int bars = iBars(_Symbol, PERIOD_W1);
int count = 0;
double weeklows;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfWeeks; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_W1, i);
TimeToStruct(barTime, TimeStruct);
if (iOpen(_Symbol, PERIOD_W1, i) > iClose(_Symbol, PERIOD_W1, i))
{
weeklows = iLow(_Symbol, PERIOD_W1, i);
sum += weeklows;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}
double AverageOfBullMani(int pNumberOfDays)
{
int bars = iBars(_Symbol, PERIOD_D1);
int count = 0;
double daylows;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_D1, i);
TimeToStruct(barTime, TimeStruct);
if (TimeStruct.day_of_week == TimeStruct1.day_of_week && iOpen(_Symbol,
PERIOD_D1, i) < iClose(_Symbol, PERIOD_D1, i))
{
daylows = iLow(_Symbol, PERIOD_D1, i);
sum += daylows;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBullMani(int pNumberOfWeeks)


{
int bars = iBars(_Symbol, PERIOD_W1);
int count = 0;
double weeklows;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfWeeks; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_W1, i);
TimeToStruct(barTime, TimeStruct);
if (iOpen(_Symbol, PERIOD_W1, i) < iClose(_Symbol, PERIOD_W1, i))
{
weeklows = iLow(_Symbol, PERIOD_W1, i);
sum += weeklows;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBullOpens(int pNumberOfDays)


{
int bars = iBars(_Symbol, PERIOD_D1);
int count = 0;
double dayopens;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_D1, i);
TimeToStruct(barTime, TimeStruct);
if (TimeStruct.day_of_week == TimeStruct1.day_of_week && iOpen(_Symbol,
PERIOD_D1, i) < iClose(_Symbol, PERIOD_D1, i))
{
dayopens = iOpen(_Symbol, PERIOD_D1, i);
sum += dayopens;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBullOpens(int pNumberOfWeeks)


{
int bars = iBars(_Symbol, PERIOD_W1);
int count = 0;
double weekopens;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfWeeks; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_W1, i);
TimeToStruct(barTime, TimeStruct);
if (iOpen(_Symbol, PERIOD_W1, i) < iClose(_Symbol, PERIOD_W1, i))
{
weekopens = iOpen(_Symbol, PERIOD_W1, i);
sum += weekopens;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBearOpens(int pNumberOfDays)


{
int bars = iBars(_Symbol, PERIOD_D1);
int count = 0;
double daycloses;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_D1, i);
TimeToStruct(barTime, TimeStruct);
if (TimeStruct.day_of_week == TimeStruct1.day_of_week && iOpen(_Symbol,
PERIOD_D1, i) > iClose(_Symbol, PERIOD_D1, i))
{
daycloses = iOpen(_Symbol, PERIOD_D1, i);
sum += daycloses;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBearOpens(int pNumberOfWeeks)


{
int bars = iBars(_Symbol, PERIOD_W1);
int count = 0;
double weekcloses;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfWeeks; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_W1, i);
TimeToStruct(barTime, TimeStruct);
if (iOpen(_Symbol, PERIOD_W1, i) > iClose(_Symbol, PERIOD_W1, i))
{
weekcloses = iOpen(_Symbol, PERIOD_W1, i);
sum += weekcloses;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBullDistH4(int pNumberOfDays, int openTime)


{
int bars = iBars(_Symbol, PERIOD_H4);
int count = 0;
double high;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_H4, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.day_of_week == TimeStruct1.day_of_week &&
barTimeStruct.hour == openTime && iOpen(_Symbol, PERIOD_H4, i) < iClose(_Symbol,
PERIOD_H4, i))
{
high = iHigh(_Symbol, PERIOD_H4, i);
sum += high;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBearManiH4(int pNumberOfDays, int openTime)


{
int bars = iBars(_Symbol, PERIOD_H4);
int count = 0;
double high;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_H4, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.day_of_week == TimeStruct1.day_of_week &&
barTimeStruct.hour == openTime && iOpen(_Symbol, PERIOD_H4, i) > iClose(_Symbol,
PERIOD_H4, i))
{
high = iHigh(_Symbol, PERIOD_H4, i);
sum += high;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBearDistH4(int pNumberOfDays, int openTime)


{
int bars = iBars(_Symbol, PERIOD_H4);
int count = 0;
double low;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_H4, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.day_of_week == TimeStruct1.day_of_week &&
barTimeStruct.hour == openTime && iOpen(_Symbol, PERIOD_H4, i) > iClose(_Symbol,
PERIOD_H4, i))
{
low = iLow(_Symbol, PERIOD_H4, i);
sum += low;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBullManiH4(int pNumberOfDays, int openTime)


{
int bars = iBars(_Symbol, PERIOD_H4);
int count = 0;
double low;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_H4, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.day_of_week == TimeStruct1.day_of_week &&
barTimeStruct.hour == openTime && iOpen(_Symbol, PERIOD_H4, i) < iClose(_Symbol,
PERIOD_H4, i))
{
low = iLow(_Symbol, PERIOD_H4, i);
sum += low;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBullOpensH4(int pNumberOfDays, int openTime)


{
int bars = iBars(_Symbol, PERIOD_H4);
int count = 0;
double opens;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_H4, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.day_of_week == TimeStruct1.day_of_week &&
barTimeStruct.hour == openTime && iOpen(_Symbol, PERIOD_H4, i) < iClose(_Symbol,
PERIOD_H4, i))
{
opens = iOpen(_Symbol, PERIOD_H4, i);
sum += opens;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBearOpensH4(int pNumberOfDays, int openTime)


{
int bars = iBars(_Symbol, PERIOD_H4);
int count = 0;
double closes;
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfDays; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_H4, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.day_of_week == TimeStruct1.day_of_week &&
barTimeStruct.hour == openTime && iOpen(_Symbol, PERIOD_H4, i) > iClose(_Symbol,
PERIOD_H4, i))
{
closes = iOpen(_Symbol, PERIOD_H4, i);
sum += closes;
count++;
if (count > 0) average = sum / count;
}
}
return average;
}

double AverageOfMonthlyPercentageChange(int pNumberOfMonths, bool bull, bool dist)


{
int bars = iBars(_Symbol, PERIOD_MN1);
int count = 0;
double percentageChanges[60];
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfMonths; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_MN1, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.mon == TimeStruct1.mon)
{
double openPrice = iOpen(_Symbol, PERIOD_MN1, i);
double closePrice = iClose(_Symbol, PERIOD_MN1, i);
double percentageChange = (closePrice - openPrice) / openPrice * 100.0;
if ((bull && dist && closePrice > openPrice) ||
(bull && !dist && closePrice > openPrice) ||
(!bull && dist && closePrice < openPrice) ||
(!bull && !dist && closePrice < openPrice))
{
percentageChanges[count] = percentageChange;
sum += percentageChange;
count++;
}
}
}
if (count > 0) average = sum / count;
return average;
}

// Helper function to normalize time to broker's timezone


datetime NormalizeTime(datetime baseTime, int nyHour)
{
int brokerHour = TimeHour(baseTime);
int brokerMinute = TimeMinute(baseTime);
int brokerSecond = TimeSecond(baseTime);

// Convert NY time to UTC


int utcHour = nyHour + (IsDST() ? 4 : 5
if(count > 0) average = sum / count;
}
return average;
}

double AverageOfWeekBullMani(int pNumberOfWeeks)


{
int bars = iBars(_Symbol,PERIOD_W1);
int count = 0;
double weeklows;
double average = 0;
double sum = 0;
for(int i = 1; i < bars && count < pNumberOfWeeks;i++)
{
datetime barTime = iTime(_Symbol,PERIOD_W1,i);
TimeToStruct(barTime,TimeStruct);
if(iOpen(_Symbol,PERIOD_W1,i) < iClose(_Symbol,PERIOD_W1,i))
{
weeklows = iLow(_Symbol,PERIOD_W1,i);
sum += weeklows;
count++;
if(count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBullOpens(int pNumberOfDays)


{
int bars = iBars(_Symbol,PERIOD_D1);
int count = 0;
double dayopens;
double average = 0;
double sum = 0;
for(int i = 1; i < bars && count < pNumberOfDays;i++)
{
datetime barTime = iTime(_Symbol,PERIOD_D1,i);
TimeToStruct(barTime,TimeStruct);
if(TimeStruct.day_of_week == TimeStruct1.day_of_week &&
iOpen(_Symbol,PERIOD_D1,i) < iClose(_Symbol,PERIOD_D1,i))
{
dayopens = iOpen(_Symbol,PERIOD_D1,i);
sum += dayopens;
count++;
if(count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBullOpens(int pNumberOfWeeks)


{
int bars = iBars(_Symbol,PERIOD_W1);
int count = 0;
double weekopens;
double average = 0;
double sum = 0;
for(int i = 1; i < bars && count < pNumberOfWeeks;i++)
{
datetime barTime = iTime(_Symbol,PERIOD_W1,i);
TimeToStruct(barTime,TimeStruct);
if(iOpen(_Symbol,PERIOD_W1,i) < iClose(_Symbol,PERIOD_W1,i))
{
weekopens = iOpen(_Symbol,PERIOD_W1,i);
sum += weekopens;
count++;
if(count > 0) average = sum / count;
}
}
return average;
}

double AverageOfBearOpens(int pNumberOfDays)


{
int bars = iBars(_Symbol,PERIOD_D1);
int count = 0;
double daycloses;
double average = 0;
double sum = 0;
for(int i = 1; i < bars && count < pNumberOfDays;i++)
{
datetime barTime = iTime(_Symbol,PERIOD_D1,i);
TimeToStruct(barTime,TimeStruct);
if(TimeStruct.day_of_week == TimeStruct1.day_of_week &&
iOpen(_Symbol,PERIOD_D1,i) > iClose(_Symbol,PERIOD_D1,i))
{
daycloses = iOpen(_Symbol,PERIOD_D1,i);
sum += daycloses;
count++;
if(count > 0) average = sum / count;
}
}
return average;
}

double AverageOfWeekBearOpens(int pNumberOfWeeks)


{
int bars = iBars(_Symbol,PERIOD_W1);
int count = 0;
double weekcloses;
double average = 0;
double sum = 0;
for(int i = 1; i < bars && count < pNumberOfWeeks;i++)
{
datetime barTime = iTime(_Symbol,PERIOD_W1,i);
TimeToStruct(barTime,TimeStruct);
if(iOpen(_Symbol,PERIOD_W1,i) > iClose(_Symbol,PERIOD_W1,i))
{
weekcloses = iOpen(_Symbol,PERIOD_W1,i);
sum += weekcloses;
count++;
if(count > 0) average = sum / count;
}
}
return average;
}

double AverageOfMonthlyPercentageChange(int pNumberOfMonths, bool bull, bool dist)


{
int bars = iBars(_Symbol, PERIOD_MN1);
int count = 0;
double percentageChanges[60];
double average = 0;
double sum = 0;
for (int i = 1; i < bars && count < pNumberOfMonths; i++)
{
datetime barTime = iTime(_Symbol, PERIOD_MN1, i);
MqlDateTime barTimeStruct;
TimeToStruct(barTime, barTimeStruct);
if (barTimeStruct.mon == TimeStruct1.mon)
{
double openPrice = iOpen(_Symbol, PERIOD_MN1, i);
double closePrice = iClose(_Symbol, PERIOD_MN1, i);
double percentageChange = (closePrice - openPrice) / openPrice * 100.0;
if ((bull && dist && closePrice > openPrice) ||
(bull && !dist && closePrice > openPrice) ||
(!bull && dist && closePrice < openPrice) ||
(!bull && !dist && closePrice < openPrice))
{
percentageChanges[count] = percentageChange;
sum += percentageChange;
count++;
}
}
}
if (count > 0) average = sum / count;
return average;
}

// Helper function to normalize time to broker's timezone


datetime NormalizeTime(datetime baseTime, int nyHour)
{
int brokerHour = TimeHour(baseTime);
int brokerMinute = TimeMinute(baseTime);
int brokerSecond = TimeSecond(baseTime);

// Convert NY time to UTC


int utcHour = nyHour + (IsDST() ? 4 : 5); // Adjust for DST if applicable

// Convert UTC to broker's timezone


int brokerOffset = brokerHour - TimeHour(TimeCurrent());
int normalizedHour = (utcHour + brokerOffset) % 24;

datetime normalizedTime = baseTime - brokerHour * 3600 - brokerMinute * 60 -


brokerSecond;
normalizedTime += normalizedHour * 3600 + brokerMinute * 60 + brokerSecond;

return normalizedTime;
}

bool IsDST()
{
// Determine if current date is within DST period
datetime currentTime = TimeCurrent();
MqlDateTime dateTime;
TimeToStruct(currentTime, dateTime);

// Example check for US DST (adjust as needed for other regions)


if (dateTime.mon >= 3 && dateTime.mon <= 11)
{
if (dateTime.mon == 3)
return (dateTime.day >= 14 - WeekDay(dateTime.year, 0, 3));
else if (dateTime.mon == 11)
return (dateTime.day < 7 - WeekDay(dateTime.year, 0, 11));
else
return true;
}
return false;
}

You might also like