-
Notifications
You must be signed in to change notification settings - Fork 1
/
Print200EMA.mq5
122 lines (101 loc) · 3.93 KB
/
Print200EMA.mq5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//+--------------------------------------------------------------------+
//| Print200EMA.mq5 |
//| Copyright 2024, lanastasov |
//| https://www.mql5.com |
//+--------------------------------------------------------------------+
#property copyright "Copyright 2024, lanastasov"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
input ENUM_APPLIED_PRICE AppliedPrice = PRICE_CLOSE; // Applied price for EMA calculation
int EmaHandle;
int DailyEmaHandle;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create the EMA indicator handle
EmaHandle = iMA(_Symbol, PERIOD_CURRENT, 200, 0, MODE_EMA, AppliedPrice);
if(EmaHandle == INVALID_HANDLE)
{
Print("Failed to create EMA indicator handle");
return(INIT_FAILED);
}
DailyEmaHandle = iMA(_Symbol, PERIOD_D1, 200, 0, MODE_EMA, AppliedPrice);
if(DailyEmaHandle == INVALID_HANDLE)
{
Print("Failed to create EMA indicator handle");
return(INIT_FAILED);
}
// Array to store the EMA values
double emaValues[];
double closingPrice;
double emaValue;
double dailyemaValues[];
double dailyemaValue;
// Copy the last EMA value
if(CopyBuffer(EmaHandle, 0, 0, 1, emaValues) > 0)
{
emaValue = emaValues[0];
Print("Current 200 EMA value: ", emaValue);
}
else
{
int error = GetLastError();
Print("Failed to copy EMA value. Error code: ", error);
}
if(CopyBuffer(DailyEmaHandle, 0, 0, 1, dailyemaValues) > 0)
{
dailyemaValue = dailyemaValues[0];
Print("Current Daily 200 EMA value: ", dailyemaValue);
}
else
{
int error = GetLastError();
Print("Failed to copy EMA value. Error code: ", error);
}
MqlRates rates[];
if(CopyRates(_Symbol, PERIOD_CURRENT, 0, 1, rates) > 0)
{
closingPrice = rates[0].close;
Print("Current closing price of ", _Symbol, ": ", closingPrice);
}
else
{
Print("Failed to get rates data. Error code: ", GetLastError());
}
double distancePercent = ((closingPrice - emaValue) / emaValue) * 100;
Print("Distance in Percent: ", distancePercent, "%");
double dailydistancePercent = ((closingPrice - dailyemaValue) / dailyemaValue) * 100;
Print("Daily Distance in Percent: ", dailydistancePercent, "%");
// Create a text label on the chart
string labelName = "Daily-200EMA_Distance_Label-To-Current-Close-Price";
if (ObjectFind(0, labelName) == -1)
{
ObjectCreate(0, labelName, OBJ_LABEL, 0, 0, 0);
}
// Set the label properties
ObjectSetString(0, labelName, OBJPROP_TEXT, dailydistancePercent);
ObjectSetInteger(0, labelName, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0, labelName, OBJPROP_XDISTANCE, 100);
ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, 20);
ObjectSetInteger(0, labelName, OBJPROP_COLOR, clrBlack);
ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 12);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Release the indicator handle
if(EmaHandle != INVALID_HANDLE)
IndicatorRelease(EmaHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
}