-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsideBar.mq5
168 lines (142 loc) · 5.95 KB
/
InsideBar.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//+------------------------------------------------------------------+
//| InsideBar.mq5 |
//| Copyright 2024, rpanchyk |
//| https://github.com/rpanchyk |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, rpanchyk"
#property link "https://github.com/rpanchyk"
#property version "1.00"
#property description "Indicator shows inside bars"
#property description ""
#property description "Used documentation:"
#property description "- https://www.mql5.com/en/code/1349"
#property description "- https://www.mql5.com/en/docs/customind/indicators_examples/draw_color_candles"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 1
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_label1 "Open;High;Low;Close"
#property indicator_width1 3
//+------------------------------------------------------------------+
//| Model for Bar keeping High and Low prices |
//+------------------------------------------------------------------+
class HLBar
{
public:
HLBar(double high, double low) : m_High(high), m_Low(low) {}
double GetHigh() { return m_High; }
double GetLow() { return m_Low; }
private:
double m_High;
double m_Low;
};
// buffers
double InsideBarOpenBuf[], InsideBarHighBuf[], InsideBarLowBuf[], InsideBarCloseBuf[]; // Buffers for data
double InsideBarLineColorBuf[]; // Buffer for color indexes
// config
input group "Section :: Main";
input color InpUpBarColor = clrSilver; // Color of bullish inside bar
input color InpDownBarColor = clrSilver; // Color of bearish inside bar
input group "Section :: Dev";
input bool InpDebugEnabled = false; // Endble debug (verbose logging)
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Print("InsideBar indicator initialization started");
ArrayInitialize(InsideBarOpenBuf, EMPTY_VALUE);
ArrayInitialize(InsideBarHighBuf, EMPTY_VALUE);
ArrayInitialize(InsideBarLowBuf, EMPTY_VALUE);
ArrayInitialize(InsideBarCloseBuf, EMPTY_VALUE);
ArrayInitialize(InsideBarLineColorBuf, EMPTY_VALUE);
ArraySetAsSeries(InsideBarOpenBuf, true);
ArraySetAsSeries(InsideBarHighBuf, true);
ArraySetAsSeries(InsideBarLowBuf, true);
ArraySetAsSeries(InsideBarCloseBuf, true);
ArraySetAsSeries(InsideBarLineColorBuf, true);
SetIndexBuffer(0, InsideBarOpenBuf, INDICATOR_DATA);
SetIndexBuffer(1, InsideBarHighBuf, INDICATOR_DATA);
SetIndexBuffer(2, InsideBarLowBuf, INDICATOR_DATA);
SetIndexBuffer(3, InsideBarCloseBuf, INDICATOR_DATA);
SetIndexBuffer(4, InsideBarLineColorBuf, INDICATOR_COLOR_INDEX);
PlotIndexSetInteger(0, PLOT_COLOR_INDEXES, 2);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, InpUpBarColor); // 0th index color
PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, InpDownBarColor); // 1st index color
IndicatorSetString(INDICATOR_SHORTNAME, "InsideBar indicator");
Print("InsideBar indicator initialization finished");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Print("InsideBar indicator deinitialization started");
ArrayFree(InsideBarOpenBuf);
ArrayFree(InsideBarHighBuf);
ArrayFree(InsideBarLowBuf);
ArrayFree(InsideBarCloseBuf);
ArrayFree(InsideBarLineColorBuf);
Print("InsideBar indicator deinitialization finished");
}
//+------------------------------------------------------------------+
//| 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[])
{
if(rates_total == prev_calculated)
{
return rates_total;
}
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
if(InpDebugEnabled)
{
PrintFormat("RatesTotal: %i, PrevCalculated: %i", rates_total, prev_calculated);
}
HLBar *prevBar = NULL;
HLBar *currBar = NULL;
for(int i = rates_total - prev_calculated - 2; i > 0; i--)
{
InsideBarOpenBuf[i] = open[i];
InsideBarHighBuf[i] = high[i];
InsideBarLowBuf[i] = low[i];
InsideBarCloseBuf[i] = close[i];
prevBar = prevBar != NULL ? prevBar : new HLBar(high[i + 1], low[i + 1]);
delete currBar;
currBar = new HLBar(high[i], low[i]);
if(IsInsideBar(prevBar, currBar))
{
InsideBarLineColorBuf[i] = open[i] <= close[i] ? 0 : 1;
}
else
{
InsideBarLineColorBuf[i] = -1;
delete prevBar;
prevBar = NULL; // clear inside bar
}
}
delete prevBar;
delete currBar;
return rates_total; // set prev_calculated on next call
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsInsideBar(HLBar *prevBar, HLBar *currBar)
{
return prevBar.GetHigh() >= currBar.GetHigh() && prevBar.GetLow() <= currBar.GetLow();
}
//+------------------------------------------------------------------+