How to insert an alert into an MQL4 indicator-code? - mql4

I have a free MQL4 indicator that displays the most important resistance a support based on the timeframe.
It's a fantastic indicator, but I need an implementation with a message and a sound alert when a support or resistance is touched.
How can I insert alert only when H1, H4, D1, W1 and MN1 are touched?
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Black
extern int LB = 3;
extern int maxBarsForPeriod = 1000;
extern bool showM01 = TRUE;
extern bool showM05 = TRUE;
extern bool showM15 = TRUE;
extern bool showM30 = TRUE;
extern bool showH01 = TRUE;
extern bool showH04 = TRUE;
extern bool showD01 = TRUE;
extern bool showW01 = TRUE;
extern bool showMN1 = TRUE;
int gi_120 = 0;
int gi_124 = 0;
int gi_128 = 0;
int gi_132 = 0;
int gi_136 = 0;
int gi_140 = 0;
int gi_144 = 0;
int gi_148 = 0;
int gi_152 = 0;
int gi_156 = 0;
int gi_160 = 0;
int gi_164 = 0;
int gi_168 = 0;
int gi_172 = 0;
int gi_176 = 0;
int gi_180 = 0;
int gi_184 = 0;
int gi_188 = 0;
double gda_192[];
double gda_196[];
double gda_200[];
double gda_204[];
double gda_208[];
double gda_212[];
double gda_216[];
double gda_220[];
double gda_224[];
double gda_228[];
double gda_232[];
double gda_236[];
double gda_240[];
double gda_244[];
double gda_248[];
double gda_252[];
double gda_256[];
double gda_260[];
int init() {
IndicatorShortName("3 Line Break On Chart +levels");
set_prevBarTime(1, 0);
set_prevBarTime(5, 0);
set_prevBarTime(15, 0);
set_prevBarTime(30, 0);
set_prevBarTime(60, 0);
set_prevBarTime(240, 0);
set_prevBarTime(1440, 0);
set_prevBarTime(10080, 0);
set_prevBarTime(43200, 0);
return (0);
}
int deinit() {
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Res C"));
return (0);
}
double Diap(int ai_0, bool ai_4, int ai_8, int ai_12) {
double ld_ret_20;
if (ai_4) {
ld_ret_20 = get_max(ai_0, ai_12);
for (int li_16 = 1; li_16 < ai_8; li_16++)
if (get_max(ai_0, ai_12 - li_16) > ld_ret_20) ld_ret_20 = get_max(ai_0, ai_12 - li_16);
}
if (!ai_4) {
ld_ret_20 = get_min(ai_0, ai_12);
for (li_16 = 1; li_16 < ai_8; li_16++)
if (get_min(ai_0, ai_12 - li_16) < ld_ret_20) ld_ret_20 = get_min(ai_0, ai_12 - li_16);
}
return (ld_ret_20);
}
void EmulateDoubleBuffer(double ada_0[], int ai_4) {
if (ArraySize(ada_0) < ai_4) {
ArraySetAsSeries(ada_0, FALSE);
ArrayResize(ada_0, ai_4);
ArraySetAsSeries(ada_0, TRUE);
}
}
void DeleteHLineObject(string a_name_0) {
ObjectDelete(a_name_0);
ObjectDelete(a_name_0 + "_Label");
}
void ShowHLineObject(string a_name_0, int ai_8, int a_style_12, double ad_16, int ai_24) {
if (ObjectFind(a_name_0) != 0) CreateHLineObject(a_name_0, ai_8, a_style_12, ad_16, ai_24);
ObjectSet(a_name_0 + "_Label", OBJPROP_PRICE1, ad_16);
ObjectSet(a_name_0 + "_Label", OBJPROP_TIME1, Time[0] + Period() * ai_24);
ObjectSet(a_name_0 + "_Label", OBJPROP_STYLE, a_style_12);
ObjectSet(a_name_0, OBJPROP_PRICE1, ad_16);
}
void CreateHLineObject(string a_text_0, color a_color_8, int a_style_12, double a_price_16, int ai_24) {
ObjectCreate(a_text_0 + "_Label", OBJ_TEXT, 0, Time[0] + Period() * ai_24, a_price_16);
ObjectSetText(a_text_0 + "_Label", a_text_0, 7, "Verdana", a_color_8);
ObjectCreate(a_text_0, OBJ_HLINE, 0, Time[0], a_price_16);
ObjectSet(a_text_0, OBJPROP_STYLE, a_style_12);
ObjectSet(a_text_0, OBJPROP_COLOR, a_color_8);
ObjectSet(a_text_0, OBJPROP_WIDTH, 1);
}
string getPeriodAsString(int ai_0) {
string ls_ret_4 = 0;
switch (ai_0) {
case 1:
ls_ret_4 = "M1";
break;
case 5:
ls_ret_4 = "M5";
break;
case 15:
ls_ret_4 = "M15";
break;
case 30:
ls_ret_4 = "M30";
break;
case 60:
ls_ret_4 = "H1";
break;
case 240:
ls_ret_4 = "H4";
break;
case 1440:
ls_ret_4 = "D1";
break;
case 10080:
ls_ret_4 = "W1";
break;
case 43200:
ls_ret_4 = "MN1";
}
return (ls_ret_4);
}
void set_prevBarTime(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
gi_120 = ai_4;
return;
case 5:
gi_124 = ai_4;
return;
case 15:
gi_128 = ai_4;
return;
case 30:
gi_132 = ai_4;
return;
case 60:
gi_136 = ai_4;
return;
case 240:
gi_140 = ai_4;
return;
case 1440:
gi_144 = ai_4;
return;
case 10080:
gi_148 = ai_4;
return;
case 43200:
gi_152 = ai_4;
return;
return;
}
}
int get_prevBarTime(int ai_0) {
switch (ai_0) {
case 1:
return (gi_120);
break;
case 5:
return (gi_124);
break;
case 15:
return (gi_128);
break;
case 30:
return (gi_132);
break;
case 60:
return (gi_136);
break;
case 240:
return (gi_140);
break;
case 1440:
return (gi_144);
break;
case 10080:
return (gi_148);
break;
case 43200:
return (gi_152);
}
return (0);
}
void set_prevBarCount(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
gi_156 = ai_4;
return;
case 5:
gi_160 = ai_4;
return;
case 15:
gi_164 = ai_4;
return;
case 30:
gi_168 = ai_4;
return;
case 60:
gi_172 = ai_4;
return;
case 240:
gi_176 = ai_4;
return;
case 1440:
gi_180 = ai_4;
return;
case 10080:
gi_184 = ai_4;
return;
case 43200:
gi_188 = ai_4;
return;
return;
}
}
int get_prevBarCount(int ai_0) {
switch (ai_0) {
case 1:
return (gi_156);
break;
case 5:
return (gi_160);
break;
case 15:
return (gi_164);
break;
case 30:
return (gi_168);
break;
case 60:
return (gi_172);
break;
case 240:
return (gi_176);
break;
case 1440:
return (gi_180);
break;
case 10080:
return (gi_184);
break;
case 43200:
return (gi_188);
}
return (0);
}
void set_max(int ai_0, int ai_4, double ad_8) {
switch (ai_0) {
case 1:
gda_192[ai_4] = ad_8;
return;
case 5:
gda_196[ai_4] = ad_8;
return;
case 15:
gda_200[ai_4] = ad_8;
return;
case 30:
gda_204[ai_4] = ad_8;
return;
case 60:
gda_208[ai_4] = ad_8;
return;
case 240:
gda_212[ai_4] = ad_8;
return;
case 1440:
gda_216[ai_4] = ad_8;
return;
case 10080:
gda_220[ai_4] = ad_8;
return;
case 43200:
gda_224[ai_4] = ad_8;
return;
return;
}
}
double get_max(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
return (gda_192[ai_4]);
break;
case 5:
return (gda_196[ai_4]);
break;
case 15:
return (gda_200[ai_4]);
break;
case 30:
return (gda_204[ai_4]);
break;
case 60:
return (gda_208[ai_4]);
break;
case 240:
return (gda_212[ai_4]);
break;
case 1440:
return (gda_216[ai_4]);
break;
case 10080:
return (gda_220[ai_4]);
break;
case 43200:
return (gda_224[ai_4]);
}
return (0.0);
}
void set_min(int ai_0, int ai_4, double ad_8) {
switch (ai_0) {
case 1:
gda_228[ai_4] = ad_8;
return;
case 5:
gda_232[ai_4] = ad_8;
return;
case 15:
gda_236[ai_4] = ad_8;
return;
case 30:
gda_240[ai_4] = ad_8;
return;
case 60:
gda_244[ai_4] = ad_8;
return;
case 240:
gda_248[ai_4] = ad_8;
return;
case 1440:
gda_252[ai_4] = ad_8;
return;
case 10080:
gda_256[ai_4] = ad_8;
return;
case 43200:
gda_260[ai_4] = ad_8;
return;
return;
}
}
double get_min(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
return (gda_228[ai_4]);
break;
case 5:
return (gda_232[ai_4]);
break;
case 15:
return (gda_236[ai_4]);
break;
case 30:
return (gda_240[ai_4]);
break;
case 60:
return (gda_244[ai_4]);
break;
case 240:
return (gda_248[ai_4]);
break;
case 1440:
return (gda_252[ai_4]);
break;
case 10080:
return (gda_256[ai_4]);
break;
case 43200:
return (gda_260[ai_4]);
}
return (0.0);
}
void emulate_tlbmaxmin(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
EmulateDoubleBuffer(gda_192, ai_4);
EmulateDoubleBuffer(gda_228, ai_4);
return;
case 5:
EmulateDoubleBuffer(gda_196, ai_4);
EmulateDoubleBuffer(gda_232, ai_4);
return;
case 15:
EmulateDoubleBuffer(gda_200, ai_4);
EmulateDoubleBuffer(gda_236, ai_4);
return;
case 30:
EmulateDoubleBuffer(gda_204, ai_4);
EmulateDoubleBuffer(gda_240, ai_4);
return;
case 60:
EmulateDoubleBuffer(gda_208, ai_4);
EmulateDoubleBuffer(gda_244, ai_4);
return;
case 240:
EmulateDoubleBuffer(gda_212, ai_4);
EmulateDoubleBuffer(gda_248, ai_4);
return;
case 1440:
EmulateDoubleBuffer(gda_216, ai_4);
EmulateDoubleBuffer(gda_252, ai_4);
return;
case 10080:
EmulateDoubleBuffer(gda_220, ai_4);
EmulateDoubleBuffer(gda_256, ai_4);
return;
case 43200:
EmulateDoubleBuffer(gda_224, ai_4);
EmulateDoubleBuffer(gda_260, ai_4);
return;
return;
}
}
void displayPeriod(int a_timeframe_0) {
int li_4;
int l_count_8;
int li_12;
int li_20;
double ld_24;
double ld_32;
double ld_40;
double ld_48;
int l_count_56;
int l_count_60;
int li_unused_64;
if (get_prevBarTime(a_timeframe_0) == 0 || get_prevBarTime(a_timeframe_0) != iTime(Symbol(), a_timeframe_0, 0) || get_prevBarCount(a_timeframe_0) == 0 || get_prevBarCount(a_timeframe_0) != iBars(Symbol(), a_timeframe_0)) {
set_prevBarTime(a_timeframe_0, iTime(Symbol(), a_timeframe_0, 0));
set_prevBarCount(a_timeframe_0, iBars(Symbol(), a_timeframe_0));
li_4 = iBars(Symbol(), a_timeframe_0);
if (maxBarsForPeriod > 0 && li_4 > maxBarsForPeriod) li_4 = maxBarsForPeriod;
l_count_8 = 0;
li_12 = li_4;
emulate_tlbmaxmin(a_timeframe_0, li_4);
li_20 = 1;
while (iClose(Symbol(), a_timeframe_0, li_12 - 1) == iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - 1) > iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
set_max(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1));
set_min(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20));
}
if (iClose(Symbol(), a_timeframe_0, li_12 - 1) < iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
set_max(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20));
set_min(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1));
}
for (int li_16 = 1; li_16 < LB; li_16++) {
while (iClose(Symbol(), a_timeframe_0, li_12 - li_20) <= Diap(a_timeframe_0, 1, li_16, l_count_8) && iClose(Symbol(), a_timeframe_0, li_12 - li_20) >= Diap(a_timeframe_0, 0, li_16, l_count_8)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (li_20 > li_12 - 1) break;
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) > get_max(a_timeframe_0, li_16 - 1)) {
set_max(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_min(a_timeframe_0, li_16, get_max(a_timeframe_0, li_16 - 1));
l_count_8++;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) < get_min(a_timeframe_0, li_16 - 1)) {
set_min(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_max(a_timeframe_0, li_16, get_min(a_timeframe_0, li_16 - 1));
l_count_8++;
}
}
for (li_16 = LB; li_16 < li_12; li_16++) {
while (iClose(Symbol(), a_timeframe_0, li_12 - li_20) <= Diap(a_timeframe_0, 1, LB, l_count_8) && iClose(Symbol(), a_timeframe_0, li_12 - li_20) >= Diap(a_timeframe_0, 0, LB, l_count_8)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (li_20 > li_12 - 1) break;
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) > get_max(a_timeframe_0, li_16 - 1)) {
set_max(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_min(a_timeframe_0, li_16, get_max(a_timeframe_0, li_16 - 1));
l_count_8++;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) < get_min(a_timeframe_0, li_16 - 1)) {
set_min(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_max(a_timeframe_0, li_16, get_min(a_timeframe_0, li_16 - 1));
l_count_8++;
}
}
ld_24 = 0;
ld_32 = 0;
ld_40 = 0;
ld_48 = 0;
l_count_56 = 0;
l_count_60 = 0;
li_unused_64 = 0;
for (li_16 = 1; li_16 <= l_count_8; li_16++) {
if (get_max(a_timeframe_0, li_16) > get_max(a_timeframe_0, li_16 - 1)) {
if (l_count_60 >= LB) ld_24 = get_max(a_timeframe_0, li_16 - LB);
else ld_24 = get_min(a_timeframe_0, li_16 - l_count_60 - 1);
ld_48 = get_max(a_timeframe_0, li_16);
ld_40 = 0;
ld_32 = 0;
l_count_60++;
l_count_56 = 0;
}
if (get_max(a_timeframe_0, li_16) < get_max(a_timeframe_0, li_16 - 1)) {
if (l_count_56 >= LB) ld_32 = get_min(a_timeframe_0, li_16 - LB);
else ld_32 = get_max(a_timeframe_0, li_16 - l_count_56 - 1);
ld_40 = get_min(a_timeframe_0, li_16);
ld_24 = 0;
ld_48 = 0;
l_count_60 = 0;
l_count_56++;
}
}
if (ld_24 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup"), 16711680, STYLE_SOLID, ld_24, 500);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup"));
if (ld_32 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res"), 255, STYLE_SOLID, ld_32, 500);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res"));
if (ld_40 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup C"), 16711680, STYLE_DASHDOTDOT, ld_40, 1200);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup C"));
if (ld_48 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res C"), 255, STYLE_DASHDOTDOT, ld_48, 1200);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res C"));
}
}
int start() {
if (Period() <= PERIOD_M1 && showM01) displayPeriod(PERIOD_M1);
if (Period() <= PERIOD_M5 && showM05) displayPeriod(PERIOD_M5);
if (Period() <= PERIOD_M15 && showM15) displayPeriod(PERIOD_M15);
if (Period() <= PERIOD_M30 && showM30) displayPeriod(PERIOD_M30);
if (Period() <= PERIOD_H1 && showH01) displayPeriod(PERIOD_H1);
if (Period() <= PERIOD_H4 && showH04) displayPeriod(PERIOD_H4);
if (Period() <= PERIOD_D1 && showD01) displayPeriod(PERIOD_D1);
if (Period() <= PERIOD_W1 && showW01) displayPeriod(PERIOD_W1);
if (Period() <= PERIOD_MN1 && showMN1) displayPeriod(PERIOD_MN1);
return (0);
}

