enum question

I'm learning C++ and the enum Creation code below I don't grasp how that is being used in the code?

I see Call_iIchimoku is used a few lines down in the input Creation but what structure does Call_iIchimoku have?

In the file I don't see Call_IndicatorCreate being used at all. Why is that?

Thank you...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//+------------------------------------------------------------------+
//| Enumeration of the methods of handle creation                    |
//+------------------------------------------------------------------+
enum Creation
  {
   Call_iIchimoku,         // use iIchimoku
   Call_IndicatorCreate    // use IndicatorCreate
  };
//--- input parameters
input Creation             type=Call_iIchimoku;       // type of the function 
input int                  tenkan_sen=9;              // period of Tenkan-sen
input int                  kijun_sen=26;              // period of Kijun-sen
input int                  senkou_span_b=52;          // period of Senkou Span B
input string               symbol=" ";                // symbol 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // timeframe 
What is "input" ? It is not a standard C++ keyword.

what structure does Call_iIchimoku have?
It is not a structure, it is simply an enum value, which is an int. The value of Call_iIchimoku is 0 by default, since it's the first value of the Creation enum.

We can't tell how it's really being used, because all you show is the declaration of a few variables.

In the file I don't see Call_IndicatorCreate being used at all. Why is that?
We cannot answer that without more context. Maybe it's just dead code that the developer forgot to remove.
Last edited on
you may want to look at a reference on enums, but here goes the 2 cent version:
enums are a group of constant integers starting from zero by default (can be over-ridden)
so

enum Creation
{
Call_iIchimoku, // use iIchimoku
Call_IndicatorCreate // use IndicatorCreate
};

Creation is a type, which is really a kind of integer with a limited range.
so you can say this: Creation c = Call_iIchimoku; //it is now zero.
later you can say c = Call_IndicatorCreate; //now it is 1
or mix and match it:
int z;
cin >> z;
if(z == Call_IndicatorCreate ) ... etc;

you can iterate with them them, switch them, etc -- anything you can do with a bunch of constant integers...
for(Creation x = Call_iIchimoku; x < Call_IndicatorCreate; x++)

I find the most handy for naming constant array locations.
say you had a serialized struct with an array of 3 unrelated doubles. you can use
array[enum_name] to refer to it, rather than [0], making it clear what it is when reading it.

without more code, its hard to say, but its likely that this code is using them to find an offset to a function pointer to call? Seems C-ish, but they may have a good reason.

the comment on line 10 seems wrong, the type is creation (first column is ok, second is weird) -- its *value* may be Call_iIchimoku or maybe I just do not follow the commenting scheme
Last edited on
Here is the code. I had to trim off the top because the post was too long. There must be a way to attach files but I don't see any links for that?

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
//+------------------------------------------------------------------+
//| Enumeration of the methods of handle creation                    |
//+------------------------------------------------------------------+
enum Creation
  {
   Call_iIchimoku,         // use iIchimoku
   Call_IndicatorCreate    // use IndicatorCreate
  };
//--- input parameters
input Creation             type=Call_iIchimoku;       // type of the function 
input int                  tenkan_sen=9;              // period of Tenkan-sen
input int                  kijun_sen=26;              // period of Kijun-sen
input int                  senkou_span_b=52;          // period of Senkou Span B
input string               symbol=" ";                // symbol 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // timeframe
//--- indicator buffer
double         Tenkan_sen_Buffer[];    // fast MA
double         Kijun_sen_Buffer[];     // slow MA
double         Senkou_Span_A_Buffer[]; // cloud
double         Senkou_Span_B_Buffer[]; // cloud
double         Chinkou_Span_Buffer[];  // wild card
//--- variable for storing the handle of the iIchimoku indicator
int    handle;
//--- variable for storing
string name=symbol;
//--- name of the indicator on a chart
string short_name;
//--- we will keep the number of values in the Ichimoku Kinko Hyo indicator
int    bars_calculated=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- assignment of arrays to indicator buffers
   SetIndexBuffer(0,Tenkan_sen_Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,Kijun_sen_Buffer,INDICATOR_DATA);
   SetIndexBuffer(2,Senkou_Span_A_Buffer,INDICATOR_DATA);
   SetIndexBuffer(3,Senkou_Span_B_Buffer,INDICATOR_DATA);
   SetIndexBuffer(4,Chinkou_Span_Buffer,INDICATOR_DATA);
