-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStatistics of candles 2.mq5
131 lines (130 loc) · 10.3 KB
/
Statistics of candles 2.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
enum ENUM_OUTPUT
{
count=0, // count of series
percent=1, // percent from count of bars
};
//--- input parameters
input int InputCountBars = 1200; // Count of bars
input bool InpVerification = false; // Verification
input ENUM_OUTPUT InpOutput = percent; // Output statistics
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
enum ENUM_SERIES_TYPE
{
Bull=1, // ↑
Bear=-1, // ↓
};
//+------------------------------------------------------------------+
//| arr_series[][0]: series type |
//| arr_series[][1]: count of series |
//| OR |
//| arr_series[][1]: percent from count of bars |
//+------------------------------------------------------------------+
double arr_series[][2];
MqlRates rates[];
int copied=CopyRates(Symbol(),0,0,InputCountBars,rates);
if(copied==InputCountBars)
{
for(int i=0;i<copied;i++)
{
//--- here we define type of series
static int prev_series_type = 0;
int current_series_type = 0;
static int count_series = 0;
if(rates[i].open<rates[i].close)
current_series_type=Bull;
else if(rates[i].open>rates[i].close)
current_series_type=Bear;
else
continue;
//--- count of series
if(current_series_type==prev_series_type || prev_series_type==0)
count_series++;
else // generate the name of the series (only if the bar type has changed)
{
int name_series=current_series_type*count_series;
count_series=1;
//--- search for this series in an array and enter data
bool IsFound=false;
int size=ArrayRange(arr_series,0);
for(int j=0;j<size;j++)
{
//+------------------------------------------------------------------+
//| arr_series[][0]: series type |
//| arr_series[][1]: count of series |
//| OR |
//| arr_series[][1]: percent from count of bars |
//+------------------------------------------------------------------+
if(arr_series[j][0]==name_series)
{
IsFound=true;
arr_series[j][1]=arr_series[j][1]+1;
}
}
if(!IsFound)
{
ArrayResize(arr_series,size+1);
arr_series[size][0]=name_series;
arr_series[size][1]=1;
}
}
prev_series_type=current_series_type;
}
}
else
{
Print("Failed to get history data for the symbol ",Symbol(),". Bars copied ",copied," of ",InputCountBars);
return;
}
//---
ArraySort(arr_series);
if(InpOutput==percent)
{
//+------------------------------------------------------------------+
//| arr_series[][0]: series type |
//| arr_series[][1]: count of series |
//| OR |
//| arr_series[][1]: percent from count of bars |
//+------------------------------------------------------------------+
int size=ArrayRange(arr_series,0);
double coefficient=100.0/(double)InputCountBars;
for(int i=0;i<size;i++)
arr_series[i][1]=arr_series[i][1]*coefficient;
}
//--- verification
if(InpVerification)
{
string out="";
string format="open = %G, high = %G, low = %G, close = %G, volume = %d";
for(int i=0;i<copied;i++)
{
out=IntegerToString(i)+":"+TimeToString(rates[i].time);
out=out+" "+StringFormat(format,
rates[i].open,
rates[i].high,
rates[i].low,
rates[i].close,
rates[i].tick_volume);
if(rates[i].open<rates[i].close)
out=out+" "+"Bull";
else if(rates[i].open>rates[i].close)
out=out+" "+"Bear";
Print(out);
}
Print("//| arr_series[][0]: series type |");
if(InpOutput==percent)
Print("//| arr_series[][1]: percent from count of bars |");
else
Print("//| arr_series[][1]: count of series |");
uint digits=(InpOutput==percent)?1:0;
ArrayPrint(arr_series,digits);
}
//---
;
}
//---