How can I insert alert only when H1, H4, D1, W1 and MN1 are touched?
First, straight forget to use a call to Alert() in CustomIndicator
MQL4/5 architecture strictly forbids one to call any blocking-mode method inside a CustomIndicator-type-of-MQL4/5-code.
This will not be possible, so let's focus on can-do alternative(s):
A ) one may send a signalling message to any block-able MQL4/5 script ( which is executed in another thread or an external process.
B ) one may use pipe-based signalling, if O/S permits to use these rudimentary tools.
C ) one may use an internal GlobalVariableSet() / GlobalVariableCheck() native MQL4/5 support for inter-thread data-based signalling / messaging, allowing a non-CustomIndicator ( Script ) MQL4/5 type of code to "remotely" handle any of the blocking parts of a service ( a call to native Alert() et al )
D ) one may use a file-based signalling, if all of above seems too complicated.
Having started with A ) years ago, I have never had a need to resort to B), the less the others "beyond" A), since then, yet these options still remains also possible.
The code will go as easy as:
if ( ... )
{ int RC = mql4zmq_msg_init_data( aSendMsgOBJ,
aSendMsgBuffUCHAR,
aSendMsgBUFF_SIZE * 4
);
// if RC
// ------------------------------------------------------.SEND()
zmq_send( aZmqPUBLISHER, aSendMsgOBJ );
// ------------------------------------------------------.SEND()
...
}
The remote receivers ( could be many ) then do whatever they want, be it in MQL4/5, python, Go, C++, java, ... chapel, FORTRAN, MATLAB, ...
Cool, isn't it?