//--- set the shift for the Senkou Span channel of kijun_sen bars in the future direction
   PlotIndexSetInteger(2,PLOT_SHIFT,kijun_sen);
//--- setting a shift for the Chikou Span line is not required, since the Chinkou data Span
//--- is already stored with a shift in iIchimoku
//--- determine the symbol the indicator is drawn for
   name=symbol;
//--- delete spaces to the right and to the left
   StringTrimRight(name);
   StringTrimLeft(name);
//--- if it results in zero length of the 'name' string
   if(StringLen(name)==0)
     {
      //--- take the symbol of the chart the indicator is attached to
      name=_Symbol;
     }
//--- create handle of the indicator
   if(type==Call_iIchimoku)
      handle=iIchimoku(name,period,tenkan_sen,kijun_sen,senkou_span_b);
   else
     {
      //--- fill the structure with parameters of the indicator
      MqlParam pars[3];
      //--- periods and shifts of the Alligator lines
      pars[0].type=TYPE_INT;
      pars[0].integer_value=tenkan_sen;
      pars[1].type=TYPE_INT;
      pars[1].integer_value=kijun_sen;
      pars[2].type=TYPE_INT;
      pars[2].integer_value=senkou_span_b;
      //--- create handle
      handle=IndicatorCreate(name,period,IND_ICHIMOKU,3,pars);
     }
//--- if the handle is not created
   if(handle==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iIchimoku indicator for the symbol %s/%s, error code %d",
                  name,
                  EnumToString(period),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//--- show the symbol/timeframe the Ichimoku Kinko Hyo indicator is calculated for
   short_name=StringFormat("iIchimoku(%s/%s, %d, %d ,%d)",name,EnumToString(period),
                           tenkan_sen,kijun_sen,senkou_span_b);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- normal initialization of the indicator    
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--- number of values copied from the iIchimoku indicator
   int values_to_copy;
//--- determine the number of values calculated in the indicator
   int calculated=BarsCalculated(handle);
   if(calculated<=0)
     {
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
      return(0);
     }
//--- if it is the first start of calculation of the indicator or if the number of values in the iIchimoku indicator changed
//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
     {
      //--- if the Tenkan_sen_Buffer array is greater than the number of values in the iIchimoku indicator for symbol/period, then we don't copy everything 
      //--- otherwise, we copy less than the size of indicator buffers
      if(calculated>rates_total) values_to_copy=rates_total;
      else                       values_to_copy=calculated;
     }
   else
     {
      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()
      //--- for calculation not more than one bar is added
      values_to_copy=(rates_total-prev_calculated)+1;
     }
//--- fill the arrays with values of the Ichimoku Kinko Hyo indicator
//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation
   if (!FillArraysFromBuffers(Tenkan_sen_Buffer, Kijun_sen_Buffer, Senkou_Span_A_Buffer, Senkou_Span_B_Buffer, Chinkou_Span_Buffer, kijun_sen, handle, values_to_copy)) return(0);
//--- form the message
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d",
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS),
                            short_name,
                            values_to_copy);
//--- display the service message on the chart
   Comment(comm);
//--- memorize the number of values in the Ichimoku Kinko Hyo indicator
   bars_calculated=calculated;
//--- return the prev_calculated value for the next call
   return(rates_total);
  }
 
 
//--- everything is fine
   return(true);
  }
//+------------------------------------------------------------------+
//| Indicator deinitialization function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(handle!=INVALID_HANDLE)
      IndicatorRelease(handle);
//--- clear the chart after deleting the indicator
   Comment("");
  }
if you need to attach giant files you can host it on a file share site and post a link is all.
You can't drop them HERE. Or make many, many posts (please, do not!).

I personally can't look at file share sites when at work.

Its right there, though:
if(type==Call_iIchimoku)
handle=iIchimoku(name,period,tenkan_sen,kijun_sen,senkou_span_b);
else blah blah

So there is one place it is being used. Its just a decision flag, here.

its the exact same as something like:

type = 0
...

if(type == 0)
handle=iIchimoku(name,period,tenkan_sen,kijun_sen,senkou_span_b);
else ...
except 0 is ugly and confusing and a constant with a good name is easier to follow/understand.
Last edited on
I understand re using links for file attachments. Now I know.

So I think I understand now how that first enum is being used.

Apparently that second one (Call_IndicatorCreate) was created but since it appears nowhere else in the code, it is not being used.

Thanks for the help...!!!
Topic archived. No new replies allowed.