Related

Expert Advisor will not open buy trades and doesn't follow the conditions

The EA has no bugs but it doesn't really do what I expected it to do, it should, for example, sell from var_30[i], put a tp at var_2[i] and sl at var_30[i]+(var_30[i]-var_2[i])/2 or buy from var_20[i], put a tp at var_3[i] and sl at var_20[i]+(var_20[i]-var_3[i])/2
I tried to modify it several times but in the end, after using the strategy tester feature the graph always goes to 0, which shouldn't.
Here is the code:
#property description "Price Border strategy expert advisor by Razvan"
//--- Inputs
extern double Lots =1;
extern double MaximumRisk =2;
extern string TimeFrame = "All tf";
extern int HalfLength = 61;
extern int Price = 0;
extern double ATRMultiplier = 1.6;
extern double ATRMultiplier1 = 3.6;
extern int ATRPeriod = 480;
extern bool Interpolate = TRUE;
extern bool alertsOn = TRUE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsOnHighLow = TRUE;
extern bool alertsMessage = FALSE;
extern bool alertsSound = TRUE;
extern bool alertsEmail = FALSE;
double var_1[];
double var_2[];
double var_3[];
double var_4[];
double var_20[];
double var_30[];
string string_1;
bool bool_1;
bool bool_2;
int var_5;
string string_2 = "nothing";
datetime Gt_176;
string string_3[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
int var_6[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int res;
// init function
int init() {
string str_4[256];
for (int i = 0; i < 256; i++) str_4[i] = CharToStr(i);
int var_7 = StrToInteger(str_4[67] + str_4[111] + str_4[112] + str_4[121] + str_4[32] + str_4[82]+ str_4[105] + str_4[103] + str_4[104] + str_4[116] + str_4[32] +
str_4[169] + str_4[32] + str_4[75] + str_4[97] + str_4[122] + str_4[97] + str_4[111] + str_4[111] + str_4[32] + str_4[50] + str_4[48] + str_4[49] + str_4[49] + str_4[32]);
IndicatorBuffers(4);
HalfLength = MathMax(HalfLength, 1);
SetIndexBuffer(0, var_1);
SetIndexDrawBegin(0, HalfLength);
SetIndexBuffer(1, var_2);
SetIndexDrawBegin(1, HalfLength);
SetIndexBuffer(2, var_3);
SetIndexDrawBegin(2, HalfLength);
SetIndexBuffer(3, var_4);
string_1 = WindowExpertName();
bool_2 = TimeFrame == "returnBars";
if (bool_2) return (0);
bool_1 = TimeFrame == "calculateValue";
if (bool_1) return (0);
var_5 = func_3(TimeFrame);
IndicatorShortName(func_0(var_5) + " TMA bands )" + HalfLength + ")");
return (0);
}
// deinit function
int deinit() {
return (0);
}
// start function
int start() {
int var_7;
double double_1;
double double_2;
double double_3;
double double_31;
int var_8;
int var_9;
int var_10 = IndicatorCounted();
if (var_10 < 0) return (-1);
if (var_10 > 0) var_10--;
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
if (bool_2) {
var_1[0] = var_11 + 1;
return (0);
}
if (bool_1 || var_5 == Period()) {
for (int i = var_11; i >= 0; i--) {
double_1 = (HalfLength + 1) * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i);
double_2 = HalfLength + 1;
var_7 = 1;
for (int var_12 = HalfLength; var_7 <= HalfLength; var_12--) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i + var_7);
double_2 += var_12;
if (var_7 <= i) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i - var_7);
double_2 += var_12;
}
var_7++;
}
double_3 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier;
double_31 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier1;
var_1[i] = double_1 / double_2;
var_2[i] = var_1[i] + double_3;
var_20[i] = var_1[i] + double_31;
var_30[i] = var_1[i] - double_31;
var_3[i] = var_1[i] - double_3;
var_4[i] = 0;
if (alertsOnHighLow) {
if (High[i] > var_2[i]) var_4[i] = 1;
if (Low[i] < var_3[i]) var_4[i] = -1;
} else {
if (Close[i] > var_2[i]) var_4[i] = 1;
if (Close[i] < var_3[i]) var_4[i] = -1;
}
}
if (!(!bool_1)) return (0);
func_1();
return (0);
}
var_11 = MathMax(var_11, MathMin(Bars - 1, iCustom(NULL, var_5, string_1, "returnBars", 0, 0) * var_5 / Period()));
for (i = var_11; i >= 0; i--) {
var_8 = iBarShift(NULL, var_5, Time[i]);
var_1[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 0, var_8);
var_2[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 1, var_8);
var_3[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 2, var_8);
var_4[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 3, var_8);
if (var_5 <= Period() || var_8 == iBarShift(NULL, var_5, Time[i - 1])) continue;
if (Interpolate) {
var_9 = iTime(NULL, var_5, var_8);
for (int var_14 = 1; i + var_14 < Bars && Time[i + var_14] >= var_9; var_14++) {
}
for (var_14 = 1; var_12 < var_14; var_12++) {
var_1[i + var_12] = var_1[i] + (var_1[i + var_14] - var_1[i]) * var_12 / var_14;
var_2[i + var_12] = var_2[i] + (var_2[i + var_14] - var_2[i]) * var_12 / var_14;
var_3[i + var_12] = var_3[i] + (var_3[i + var_14] - var_3[i]) * var_12 / var_14;
}
}
}
func_1();
return (0);
}
// function1
void func_1() {
int var_10;
if (alertsOn) {
if (alertsOnCurrent) var_10 = 0;
else var_10 = 1;
var_10 = iBarShift(NULL, 0, iTime(NULL, var_5, var_10));
if (var_4[var_10] != var_4[var_10 + 1]) {
if (var_4[var_10] == 1.0) func_4(var_10, "up");
if (var_4[var_10] == -1.0) func_4(var_10, "down");
}
}
}
// function4
void func_4(int var_12, string str_5) {
string str_6;
if (string_2 != str_5 || Gt_176 != Time[var_12]) {
string_2 = str_5;
Gt_176 = Time[var_12];
str_6 = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " " + func_0(var_5) + " TMA bands price penetrated ", str_5, " band");
if (alertsMessage) Alert(str_6);
if (alertsEmail) SendMail(StringConcatenate(Symbol(), "TMA bands "), str_6);
if (alertsSound) PlaySound("alert2.wav");
}
}
// function3
int func_3(string str_7) {
str_7 = func_2(str_7);
for (int var_7 = ArraySize(var_6) - 1; var_7 >= 0; var_7--)
if (str_7 == string_3[var_7] || str_7 == "" + var_6[var_7]) return (MathMax(var_6[var_7], Period()));
return (Period());
}
// function0
string func_0(int var_12) {
for (int i = ArraySize(var_6) - 1; i >= 0; i--)
if (var_12 == var_6[i]) return (string_3[i]);
return ("");
}
// function2
string func_2(string str_7) {
int var_13;
string str_8 = str_7;
for (int var_11 = StringLen(str_7) - 1; var_11 >= 0; var_11--) {
var_13 = StringGetChar(str_8, var_11);
if ((var_13 > '`' && var_13 < '{') || (var_13 > 'ß' && var_13 < 256)) str_8 = StringSetChar(str_8, var_11, var_13 - 32);
else
if (var_13 > -33 && var_13 < 0) str_8 = StringSetChar(str_8, var_11, var_13 + 224);
}
return (str_8);
}
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{ int var_10 = IndicatorCounted();
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
for (int i = var_11; i >= 0; i--)
{if(High[i] > var_30[i] || Close[i] > var_30[i])
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,var_30[i]+(var_30[i]- var_2[i])/2,var_2[i],"",0,Red); //Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",16384,0,clrGreen
return;
}
//--- buy conditions
else if(Low[i] < var_20[i] || Close[i] < var_20[i])
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,var_20[i]+(var_20[i]-var_3[i])/2,var_3[i],"",0,Green);
return;
}}
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
if(Volume[0]>0.1) return;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(High[i] >= var_3[i] || Low[i] <= var_30[i]+(var_30[i]-var_2[i])/2)
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,30,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Low[i] <= var_2[i] || High[i] >= var_20[i]+(var_20[i]-var_3[i])/2)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,30,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
//---
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//---
}
//+------------------------------------------------------------------+

Expert Advisor is not showing any bug or warning, but doesn't work

I created an Expert Advisor that will open long and short positions based on some bands, there are the 1.6 band and 3.6 band, it sells at the 3.6 upper band, puts the tp at 1.6 lower band and sl at half the tp opposite to the entry. It has no bug or warning, but it simply doesn't work when I try to use it. Any help would be greatly appreciated.
#property description "Price Border strategy expert advisor by Razvan"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_color2 Red
#property indicator_color3 Lime
//Inputs
extern double Lots =1;
extern double MaximumRisk =2;
extern double DecreaseFactor=3;
extern string TimeFrame = "All tf";
extern int HalfLength = 61;
extern int Price = 0;
extern double ATRMultiplier = 1.6;
extern double ATRMultiplier1 = 3.6;
extern int ATRPeriod = 480;
extern bool Interpolate = TRUE;
extern bool alertsOn = TRUE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsOnHighLow = TRUE;
extern bool alertsMessage = FALSE;
extern bool alertsSound = TRUE;
extern bool alertsEmail = FALSE;
double var_1[];
double var_2[];
double var_3[];
double var_4[];
double var_20[];
double var_30[];
string string_1;
bool bool_1;
bool bool_2;
int var_5;
string string_2 = "nothing";
datetime Gt_176;
string string_3[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
int var_6[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int res;
//init function
int init() {
string str_4[256];
for (int i = 0; i < 256; i++) str_4[i] = CharToStr(i);
int var_7 = StrToInteger(str_4[67] + str_4[111] + str_4[112] + str_4[121] + str_4[32] + str_4[82] + str_4[105] + str_4[103] + str_4[104] + str_4[116] + str_4[32] +
str_4[169] + str_4[32] + str_4[75] + str_4[97] + str_4[122] + str_4[97] + str_4[111] + str_4[111] + str_4[32] + str_4[50] + str_4[48] + str_4[49] + str_4[49] + str_4[32]);
IndicatorBuffers(4);
HalfLength = MathMax(HalfLength, 1);
SetIndexBuffer(0, var_1);
SetIndexDrawBegin(0, HalfLength);
SetIndexBuffer(1, var_2);
SetIndexDrawBegin(1, HalfLength);
SetIndexBuffer(2, var_3);
SetIndexDrawBegin(2, HalfLength);
SetIndexBuffer(3, var_4);
string_1 = WindowExpertName();
bool_2 = TimeFrame == "returnBars";
if (bool_2) return (0);
bool_1 = TimeFrame == "calculateValue";
if (bool_1) return (0);
var_5 = func_3(TimeFrame);
IndicatorShortName(func_0(var_5) + " TMA bands )" + HalfLength + ")");
return (0);
}
//deinit function
int deinit() {
return (0);
}
//start function
int start() {
int var_7;
double double_1;
double double_2;
double double_3;
double double_31;
int var_8;
int var_9;
int var_10 = IndicatorCounted();
if (var_10 < 0) return (-1);
if (var_10 > 0) var_10--;
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
if (bool_2) {
var_1[0] = var_11 + 1;
return (0);
}
if (bool_1 || var_5 == Period()) {
for (int i = var_11; i >= 0; i--) {
double_1 = (HalfLength + 1) * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i);
double_2 = HalfLength + 1;
var_7 = 1;
for (int var_12 = HalfLength; var_7 <= HalfLength; var_12--) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i + var_7);
double_2 += var_12;
if (var_7 <= i) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i - var_7);
double_2 += var_12;
}
var_7++;
}
double_3 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier;
double_31 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier1;
var_1[i] = double_1 / double_2;
var_2[i] = var_1[i] + double_3;
var_20[i] = var_1[i] + double_31;
var_30[i] = var_1[i] - double_31;
var_3[i] = var_1[i] - double_3;
var_4[i] = 0;
if (alertsOnHighLow) {
if (High[i] > var_2[i]) var_4[i] = 1;
if (Low[i] < var_3[i]) var_4[i] = -1;
} else {
if (Close[i] > var_2[i]) var_4[i] = 1;
if (Close[i] < var_3[i]) var_4[i] = -1;
}
}
if (!(!bool_1)) return (0);
func_1();
return (0);
}
var_11 = MathMax(var_11, MathMin(Bars - 1, iCustom(NULL, var_5, string_1, "returnBars", 0, 0) *
var_5 / Period()));
for (i = var_11; i >= 0; i--) {
var_8 = iBarShift(NULL, var_5, Time[i]);
var_1[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 0, var_8);
var_2[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 1, var_8);
var_3[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 2, var_8);
var_4[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 3, var_8);
if (var_5 <= Period() || var_8 == iBarShift(NULL, var_5, Time[i - 1])) continue;
if (Interpolate) {
var_9 = iTime(NULL, var_5, var_8);
for (int var_14 = 1; i + var_14 < Bars && Time[i + var_14] >= var_9; var_14++) {
}
for (var_14 = 1; var_12 < var_14; var_12++) {
var_1[i + var_12] = var_1[i] + (var_1[i + var_14] - var_1[i]) * var_12 / var_14;
var_2[i + var_12] = var_2[i] + (var_2[i + var_14] - var_2[i]) * var_12 / var_14;
var_3[i + var_12] = var_3[i] + (var_3[i + var_14] - var_3[i]) * var_12 / var_14;
}
}
}
func_1();
return (0);
}
//function1
void func_1() {
int var_10;
if (alertsOn) {
if (alertsOnCurrent) var_10 = 0;
else var_10 = 1;
var_10 = iBarShift(NULL, 0, iTime(NULL, var_5, var_10));
if (var_4[var_10] != var_4[var_10 + 1]) {
if (var_4[var_10] == 1.0) func_4(var_10, "up");
if (var_4[var_10] == -1.0) func_4(var_10, "down");
}
}
}
//function4
void func_4(int var_12, string str_5) {
string str_6;
if (string_2 != str_5 || Gt_176 != Time[var_12]) {
string_2 = str_5;
Gt_176 = Time[var_12];
str_6 = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " " +
func_0(var_5) + " TMA bands price penetrated ", str_5, " band");
if (alertsMessage) Alert(str_6);
if (alertsEmail) SendMail(StringConcatenate(Symbol(), "TMA bands "), str_6);
if (alertsSound) PlaySound("alert2.wav");
}
}
//function3
int func_3(string str_7) {
str_7 = func_2(str_7);
for (int var_7 = ArraySize(var_6) - 1; var_7 >= 0; var_7--)
if (str_7 == string_3[var_7] || str_7 == "" + var_6[var_7]) return (MathMax(var_6[var_7],
Period()));
return (Period());
}
//function0
string func_0(int var_12) {
for (int i = ArraySize(var_6) - 1; i >= 0; i--)
if (var_12 == var_6[i]) return (string_3[i]);
return ("");
}
//function2
string func_2(string str_7) {
int var_13;
string str_8 = str_7;
for (int var_11 = StringLen(str_7) - 1; var_11 >= 0; var_11--) {
var_13 = StringGetChar(str_8, var_11);
if ((var_13 > '`'` && var_13 < '{') || (var_13 > 'ß' && var_13 < 256)) str_8 = StringSetChar(str_8,
var_11, var_13 - 32);
else
if (var_13 > -33 && var_13 < 0) str_8 = StringSetChar(str_8, var_11, var_13 + 224);
}
return (str_8);
}
//Calculate open positions
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//Calculate optimal lot size
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//calculate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//---
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1)
lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//return lot size
if(lot<1) lot=1;
return(lot);
}
//Check for open order conditions
void CheckForOpen()
{ int var_10 = IndicatorCounted();
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
for (int i = var_11; i >= 0; i--)
{if(High[i] > var_20[i] || Close[i] > var_20[i])
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,var_20[i]+(fabs(var_20[i]-
var_3[i])/2),var_2[i],"",0,Red); //Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My
order",16384,0,clrGreen
return;
}
//buy conditions
if(Low[i] < var_30[i] || Close[i] < var_30[i])
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,var_30[i]-(fabs(var_30[i]-
var_2[i])/2),var_3[i],"",0,Green);
return;
}}
}
//Check for close order conditions
void CheckForClose()
{
if(Volume[0]>1) return;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(High[i] >= var_3[i] || Low[i] <= var_30[i]-(fabs(var_30[i]-var_2[i])/2))
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Low[i] <= var_2[i] || High[i] >= var_20[i]+(fabs(var_20[i]-var_3[i])/2))
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
}
//OnTick function
void OnTick()
{
//check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
}
'

MQL4 Expert Advisor isn't doing anything

This bot is supposed to buy based on support and resistance, stop loss is based on support resistance of the moving averages. When I test it out it won't do anything trying. How do I debug this to do what I want it to do?
After viewing the code do you have any suggestions input or criticism?
I tried to comment the code the best I could to make sense.
#property strict
string BotName = "SRMATrader";
/********** SETTINGS *************/
extern int Magic = 8008;
string CandleColor;
int MaxCloseSpreadPips = 600;
int MaxTrades = 1; // was 10
int AcceptableSpread = 2;
double LotsToTrade = .2; // 0.1
double StopLoss = -3700; // 3800
double ProfitTarget = 20.00; // $280 at 2.0 trade size
int MaxOpenOrderDurationSeconds = (5 * 24 * 60 * 60); // 5 days was profitable
int TradeDelayTimeSeconds = 30; //(10 * 24 * 60 * 60); // 10 Days
int PendingOrderExpirationSeconds = (4 * 24 * 60 * 60); // 4 Days
datetime LastTradePlacedTimestamp = 0;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
// MAIN LOOP
void OnTick()
{
double CurrentPrice = MarketInfo(Symbol(), MODE_BID);
double CandleResistance = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_HIGH, 0);
double CandleSupport = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_LOW, 0);
// Should we place a trade?
if (GetTotalOpenTrades() < MaxTrades)
{
if ( (TimeCurrent() - LastTradePlacedTimestamp) > TradeDelayTimeSeconds )
{
// long
if ( CurrentPrice <= CandleSupport )
{
if (CandleColor == "Green")
{
PlacePendingOrder("Green", LotsToTrade, CandleSupport, PendingOrderExpirationSeconds);
LastTradePlacedTimestamp = TimeCurrent();
}
}
// short
if ( CurrentPrice >= CandleResistance)
{
if (CheckForTradeSetup() == "Red" )
{
PlacePendingOrder("Red", LotsToTrade, CandleResistance, PendingOrderExpirationSeconds);
LastTradePlacedTimestamp = TimeCurrent();
}
}
}
}
if (GetTotalOpenTrades() > 0)
{
CloseTradeAfterAge(MaxOpenOrderDurationSeconds);
CheckForOrderClose(ProfitTarget, StopLoss);
}
} // end OnTick()
string CheckForTradeSetup()
{
double CurrentPrice = MarketInfo(Symbol(), MODE_BID);
double CandleSupport = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_LOW, 0);
double CandleResistance = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_HIGH, 0);
if ( CurrentPrice <= CandleSupport )
{
CandleColor = "Green";
}
if ( CurrentPrice >= CandleResistance )
{
CandleColor = "Red";
}
return "no-setup";
}
void PlacePendingOrder(string Trade_Type, double Lots, double At_Price, int Expiration_Seconds)
{
int TicketResult = 0;
datetime Expiration_Time = (TimeCurrent() + Expiration_Seconds);
double Price = NormalizeDouble(At_Price, Digits);
if (Trade_Type == "Green")
{
if (Ask < At_Price) return;
double StopPrice = CalculateStopLossPrice(Price, Lots, StopLoss, Trade_Type);
TicketResult = OrderSend(Symbol(), OP_BUYLIMIT, Lots, Price, 10, StopPrice, 0, " Buy", Magic, Expiration_Time, clrGreen);
}
if (Trade_Type == "Red")
{
if (Bid > At_Price) return;
double StopPrice = CalculateStopLossPrice(Price, Lots, StopLoss, Trade_Type);
TicketResult = OrderSend(Symbol(),OP_SELLLIMIT, Lots, NormalizeDouble(At_Price, Digits), 10, StopPrice, 0, " Sell", Magic, Expiration_Time, clrRed);
}
if(TicketResult < 0)
{
Print("OrderSend failed with error #",GetLastError());
}
else
{
Print("OrderSend placed successfully");
}
}
double CalculateStopLossPrice(double OrderPrice, double TradeSize, double StopLossDollars, string PositionType)
{
// Convert stop loss dollars to positive number
double CurrentSpread = MarketInfo(Symbol(), MODE_SPREAD);
Print("*** CurrentSpread: ", CurrentSpread);
double PipValue = (TradeSize * 10);
double StopLossPips = (StopLossDollars / PipValue);
double CandleSupport = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_LOW, 0);
double CandleResistance = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_HIGH, 0);
if (PositionType == "Green")
{
double StopLossPrice = CandleResistance;
}
if (PositionType == "Red")
{
double StopLossPrice = CandleSupport;
}
return 0.0;
}
void CloseAllTrades()
{
int CloseResult = 0;
for(int t=0; t<OrdersTotal(); t++)
{
if(OrderSelect(t, SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber() != Magic) continue;
if(OrderSymbol() != Symbol()) continue;
if(OrderType() == OP_BUY) CloseResult = OrderClose(OrderTicket(), OrderLots(), Bid, MaxCloseSpreadPips, clrRed);
if(OrderType() == OP_SELL) CloseResult = OrderClose(OrderTicket(), OrderLots(), Ask, MaxCloseSpreadPips, clrGreen);
t--;
}
}
if(CloseResult < 0)
{
Print("OrderSend failed with error #", GetLastError());
}
else
{
Print("OrderSend placed successfully");
}
return;
}
int GetTotalOpenTrades()
{
int TotalTrades = 0;
for (int t=0; t<OrdersTotal(); t++)
{
if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() != Symbol()) continue;
if(OrderMagicNumber() != Magic) continue;
if(OrderCloseTime() != 0) continue;
TotalTrades = (TotalTrades + 1);
}
}
return TotalTrades;
}
double GetTotalProfits()
{
double TotalProfits = 0.0;
for (int t=0; t<OrdersTotal(); t++)
{
if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() != Symbol()) continue;
if(OrderMagicNumber() != Magic) continue;
if(OrderCloseTime() != 0) continue;
TotalProfits = (TotalProfits + OrderProfit());
}
}
return TotalProfits;
}
// Close all trades if we are at profit or loss
void CheckForOrderClose(double Target, double Stop)
{
// check for profit or loss
if (GetTotalProfits() > Target)
{
CloseAllTrades();
}
}
// Close if trade is more than (n) seconds old
string CloseTradeAfterAge(int MaxOpenTradeAgeSeconds)
{
for(int t=0; t < OrdersTotal(); t++)
{
if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() != Symbol()) return "wrong symbol";
if(OrderMagicNumber() != Magic) return "magic number does not match";
if(OrderCloseTime() != 0) return "order already closed";
datetime OrderOpenTime = OrderOpenTime();
string Now = (TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS));
datetime NowTimeStamp = (StrToTime(Now));
if ((NowTimeStamp - OrderOpenTime) > MaxOpenTradeAgeSeconds) // 1 * 24 * 60 * 60
{
if ((OrderType() == 0) || (OrderType() == 1)) // Only close orders that are live (not limit ordes)
{
CloseAllTrades();
return "all trades closed";
}
}
}
}
return "trades not closed";
}
check and go over this piece of code until you are sure you understand the logic and make it working
//...
if (CandleColor == "Green") //this one is NULL
{
PlacePendingOrder("Green", LotsToTrade, CandleSupport, PendingOrderExpirationSeconds);
LastTradePlacedTimestamp = TimeCurrent();
}
}
// short
if ( CurrentPrice >= CandleResistance)
{
if (CheckForTradeSetup() == "Red" )//this fn() returns "no-setup"
//....
so edit the CheckForTradeSetup() function and call it before checking the colors, and also comparing two strings is a very bad idea because it is too slow. +1/0/-1 or enums or even colors (==int) is preferred

how to reduce if else condition in code?

I am new beginner in iOS, i want to remove if else condition and use proper code for this following code ..
can anyone help me for this ?
I have to Many condition
if (B == 99)
{
B = 11;
[Search_color addObject:Z];
}else if(B == 4)
{
B = 1;
[Search_color addObject:D];
}else if (B == 5)
{
B = 2;
[Search_color addObject:E];
}else if (A == 14)
{
B = 11; A = 10;
[Search_color addObject:N];
[Search_color addObject:Z];
}
else if (B == 26)
{
B = 11;
[Search_color addObject:Z];
}
can use switch with same result :
switch (B) {
case 99:
B = 11;
//[Search_color addObject:Z];
break;
case 4:
B = 1;
//[Search_color addObject:D];
break;
case 5:
B = 2;
//[Search_color addObject:E];
break;
case 26:
if (A != 14) {
B = 11;
//[Search_color addObject:Z];
break;
}
default:
if (A == 14) {
B = 11; A = 10;
//[Search_color addObject:N];
//[Search_color addObject:Z];
}
break;
}
Code use switch in function
- (void)yourFunction
{
NSInteger B, A;
B = 99;
switch (B) {
case 99:
B = 11;
//[Search_color addObject:Z];
break;
case 4:
B = 1;
//[Search_color addObject:D];
break;
case 5:
B = 2;
//[Search_color addObject:E];
break;
case 26:
if (A != 14) {
B = 11;
//[Search_color addObject:Z];
break;
}
default:
if (A == 14) {
B = 11; A = 10;
//[Search_color addObject:N];
//[Search_color addObject:Z];
}
break;
}
NSLog(#"B = %ld", B);
}
output: B = 11.

wrong perspective image after taking picture on accelerometer supported blackberry device

There is a perspective image issue when I read a picture that is taken from the camera. When the direction is north, the picture looks like needed to be rotated 270 degrees. When the direction is east, picture should be rotated 180 degrees. But it's good when the direction is west. I tried getMetaData().getKeyValue("orientation") in EncodedImage for producing a good rotating formula, but it returned empty string. Please help me how to solve this problem.
Found solution here:
https://gist.github.com/3788313
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
public class ExifRotate {
/**
* Flip the image horizontally.
*/
public static final int FLIP_H = 1;
/**
* Flip the image vertically.
*/
public static final int FLIP_V = 2;
/**
* Flip the image horizontally and vertically.
*/
public static final int FLIP_HV = 3;
/**
* Rotate the image 90 degrees clockwise.
*/
public static final int FLIP_90CW = 4;
/**
* Rotate the image 90 degrees counter-clockwise.
*/
public static final int FLIP_90CCW = 5;
/**
* Rotate the image 180 degrees.
*/
public static final int FLIP_180 = 6;
private final static int read2bytes(InputStream in) throws IOException {
return in.read() << 8 | in.read();
}
private final static int readByte(InputStream in) throws IOException {
return in.read();
}
public static Bitmap readImageFromFile(String filename, int width, int height) throws IOException {
EncodedImage img = null;
byte[] data = null;
FileConnection file = null;
try {
file = (FileConnection) Connector.open(filename, Connector.READ);
int fileSize = (int) file.fileSize();
if (fileSize == 0) {
throw new IOException("File is empty");
}
data = new byte[fileSize];
InputStream input = file.openInputStream();
input.read(data);
input.close();
img = EncodedImage.createEncodedImage(data, 0, data.length);
int orientation = -1;
if ( filename.toLowerCase().endsWith(".jpg") || filename.toLowerCase().endsWith(".jpeg")) {
ByteArrayInputStream is = new ByteArrayInputStream( data );
orientation = getRotation(is);
}
if ( orientation == 2 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_H);
} else if ( orientation == 3 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_180);
} else if ( orientation == 4 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_V);
} else if ( orientation == 5 ) {
Bitmap tmp = rotateBitmap(img.getBitmap(), ImageUtil.FLIP_H);
tmp = rotateBitmap(tmp, ImageUtil.FLIP_90CCW);
return tmp;
} else if ( orientation == 6 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_90CW);
} else if ( orientation == 7 ) {
Bitmap tmp = rotateBitmap(img.getBitmap(), ImageUtil.FLIP_H);
tmp = rotateBitmap(tmp, ImageUtil.FLIP_90CW);
return tmp;
} else if ( orientation == 8 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_90CCW);
} else {
return img.getBitmap();
}
} finally {
if (file != null) {
try { file.close(); }
catch(Exception ex){}
}
}
}
public static int getRotation(InputStream in) throws IOException {
int [] exif_data = new int[100];
int n_flag = 0, set_flag = 0;
int is_motorola = 0;
/* Read File head, check for JPEG SOI + Exif APP1 */
for (int i = 0; i < 4; i++)
exif_data[i] = readByte(in);
if (exif_data[0] != 0xFF || exif_data[1] != 0xD8 || exif_data[2] != 0xFF || exif_data[3] != 0xE1)
return -2;
/* Get the marker parameter length count */
int length = read2bytes(in);
// exif_data = new int[length];
/* Length includes itself, so must be at least 2 */
/* Following Exif data length must be at least 6 */
if (length < 8)
return -1;
length -= 8;
/* Read Exif head, check for "Exif" */
for (int i = 0; i < 6; i++)
exif_data[i] = in.read();
if (exif_data[0] != 0x45 || exif_data[1] != 0x78 || exif_data[2] != 0x69 || exif_data[3] != 0x66 || exif_data[4] != 0 || exif_data[5] != 0)
return -1;
/* Read Exif body */
length = length > exif_data.length ? exif_data.length : length;
for (int i = 0; i < length; i++)
exif_data[i] = in.read();
if (length < 12)
return -1; /* Length of an IFD entry */
/* Discover byte order */
if (exif_data[0] == 0x49 && exif_data[1] == 0x49)
is_motorola = 0;
else if (exif_data[0] == 0x4D && exif_data[1] == 0x4D)
is_motorola = 1;
else
return -1;
/* Check Tag Mark */
if (is_motorola == 1) {
if (exif_data[2] != 0)
return -1;
if (exif_data[3] != 0x2A)
return -1;
} else {
if (exif_data[3] != 0)
return -1;
if (exif_data[2] != 0x2A)
return -1;
}
/* Get first IFD offset (offset to IFD0) */
int offset;
if (is_motorola == 1) {
if (exif_data[4] != 0)
return -1;
if (exif_data[5] != 0)
return -1;
offset = exif_data[6];
offset <<= 8;
offset += exif_data[7];
} else {
if (exif_data[7] != 0)
return -1;
if (exif_data[6] != 0)
return -1;
offset = exif_data[5];
offset <<= 8;
offset += exif_data[4];
}
if (offset > length - 2)
return -1; /* check end of data segment */
/* Get the number of directory entries contained in this IFD */
int number_of_tags;
if (is_motorola == 1) {
number_of_tags = exif_data[offset];
number_of_tags <<= 8;
number_of_tags += exif_data[offset + 1];
} else {
number_of_tags = exif_data[offset + 1];
number_of_tags <<= 8;
number_of_tags += exif_data[offset];
}
if (number_of_tags == 0)
return -1;
offset += 2;
/* Search for Orientation Tag in IFD0 */
for (;;) {
if (offset > length - 12)
return -1; /* check end of data segment */
/* Get Tag number */
int tagnum;
if (is_motorola == 1) {
tagnum = exif_data[offset];
tagnum <<= 8;
tagnum += exif_data[offset + 1];
} else {
tagnum = exif_data[offset + 1];
tagnum <<= 8;
tagnum += exif_data[offset];
}
if (tagnum == 0x0112)
break; /* found Orientation Tag */
if (--number_of_tags == 0)
return -1;
offset += 12;
}
/*
* if (set_flag==1) { Set the Orientation value if (is_motorola==1) {
* exif_data[offset+2] = 0; Format = unsigned short (2 octets)
* exif_data[offset+3] = 3; exif_data[offset+4] = 0; Number Of
* Components = 1 exif_data[offset+5] = 0; exif_data[offset+6] = 0;
* exif_data[offset+7] = 1; exif_data[offset+8] = 0; exif_data[offset+9]
* = set_flag; exif_data[offset+10] = 0; exif_data[offset+11] = 0; }
* else { exif_data[offset+2] = 3; Format = unsigned short (2 octets)
* exif_data[offset+3] = 0; exif_data[offset+4] = 1; Number Of
* Components = 1 exif_data[offset+5] = 0; exif_data[offset+6] = 0;
* exif_data[offset+7] = 0; exif_data[offset+8] = set_flag;
* exif_data[offset+9] = 0; exif_data[offset+10] = 0;
* exif_data[offset+11] = 0; } }
*/
// else {
/* Get the Orientation value */
if (is_motorola == 1) {
if (exif_data[offset + 8] != 0)
return -1;
set_flag = exif_data[offset + 9];
} else {
if (exif_data[offset + 9] != 0)
return -1;
set_flag = exif_data[offset + 8];
}
if (set_flag > 8)
return -1;
// }
/* Write out Orientation value */
if (n_flag == 1)
System.out.println("set_flag " + set_flag);
else
System.out.println("set_flag " + set_flag);
return set_flag;
}
public static Bitmap rotateBitmap(Bitmap src, int operation) {
int width = src.getWidth();
int height = src.getHeight();
int[] inPixels = new int[width*height];
src.getARGB(inPixels, 0, width, 0, 0, width, height);
int x = 0, y = 0;
int w = width;
int h = height;
int newX = 0;
int newY = 0;
int newW = w;
int newH = h;
switch (operation) {
case FLIP_H:
newX = width - (x + w);
break;
case FLIP_V:
newY = height - (y + h);
break;
case FLIP_HV:
newW = h;
newH = w;
newX = y;
newY = x;
break;
case FLIP_90CW:
newW = h;
newH = w;
newX = height - (y + h);
newY = x;
break;
case FLIP_90CCW:
newW = h;
newH = w;
newX = y;
newY = width - (x + w);
break;
case FLIP_180:
newX = width - (x + w);
newY = height - (y + h);
break;
}
int[] newPixels = new int[newW * newH];
int index, newRow, newCol, newIndex;
if ( operation == FLIP_H ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = row;
newCol = w - col - 1;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_V ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = h - row - 1;
newCol = col;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_HV ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = col;
newCol = row;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_90CW ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = col;
newCol = h - row - 1;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_90CCW ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = w - col - 1;
newCol = row;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_180 ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = h - row - 1;
newCol = w - col - 1;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
}
Bitmap dst = new Bitmap( newW, newH );
dst.setARGB(newPixels, 0, newW, 0, 0, newW, newH);
return dst;
}
}

Resources